foreach-prop
Version:
Array-like methods for objects
515 lines (397 loc) • 12.1 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.eachProp = {}));
}(this, (function (exports) { 'use strict';
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';
}
/**
* isArray
*/
var isArray = Array.isArray;
/**
* toString
*/
var str = Object.prototype.toString;
/**
* Whether or not the given `val`
* is an array.
*
* example:
*
* isArray([]);
* // > true
* isArray(arguments);
* // > false
* isArray('');
* // > false
*
* @param {mixed} val
* @return {bool}
*/
var isArray_1 = isArray || function (val) {
return !! val && '[object Array]' == str.call(val);
};
var isWindow = function (obj) {
if (obj == null) {
return false;
}
var o = Object(obj);
return o === o.window;
};
var isFunction_1 = isFunction;
var toString = Object.prototype.toString;
function isFunction (fn) {
if (!fn) {
return false
}
var string = toString.call(fn);
return string === '[object Function]' ||
(typeof fn === 'function' && string !== '[object RegExp]') ||
(typeof window !== 'undefined' &&
// IE8 and below
(fn === window.setTimeout ||
fn === window.alert ||
fn === window.confirm ||
fn === window.prompt))
}
var isArrayLike = function (obj) {
if (!obj) {
return false;
}
if (isArray_1(obj)) {
return true;
}
if (isFunction_1(obj) || isWindow(obj)) {
return false;
}
obj = Object(obj);
var length = 'length' in obj && obj.length;
if (obj.nodeType === 1 && length) {
return true;
}
return length === 0 ||
typeof length === 'number' && length > 0 && ( length - 1 ) in obj;
};
function toArray(args, start) {
if (!isArrayLike(args) && args !== '') {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new TypeError("".concat(args, " can't be converted to array."));
}
if (start == null) {
start = 0;
}
if (typeof start !== 'number' || !Number.isFinite(start)) {
throw new TypeError("".concat(start, " is not a valid start point."));
}
var len = args.length;
if (start < 0) {
start += len;
}
var argsObj = Object(args);
var result = new Array(len - start);
for (var i = start; i < len; i++) {
if (i in argsObj) {
result[i - start] = argsObj[i];
}
}
return result;
}
function wrapFilterCallback(callback, thisArg, object, args, argsLen) {
if (!isFunction_1(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_1(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;
}
exports.create = create;
exports.every = every;
exports.fill = fill;
exports.filter = filter;
exports.find = find;
exports.findKey = findKey;
exports.forEach = forEach;
exports.includes = includes;
exports.keyOf = keyOf;
exports.lastKeyOf = lastKeyOf;
exports.map = map;
exports.reduce = reduce;
exports.some = some;
})));
//# sourceMappingURL=each-prop.umd.js.map