pig-dam-core
Version:
Library that should be included in every Pig DAM project we build
106 lines (105 loc) • 3.33 kB
JavaScript
;
/**
* 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 (location === undefined) {
return (array || []).concat(element);
}
else {
const index = utils_1.findInsertLocation(array, location);
return array.slice(0, index)
.concat(element)
.concat(array.slice(index));
}
}
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 (location === undefined) {
return (array || []).concat(elements);
}
else {
const index = utils_1.findInsertLocation(array, location);
return array.slice(0, index)
.concat(elements)
.concat(array.slice(index));
}
}
exports.concat = concat;
/**
* Performs omit on each element in the specified array
*/
function omit(array, path) {
return (array || []).map(_.partialRight(_.omit, path));
}
exports.omit = omit;
/**
* Performs pick on each element in the specified array
*/
function pick(array, path) {
return (array || []).map(_.partialRight(_.pick, path));
}
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);
return (index > -1)
? array.slice(0, index).concat(array.slice(index + 1))
: 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 = array.slice();
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 = array.slice();
array.sort((o1, o2) => comparer(_.get(o1, property), _.get(o2, property)));
if (reverse) {
array.reverse();
}
}
return array;
}
exports.sort = sort;