Comparing version 1.5.5 to 1.6.1
108
index.js
@@ -0,1 +1,50 @@ | ||
var inherits = require('inherits') | ||
function TfTypeError (type, value) { | ||
this.tfError = Error.call(this) | ||
this.tfType = type | ||
this.tfValue = value | ||
var message | ||
Object.defineProperty(this, 'message', { | ||
get: function () { | ||
if (message) return message | ||
message = tfErrorString(type, value) | ||
return message | ||
} | ||
}) | ||
} | ||
inherits(TfTypeError, Error) | ||
Object.defineProperty(TfTypeError, 'stack', { get: function () { return this.tfError.stack } }) | ||
function TfPropertyTypeError (type, property, value, error) { | ||
this.tfError = error || Error.call(this) | ||
this.tfProperty = property | ||
this.tfType = type | ||
this.tfValue = value | ||
var message | ||
Object.defineProperty(this, 'message', { | ||
get: function () { | ||
if (message) return message | ||
if (type) { | ||
message = tfPropertyErrorString(type, property, value) | ||
} else { | ||
message = 'Unexpected property "' + property + '"' | ||
} | ||
return message | ||
} | ||
}) | ||
} | ||
inherits(TfPropertyTypeError, Error) | ||
Object.defineProperty(TfPropertyTypeError, 'stack', { get: function () { return this.tfError.stack } }) | ||
TfPropertyTypeError.prototype.asChildOf = function (property) { | ||
return new TfPropertyTypeError(this.tfType, property + '.' + this.tfProperty, this.tfValue, this.tfError) | ||
} | ||
function getFunctionName (fn) { | ||
@@ -59,7 +108,7 @@ return fn.name || fn.toString().match(/function (.*?)\s*\(/)[1] | ||
function arrayOf (value, strict) { | ||
try { | ||
return nativeTypes.Array(value) && value.every(function (x) { return typeforce(type, x, strict) }) | ||
} catch (e) { | ||
return false | ||
} | ||
if (!nativeTypes.Array(value)) return false | ||
return value.every(function (x) { | ||
return typeforce(type, x, strict, arrayOf) | ||
}) | ||
} | ||
@@ -73,3 +122,3 @@ arrayOf.toJSON = function () { return [tfJSON(type)] } | ||
function maybe (value, strict) { | ||
return nativeTypes.Null(value) || typeforce(type, value, strict) | ||
return nativeTypes.Null(value) || typeforce(type, value, strict, maybe) | ||
} | ||
@@ -83,10 +132,11 @@ maybe.toJSON = function () { return '?' + stfJSON(type) } | ||
function object (value, strict) { | ||
typeforce(nativeTypes.Object, value, strict) | ||
if (!nativeTypes.Object(value)) return false | ||
if (nativeTypes.Null(value)) return false | ||
var propertyName, propertyType, propertyValue | ||
var propertyName | ||
try { | ||
for (propertyName in type) { | ||
propertyType = type[propertyName] | ||
propertyValue = value[propertyName] | ||
var propertyType = type[propertyName] | ||
var propertyValue = value[propertyName] | ||
@@ -96,8 +146,9 @@ typeforce(propertyType, propertyValue, strict) | ||
} catch (e) { | ||
if (/Expected property "/.test(e.message)) { | ||
e.message = e.message.replace(/Expected property "(.+)" of/, 'Expected property "' + propertyName + '.$1" of') | ||
throw e | ||
if (e instanceof TfPropertyTypeError) { | ||
throw e.asChildOf(propertyName) | ||
} else if (e instanceof TfTypeError) { | ||
throw new TfPropertyTypeError(e.tfType, propertyName, e.tfValue, e.tfError) | ||
} | ||
throw new TypeError(tfPropertyErrorString(propertyType, propertyName, propertyValue)) | ||
throw e | ||
} | ||
@@ -109,3 +160,3 @@ | ||
throw new TypeError('Unexpected property "' + propertyName + '"') | ||
throw new TfPropertyTypeError(undefined, propertyName) | ||
} | ||
@@ -124,4 +175,5 @@ } | ||
typeforce(nativeTypes.Object, value, strict) | ||
if (nativeTypes.Null(value)) return false | ||
var propertyName, propertyValue | ||
var propertyName | ||
@@ -134,12 +186,13 @@ try { | ||
propertyValue = value[propertyName] | ||
var propertyValue = value[propertyName] | ||
typeforce(propertyType, propertyValue, strict) | ||
} | ||
} catch (e) { | ||
if (/Expected property "/.test(e.message)) { | ||
e.message = e.message.replace(/Expected property "(.+)" of/, 'Expected property "' + propertyName + '.$1" of') | ||
throw e | ||
if (e instanceof TfPropertyTypeError) { | ||
throw e.asChildOf(propertyName) | ||
} else if (e instanceof TfTypeError) { | ||
throw new TfPropertyTypeError(e.tfType, propertyKeyType || propertyName, e.tfValue) | ||
} | ||
throw new TypeError(tfPropertyErrorString(propertyType, propertyKeyType || propertyName, propertyValue)) | ||
throw e | ||
} | ||
@@ -149,2 +202,3 @@ | ||
} | ||
if (propertyKeyType) { | ||
@@ -167,3 +221,5 @@ map.toJSON = function () { return '{' + stfJSON(propertyKeyType) + ': ' + stfJSON(propertyType) + '}' } | ||
} catch (e) { | ||
return false | ||
if (e instanceof TfTypeError || e instanceof TfPropertyTypeError) return false | ||
throw e | ||
} | ||
@@ -231,7 +287,7 @@ }) | ||
function typeforce (type, value, strict) { | ||
function typeforce (type, value, strict, surrogate) { | ||
if (nativeTypes.Function(type)) { | ||
if (type(value, strict)) return true | ||
throw new TypeError(tfErrorString(type, value)) | ||
throw new TfTypeError(surrogate || type, value) | ||
} | ||
@@ -258,1 +314,5 @@ | ||
module.exports.compile = compile | ||
// export Error objects | ||
module.exports.TfTypeError = TfTypeError | ||
module.exports.TfPropertyTypeError = TfPropertyTypeError |
{ | ||
"name": "typeforce", | ||
"version": "1.5.5", | ||
"version": "1.6.1", | ||
"description": "Another biased type checking solution for Javascript", | ||
@@ -26,6 +26,6 @@ "author": "Daniel Cousens", | ||
"coverage-local": "mocha --require blanket -R html-cov", | ||
"generate-test": "mocha test/__generate.js", | ||
"generate-test": "mocha scripts/__generate.js", | ||
"standard": "standard", | ||
"test": "npm run standard && npm run unit", | ||
"unit": "mocha test/index.js" | ||
"unit": "mocha" | ||
}, | ||
@@ -46,3 +46,5 @@ "config": { | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"inherits": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
@@ -49,0 +51,0 @@ "blanket": "*", |
var typeforce = require('../') | ||
function Tt () { return false } | ||
function Unmatchable () { return false } | ||
@@ -22,5 +22,5 @@ function Letter (value) { | ||
'{ a: ?{ b: ?{ c: Number } } }': { a: typeforce.maybe({ b: typeforce.maybe({ c: 'Number' }) }) }, | ||
'?Tt': Tt, | ||
'{ a: ?Tt }': { a: typeforce.maybe(Tt) }, | ||
'{ a: { b: Tt } }': { a: { b: Tt } }, | ||
'?Unmatchable': Unmatchable, | ||
'{ a: ?Unmatchable }': { a: typeforce.maybe(Unmatchable) }, | ||
'{ a: { b: Unmatchable } }': { a: { b: Unmatchable } }, | ||
'>CustomType': typeforce.quacksLike('CustomType'), | ||
@@ -27,0 +27,0 @@ '{ String }': typeforce.map('String'), |
Sorry, the diff of this file is too big to display
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
198794
9492
1
+ Addedinherits@^2.0.1
+ Addedinherits@2.0.4(transitive)