Comparing version 1.2.1 to 1.3.0
{ | ||
"name": "typology", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "A data validation library for Node.js and the browser.", | ||
"main": "typology.js", | ||
"files": [ | ||
"typology.js", | ||
"typology.d.ts" | ||
], | ||
"scripts": { | ||
"test": "nyc mocha" | ||
"test": "mocha -r ts-node/register test/unit.test.ts" | ||
}, | ||
@@ -33,9 +37,14 @@ "husky": { | ||
"devDependencies": { | ||
"@types/chai": "^4.3.1", | ||
"@types/lodash": "^4.14.182", | ||
"@types/mocha": "^9.1.1", | ||
"chai": "^4.3.6", | ||
"husky": "^3.0.9", | ||
"lodash": "^4.17.15", | ||
"mocha": "^6.2.2", | ||
"nyc": "^14.1.1", | ||
"lodash": "^4.17.21", | ||
"mocha": "^10.0.0", | ||
"prettier": "^1.18.2", | ||
"pretty-quick": "^2.0.0" | ||
"pretty-quick": "^2.0.0", | ||
"ts-node": "^10.8.2", | ||
"typescript": "^4.7.4" | ||
} | ||
} |
160
typology.js
@@ -15,3 +15,3 @@ /** | ||
*/ | ||
const __nativeTypes = { "*": true }; | ||
const __nativeTypes = { '*': true }; | ||
@@ -22,18 +22,18 @@ (function() { | ||
const classes = [ | ||
"Boolean", | ||
"Number", | ||
"String", | ||
"Object", | ||
"Array", | ||
"Function", | ||
"Arguments", | ||
"RegExp", | ||
"Date", | ||
'Boolean', | ||
'Number', | ||
'String', | ||
'Object', | ||
'Array', | ||
'Function', | ||
'Arguments', | ||
'RegExp', | ||
'Date', | ||
// ES2015 | ||
"Map", | ||
"Set", | ||
"WeakMap", | ||
"WeakSet", | ||
"Symbol" | ||
'Map', | ||
'Set', | ||
'WeakMap', | ||
'WeakSet', | ||
'Symbol' | ||
]; | ||
@@ -45,3 +45,3 @@ | ||
__nativeTypes[className.toLowerCase()] = true; | ||
__class2type["[object " + className + "]"] = className.toLowerCase(); | ||
__class2type['[object ' + className + ']'] = className.toLowerCase(); | ||
} | ||
@@ -60,3 +60,3 @@ })(); | ||
? String(value) | ||
: __class2type[Object.prototype.toString.call(value)] || "object"; | ||
: __class2type[Object.prototype.toString.call(value)] || 'object'; | ||
} | ||
@@ -176,17 +176,17 @@ | ||
if (requiredTypeOf === "string") { | ||
a = type.replace(/^[\?\!]/, "").split(/\|/); | ||
if (requiredTypeOf === 'string') { | ||
a = type.replace(/^[\?\!]/, '').split(/\|/); | ||
l = a.length; | ||
for (i = 0; i < l; i++) | ||
if (!__nativeTypes[a[i]] && typeof _customTypes[a[i]] === "undefined") | ||
throw new Error("Invalid type."); | ||
if (!__nativeTypes[a[i]] && typeof _customTypes[a[i]] === 'undefined') | ||
throw new Error('Invalid type.'); | ||
if (type.charAt(0) === "?") optional = true; | ||
else if (type.charAt(0) === "!") exclusive = true; | ||
if (type.charAt(0) === '?') optional = true; | ||
else if (type.charAt(0) === '!') exclusive = true; | ||
l = a.length; | ||
for (i = 0; i < l; i++) | ||
if (typeof _customTypes[a[i]] !== "undefined") | ||
if (typeof _customTypes[a[i]] !== 'undefined') | ||
if ( | ||
typeof _customTypes[a[i]].type === "function" | ||
typeof _customTypes[a[i]].type === 'function' | ||
? _customTypes[a[i]].type.call(_self, obj) === true | ||
@@ -197,4 +197,3 @@ : !this.scan(_customTypes[a[i]].type, obj).error | ||
return { | ||
error: | ||
'Expected a "' + type + '" but found a ' + '"' + a[i] + '".', | ||
error: 'Expected a "' + type + '" but found a ' + '"' + a[i] + '".', | ||
expected: type, | ||
@@ -215,4 +214,3 @@ type: a[i], | ||
return { | ||
error: | ||
'Expected a "' + type + '" but found a ' + '"' + typeOf + '".', | ||
error: 'Expected a "' + type + '" but found a ' + '"' + typeOf + '".', | ||
expected: type, | ||
@@ -230,3 +228,3 @@ type: typeOf, | ||
} else { | ||
hasStar = ~a.indexOf("*"); | ||
hasStar = ~a.indexOf('*'); | ||
hasTypeOf = ~a.indexOf(typeOf); | ||
@@ -236,10 +234,5 @@ if (exclusive && (hasStar || hasTypeOf)) | ||
error: | ||
'Expected a "' + | ||
type + | ||
'" but found a ' + | ||
'"' + | ||
(hasTypeOf ? typeOf : "*") + | ||
'".', | ||
'Expected a "' + type + '" but found a ' + '"' + (hasTypeOf ? typeOf : '*') + '".', | ||
expected: type, | ||
type: hasTypeOf ? typeOf : "*", | ||
type: hasTypeOf ? typeOf : '*', | ||
value: obj | ||
@@ -249,4 +242,3 @@ }; | ||
return { | ||
error: | ||
'Expected a "' + type + '" but found a ' + '"' + typeOf + '".', | ||
error: 'Expected a "' + type + '" but found a ' + '"' + typeOf + '".', | ||
expected: type, | ||
@@ -263,4 +255,4 @@ type: typeOf, | ||
} | ||
} else if (requiredTypeOf === "object") { | ||
if (typeOf !== "object") | ||
} else if (requiredTypeOf === 'object') { | ||
if (typeOf !== 'object') | ||
return { | ||
@@ -288,3 +280,3 @@ error: 'Expected an object but found a "' + typeOf + '".', | ||
for (k = 0; k < l; k++) | ||
if (typeof type[objKeys[k]] === "undefined") | ||
if (typeof type[objKeys[k]] === 'undefined') | ||
return { | ||
@@ -302,3 +294,3 @@ error: 'Unexpected key "' + objKeys[k] + '".', | ||
}; | ||
} else if (requiredTypeOf === "function") { | ||
} else if (requiredTypeOf === 'function') { | ||
const output = { | ||
@@ -311,10 +303,9 @@ expected: type, | ||
// Just applying a function | ||
if (!type(obj)) | ||
output.error = "The target did not pass the given test (function)."; | ||
if (!type(obj)) output.error = 'The target did not pass the given test (function).'; | ||
return output; | ||
} else if (requiredTypeOf === "array") { | ||
if (type.length !== 1) throw new Error("Invalid type."); | ||
} else if (requiredTypeOf === 'array') { | ||
if (type.length !== 1) throw new Error('Invalid type.'); | ||
if (typeOf !== "array") | ||
if (typeOf !== 'array') | ||
return { | ||
@@ -341,3 +332,3 @@ error: 'Expected an array but found a "' + typeOf + '".', | ||
}; | ||
} else throw new Error("Invalid type."); | ||
} else throw new Error('Invalid type.'); | ||
}; | ||
@@ -407,3 +398,3 @@ | ||
if (arguments.length === 1) { | ||
if (this.get(a1) === "object") { | ||
if (this.get(a1) === 'object') { | ||
o = a1; | ||
@@ -414,10 +405,9 @@ id = o.id; | ||
throw new Error( | ||
"If types.add is called with one argument, " + | ||
"this one has to be an object." | ||
'If types.add is called with one argument, ' + 'this one has to be an object.' | ||
); | ||
} else if (arguments.length === 2) { | ||
if (typeof a1 !== "string" || !a1) | ||
if (typeof a1 !== 'string' || !a1) | ||
throw new Error( | ||
"If types.add is called with more than one " + | ||
"argument, the first one must be the string id." | ||
'If types.add is called with more than one ' + | ||
'argument, the first one must be the string id.' | ||
); | ||
@@ -427,15 +417,11 @@ else id = a1; | ||
type = a2; | ||
} else | ||
throw new Error( | ||
"types.add has to be called " + "with one or two arguments." | ||
); | ||
} else throw new Error('types.add has to be called ' + 'with one or two arguments.'); | ||
if (this.get(id) !== "string" || id.length === 0) | ||
throw new Error("A type requires an string id."); | ||
if (this.get(id) !== 'string' || id.length === 0) | ||
throw new Error('A type requires an string id.'); | ||
if (_customTypes[id] !== undefined && _customTypes[id] !== "proto") | ||
if (_customTypes[id] !== undefined && _customTypes[id] !== 'proto') | ||
throw new Error('The type "' + id + '" already exists.'); | ||
if (__nativeTypes[id]) | ||
throw new Error('"' + id + '" is a reserved type name.'); | ||
if (__nativeTypes[id]) throw new Error('"' + id + '" is a reserved type name.'); | ||
@@ -454,7 +440,7 @@ _customTypes[id] = 1; | ||
if (this.get(type) !== "function" && !this.isValid(type)) | ||
if (this.get(type) !== 'function' && !this.isValid(type)) | ||
throw new Error( | ||
"A type requires a valid definition. " + | ||
"This one can be a preexistant type or else " + | ||
"a function testing given objects." | ||
'A type requires a valid definition. ' + | ||
'This one can be a preexistant type or else ' + | ||
'a function testing given objects.' | ||
); | ||
@@ -567,22 +553,17 @@ | ||
if (typeOf === "string") { | ||
a = type.replace(/^[\?\!]/, "").split(/\|/); | ||
if (typeOf === 'string') { | ||
a = type.replace(/^[\?\!]/, '').split(/\|/); | ||
aKeys = Object.keys(a); | ||
l = aKeys.length; | ||
for (i = 0; i < l; i++) | ||
if ( | ||
!__nativeTypes[a[aKeys[i]]] && | ||
typeof _customTypes[a[aKeys[i]]] === "undefined" | ||
) | ||
if (!__nativeTypes[a[aKeys[i]]] && typeof _customTypes[a[aKeys[i]]] === 'undefined') | ||
return false; | ||
return true; | ||
} else if (typeOf === "object") { | ||
} else if (typeOf === 'object') { | ||
typeKeys = Object.keys(type); | ||
l = typeKeys.length; | ||
for (k = 0; k < l; k++) | ||
if (!this.isValid(type[typeKeys[k]])) return false; | ||
for (k = 0; k < l; k++) if (!this.isValid(type[typeKeys[k]])) return false; | ||
return true; | ||
} else if (typeOf === "array") | ||
return type.length === 1 ? this.isValid(type[0]) : false; | ||
else if (typeOf === "function") return true; | ||
} else if (typeOf === 'array') return type.length === 1 ? this.isValid(type[0]) : false; | ||
else if (typeOf === 'function') return true; | ||
else return false; | ||
@@ -598,3 +579,3 @@ }; | ||
this.add( | ||
"type", | ||
'type', | ||
function(v) { | ||
@@ -606,7 +587,7 @@ return this.isValid(v); | ||
// Add a type "primitive" to match every primitive types (including null): | ||
this.add("primitive", v => v !== Object(v)); | ||
this.add('primitive', v => v !== Object(v)); | ||
// Adding custom types at instantiation: | ||
defs = defs || {}; | ||
if (this.get(defs) !== "object") throw Error("Invalid argument."); | ||
if (this.get(defs) !== 'object') throw Error('Invalid argument.'); | ||
@@ -626,4 +607,4 @@ for (let k in defs) this.add(k, defs[k]); | ||
// Version: | ||
Object.defineProperty(types, "version", { | ||
value: "1.2.1" | ||
Object.defineProperty(types, 'version', { | ||
value: '1.3.0' | ||
}); | ||
@@ -635,10 +616,9 @@ | ||
*/ | ||
if (typeof exports !== "undefined") { | ||
if (typeof module !== "undefined" && module.exports) | ||
exports = module.exports = types; | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) exports = module.exports = types; | ||
exports.types = types; | ||
} else if (typeof define === "function" && define.amd) | ||
define("typology", [], function() { | ||
} else if (typeof define === 'function' && define.amd) | ||
define('typology', [], function() { | ||
return types; | ||
}); | ||
else scope.types = types; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
0
0
24896
11
5
589