@stackbit/utils
Advanced tools
Comparing version 0.2.16 to 0.2.17-alpha.0
@@ -86,56 +86,2 @@ import { PropertyPath } from 'lodash'; | ||
/** | ||
* Deeply maps the passed `value` by recursively calling the `iteratee` with the | ||
* original `value` and then its properties if the `value` is an object or its | ||
* elements if the `value` is an array. | ||
* | ||
* The return value of every `iteratee` call will replace the original node. | ||
* | ||
* The object tree is traversed in pre-order depth-first-search algorithm. | ||
* Meaning, the `iteratee` is first called on the parent nodes and only then on | ||
* their children. Therefore if iteratee maps/replaces the parent node, then the | ||
* children of the replaced node will be traversed. | ||
* | ||
* The iteratee is invoked with three arguments - the `value` being iterated, | ||
* the `keyPath` of the current `value` relative to the original passed value, | ||
* and the `stack` of ancestors objects of the current `value`. | ||
* | ||
* The first `iterate` call will receive the original `value` and empty arrays | ||
* for `keyPath` and `stack`. | ||
* | ||
* In other words, the `value` passed to every `iteratee` call (except the first | ||
* call, and assuming objects properties and array indexes not mapped) will be | ||
* equal to: `_.get(originalValue, keyPath)` | ||
* | ||
* @example | ||
* mapDeep({ prop: 'foo', arr: [ 'bar' , 1, 2 ] }, (value) => { | ||
* if (_.isString(value)) return '__' + value; | ||
* if (_.isNumber(value)) return value * 10; | ||
* return value; | ||
* }) | ||
* => { prop: '__foo', arr: [ '__bar', 10, 20 ] } | ||
* | ||
* mapDeep({ prop: 'foo', arr: [ 'bar' ] }, (value, keyPath) => { | ||
* if ((_.isString(value)) return value + '__' + keyPath.join('.'); | ||
* return value; | ||
* }) | ||
* => { prop: 'foo__prop', arr: [ 'bar__arr.0' ] } | ||
* | ||
* @param {*} value A value to map | ||
* @param {Function} iteratee Function (value: any, keyPath: Array, stack: Array) | ||
* @param {object} [options] | ||
* @param {boolean} [options.context] The context (`this`) that will be used to invoke the `iteratee` | ||
* @param {boolean} [options.iterateCollections] Call `iteratee` for objects and arrays. Default: true | ||
* @param {boolean} [options.iteratePrimitives] Call `iteratee` for primitives. Default: true | ||
* @param {boolean} [options.postOrder] Change the invocation of iteratee from pre-order to post-order depth-first-search. Default: false | ||
* @returns {*} | ||
*/ | ||
export declare function mapDeep(value: any, iteratee: (value: any, keyPath: KeyPath, stack: any[]) => any, options?: { | ||
postOrder?: boolean; | ||
iterateCollections?: boolean; | ||
iteratePrimitives?: boolean; | ||
iterateScalars?: boolean; | ||
context?: any; | ||
}): any; | ||
/** | ||
* Newer more performant version of `mapDeep`. Use this version if you don't need the value stack. | ||
* Creates an object with the same keys as `object` and values generated by | ||
@@ -142,0 +88,0 @@ * recursively running each own enumerable string keyed property of `object` through |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.asyncMapDeep = exports.deepMap = exports.mapDeep = exports.omitByNil = exports.rename = exports.copyIfNotSet = exports.copy = exports.concat = exports.prepend = exports.append = exports.getFirst = void 0; | ||
exports.asyncMapDeep = exports.deepMap = exports.omitByNil = exports.rename = exports.copyIfNotSet = exports.copy = exports.concat = exports.prepend = exports.append = exports.getFirst = void 0; | ||
const lodash_1 = __importDefault(require("lodash")); | ||
@@ -144,75 +144,2 @@ /** | ||
exports.omitByNil = omitByNil; | ||
/** | ||
* Deeply maps the passed `value` by recursively calling the `iteratee` with the | ||
* original `value` and then its properties if the `value` is an object or its | ||
* elements if the `value` is an array. | ||
* | ||
* The return value of every `iteratee` call will replace the original node. | ||
* | ||
* The object tree is traversed in pre-order depth-first-search algorithm. | ||
* Meaning, the `iteratee` is first called on the parent nodes and only then on | ||
* their children. Therefore if iteratee maps/replaces the parent node, then the | ||
* children of the replaced node will be traversed. | ||
* | ||
* The iteratee is invoked with three arguments - the `value` being iterated, | ||
* the `keyPath` of the current `value` relative to the original passed value, | ||
* and the `stack` of ancestors objects of the current `value`. | ||
* | ||
* The first `iterate` call will receive the original `value` and empty arrays | ||
* for `keyPath` and `stack`. | ||
* | ||
* In other words, the `value` passed to every `iteratee` call (except the first | ||
* call, and assuming objects properties and array indexes not mapped) will be | ||
* equal to: `_.get(originalValue, keyPath)` | ||
* | ||
* @example | ||
* mapDeep({ prop: 'foo', arr: [ 'bar' , 1, 2 ] }, (value) => { | ||
* if (_.isString(value)) return '__' + value; | ||
* if (_.isNumber(value)) return value * 10; | ||
* return value; | ||
* }) | ||
* => { prop: '__foo', arr: [ '__bar', 10, 20 ] } | ||
* | ||
* mapDeep({ prop: 'foo', arr: [ 'bar' ] }, (value, keyPath) => { | ||
* if ((_.isString(value)) return value + '__' + keyPath.join('.'); | ||
* return value; | ||
* }) | ||
* => { prop: 'foo__prop', arr: [ 'bar__arr.0' ] } | ||
* | ||
* @param {*} value A value to map | ||
* @param {Function} iteratee Function (value: any, keyPath: Array, stack: Array) | ||
* @param {object} [options] | ||
* @param {boolean} [options.context] The context (`this`) that will be used to invoke the `iteratee` | ||
* @param {boolean} [options.iterateCollections] Call `iteratee` for objects and arrays. Default: true | ||
* @param {boolean} [options.iteratePrimitives] Call `iteratee` for primitives. Default: true | ||
* @param {boolean} [options.postOrder] Change the invocation of iteratee from pre-order to post-order depth-first-search. Default: false | ||
* @returns {*} | ||
*/ | ||
function mapDeep(value, iteratee, options = {}) { | ||
const context = lodash_1.default.get(options, 'context'); | ||
const iterateCollections = lodash_1.default.get(options, 'iterateCollections', true); | ||
const iteratePrimitives = lodash_1.default.get(options, 'iteratePrimitives', lodash_1.default.get(options, 'iterateScalars', true)); | ||
const postOrder = lodash_1.default.get(options, 'postOrder', false); | ||
function _mapDeep(value, keyPath, stack) { | ||
const invokeIteratee = lodash_1.default.isPlainObject(value) || lodash_1.default.isArray(value) ? iterateCollections : iteratePrimitives; | ||
if (invokeIteratee && !postOrder) { | ||
value = iteratee.call(context, value, keyPath, stack); | ||
} | ||
const childrenIterator = (val, key) => { | ||
return _mapDeep(val, lodash_1.default.concat(keyPath, key), lodash_1.default.concat(stack, [value])); | ||
}; | ||
if (lodash_1.default.isPlainObject(value)) { | ||
value = lodash_1.default.mapValues(value, childrenIterator); | ||
} | ||
else if (lodash_1.default.isArray(value)) { | ||
value = lodash_1.default.map(value, childrenIterator); | ||
} | ||
if (invokeIteratee && postOrder) { | ||
value = iteratee.call(context, value, keyPath, stack); | ||
} | ||
return value; | ||
} | ||
return _mapDeep(value, [], []); | ||
} | ||
exports.mapDeep = mapDeep; | ||
function deepMap(object, iteratee, options) { | ||
@@ -219,0 +146,0 @@ const context = lodash_1.default.get(options, 'context'); |
{ | ||
"name": "@stackbit/utils", | ||
"version": "0.2.16", | ||
"version": "0.2.17-alpha.0", | ||
"description": "Stackbit utilities", | ||
@@ -46,3 +46,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "2774d9c8400f87aaa9ec49b2bd2b90fce215b00d" | ||
"gitHead": "90d13acd2074620c6784a66cb4a8de99df81bf0a" | ||
} |
@@ -141,81 +141,2 @@ import _, { PropertyPath } from 'lodash'; | ||
/** | ||
* Deeply maps the passed `value` by recursively calling the `iteratee` with the | ||
* original `value` and then its properties if the `value` is an object or its | ||
* elements if the `value` is an array. | ||
* | ||
* The return value of every `iteratee` call will replace the original node. | ||
* | ||
* The object tree is traversed in pre-order depth-first-search algorithm. | ||
* Meaning, the `iteratee` is first called on the parent nodes and only then on | ||
* their children. Therefore if iteratee maps/replaces the parent node, then the | ||
* children of the replaced node will be traversed. | ||
* | ||
* The iteratee is invoked with three arguments - the `value` being iterated, | ||
* the `keyPath` of the current `value` relative to the original passed value, | ||
* and the `stack` of ancestors objects of the current `value`. | ||
* | ||
* The first `iterate` call will receive the original `value` and empty arrays | ||
* for `keyPath` and `stack`. | ||
* | ||
* In other words, the `value` passed to every `iteratee` call (except the first | ||
* call, and assuming objects properties and array indexes not mapped) will be | ||
* equal to: `_.get(originalValue, keyPath)` | ||
* | ||
* @example | ||
* mapDeep({ prop: 'foo', arr: [ 'bar' , 1, 2 ] }, (value) => { | ||
* if (_.isString(value)) return '__' + value; | ||
* if (_.isNumber(value)) return value * 10; | ||
* return value; | ||
* }) | ||
* => { prop: '__foo', arr: [ '__bar', 10, 20 ] } | ||
* | ||
* mapDeep({ prop: 'foo', arr: [ 'bar' ] }, (value, keyPath) => { | ||
* if ((_.isString(value)) return value + '__' + keyPath.join('.'); | ||
* return value; | ||
* }) | ||
* => { prop: 'foo__prop', arr: [ 'bar__arr.0' ] } | ||
* | ||
* @param {*} value A value to map | ||
* @param {Function} iteratee Function (value: any, keyPath: Array, stack: Array) | ||
* @param {object} [options] | ||
* @param {boolean} [options.context] The context (`this`) that will be used to invoke the `iteratee` | ||
* @param {boolean} [options.iterateCollections] Call `iteratee` for objects and arrays. Default: true | ||
* @param {boolean} [options.iteratePrimitives] Call `iteratee` for primitives. Default: true | ||
* @param {boolean} [options.postOrder] Change the invocation of iteratee from pre-order to post-order depth-first-search. Default: false | ||
* @returns {*} | ||
*/ | ||
export function mapDeep( | ||
value: any, | ||
iteratee: (value: any, keyPath: KeyPath, stack: any[]) => any, | ||
options: { postOrder?: boolean; iterateCollections?: boolean; iteratePrimitives?: boolean; iterateScalars?: boolean; context?: any } = {} | ||
) { | ||
const context = _.get(options, 'context'); | ||
const iterateCollections = _.get(options, 'iterateCollections', true); | ||
const iteratePrimitives = _.get(options, 'iteratePrimitives', _.get(options, 'iterateScalars', true)); | ||
const postOrder = _.get(options, 'postOrder', false); | ||
function _mapDeep(value: any, keyPath: KeyPath, stack: any[]) { | ||
const invokeIteratee = _.isPlainObject(value) || _.isArray(value) ? iterateCollections : iteratePrimitives; | ||
if (invokeIteratee && !postOrder) { | ||
value = iteratee.call(context, value, keyPath, stack); | ||
} | ||
const childrenIterator = (val: any, key: KeyPath) => { | ||
return _mapDeep(val, _.concat(keyPath, key), _.concat(stack, [value])); | ||
}; | ||
if (_.isPlainObject(value)) { | ||
value = _.mapValues(value, childrenIterator); | ||
} else if (_.isArray(value)) { | ||
value = _.map(value, childrenIterator); | ||
} | ||
if (invokeIteratee && postOrder) { | ||
value = iteratee.call(context, value, keyPath, stack); | ||
} | ||
return value; | ||
} | ||
return _mapDeep(value, [], []); | ||
} | ||
/** | ||
* Newer more performant version of `mapDeep`. Use this version if you don't need the value stack. | ||
* Creates an object with the same keys as `object` and values generated by | ||
@@ -222,0 +143,0 @@ * recursively running each own enumerable string keyed property of `object` through |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
157882
2841