Comparing version 2.0.5 to 2.0.6
@@ -27,5 +27,7 @@ /** | ||
to: function(v) { | ||
if(_.isUndefined(v)) { | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
} | ||
return v; | ||
@@ -32,0 +34,0 @@ }, |
@@ -26,2 +26,3 @@ /** | ||
var isExpectingDictionary; | ||
var isExpectingAnything; | ||
var allowAnyArray; | ||
@@ -43,2 +44,4 @@ var allowAnyDictionary; | ||
// console.log('validating',actual,'against',expected,'...'); | ||
// // "*" (allow anything) | ||
@@ -53,2 +56,6 @@ // if (expected === '*'){ | ||
// Flag '*' (allow anything that's not undefined) | ||
if (expected === '*') { | ||
isExpectingAnything = true; | ||
} | ||
// Normalize: [] or ['*'] or "array" (allow any array) | ||
@@ -75,3 +82,3 @@ if (expected === 'array' || (_.isArray(expected) && (expected.length === 0 || expected[0] === '*'))) { | ||
} | ||
// Primitives | ||
// string, boolean, number, and '*' | ||
else { | ||
@@ -103,3 +110,2 @@ expectedType = types[expected]; | ||
if (!expectedType.is(actual)){ | ||
// console.log('.is() failed!'); | ||
@@ -138,2 +144,3 @@ // Build an E_INVALID_TYPE error | ||
// Invalid expected type. Try to coerce: | ||
@@ -171,5 +178,10 @@ try { | ||
// (taking recursive step if necessary) | ||
if (isExpectingAnything) { | ||
coercedValue = sanitizeGenericDictionary(coercedValue); | ||
coercedValue = sanitizeGenericArray(coercedValue); | ||
return coercedValue; | ||
} | ||
if (isExpectingArray) { | ||
if (allowAnyArray){ | ||
return coercedValue; | ||
if (allowAnyArray) { | ||
return sanitizeGenericArray(coercedValue); | ||
} | ||
@@ -184,3 +196,3 @@ var arrayItemTpl = expected[0]; | ||
if (allowAnyDictionary){ | ||
return coercedValue; | ||
return sanitizeGenericDictionary(coercedValue); | ||
} | ||
@@ -195,1 +207,35 @@ return _.reduce(expected, function (memo, expectedVal, expectedKey) { | ||
}; | ||
/** | ||
* sanitize a dictionary used in examples `['*']`, `{}`, or `'*'` | ||
*/ | ||
function sanitizeGenericDictionary(val){ | ||
// TODO: also consider doing all the things in the `dictionary` | ||
// type `.to()` function here... | ||
if (!_.isPlainObject(val)) return val; | ||
// Strip out keys w/ undefined values | ||
return _.reduce(_.keys(val), function (memo, key){ | ||
if (!_.isUndefined(val[key])) { | ||
memo[key] = val[key]; | ||
} | ||
return memo; | ||
}, {}); | ||
} | ||
/** | ||
* sanitize an array used in examples `['*']`, `{}`, or `'*'` | ||
*/ | ||
function sanitizeGenericArray(val){ | ||
if (!_.isArray(val)) return val; | ||
return _.reduce(val, function (memo, item){ | ||
item = sanitizeGenericDictionary(item); | ||
memo.push(item); | ||
return memo; | ||
}, []); | ||
} |
{ | ||
"name": "rttc", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"description": "Runtime type-checking for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -237,9 +237,21 @@ // Export the array of tests below | ||
// Missing keys: | ||
// Missing keys (general case) | ||
{ example: {a:1, b:'hi', c: false}, actual: {a: 11}, error: true }, | ||
{ example: {a:1, b:'hi', c: false}, actual: {a: 23, b: undefined, c: undefined}, error: true }, | ||
{ example: {a:1}, actual: {a: undefined}, error: true }, | ||
// Missing keys (`*` case) | ||
{ example: {a:'*'}, actual: {a: undefined}, error: true }, | ||
// Keys with `undefined` values (`{}` case) | ||
{ example: {}, actual: {a: undefined, b: 3}, result: {b: 3} }, | ||
// Keys with `undefined` values (`*` case) | ||
{ example: '*', actual: {a: undefined, b: 3}, result: {b: 3} }, | ||
// Extra keys: | ||
{ example: {a:1, b:'hi'}, actual: {a: 23, b: 'stuff', d: true}, result: {a: 23, b: 'stuff'} }, | ||
{ example: {a:1, b:'hi'}, actual: {a: 23, b: 'stuff', d: undefined}, result: {a: 23, b: 'stuff'} }, | ||
//////////////////////////////////////////// | ||
// MISC | ||
// example: * | ||
//////////////////////////////////////////// | ||
@@ -246,0 +258,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63128
1424