Comparing version 1.2.0 to 2.0.0
66
index.js
'use strict' | ||
module.exports = validate | ||
@@ -7,20 +8,20 @@ function isArguments (thingy) { | ||
var types = { | ||
'*': {label: 'any', check: function () { return true }}, | ||
A: {label: 'array', check: function (thingy) { return Array.isArray(thingy) || isArguments(thingy) }}, | ||
S: {label: 'string', check: function (thingy) { return typeof thingy === 'string' }}, | ||
N: {label: 'number', check: function (thingy) { return typeof thingy === 'number' }}, | ||
F: {label: 'function', check: function (thingy) { return typeof thingy === 'function' }}, | ||
O: {label: 'object', check: function (thingy) { return typeof thingy === 'object' && thingy != null && !types.A.check(thingy) && !types.E.check(thingy) }}, | ||
B: {label: 'boolean', check: function (thingy) { return typeof thingy === 'boolean' }}, | ||
E: {label: 'error', check: function (thingy) { return thingy instanceof Error }}, | ||
Z: {label: 'null', check: function (thingy) { return thingy == null }} | ||
const types = { | ||
'*': {label: 'any', check: () => true}, | ||
A: {label: 'array', check: _ => Array.isArray(_) || isArguments(_)}, | ||
S: {label: 'string', check: _ => typeof _ === 'string'}, | ||
N: {label: 'number', check: _ => typeof _ === 'number'}, | ||
F: {label: 'function', check: _ => typeof _ === 'function'}, | ||
O: {label: 'object', check: _ => typeof _ === 'object' && _ != null && !types.A.check(_) && !types.E.check(_)}, | ||
B: {label: 'boolean', check: _ => typeof _ === 'boolean'}, | ||
E: {label: 'error', check: _ => _ instanceof Error}, | ||
Z: {label: 'null', check: _ => _ == null} | ||
} | ||
function addSchema (schema, arity) { | ||
var group = arity[schema.length] = arity[schema.length] || [] | ||
const group = arity[schema.length] = arity[schema.length] || [] | ||
if (group.indexOf(schema) === -1) group.push(schema) | ||
} | ||
var validate = module.exports = function (rawSchemas, args) { | ||
function validate (rawSchemas, args) { | ||
if (arguments.length !== 2) throw wrongNumberOfArgs(['SA'], arguments.length) | ||
@@ -31,8 +32,8 @@ if (!rawSchemas) throw missingRequiredArg(0, 'rawSchemas') | ||
if (!types.A.check(args)) throw invalidType(1, ['array'], args) | ||
var schemas = rawSchemas.split('|') | ||
var arity = {} | ||
const schemas = rawSchemas.split('|') | ||
const arity = {} | ||
schemas.forEach(function (schema) { | ||
for (var ii = 0; ii < schema.length; ++ii) { | ||
var type = schema[ii] | ||
schemas.forEach(schema => { | ||
for (let ii = 0; ii < schema.length; ++ii) { | ||
const type = schema[ii] | ||
if (!types[type]) throw unknownType(ii, type) | ||
@@ -48,16 +49,14 @@ } | ||
}) | ||
var matching = arity[args.length] | ||
let matching = arity[args.length] | ||
if (!matching) { | ||
throw wrongNumberOfArgs(Object.keys(arity), args.length) | ||
} | ||
for (var ii = 0; ii < args.length; ++ii) { | ||
var newMatching = matching.filter(function (schema) { | ||
var type = schema[ii] | ||
var typeCheck = types[type].check | ||
for (let ii = 0; ii < args.length; ++ii) { | ||
let newMatching = matching.filter(schema => { | ||
const type = schema[ii] | ||
const typeCheck = types[type].check | ||
return typeCheck(args[ii]) | ||
}) | ||
if (!newMatching.length) { | ||
var labels = matching.map(function (schema) { | ||
return types[schema[ii]].label | ||
}).filter(function (schema) { return schema != null }) | ||
const labels = matching.map(_ => types[_[ii]].label).filter(_ => _ != null) | ||
throw invalidType(ii, labels, args[ii]) | ||
@@ -78,4 +77,4 @@ } | ||
function invalidType (num, expectedTypes, value) { | ||
var valueType | ||
Object.keys(types).forEach(function (typeCode) { | ||
let valueType | ||
Object.keys(types).forEach(typeCode => { | ||
if (types[typeCode].check(value)) valueType = types[typeCode].label | ||
@@ -92,4 +91,4 @@ }) | ||
function wrongNumberOfArgs (expected, got) { | ||
var english = englishList(expected) | ||
var args = expected.every(function (ex) { return ex.length === 1 }) | ||
const english = englishList(expected) | ||
const args = expected.every(ex => ex.length === 1) | ||
? 'argument' | ||
@@ -106,6 +105,7 @@ : 'arguments' | ||
function newException (code, msg) { | ||
var e = new Error(msg) | ||
e.code = code | ||
if (Error.captureStackTrace) Error.captureStackTrace(e, validate) | ||
return e | ||
const err = new Error(msg) | ||
err.code = code | ||
/* istanbul ignore else */ | ||
if (Error.captureStackTrace) Error.captureStackTrace(err, validate) | ||
return err | ||
} |
{ | ||
"name": "aproba", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A ridiculously light-weight argument validator (now browser friendly)", | ||
@@ -11,4 +11,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"standard": "^10.0.3", | ||
"tap": "^10.0.2" | ||
"standard": "^11.0.1", | ||
"tap": "^12.0.1" | ||
}, | ||
@@ -19,3 +19,4 @@ "files": [ | ||
"scripts": { | ||
"test": "standard && tap -j3 test/*.js" | ||
"pretest": "standard", | ||
"test": "tap --100 -J test/*.js" | ||
}, | ||
@@ -22,0 +23,0 @@ "repository": { |
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
5
8047