node-powertools
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -147,11 +147,3 @@ (function (root, factory) { | ||
Powertools.force = function (input, type, options) { | ||
if (type === 'string') { | ||
return forceString(input); | ||
} else if (type === 'number') { | ||
return forceNumber(input); | ||
} else if (type === 'boolean') { | ||
return forceBoolean(input); | ||
} else if (type === 'array') { | ||
return forceArray(input, options); | ||
} | ||
return forceType(input, type, options); | ||
}; | ||
@@ -212,2 +204,16 @@ | ||
function forceType(input, type, options) { | ||
if (type === 'string') { | ||
return forceString(input); | ||
} else if (type === 'number') { | ||
return forceNumber(input); | ||
} else if (type === 'boolean') { | ||
return forceBoolean(input); | ||
} else if (type === 'array') { | ||
return forceArray(input, options); | ||
} else if (type === 'undefined') { | ||
return undefined; | ||
} | ||
} | ||
function forceString(input) { | ||
@@ -307,2 +313,8 @@ if (typeof input === 'string') { | ||
// If only one type is allowed, force it | ||
if (types.length === 1 && types[0] !== 'any') { | ||
return isValidType ? value : forceType(value, types[0]); | ||
} | ||
// If multiple types are allowed, return the value if it is valid type, otherwise return the default | ||
return isValidType ? value : def; | ||
@@ -309,0 +321,0 @@ } |
{ | ||
"name": "node-powertools", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Powerful assistive functions for Node and Browser environments.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -147,11 +147,3 @@ (function (root, factory) { | ||
Powertools.force = function (input, type, options) { | ||
if (type === 'string') { | ||
return forceString(input); | ||
} else if (type === 'number') { | ||
return forceNumber(input); | ||
} else if (type === 'boolean') { | ||
return forceBoolean(input); | ||
} else if (type === 'array') { | ||
return forceArray(input, options); | ||
} | ||
return forceType(input, type, options); | ||
}; | ||
@@ -212,2 +204,16 @@ | ||
function forceType(input, type, options) { | ||
if (type === 'string') { | ||
return forceString(input); | ||
} else if (type === 'number') { | ||
return forceNumber(input); | ||
} else if (type === 'boolean') { | ||
return forceBoolean(input); | ||
} else if (type === 'array') { | ||
return forceArray(input, options); | ||
} else if (type === 'undefined') { | ||
return undefined; | ||
} | ||
} | ||
function forceString(input) { | ||
@@ -307,2 +313,8 @@ if (typeof input === 'string') { | ||
// If only one type is allowed, force it | ||
if (types.length === 1 && types[0] !== 'any') { | ||
return isValidType ? value : forceType(value, types[0]); | ||
} | ||
// If multiple types are allowed, return the value if it is valid type, otherwise return the default | ||
return isValidType ? value : def; | ||
@@ -309,0 +321,0 @@ } |
@@ -283,3 +283,3 @@ const package = require('../package.json'); | ||
min: 1, | ||
max: 2, | ||
max: 10, | ||
}, | ||
@@ -352,3 +352,3 @@ admin: { | ||
it('should enforce acceptable types', () => { | ||
it('should disallow unacceptable types', () => { | ||
const user = { | ||
@@ -365,6 +365,20 @@ name: 123, | ||
assert.strictEqual(result.name, defaults[planId].name.default); | ||
assert.strictEqual(result.name, `${user.name}`); | ||
assert.strictEqual(result.stats.level, defaults[planId].stats.level.default); | ||
}); | ||
it('should enforce acceptable types', () => { | ||
const user = { | ||
stats: { | ||
level: '3', | ||
}, | ||
}; | ||
const planId = 'basic'; | ||
const result = powertools.defaults(user, defaults[planId]); | ||
assert.strictEqual(typeof result.stats.level, typeof defaults[planId].stats.level.default); | ||
assert.strictEqual(result.stats.level, parseInt(user.stats.level)); | ||
}); | ||
it('should enforce min and max constraints', () => { | ||
@@ -374,3 +388,3 @@ const user = { | ||
stats: { | ||
level: 5, | ||
level: 50, | ||
}, | ||
@@ -377,0 +391,0 @@ }; |
54579
1235