Comparing version 1.0.1 to 1.1.0
16
index.js
@@ -11,3 +11,3 @@ function getName (value) { | ||
module.exports = function enforce (type, value) { | ||
module.exports = function enforce (type, value, strict) { | ||
var typeName = type | ||
@@ -74,3 +74,5 @@ | ||
enforce('Array', value) | ||
value.forEach(enforce.bind(undefined, subType)) | ||
value.forEach(function (x) { | ||
enforce(subType, x, strict) | ||
}) | ||
@@ -81,8 +83,14 @@ return | ||
enforce('Object', value) | ||
for (var propertyName in type) { | ||
var propertyNames = strict ? value : type | ||
for (var propertyName in propertyNames) { | ||
var propertyType = type[propertyName] | ||
var propertyValue = value[propertyName] | ||
if (!propertyType) { | ||
throw new TypeError('Unexpected property "' + propertyName + '"') | ||
} | ||
try { | ||
enforce(propertyType, propertyValue) | ||
enforce(propertyType, propertyValue, strict) | ||
} catch (e) { | ||
@@ -89,0 +97,0 @@ throw new TypeError('Expected property "' + propertyName + '" of type ' + JSON.stringify(propertyType) + ', got ' + getName(propertyValue) + ' ' + propertyValue) |
{ | ||
"name": "typeforce", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Another biased type checking solution for Javascript", | ||
"main": "./index.js", | ||
"main": "index.js", | ||
"author": "Daniel Cousens", | ||
"license": "MIT", | ||
"scripts": { | ||
@@ -10,4 +12,4 @@ "coverage": "istanbul cover _mocha -- test/*.js", | ||
"standard": "standard", | ||
"test": "npm run-script unit", | ||
"unit": "istanbul test mocha -- --reporter list test/*.js" | ||
"test": "npm run standard && npm run unit", | ||
"unit": "mocha" | ||
}, | ||
@@ -18,4 +20,2 @@ "repository": { | ||
}, | ||
"author": "Daniel Cousens", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -28,6 +28,6 @@ "url": "https://github.com/dcousens/typeforce/issues" | ||
"istanbul": "^0.3.7", | ||
"mocha": "^2.2.1", | ||
"standard": "^2.11.0" | ||
"mocha": "*", | ||
"standard": "*" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -58,8 +58,25 @@ { | ||
{ | ||
"type": ["String"], | ||
"value": ["foo", "bar"] | ||
"type": [ | ||
"String" | ||
], | ||
"value": [ | ||
"foo", | ||
"bar" | ||
] | ||
}, | ||
{ | ||
"type": [{ "a": "Number" }], | ||
"value": [{ "a": 1 }, { "a": 2, "b": 3 }] | ||
"type": [ | ||
{ | ||
"a": "Number" | ||
} | ||
], | ||
"value": [ | ||
{ | ||
"a": 1 | ||
}, | ||
{ | ||
"a": 2, | ||
"b": 3 | ||
} | ||
] | ||
}, | ||
@@ -71,8 +88,25 @@ { | ||
{ | ||
"type": ["?Number"], | ||
"value": [1, null] | ||
"type": [ | ||
"?Number" | ||
], | ||
"value": [ | ||
1, | ||
null | ||
] | ||
}, | ||
{ | ||
"type": [{ "a": "?Number" }], | ||
"value": [{ "a": 1 }, { "a": null }, {}] | ||
"type": [ | ||
{ | ||
"a": "?Number" | ||
} | ||
], | ||
"value": [ | ||
{ | ||
"a": 1 | ||
}, | ||
{ | ||
"a": null | ||
}, | ||
{} | ||
] | ||
}, | ||
@@ -391,4 +425,17 @@ { | ||
{ | ||
"exception": "Unexpected property \"b\"", | ||
"type": { | ||
"a": "Number" | ||
}, | ||
"value": { | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"strict": true | ||
}, | ||
{ | ||
"exception": "Expected Array, got null", | ||
"type": ["String"], | ||
"type": [ | ||
"String" | ||
], | ||
"value": null | ||
@@ -398,3 +445,5 @@ }, | ||
"exception": "Expected String, got Number 1", | ||
"type": ["String"], | ||
"type": [ | ||
"String" | ||
], | ||
"value": [ | ||
@@ -401,0 +450,0 @@ 1 |
@@ -20,3 +20,3 @@ /* global describe, it */ | ||
it('passes for ' + JSON.stringify(f.type) + ' with ' + (f.custom ? f.custom : JSON.stringify(f.value)), function () { | ||
typeForce(f.type, actualValue) | ||
typeForce(f.type, actualValue, f.strict) | ||
}) | ||
@@ -31,3 +31,3 @@ }) | ||
assert.throws(function () { | ||
typeForce(f.type, actualValue) | ||
typeForce(f.type, actualValue, f.strict) | ||
}, new RegExp(f.exception)) | ||
@@ -34,0 +34,0 @@ }) |
Sorry, the diff of this file is not supported yet
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
16895
611