UNPKG

obj-walker

Version:

Walk or map over objects in a depth-first preorder or postorder manner.

101 lines (100 loc) 3.85 kB
import { Compact, Exclude, FindNode, Flatten, Map, Truncate, Unflatten, Walk, WalkEach, WalkEachAsync, Walker } from './types'; export declare const SHORT_CIRCUIT: unique symbol; /** * Walk an object depth-first in a preorder (default) or postorder manner. * Call walkFn for each node visited. Supports traversing the object in * arbitrary ways by passing a traverse fn in options. Short circuit traversal * by returning the exported symbol `SHORT_CIRCUIT`. * * Note: this is a low-level function and probably isn't what you want. */ export declare const walker: Walker; /** * Map over an object modifying values with a fn depth-first in a * preorder or postorder manner. The output of the mapper fn * will be traversed if possible when traversing preorder. * * By default, nodes will be excluded by returning `undefined`. * Undefined array values will not be excluded. To customize * pass a fn for `options.shouldSkip`. */ export declare const map: Map; /** * Walk an object depth-first in a preorder (default) or * postorder manner. Returns an array of nodes. */ export declare const walk: Walk; /** * Walk over an object calling `walkFn` for each node. The original * object is deep-cloned by default making it possible to simply mutate each * node as needed in order to transform the object. The cloned object * is returned if `options.modifyInPlace` is not set to true. */ export declare const walkEach: WalkEach; /** * @deprecated Use walkEach */ export declare const walkie: WalkEach; /** * Like `walkEach` but awaits the promise returned by `walkFn` before proceeding to * the next node. */ export declare const walkEachAsync: WalkEachAsync; /** * @deprecated Use walkEachAsync */ export declare const walkieAsync: WalkEachAsync; /** * Map over the leaves of an object with a fn. By default, nodes will be excluded * by returning `undefined`. Undefined array values will not be excluded. To customize * pass a fn for `options.shouldSkip`. */ export declare const mapLeaves: Map; /** * Search for a node and short-circuit the traversal if it's found. */ export declare const findNode: FindNode; /** * Flatten an object's keys. Optionally pass `separator` to determine * what character to join keys with. Defaults to '.'. If an array is * passed, an object of path to values is returned unless the `objectsOnly` * option is set. */ export declare const flatten: Flatten; /** * Unflatten an object previously flattened. Optionally pass `separator` * to determine what character or RegExp to split keys with. * Defaults to '.'. */ export declare const unflatten: Unflatten; /** * Compact an object, removing fields recursively according to the supplied options. * All option flags are `false` by default. If `compactArrays` is set to `true`, arrays * will be compacted based on the enabled 'remove' option flags. */ export declare const compact: Compact; /** * Truncate allows you to limit the depth of nested objects/arrays, * the length of strings, and the length of arrays. Instances of Error * can be converted to plain objects so that the enabled truncation options * also apply to the error fields. All truncation methods are opt-in. * * Note: For the best performance you should consider setting `modifyInPlace` * to `true`. * * Inspiration: https://github.com/runk/dtrim */ export declare const truncate: Truncate; /** * Estimate the size in bytes. */ export declare const size: (val: any) => number; /** * Exclude paths from an object. Supports star patterns where '*' matches * any single field name. For example, 'documents.*.fileName' will match * 'documents.0.fileName', 'documents.1.fileName', etc. * * Also supports prefix matching: excluding 'documents' will exclude * 'documents.fileName', 'documents.0.fileName', etc. */ export declare const exclude: Exclude;