added bower, gulp
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
||||
Copyright (c) 2015, Rebecca Turner <me@re-becca.org>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
aproba
|
||||
======
|
||||
|
||||
A ridiculously light-weight function argument validator
|
||||
|
||||
```
|
||||
var validate = require("aproba")
|
||||
|
||||
function myfunc(a, b, c) {
|
||||
// `a` must be a string, `b` a number, `c` a function
|
||||
validate('SNF', arguments) // [a,b,c] is also valid
|
||||
}
|
||||
|
||||
myfunc('test', 23, function () {}) // ok
|
||||
myfunc(123, 23, function () {}) // type error
|
||||
myfunc('test', 23) // missing arg error
|
||||
myfunc('test', 23, function () {}, true) // too many args error
|
||||
|
||||
```
|
||||
|
||||
Valid types are:
|
||||
|
||||
type | description
|
||||
---- | -----------
|
||||
* | matches any type
|
||||
A | Array.isArray OR an arguments object
|
||||
S | typeof == string
|
||||
N | typeof == number
|
||||
F | typeof == function
|
||||
O | typeof == object and not type A and not type E
|
||||
B | typeof == boolean
|
||||
E | instanceof Error OR null
|
||||
|
||||
Validation failures throw one of three exception types, distinguished by a
|
||||
`code` property of `EMISSINGARG`, `EINVALIDTYPE` or `ETOOMANYARGS`.
|
||||
|
||||
If you pass in an invalid type then it will throw with a code of
|
||||
`EUNKNOWNTYPE`.
|
||||
|
||||
If an error argument is found and is not null then the remaining arguments
|
||||
will not be validated.
|
||||
|
||||
### Why this exists
|
||||
|
||||
I wanted a very simple argument validator. It needed to do two things:
|
||||
|
||||
1. Be more concise and easier to use than assertions
|
||||
|
||||
2. Not encourage an infinite bikeshed of DSLs
|
||||
|
||||
This is why types are specified by a single character and there's no such
|
||||
thing as an optional argument.
|
||||
|
||||
This is not intended to validate user data. This is specifically about
|
||||
asserting the interface of your functions.
|
||||
|
||||
If you need greater validation, I encourage you to write them by hand or
|
||||
look elsewhere.
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
'use strict'
|
||||
|
||||
function isArguments (thingy) {
|
||||
return typeof thingy === 'object' && thingy.hasOwnProperty('callee')
|
||||
}
|
||||
|
||||
var types = {
|
||||
'*': ['any', function () { return true }],
|
||||
A: ['array', function (thingy) { return Array.isArray(thingy) || isArguments(thingy) }],
|
||||
S: ['string', function (thingy) { return typeof thingy === 'string' }],
|
||||
N: ['number', function (thingy) { return typeof thingy === 'number' }],
|
||||
F: ['function', function (thingy) { return typeof thingy === 'function' }],
|
||||
O: ['object', function (thingy) { return typeof thingy === 'object' && !types.A[1](thingy) && !types.E[1](thingy) }],
|
||||
B: ['boolean', function (thingy) { return typeof thingy === 'boolean' }],
|
||||
E: ['error', function (thingy) { return thingy instanceof Error }]
|
||||
}
|
||||
|
||||
var validate = module.exports = function (schema, args) {
|
||||
if (!schema) throw missingRequiredArg(0, 'schema')
|
||||
if (!args) throw missingRequiredArg(1, 'args')
|
||||
if (!types.S[1](schema)) throw invalidType(0, 'string', schema)
|
||||
if (!types.A[1](args)) throw invalidType(1, 'array', args)
|
||||
for (var ii = 0; ii < schema.length; ++ii) {
|
||||
var type = schema[ii]
|
||||
if (!types[type]) throw unknownType(ii, type)
|
||||
var typeLabel = types[type][0]
|
||||
var typeCheck = types[type][1]
|
||||
if (type === 'E' && args[ii] == null) continue
|
||||
if (args[ii] == null) throw missingRequiredArg(ii)
|
||||
if (!typeCheck(args[ii])) throw invalidType(ii, typeLabel, args[ii])
|
||||
if (type === 'E') return
|
||||
}
|
||||
if (schema.length < args.length) throw tooManyArgs(schema.length, args.length)
|
||||
}
|
||||
|
||||
function missingRequiredArg (num) {
|
||||
return newException('EMISSINGARG', 'Missing required argument #' + (num + 1))
|
||||
}
|
||||
|
||||
function unknownType (num, type) {
|
||||
return newException('EUNKNOWNTYPE', 'Unknown type ' + type + ' in argument #' + (num + 1))
|
||||
}
|
||||
|
||||
function invalidType (num, expectedType, value) {
|
||||
var valueType
|
||||
Object.keys(types).forEach(function (typeCode) {
|
||||
if (types[typeCode][1](value)) valueType = types[typeCode][0]
|
||||
})
|
||||
return newException('EINVALIDTYPE', 'Argument #' + (num + 1) + ': Expected ' +
|
||||
expectedType + ' but got ' + valueType)
|
||||
}
|
||||
|
||||
function tooManyArgs (expected, got) {
|
||||
return newException('ETOOMANYARGS', 'Too many arguments, expected ' + expected + ' and got ' + got)
|
||||
}
|
||||
|
||||
function newException (code, msg) {
|
||||
var e = new Error(msg)
|
||||
e.code = code
|
||||
Error.captureStackTrace(e, validate)
|
||||
return e
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
{
|
||||
"raw": "aproba@^1.0.3",
|
||||
"scope": null,
|
||||
"escapedName": "aproba",
|
||||
"name": "aproba",
|
||||
"rawSpec": "^1.0.3",
|
||||
"spec": ">=1.0.3 <2.0.0",
|
||||
"type": "range"
|
||||
},
|
||||
"/mnt/Data/bach/Sites/clameurs.org/sites/all/themes/figureslibres/inifig/node_modules/gauge"
|
||||
]
|
||||
],
|
||||
"_from": "aproba@>=1.0.3 <2.0.0",
|
||||
"_id": "aproba@1.0.4",
|
||||
"_inCache": true,
|
||||
"_location": "/aproba",
|
||||
"_nodeVersion": "4.4.0",
|
||||
"_npmOperationalInternal": {
|
||||
"host": "packages-12-west.internal.npmjs.com",
|
||||
"tmp": "tmp/aproba-1.0.4.tgz_1466718885402_0.5348939662799239"
|
||||
},
|
||||
"_npmUser": {
|
||||
"name": "iarna",
|
||||
"email": "me@re-becca.org"
|
||||
},
|
||||
"_npmVersion": "3.10.2",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"raw": "aproba@^1.0.3",
|
||||
"scope": null,
|
||||
"escapedName": "aproba",
|
||||
"name": "aproba",
|
||||
"rawSpec": "^1.0.3",
|
||||
"spec": ">=1.0.3 <2.0.0",
|
||||
"type": "range"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/gauge"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/aproba/-/aproba-1.0.4.tgz",
|
||||
"_shasum": "2713680775e7614c8ba186c065d4e2e52d1072c0",
|
||||
"_shrinkwrap": null,
|
||||
"_spec": "aproba@^1.0.3",
|
||||
"_where": "/mnt/Data/bach/Sites/clameurs.org/sites/all/themes/figureslibres/inifig/node_modules/gauge",
|
||||
"author": {
|
||||
"name": "Rebecca Turner",
|
||||
"email": "me@re-becca.org"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/iarna/aproba/issues"
|
||||
},
|
||||
"dependencies": {},
|
||||
"description": "A rediculously light-weight argument validator",
|
||||
"devDependencies": {
|
||||
"standard": "^7.1.2",
|
||||
"tap": "^5.7.3"
|
||||
},
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"dist": {
|
||||
"shasum": "2713680775e7614c8ba186c065d4e2e52d1072c0",
|
||||
"tarball": "https://registry.npmjs.org/aproba/-/aproba-1.0.4.tgz"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"gitHead": "c6c8f82d519b9ec3816f20f23a9101083c022200",
|
||||
"homepage": "https://github.com/iarna/aproba",
|
||||
"keywords": [
|
||||
"argument",
|
||||
"validate"
|
||||
],
|
||||
"license": "ISC",
|
||||
"main": "index.js",
|
||||
"maintainers": [
|
||||
{
|
||||
"name": "iarna",
|
||||
"email": "me@re-becca.org"
|
||||
}
|
||||
],
|
||||
"name": "aproba",
|
||||
"optionalDependencies": {},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/iarna/aproba.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "standard && tap test/*.js"
|
||||
},
|
||||
"version": "1.0.4"
|
||||
}
|
||||
Reference in New Issue
Block a user