array-helper-functions
Version:
Lightweight Array helper methods
233 lines (207 loc) • 6.47 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["arrayHelper"] = factory();
else
root["arrayHelper"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"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
/***/ })
/******/ ]);
});
//# sourceMappingURL=index.umd.js.map