Comparing version 1.2.1 to 1.2.2
129
lib/index.js
@@ -7,2 +7,6 @@ 'use strict'; | ||
function getFunctionTypeName(fn) { | ||
return fn.toJSON ? fn.toJSON() : getFunctionName(fn); | ||
} | ||
function getTypeName(value) { | ||
@@ -20,2 +24,16 @@ if (nativeTypes.Null(value)) return ''; | ||
function tfPropertyErrorString(type, name, value) { | ||
var typeName; | ||
if (nativeTypes.Function(type)) { | ||
typeName = getFunctionTypeName(type); | ||
} else if (nativeTypes.Object(type)) { | ||
typeName = JSON.stringify(type); | ||
} else if (nativeTypes.String(type)) { | ||
typeName = type; | ||
} | ||
return tfErrorString('property \"' + name + '\" of type ' + typeName, value); | ||
} | ||
var nativeTypes = { | ||
@@ -73,21 +91,36 @@ Array: (function (_Array) { | ||
arrayOf: function arrayOf(type) { | ||
return function arrayOf(value, strict) { | ||
typeforce(nativeTypes.Array, value, strict); | ||
function arrayOf(value, strict) { | ||
if (!nativeTypes.Array(value)) return false; | ||
return value.every(function (x) { | ||
return typeforce(type, x, strict); | ||
try { | ||
return typeforce(type, x, strict); | ||
} catch (e) { | ||
return false; | ||
} | ||
}); | ||
} | ||
arrayOf.toJSON = function () { | ||
return '[' + JSON.stringify(type) + ']'; | ||
}; | ||
return arrayOf; | ||
}, | ||
maybe: function maybe(type) { | ||
return function maybe(value, strict) { | ||
function maybe(value, strict) { | ||
if (nativeTypes.Null(value, strict)) return true; | ||
return typeforce(type, value, strict); | ||
} | ||
// TODO: FIXME: why do I need to do this? | ||
maybe.toJSON = function () { | ||
return '?' + (type.toJSON && type.toJSON() || JSON.stringify(type)); | ||
}; | ||
return maybe; | ||
}, | ||
object: function object(type) { | ||
return function object(value, strict) { | ||
function object(value, strict) { | ||
typeforce(nativeTypes.Object, value, strict); | ||
@@ -102,3 +135,3 @@ | ||
} catch (e) { | ||
throw new TypeError(tfErrorString('property \"' + propertyName + '\" of type ' + JSON.stringify(propertyType), propertyValue)); | ||
throw new TypeError(tfPropertyErrorString(propertyType, propertyName, propertyValue)); | ||
} | ||
@@ -114,5 +147,10 @@ } | ||
} | ||
object.toJSON = function () { | ||
return JSON.stringify(type); | ||
}; | ||
return true; | ||
}; | ||
} | ||
return object; | ||
}, | ||
@@ -145,2 +183,6 @@ | ||
} else if (nativeTypes.Object(type)) { | ||
if (nativeTypes.Array(type)) { | ||
return otherTypes.arrayOf(otherTypes.compile(type[0])); | ||
} | ||
var compiled = {}; | ||
@@ -177,19 +219,9 @@ | ||
case 'function': | ||
{ | ||
if (type(value, strict)) return true; | ||
if (type(value, strict)) return true; | ||
throw new TypeError(tfErrorString(getFunctionName(type), value)); | ||
} | ||
throw new TypeError(tfErrorString(getFunctionTypeName(type), value)); | ||
case 'object': | ||
{ | ||
if (nativeTypes.Array(type)) { | ||
_x3 = otherTypes.arrayOf(type[0]); | ||
_x4 = value; | ||
_x5 = strict; | ||
_again = true; | ||
continue _function; | ||
} | ||
_x3 = otherTypes.object(type); | ||
if (nativeTypes.Array(type)) { | ||
_x3 = otherTypes.arrayOf(type[0]); | ||
_x4 = value; | ||
@@ -201,26 +233,30 @@ _x5 = strict; | ||
_x3 = otherTypes.object(type); | ||
_x4 = value; | ||
_x5 = strict; | ||
_again = true; | ||
continue _function; | ||
case 'string': | ||
{ | ||
if (type[0] === '?') { | ||
type = type.slice(1); | ||
if (type[0] === '?') { | ||
type = type.slice(1); | ||
_x3 = otherTypes.maybe(type); | ||
_x4 = value; | ||
_x5 = strict; | ||
_again = true; | ||
continue _function; | ||
} | ||
_x3 = otherTypes.maybe(type); | ||
_x4 = value; | ||
_x5 = strict; | ||
_again = true; | ||
continue _function; | ||
} | ||
var tfType = nativeTypes[type]; | ||
if (tfType) { | ||
_x3 = tfType; | ||
_x4 = value; | ||
_x5 = strict; | ||
_again = true; | ||
continue _function; | ||
} | ||
if (type === getTypeName(value)) return true; | ||
var tfType = nativeTypes[type]; | ||
if (tfType) { | ||
_x3 = tfType; | ||
_x4 = value; | ||
_x5 = strict; | ||
_again = true; | ||
continue _function; | ||
} | ||
if (type === getTypeName(value)) return true; | ||
break; | ||
} | ||
break; | ||
} | ||
@@ -235,6 +271,11 @@ | ||
var typeName; | ||
for (typeName in nativeTypes) { | ||
typeforce[typeName] = nativeTypes[typeName]; | ||
} | ||
Object.keys(nativeTypes).forEach(function (typeName) { | ||
var nativeType = nativeTypes[typeName]; | ||
nativeType.toJSON = function () { | ||
return typeName; | ||
}; | ||
typeforce[typeName] = nativeType; | ||
}); | ||
for (typeName in otherTypes) { | ||
@@ -241,0 +282,0 @@ typeforce[typeName] = otherTypes[typeName]; |
{ | ||
"name": "typeforce", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Another biased type checking solution for Javascript", | ||
@@ -5,0 +5,0 @@ "author": "Daniel Cousens", |
@@ -71,3 +71,3 @@ # typeforce | ||
// also use strictness for recursive types to enforce whitelisting properties | ||
// use strictness for recursive types to enforce whitelisting properties | ||
typeforce({ | ||
@@ -74,0 +74,0 @@ x: 'Number' |
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
10932
223