UNPKG

array-helper-functions

Version:
148 lines (125 loc) 3.56 kB
'use strict'; var isFunction = function isFunction(func) { if (typeof func === 'function') { return true; } return false; }; /** * @flatten: flatten arrays going only one level deep. If `isDeep` * is set to true then deep nested arrays will flatten too. * e.g [1, 2, 3, [[[10]]]] => [1, 2, 3, 10] * @param {} array @type array: [1, 2, 3] * @param {} isDeep @type boolean: passing `true` will result in: * flatten([1, 2, [[3]]], true) => [1, 2, 3] * */ var flatten = function flatten(array, isDeep) { return array.reduce(function (a, b) { if (Array.isArray(b) && isDeep) { return a.concat(flatten(b, isDeep)); } return a.concat(b); }, []); }; /** * @filter: Filter out array elements. If `isDeep` is set to true * then deep nested arrays will be flattened and then filtered. * e.g ([1, 2, 3], 3) => [1, 2] * e.g ([1, 2, [[[3]]], 3, true) // `isDeep` set to true * * @param {} array @type array: [1, 2, 3] * @param {} filterItem @type element: filter([1, 2, 3], 3) => [1, 2] * @param {} isDeep @type boolean: true = calls `flatten` before filtering. */ var filter = function filter(array, filterItem, isDeep) { var filterArray = array; if (isDeep) { filterArray = flatten(filterArray, true); } for (var i = filterArray.length; i--;) { if (filterArray[i] === filterItem) { filterArray.splice(i, 1); } } return filterArray; }; /** * @compact: Remove falsy values from a given array. * e.g [1, 2, 10, 0, null] => [1, 2, 10] * * @param {} array */ var compact = function compact(array) { return array.filter(function (value) { return !!value; }); }; /** * @partial: Call a function as a parameter with given arguments in it's place. * * @param {} func * @param {} arguments */ var partial = function partial(func, arg) { if (isFunction(func)) { return func.apply(undefined, arg); } throw new Error('Error: Please pass in a function.'); }; /** * Executes the provided callback function once for each element present * in the array until it finds one where the callback returns a falsy value. * * @param {*} func * @param {*} array */ var every = function every(func, array) { if (isFunction(func) && Array.isArray(array)) { return array.every(func); } throw new Error('Error: Please pass in a function.'); }; /** * Converts a given array element to a key value pair object. * * @param {*} array */ var object = function object(array) { if (Array.isArray(array)) { return array.reduce(function (acc, cur, i) { acc[i] = cur; return acc; }, {}); } throw new Error('Error: Please pass in an array.'); }; /** * Map and return a `new` array set. * * @param {*} array * @param {*} iteratee */ var map = function map(array, func) { var newArrayInstance = void 0; if (Array.isArray(array) && isFunction(func)) { newArrayInstance = []; array.forEach(function (el) { newArrayInstance.push(func.call(undefined, el)); }); return newArrayInstance; } throw new Error('Error: Please pass in valid arguments'); }; var mainExport = { isFunction: isFunction, flatten: flatten, filter: filter, compact: compact, partial: partial, every: every, object: object, map: map }; // export default mainExport; // need to fix module.exports = mainExport; // for commonJS compatibility