Created
February 7, 2017 21:29
-
-
Save JaKXz/c7f077d271a441e8d717008b885019bb to your computer and use it in GitHub Desktop.
argsert transpiled by babel 6.22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | |
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
module.exports = function argsert(typeConfig) { | |
var _this = this; | |
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | |
args[_key - 1] = arguments[_key]; | |
} | |
if (typeof typeConfig !== 'string' || !isRequired(typeConfig) && !isOptional(typeConfig)) { | |
args = [typeConfig]; | |
typeConfig = ''; | |
} | |
var types = getTypes(typeConfig); | |
var configuredKeys = Object.keys(types); | |
var numRequired = configuredKeys.filter(function (k) { | |
return 'required' in types[k]; | |
}).length; | |
args = compactArgs(args); | |
if (args.length < numRequired) { | |
throw new Error('Not enough arguments provided. Expected ' + numRequired + ' but received ' + args.length + '.'); | |
} | |
if (args.length > configuredKeys.length) { | |
throw new Error('Too many arguments provided. Expected max ' + configuredKeys.length + ' but received ' + args.length + '.'); | |
} | |
args.forEach(function (arg, index) { | |
var observedType = Array.isArray(arg) ? 'array' : arg === null ? 'null' : typeof arg === 'undefined' ? 'undefined' : _typeof(arg); | |
var typesAtIndex = types[index]; | |
var errorMessage = invalidArgMessage.bind(_this, positionName(index), typesAtIndex, observedType); | |
if ('required' in typesAtIndex) { | |
var required = typesAtIndex.required; | |
if (required.indexOf(observedType) < 0 && required.indexOf('*') < 0) { | |
throw new TypeError(errorMessage('required')); | |
} | |
} | |
if ('optional' in typesAtIndex) { | |
var optional = typesAtIndex.optional; | |
if (optional.indexOf('*') < 0 && optional.indexOf(observedType) < 0) { | |
throw new TypeError(errorMessage('optional')); | |
} | |
} | |
}); | |
return true; | |
}; | |
function getTypes(typeConfig) { | |
return typeConfig.split(' ').reduce(function (result, str, index) { | |
if (isOptional(str)) { | |
return Object.assign({}, result, _defineProperty({}, index, { | |
optional: str.split('|').map(function (s) { | |
return s.replace('[', '').replace(']', ''); | |
}) | |
})); | |
} else if (isRequired(str)) { | |
return Object.assign({}, result, _defineProperty({}, index, { | |
required: str.split('|').map(function (s) { | |
return s.replace('<', '').replace('>', ''); | |
}) | |
})); | |
} else if (str === '') { | |
return result; | |
} else { | |
throw new Error('Invalid type config in the ' + positionName(index) + ' position.'); | |
} | |
}, {}); | |
} | |
function compactArgs(args) { | |
var lastArg = args[args.length - 1]; | |
if (lastArg === undefined || lastArg === '') { | |
return args.filter(function (arg) { | |
return arg !== undefined && arg !== ''; | |
}); | |
} | |
return args; | |
} | |
function invalidArgMessage(position, types, observed, kind) { | |
return 'Invalid ' + position + ' argument. Expected ' + expectedTypes(types, kind) + ' but received ' + observed + '.'; | |
} | |
function positionName(index) { | |
var positionNames = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']; | |
return positionNames[index] || 'manyith'; | |
} | |
function expectedTypes(types, kind) { | |
return types[kind].join(' or '); | |
} | |
function isOptional(arg) { | |
return arg.match(/^\[(\w+|\*)((?:\|(\w+))*?)]/); | |
} | |
function isRequired(arg) { | |
return arg.match(/^<(\w+|\*)((?:\|(\w+))*?)>/); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment