"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/aproba/index.js" (8 Mar 2017, 2379 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 'use strict'
    2 
    3 function isArguments (thingy) {
    4   return typeof thingy === 'object' && thingy.hasOwnProperty('callee')
    5 }
    6 
    7 var types = {
    8   '*': ['any', function () { return true }],
    9   A: ['array', function (thingy) { return Array.isArray(thingy) || isArguments(thingy) }],
   10   S: ['string', function (thingy) { return typeof thingy === 'string' }],
   11   N: ['number', function (thingy) { return typeof thingy === 'number' }],
   12   F: ['function', function (thingy) { return typeof thingy === 'function' }],
   13   O: ['object', function (thingy) { return typeof thingy === 'object' && !types.A[1](thingy) && !types.E[1](thingy) }],
   14   B: ['boolean', function (thingy) { return typeof thingy === 'boolean' }],
   15   E: ['error', function (thingy) { return thingy instanceof Error }]
   16 }
   17 
   18 var validate = module.exports = function (schema, args) {
   19   if (!schema) throw missingRequiredArg(0, 'schema')
   20   if (!args) throw missingRequiredArg(1, 'args')
   21   if (!types.S[1](schema)) throw invalidType(0, 'string', schema)
   22   if (!types.A[1](args)) throw invalidType(1, 'array', args)
   23   for (var ii = 0; ii < schema.length; ++ii) {
   24     var type = schema[ii]
   25     if (!types[type]) throw unknownType(ii, type)
   26     var typeLabel = types[type][0]
   27     var typeCheck = types[type][1]
   28     if (type === 'E' && args[ii] == null) continue
   29     if (args[ii] == null) throw missingRequiredArg(ii)
   30     if (!typeCheck(args[ii])) throw invalidType(ii, typeLabel, args[ii])
   31     if (type === 'E') return
   32   }
   33   if (schema.length < args.length) throw tooManyArgs(schema.length, args.length)
   34 }
   35 
   36 function missingRequiredArg (num) {
   37   return newException('EMISSINGARG', 'Missing required argument #' + (num + 1))
   38 }
   39 
   40 function unknownType (num, type) {
   41   return newException('EUNKNOWNTYPE', 'Unknown type ' + type + ' in argument #' + (num + 1))
   42 }
   43 
   44 function invalidType (num, expectedType, value) {
   45   var valueType
   46   Object.keys(types).forEach(function (typeCode) {
   47     if (types[typeCode][1](value)) valueType = types[typeCode][0]
   48   })
   49   return newException('EINVALIDTYPE', 'Argument #' + (num + 1) + ': Expected ' +
   50     expectedType + ' but got ' + valueType)
   51 }
   52 
   53 function tooManyArgs (expected, got) {
   54   return newException('ETOOMANYARGS', 'Too many arguments, expected ' + expected + ' and got ' + got)
   55 }
   56 
   57 function newException (code, msg) {
   58   var e = new Error(msg)
   59   e.code = code
   60   Error.captureStackTrace(e, validate)
   61   return e
   62 }