UNPKG

foreach-prop

Version:
377 lines (289 loc) 8.63 kB
import toArray from 'args-to-arr'; import isFunction from 'is-function'; function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function createErrorFactory(template) { return function () { // eslint-disable-next-line prefer-rest-params const args = arguments; return new TypeError(template.replace(/\$(\d+)/g, (_, i) => { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions return `${args[+i]}`; })); }; } const notEnoughArgs = createErrorFactory('expected $1 arguments, got $0.'); const invalidObject = createErrorFactory('$0 is not an object.'); const invalidCallback = createErrorFactory('$0 is not a function.'); const invalidArray = createErrorFactory('$0 is not an array.'); function create(keys, value) { if (!Array.isArray(keys)) { throw invalidArray(keys); } const result = {}; for (var _iterator = _createForOfIteratorHelperLoose(keys), _step; !(_step = _iterator()).done;) { const key = _step.value; result[key] = value; } return result; } // eslint-disable-next-line @typescript-eslint/unbound-method const hasOwn = {}.hasOwnProperty; function isObject(param) { return !!param && typeof param === 'object'; } function wrapFilterCallback(callback, thisArg, object, args, argsLen) { if (!isFunction(callback)) { throw invalidCallback(callback); } if (argsLen === 2) { return key => callback.call(thisArg, object[key], key); } if (argsLen === 3) { const extra = args[2]; return key => callback.call(thisArg, object[key], key, extra); } const extra = toArray(args, 2); return key => callback.call(thisArg, object[key], key, ...extra); } function wrapReduceCallback(callback, thisArg, object, args, argsLen) { if (!isFunction(callback)) { throw invalidCallback(callback); } if (argsLen === 3) { return (key, result) => callback.call(thisArg, result, object[key], key); } if (argsLen === 4) { const extra = args[3]; return (key, result) => callback.call(thisArg, result, object[key], key, extra); } const extra = toArray(args, 3); return (key, result) => callback.call(thisArg, result, object[key], key, ...extra); } function every(object, callback) { // eslint-disable-next-line prefer-rest-params const args = arguments; const argsLen = args.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const wrapped = wrapFilterCallback(callback, this, object, args, argsLen); for (const key in object) { if (hasOwn.call(object, key) && !wrapped(key)) { return false; } } return true; } function fill(object, value) { // eslint-disable-next-line prefer-rest-params const argsLen = arguments.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const result = {}; for (const key in object) { if (hasOwn.call(object, key)) { result[key] = value; } } return result; } function filter(object, callback) { // eslint-disable-next-line prefer-rest-params const args = arguments; const argsLen = args.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const wrapped = wrapFilterCallback(callback, this, object, args, argsLen); const result = {}; for (const key in object) { if (hasOwn.call(object, key) && wrapped(key)) { result[key] = object[key]; } } return result; } function find(object, callback) { // eslint-disable-next-line prefer-rest-params const args = arguments; const argsLen = args.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const wrapped = wrapFilterCallback(callback, this, object, args, argsLen); for (const key in object) { if (hasOwn.call(object, key) && wrapped(key)) { return object[key]; } } } function findKey(object, callback) { // eslint-disable-next-line prefer-rest-params const args = arguments; const argsLen = args.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const wrapped = wrapFilterCallback(callback, this, object, args, argsLen); for (const key in object) { if (hasOwn.call(object, key) && wrapped(key)) { return key; } } return null; } function forEach(object, callback) { // eslint-disable-next-line prefer-rest-params const args = arguments; const argsLen = args.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const wrapped = wrapFilterCallback(callback, this, object, args, argsLen); for (const key in object) { if (hasOwn.call(object, key)) { wrapped(key); } } } function keyOf(object, value) { const argsLen = arguments.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } for (const key in object) { if (hasOwn.call(object, key) && object[key] === value) { return key; } } return null; } function includes(object, value) { return keyOf(object, value) !== null; } function lastKeyOf(object, value) { const argsLen = arguments.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } let result = null; for (const key in object) { if (hasOwn.call(object, key) && object[key] === value) { result = key; } } return result; } function map(object, callback) { // eslint-disable-next-line prefer-rest-params const args = arguments; const argsLen = args.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const wrapped = wrapFilterCallback(callback, this, object, args, argsLen); const result = {}; for (const key in object) { if (hasOwn.call(object, key)) { result[key] = wrapped(key); } } return result; } function reduce(object, callback, initial) { // eslint-disable-next-line prefer-rest-params const args = arguments; const argsLen = args.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const wrapped = wrapReduceCallback(callback, this, object, args, argsLen); let result = initial; for (const key in object) { if (hasOwn.call(object, key)) { result = wrapped(key, result); } } return result; } function some(object, callback) { // eslint-disable-next-line prefer-rest-params const args = arguments; const argsLen = args.length; if (argsLen < 2) { throw notEnoughArgs(argsLen, 2); } if (!isObject(object)) { throw invalidObject(object); } const wrapped = wrapFilterCallback(callback, this, object, args, argsLen); for (const key in object) { if (hasOwn.call(object, key) && wrapped(key)) { return true; } } return false; } export { create, every, fill, filter, find, findKey, forEach, includes, keyOf, lastKeyOf, map, reduce, some }; //# sourceMappingURL=each-prop.mjs.map