Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRebecca Turner <me@re-becca.org>2015-02-26 16:49:21 +0300
committerRebecca Turner <me@re-becca.org>2015-06-26 03:26:36 +0300
commit036e0a259f618fae746fbb29d6917cc3842dfa83 (patch)
treebee556cbdf67dbc56a7ad53fa73fa25c8b4633fb
parent85924ba6112e4162afccc0e16613f5c53202d46b (diff)
aproba@1.0.1
-rw-r--r--node_modules/aproba/.npmignore3
-rw-r--r--node_modules/aproba/LICENSE14
-rw-r--r--node_modules/aproba/README.md55
-rw-r--r--node_modules/aproba/index.js53
-rw-r--r--node_modules/aproba/package.json39
-rw-r--r--node_modules/aproba/test/index.js85
-rw-r--r--package.json2
7 files changed, 251 insertions, 0 deletions
diff --git a/node_modules/aproba/.npmignore b/node_modules/aproba/.npmignore
new file mode 100644
index 000000000..24001896d
--- /dev/null
+++ b/node_modules/aproba/.npmignore
@@ -0,0 +1,3 @@
+*~
+node_modules
+.#*
diff --git a/node_modules/aproba/LICENSE b/node_modules/aproba/LICENSE
new file mode 100644
index 000000000..f4be44d88
--- /dev/null
+++ b/node_modules/aproba/LICENSE
@@ -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.
+
diff --git a/node_modules/aproba/README.md b/node_modules/aproba/README.md
new file mode 100644
index 000000000..ef5567aa7
--- /dev/null
+++ b/node_modules/aproba/README.md
@@ -0,0 +1,55 @@
+aproba
+======
+
+A rediculously 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 | instanceof Array 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 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.
+
diff --git a/node_modules/aproba/index.js b/node_modules/aproba/index.js
new file mode 100644
index 000000000..7d8ff07f8
--- /dev/null
+++ b/node_modules/aproba/index.js
@@ -0,0 +1,53 @@
+"use strict"
+
+var types = {
+ "*": ["any", function () { return true }],
+ A: ["array", function (thingy) { return thingy instanceof Array || (typeof thingy === "object" && thingy.hasOwnProperty("callee")) }],
+ 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, type, value) {
+ return newException("EINVALIDTYPE", "Argument #"+(num+1)+": Expected "+type+" but got "+typeof value)
+}
+
+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
+}
diff --git a/node_modules/aproba/package.json b/node_modules/aproba/package.json
new file mode 100644
index 000000000..f0e3225ae
--- /dev/null
+++ b/node_modules/aproba/package.json
@@ -0,0 +1,39 @@
+{
+ "name": "aproba",
+ "version": "1.0.1",
+ "description": "A rediculously light-weight argument validator",
+ "main": "index.js",
+ "directories": {
+ "test": "test"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "tap": "^0.7.0"
+ },
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/iarna/aproba.git"
+ },
+ "keywords": [
+ "argument",
+ "validate"
+ ],
+ "author": {
+ "name": "Rebecca Turner",
+ "email": "me@re-becca.org"
+ },
+ "license": "ISC",
+ "bugs": {
+ "url": "https://github.com/iarna/aproba/issues"
+ },
+ "homepage": "https://github.com/iarna/aproba",
+ "readme": "aproba\n======\n\nA rediculously light-weight function argument validator\n\n```\nvar validate = require(\"aproba\")\n\nfunction myfunc(a, b, c) {\n // `a` must be a string, `b` a number, `c` a function\n validate('SNF', arguments) // [a,b,c] is also valid\n}\n\nmyfunc('test', 23, function () {}) // ok\nmyfunc(123, 23, function () {}) // type error\nmyfunc('test', 23) // missing arg error\nmyfunc('test', 23, function () {}, true) // too many args error\n\n```\n\nValid types are:\n\ntype | description\n---- | -----------\n* | matches any type\nA | instanceof Array OR an arguments object\nS | typeof == string\nN | typeof == number\nF | typeof == function\nO | typeof == object and not type A and not type E\nB | typeof == boolean\nE | instanceof Error OR null\n\nValidation failures throw one of three exception types, distinguished by a\n`code` property of `EMISSINGARG`, `EINVALIDTYPE` or `ETOOMANYARGS`.\n\nIf an error argument is found and is not null then the remaining arguments will not be validated.\n\n### Why this exists\n\nI wanted a very simple argument validator. It needed to do two things:\n\n1. Be more concise and easier to use than assertions\n\n2. Not encourage an infinite bikeshed of DSLs\n\nThis is why types are specified by a single character and there's no such\nthing as an optional argument. \n\nThis is not intended to validate user data. This is specifically about\nasserting the interface of your functions.\n\nIf you need greater validation, I encourage you to write them by hand or\nlook elsewhere.\n\n",
+ "readmeFilename": "README.md",
+ "gitHead": "a2ea029793a14cddb9457afd0a83dc421889c7ad",
+ "_id": "aproba@1.0.1",
+ "_shasum": "c4ac2cc5becfb8b099de7ef9f02790e7d32d99ef",
+ "_from": "aproba@>=1.0.0 <1.1.0"
+}
diff --git a/node_modules/aproba/test/index.js b/node_modules/aproba/test/index.js
new file mode 100644
index 000000000..0574709c8
--- /dev/null
+++ b/node_modules/aproba/test/index.js
@@ -0,0 +1,85 @@
+"use strict"
+var test = require("tap").test
+var validate = require("../index.js")
+
+function thrown (t, code, msg, todo) {
+ validate("OSSF", arguments)
+ try {
+ todo()
+ t.fail(msg)
+ }
+ catch (e) {
+ t.is(e.code, code, msg + e.message)
+ }
+}
+
+function notThrown (t, msg, todo) {
+ validate("OSF", arguments)
+ try {
+ todo()
+ t.pass(msg)
+ }
+ catch (e) {
+ t.fail(msg+"\n"+e.stack)
+ }
+}
+
+test("general", function (t) {
+ t.plan(69)
+ var values = {
+ "A": [],
+ "S": "test",
+ "N": 123,
+ "F": function () {},
+ "O": {},
+ "B": false,
+ "E": new Error()
+ }
+ Object.keys(values).forEach(function (type) {
+ Object.keys(values).forEach(function (contraType) {
+ if (type === contraType) {
+ notThrown(t, type + " matches " + contraType, function () {
+ validate(type, [values[contraType]])
+ })
+ }
+ else {
+ thrown(t, "EINVALIDTYPE", type + " does not match " + contraType, function () {
+ validate(type, [values[contraType]])
+ })
+ }
+ })
+ if (type === "E") {
+ notThrown(t, "null is ok for E", function () {
+ validate(type, [null])
+ })
+ }
+ else {
+ thrown(t, "EMISSINGARG", "null not ok for "+type, function () {
+ validate(type, [null])
+ })
+ }
+ })
+ Object.keys(values).forEach(function (contraType) {
+ notThrown(t, "* matches " + contraType, function () {
+ validate("*", [values[contraType]])
+ })
+ })
+ thrown(t, "EMISSINGARG", "not enough args", function () {
+ validate("SNF", ["abc", 123])
+ })
+ thrown(t, "ETOOMANYARGS", "too many args", function () {
+ validate("SNF", ["abc", 123, function () {}, true])
+ })
+ notThrown(t, "E matches null", function () {
+ validate("E", [null])
+ })
+ notThrown(t, "E matches undefined", function () {
+ validate("E", [undefined])
+ })
+ notThrown(t, "E w/ error requires nothing else", function () {
+ validate("ESN", [new Error(), "foo"])
+ })
+ thrown(t, "EMISSINGARG", "E w/o error works as usual", function () {
+ validate("ESN", [null, "foo"])
+ })
+})
diff --git a/package.json b/package.json
index 4a88bc588..ff11895fe 100644
--- a/package.json
+++ b/package.json
@@ -35,6 +35,7 @@
"ansi-regex": "~1.1.1",
"ansicolors": "~0.3.2",
"ansistyles": "~0.1.3",
+ "aproba": "~1.0.1",
"archy": "~1.0.0",
"async-some": "~1.0.2",
"block-stream": "0.0.8",
@@ -109,6 +110,7 @@
"ansi",
"ansicolors",
"ansistyles",
+ "aproba",
"archy",
"async-some",
"block-stream",