Pathologic was missing because of a .git folder inside.
[yaffs-website] / node_modules / aproba / index.js
1 'use strict'
2
3 function isArguments (thingy) {
4   return thingy != null && typeof thingy === 'object' && thingy.hasOwnProperty('callee')
5 }
6
7 var types = {
8   '*': {label: 'any', check: function () { return true }},
9   A: {label: 'array', check: function (thingy) { return Array.isArray(thingy) || isArguments(thingy) }},
10   S: {label: 'string', check: function (thingy) { return typeof thingy === 'string' }},
11   N: {label: 'number', check: function (thingy) { return typeof thingy === 'number' }},
12   F: {label: 'function', check: function (thingy) { return typeof thingy === 'function' }},
13   O: {label: 'object', check: function (thingy) { return typeof thingy === 'object' && thingy != null && !types.A.check(thingy) && !types.E.check(thingy) }},
14   B: {label: 'boolean', check: function (thingy) { return typeof thingy === 'boolean' }},
15   E: {label: 'error', check: function (thingy) { return thingy instanceof Error }},
16   Z: {label: 'null', check: function (thingy) { return thingy == null }}
17 }
18
19 var validate = module.exports = function (rawSchemas, args) {
20   if (arguments.length !== 2) throw wrongNumberOfArgs(['SA'], arguments.length)
21   if (!rawSchemas) throw missingRequiredArg(0, 'rawSchemas')
22   if (!args) throw missingRequiredArg(1, 'args')
23   if (!types.S.check(rawSchemas)) throw invalidType(0, ['string'], rawSchemas)
24   if (!types.A.check(args)) throw invalidType(1, ['array'], args)
25   var schemas = rawSchemas.split('|')
26   var arity = {}
27   function addSchema (schema) {
28     var group = arity[schema.length] = arity[schema.length] || []
29     if (group.indexOf(schema) === -1) group.push(schema)
30   }
31   schemas.forEach(function (schema) {
32     for (var ii = 0; ii < schema.length; ++ii) {
33       var type = schema[ii]
34       if (!types[type]) throw unknownType(ii, type)
35     }
36     if (/E.*E/.test(schema)) throw moreThanOneError(schema)
37     addSchema(schema)
38     if (/E/.test(schema)) {
39       addSchema(schema.replace(/E.*$/, 'E'))
40       addSchema(schema.replace(/E/, 'Z'))
41       if (schema.length === 1) addSchema('')
42     }
43   })
44   var matching = arity[args.length]
45   if (!matching) {
46     throw wrongNumberOfArgs(Object.keys(arity), args.length)
47   }
48   for (var ii = 0; ii < args.length; ++ii) {
49     var newMatching = matching.filter(function (schema) {
50       var type = schema[ii]
51       var typeCheck = types[type].check
52       return typeCheck(args[ii])
53     })
54     if (!newMatching.length) {
55       var labels = matching.map(function (schema) {
56         return types[schema[ii]].label
57       }).filter(function (schema) { return schema != null })
58       throw invalidType(ii, labels, args[ii])
59     }
60     matching = newMatching
61   }
62 }
63
64 function missingRequiredArg (num) {
65   return newException('EMISSINGARG', 'Missing required argument #' + (num + 1))
66 }
67
68 function unknownType (num, type) {
69   return newException('EUNKNOWNTYPE', 'Unknown type ' + type + ' in argument #' + (num + 1))
70 }
71
72 function invalidType (num, expectedTypes, value) {
73   var valueType
74   Object.keys(types).forEach(function (typeCode) {
75     if (types[typeCode].check(value)) valueType = types[typeCode].label
76   })
77   return newException('EINVALIDTYPE', 'Argument #' + (num + 1) + ': Expected ' +
78     englishList(expectedTypes) + ' but got ' + valueType)
79 }
80
81 function englishList (list) {
82   return list.join(', ').replace(/, ([^,]+)$/, ' or $1')
83 }
84
85 function wrongNumberOfArgs (expected, got) {
86   var english = englishList(expected)
87   var args = expected.every(function (ex) { return ex.length === 1 })
88     ? 'argument'
89     : 'arguments'
90   return newException('EWRONGARGCOUNT', 'Expected ' + english + ' ' + args + ' but got ' + got)
91 }
92
93 function moreThanOneError (schema) {
94   return newException('ETOOMANYERRORTYPES',
95     'Only one error type per argument signature is allowed, more than one found in "' + schema + '"')
96 }
97
98 function newException (code, msg) {
99   var e = new Error(msg)
100   e.code = code
101   Error.captureStackTrace(e, validate)
102   return e
103 }