UNPKG

pig-dam-core

Version:

Library that should be included in every Pig DAM project we build

119 lines (118 loc) 3.44 kB
"use strict"; /** * Date: 2019-07-09 * Time: 21:45 * @license MIT (see project's LICENSE file) */ Object.defineProperty(exports, "__esModule", { value: true }); exports.sort = exports.replace = exports.remove = exports.pick = exports.omit = exports.concat = exports.add = void 0; const _ = require("lodash"); const compare_1 = require("../../compare"); const types_1 = require("../../types"); const utils_1 = require("../utils"); /** * Adds element. * @param array - may be null provided that this is an "dateAdd" operation * @param element - element to be inserted * @param location - optional location information * @throws {Error} */ function add(array, element, location) { if (!array) { array = []; } if (location) { const index = utils_1.findInsertLocation(array, location); array.splice(index, 0, element); } else { array.push(element); } return array; } exports.add = add; /** * Concatenates <param>array</param> and <param>elements</param> * @param array - may be null provided that there is no <param>index</param> * @param elements - elements be added or inserted * @param location - optional location information * @throws {Error} */ function concat(array, elements, location) { if (!array) { array = []; } if (location === undefined) { _.each(elements, function (element) { array.push(element); }); } else { const index = utils_1.findInsertLocation(array, location); _.each(elements, function (element, offset) { return array.splice(index + offset, 0, element); }); } return array; } exports.concat = concat; /** * Performs omit on each element in the specified array */ function omit(array, path) { _.forEach(array, (object, index) => { array[index] = _.omit(object, path); }); return array; } exports.omit = omit; /** * Performs pick on each element in the specified array */ function pick(array, path) { _.forEach(array, (object, index) => { array[index] = _.pick(object, path); }); return array; } exports.pick = pick; /** * @param array - array from which to remove element * @param criteria - criteria by which we find an index * @param onFail - whether to throw or not throw errors if not found */ function remove(array, criteria, onFail = types_1.FailurePolicy.Throw) { const index = utils_1.searchCriteriaToIndex(array, criteria, onFail); if (index > -1) { array.splice(index, 1); } return array; } exports.remove = remove; /** * @param array - array from which to remove element * @param newElement - element to replace found searched for element * @param criteria - criteria by which we find an index * @throws {Error} if existing element cannot be found */ function replace(array, newElement, criteria) { const index = utils_1.searchCriteriaToIndex(array, criteria, types_1.FailurePolicy.Throw); if (index > -1) { array[index] = newElement; } return array; } exports.replace = replace; /** * sorts array of objects by property key */ function sort(array, property, { comparer = compare_1.compareAny, reverse = false } = {}) { if (array) { array.sort((o1, o2) => comparer(_.get(o1, property), _.get(o2, property))); if (reverse) { array.reverse(); } } return array; } exports.sort = sort;