UNPKG

pig-dam-core

Version:

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

90 lines (89 loc) 3.04 kB
"use strict"; /** * Date: 2019-07-09 * Time: 21:45 * @license MIT (see project's LICENSE file) */ Object.defineProperty(exports, "__esModule", { value: true }); exports.scrub = exports.ensure = exports.deletePath = void 0; const _ = require("lodash"); /** * Deletes the object at the property path in `object` */ function deletePath(object, path) { function _delete(_object, property) { let index; if (_.isArray(_object) && !_.isNaN(index = Number(property))) { _object.splice(index, 1); } else { delete _object[property]; } } const index = _.lastIndexOf(path, "."); if (index > -1) { const property = path.substr(index + 1); const parent = _.get(object, path.substr(0, index), {}); _delete(parent, property); } else { _delete(object, path); } return object; } exports.deletePath = deletePath; /** * A variation of lodash's set but only sets if the value is not set: * - if object is not set then it defaults to {} * - it returns the value at "path" * @returns same object passed as <param>object</object> */ function ensure(object, path, value) { if (!_.has(object, path)) { _.set(object, path, value); } return object; } exports.ensure = ensure; /** * Removes properties of objects with <param>removables</param>values. It does not remove <param>removables</param> from arrays * but it does recursively process array elements and should they be objects then it will scrub those objects. * Note: must be careful to make sure there are no recursive references inside your object. * @param object will only process object passing isObject test * @param recursive whether to recurse into children properties * @param removables object or array of objects that qualify as or test for `remove` * @returns same object passed as <param>object</object> */ function scrub(object, { recursive = true, removables = [undefined] } = {}) { if (!_.isArray(removables)) { removables = [removables]; } const removableTests = removables.map((item) => (typeof item === "function") ? item : (value) => _.isEqual(value, item)); function _scrub(object) { if (_.isArray(object)) { for (let index = object.length - 1; index > -1; index--) { if (recursive) { object[index] = _scrub(object[index]); } } } else if (typeof (object) === "object") { _.forEach(object, function (value, key, parent) { if (recursive) { _scrub(value); } for (let index = removables.length - 1; index > -1; index--) { if (removableTests[index](value, key)) { delete parent[key]; break; } } }); } return object; } return _scrub(object); } exports.scrub = scrub;