Comparing version 0.1.0 to 1.0.0
71
index.js
@@ -1,2 +0,2 @@ | ||
function getName(value) { | ||
function getName (value) { | ||
if (value === undefined) return '' | ||
@@ -11,6 +11,16 @@ if (value === null) return '' | ||
module.exports = function enforce(type, value) { | ||
module.exports = function enforce (type, value) { | ||
if (typeof type === 'string') { | ||
if (type[0] === '?') { | ||
if (value === null || value === undefined) { | ||
return | ||
} | ||
type = type.slice(1) | ||
} | ||
} | ||
switch (type) { | ||
case 'Array': { | ||
if (Array.isArray(value)) return | ||
if (value !== null && value !== undefined && value.constructor === Array) return | ||
break | ||
@@ -41,3 +51,2 @@ } | ||
if (typeof value === 'object') return | ||
break | ||
@@ -50,40 +59,36 @@ } | ||
} | ||
} | ||
switch (typeof type) { | ||
case 'string': { | ||
if (type === getName(value)) return | ||
default: { | ||
switch (typeof type) { | ||
case 'string': { | ||
if (type === getName(value)) return | ||
break | ||
} | ||
break | ||
} | ||
// evaluate type templates | ||
case 'object': { | ||
if (Array.isArray(type)) { | ||
var subType = type[0] | ||
// evaluate type templates | ||
case 'object': { | ||
if (Array.isArray(type)) { | ||
var subType = type[0] | ||
enforce('Array', value) | ||
value.forEach(enforce.bind(undefined, subType)) | ||
enforce('Array', value) | ||
value.forEach(enforce.bind(undefined, subType)) | ||
return | ||
} | ||
return | ||
} | ||
enforce('Object', value) | ||
for (var propertyName in type) { | ||
var propertyType = type[propertyName] | ||
var propertyValue = value[propertyName] | ||
enforce('Object', value) | ||
for (var propertyName in type) { | ||
var propertyType = type[propertyName] | ||
try { | ||
enforce(propertyType, propertyValue) | ||
} catch (e) { | ||
throw new TypeError('Expected property "' + propertyName + '" of type ' + JSON.stringify(propertyType) + ', got ' + getName(propertyValue) + ' ' + propertyValue) | ||
} | ||
} | ||
if (!(propertyName in value)) { | ||
throw new TypeError('Missing property "' + propertyName + '" of type ' + JSON.stringify(propertyType)) | ||
return | ||
} | ||
var propertyValue = value[propertyName] | ||
try { | ||
enforce(propertyType, propertyValue) | ||
} catch (e) { | ||
throw new TypeError('Expected property "' + propertyName + '" of type ' + JSON.stringify(propertyType) + ', got ' + getName(propertyValue) + ' ' + propertyValue) | ||
} | ||
} | ||
return | ||
} | ||
@@ -90,0 +95,0 @@ } |
{ | ||
"name": "typeforce", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Another biased type checking solution for Javascript", | ||
@@ -9,3 +9,5 @@ "main": "./index.js", | ||
"coveralls": "npm run coverage && coveralls < coverage/lcov.info", | ||
"test": "istanbul test mocha -- --reporter list test/*.js" | ||
"standard": "standard", | ||
"test": "npm run-script unit", | ||
"unit": "istanbul test mocha -- --reporter list test/*.js" | ||
}, | ||
@@ -24,6 +26,7 @@ "repository": { | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.2", | ||
"mocha": "^1.21.5" | ||
"istanbul": "^0.3.7", | ||
"mocha": "^2.2.1", | ||
"standard": "^2.11.0" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -64,2 +64,14 @@ { | ||
"value": [{ "a": 1 }, { "a": 2, "b": 3 }] | ||
}, | ||
{ | ||
"type": "?Number", | ||
"value": null | ||
}, | ||
{ | ||
"type": ["?Number"], | ||
"value": [1, null] | ||
}, | ||
{ | ||
"type": [{ "a": "?Number" }], | ||
"value": [{ "a": 1 }, { "a": null }, {}] | ||
} | ||
@@ -264,3 +276,3 @@ ], | ||
{ | ||
"exception": "Missing property \"a\" of type \"Number\"", | ||
"exception": "Expected property \"a\" of type \"Number\", got undefined", | ||
"type": { | ||
@@ -275,3 +287,3 @@ "a": "Number", | ||
{ | ||
"exception": "Missing property \"a\" of type \"Number\"", | ||
"exception": "Expected property \"a\" of type \"Number\", got undefined", | ||
"type": { | ||
@@ -296,3 +308,3 @@ "a": "Number", | ||
{ | ||
"exception": "Missing property \"a\" of type \"Number\"", | ||
"exception": "Expected property \"a\" of type \"Number\", got undefined", | ||
"custom": "Buffer", | ||
@@ -327,3 +339,3 @@ "type": { | ||
{ | ||
"exception": "Missing property \"a\" of type \"Number\"", | ||
"exception": "Expected property \"a\" of type \"Number\", got undefined", | ||
"type": { | ||
@@ -423,4 +435,9 @@ "a": "Number", | ||
"value": {} | ||
}, | ||
{ | ||
"exception": "Expected Number, got null", | ||
"type": "Number", | ||
"value": null | ||
} | ||
] | ||
} |
@@ -0,9 +1,11 @@ | ||
/* global describe, it */ | ||
var assert = require('assert') | ||
var enforceType = require('../') | ||
var typeForce = require('../') | ||
function CustomType() { return "ensure non-greedy match".toUpperCase() } | ||
function CustomType () { return 'ensure non-greedy match'.toUpperCase() } | ||
var CUSTOM_TYPES = { | ||
'Buffer': new Buffer(1), | ||
'CustomType': new CustomType(), | ||
'Function': function() {} | ||
'Function': function () {} | ||
} | ||
@@ -13,17 +15,17 @@ | ||
describe('enforceType', function() { | ||
fixtures.valid.forEach(function(f) { | ||
describe('typeForce', function () { | ||
fixtures.valid.forEach(function (f) { | ||
var actualValue = f.custom ? CUSTOM_TYPES[f.custom] : f.value | ||
it('passes for ' + JSON.stringify(f.type) + ' with ' + (f.custom ? f.custom : JSON.stringify(f.value)), function() { | ||
enforceType(f.type, actualValue) | ||
it('passes for ' + JSON.stringify(f.type) + ' with ' + (f.custom ? f.custom : JSON.stringify(f.value)), function () { | ||
typeForce(f.type, actualValue) | ||
}) | ||
}) | ||
fixtures.invalid.forEach(function(f) { | ||
fixtures.invalid.forEach(function (f) { | ||
var actualValue = f.custom ? CUSTOM_TYPES[f.custom] : f.value | ||
it('fails for ' + JSON.stringify(f.type) + ' with ' + (f.custom ? f.custom : JSON.stringify(f.value)), function() { | ||
assert.throws(function() { | ||
enforceType(f.type, actualValue) | ||
it('fails for ' + JSON.stringify(f.type) + ' with ' + (f.custom ? f.custom : JSON.stringify(f.value)), function () { | ||
assert.throws(function () { | ||
typeForce(f.type, actualValue) | ||
}, new RegExp(f.exception)) | ||
@@ -30,0 +32,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
Mixed license
License(Experimental) Package contains multiple licenses.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17309
10
582
0
4
1