@stackbit/utils
Advanced tools
Comparing version 0.2.15 to 0.2.16-alpha.0
@@ -84,2 +84,3 @@ import { PropertyPath } from 'lodash'; | ||
export declare function omitByNil<T extends Record<string, any>>(object: T): T; | ||
export declare type KeyPath = (string | number)[]; | ||
/** | ||
@@ -131,3 +132,3 @@ * Deeply maps the passed `value` by recursively calling the `iteratee` with the | ||
*/ | ||
export declare function mapDeep(value: any, iteratee: (value: any, keyPath: (string | number)[], stack: any[]) => any, options?: { | ||
export declare function mapDeep(value: any, iteratee: (value: any, keyPath: KeyPath, stack: any[]) => any, options?: { | ||
postOrder?: boolean; | ||
@@ -139,2 +140,28 @@ iterateCollections?: boolean; | ||
}): 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 | ||
* recursively running each own enumerable string keyed property of `object` through | ||
* `iteratee`. | ||
* | ||
* @param {any} object | ||
* @param {Function} iteratee | ||
* @param [options] | ||
* @param {boolean} [options.context] The value of `this` provided for the call to `iteratee`. Default: undefined | ||
* @param {boolean} [options.iterateCollections] Should the `iteratee` be called for collections. Default: true | ||
* @param {boolean} [options.iteratePrimitives] Should the `iteratee` be called for primitives. Default: true | ||
* @param {boolean} [options.includeKeyPath] Should the `iteratee` be called with `keyPath` parameter. Default: true | ||
*/ | ||
export declare function deepMap<T>(object: T, iteratee: (value: any, object: T) => any, options: { | ||
context?: any; | ||
iterateCollections?: boolean; | ||
iteratePrimitives?: boolean; | ||
includeKeyPath: false; | ||
}): any; | ||
export declare function deepMap<T>(object: T, iteratee: (value: any, keyPath: KeyPath, mappedValueStack: any[], object: T) => any, options?: { | ||
context?: any; | ||
iterateCollections?: boolean; | ||
iteratePrimitives?: boolean; | ||
includeKeyPath?: true; | ||
}): any; | ||
export declare function asyncMapDeep(value: any, iteratee: (options: { | ||
@@ -141,0 +168,0 @@ value: any; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.asyncMapDeep = 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.mapDeep = exports.omitByNil = exports.rename = exports.copyIfNotSet = exports.copy = exports.concat = exports.prepend = exports.append = exports.getFirst = void 0; | ||
const lodash_1 = __importDefault(require("lodash")); | ||
@@ -217,2 +217,29 @@ /** | ||
exports.mapDeep = mapDeep; | ||
function deepMap(object, 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', true); | ||
const includeKeyPath = lodash_1.default.get(options, 'includeKeyPath', true); | ||
function _mapDeep(value, keyPath, mappedValueStack) { | ||
let invokeIteratee = lodash_1.default.isPlainObject(value) || lodash_1.default.isArray(value) ? iterateCollections : iteratePrimitives; | ||
if (invokeIteratee) { | ||
value = (options === null || options === void 0 ? void 0 : options.includeKeyPath) === false | ||
? iteratee.call(context, value, object) | ||
: iteratee.call(context, value, keyPath, mappedValueStack, object); | ||
} | ||
if (lodash_1.default.isPlainObject(value)) { | ||
value = lodash_1.default.mapValues(value, (val, key) => { | ||
return _mapDeep(val, includeKeyPath ? lodash_1.default.concat(keyPath, key) : null, includeKeyPath ? lodash_1.default.concat(mappedValueStack, value) : null); | ||
}); | ||
} | ||
else if (Array.isArray(value)) { | ||
value = lodash_1.default.map(value, (val, key) => { | ||
return _mapDeep(val, includeKeyPath ? lodash_1.default.concat(keyPath, key) : null, includeKeyPath ? lodash_1.default.concat(mappedValueStack, value) : null); | ||
}); | ||
} | ||
return value; | ||
} | ||
return _mapDeep(object, [], []); | ||
} | ||
exports.deepMap = deepMap; | ||
async function asyncMapDeep(value, iteratee, options = {}) { | ||
@@ -219,0 +246,0 @@ const context = lodash_1.default.get(options, 'context'); |
{ | ||
"name": "@stackbit/utils", | ||
"version": "0.2.15", | ||
"version": "0.2.16-alpha.0", | ||
"description": "Stackbit utilities", | ||
@@ -46,3 +46,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "0302cdffeda3e07c0b078f0f5d3599b74426b939" | ||
"gitHead": "cd6c3315988cf1143ab5f1dcabf97d961d0ef9be" | ||
} |
@@ -138,2 +138,4 @@ import _, { PropertyPath } from 'lodash'; | ||
export type KeyPath = (string | number)[]; | ||
/** | ||
@@ -187,3 +189,3 @@ * Deeply maps the passed `value` by recursively calling the `iteratee` with the | ||
value: any, | ||
iteratee: (value: any, keyPath: (string | number)[], stack: any[]) => any, | ||
iteratee: (value: any, keyPath: KeyPath, stack: any[]) => any, | ||
options: { postOrder?: boolean; iterateCollections?: boolean; iteratePrimitives?: boolean; iterateScalars?: boolean; context?: any } = {} | ||
@@ -196,3 +198,3 @@ ) { | ||
function _mapDeep(value: any, keyPath: (string | number)[], stack: any[]) { | ||
function _mapDeep(value: any, keyPath: KeyPath, stack: any[]) { | ||
const invokeIteratee = _.isPlainObject(value) || _.isArray(value) ? iterateCollections : iteratePrimitives; | ||
@@ -202,3 +204,3 @@ if (invokeIteratee && !postOrder) { | ||
} | ||
const childrenIterator = (val: any, key: string | number) => { | ||
const childrenIterator = (val: any, key: KeyPath) => { | ||
return _mapDeep(val, _.concat(keyPath, key), _.concat(stack, [value])); | ||
@@ -220,2 +222,46 @@ }; | ||
/** | ||
* 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 | ||
* recursively running each own enumerable string keyed property of `object` through | ||
* `iteratee`. | ||
* | ||
* @param {any} object | ||
* @param {Function} iteratee | ||
* @param [options] | ||
* @param {boolean} [options.context] The value of `this` provided for the call to `iteratee`. Default: undefined | ||
* @param {boolean} [options.iterateCollections] Should the `iteratee` be called for collections. Default: true | ||
* @param {boolean} [options.iteratePrimitives] Should the `iteratee` be called for primitives. Default: true | ||
* @param {boolean} [options.includeKeyPath] Should the `iteratee` be called with `keyPath` parameter. Default: true | ||
*/ | ||
export function deepMap<T>(object: T, iteratee: (value: any, object: T) => any, options: { context?: any; iterateCollections?: boolean; iteratePrimitives?: boolean; includeKeyPath: false }): any; | ||
export function deepMap<T>(object: T, iteratee: (value: any, keyPath: KeyPath, mappedValueStack: any[], object: T) => any, options?: { context?: any; iterateCollections?: boolean; iteratePrimitives?: boolean; includeKeyPath?: true }): any; | ||
export function deepMap(object: any, iteratee: (...args: any[]) => any, options?: { context?: any; iterateCollections?: boolean; iteratePrimitives?: boolean; includeKeyPath?: boolean }): any { | ||
const context = _.get(options, 'context'); | ||
const iterateCollections = _.get(options, 'iterateCollections', true); | ||
const iteratePrimitives = _.get(options, 'iteratePrimitives', true); | ||
const includeKeyPath = _.get(options, 'includeKeyPath', true); | ||
function _mapDeep(value: any, keyPath: KeyPath | null, mappedValueStack: any[] | null) { | ||
let invokeIteratee = _.isPlainObject(value) || _.isArray(value) ? iterateCollections : iteratePrimitives; | ||
if (invokeIteratee) { | ||
value = options?.includeKeyPath === false | ||
? iteratee.call(context, value, object) | ||
: iteratee.call(context, value, keyPath, mappedValueStack, object); | ||
} | ||
if (_.isPlainObject(value)) { | ||
value = _.mapValues(value, (val: any, key: string) => { | ||
return _mapDeep(val, includeKeyPath ? _.concat(keyPath!, key) : null, includeKeyPath ? _.concat(mappedValueStack!, value) : null); | ||
}); | ||
} else if (Array.isArray(value)) { | ||
value = _.map(value, (val: any, key: number) => { | ||
return _mapDeep(val, includeKeyPath ? _.concat(keyPath!, key) : null, includeKeyPath ? _.concat(mappedValueStack!, value) : null); | ||
}); | ||
} | ||
return value; | ||
} | ||
return _mapDeep(object, [], []); | ||
} | ||
export async function asyncMapDeep( | ||
@@ -222,0 +268,0 @@ value: any, |
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
169349
3044