Skip to content

pickPaths ​

Category
Export size
min 1.74 kB · gzip 0.93 kB
See also

Returns an object holding only the given paths of object. Paths may be nested ('user.email'), in which case the result is nested the same way. Paths that do not resolve are skipped; the source is never written to.

Each entry of paths is one path, and a segment array is one nested path — the same reading as in getPath/hasPath/setPath.

ts
  import { 
pickPaths
} from 'feathers-utils/helpers';

Example ​

ts
import { pickPaths } from 'feathers-utils/helpers'

pickPaths({ id: 1, user: { email: '[email protected]', name: 'a' } }, ['id', 'user.email'])
// => { id: 1, user: { email: '[email protected]' } }

Type declaration ​

Show Type Declarations
ts
/**
 * Returns an object holding only the given paths of `object`. Paths may be
 * nested (`'user.email'`), in which case the result is nested the same way.
 * Paths that do not resolve are skipped; the source is never written to.
 *
 * Each entry of `paths` is one path, and a segment array is one nested path —
 * the same reading as in `getPath`/`hasPath`/`setPath`.
 *
 * @example
 * ```ts
 *
 *
 * pickPaths({ id: 1, user: { email: '[email protected]', name: 'a' } }, ['id', 'user.email'])
 * // => { id: 1, user: { email: '[email protected]' } }
 * ```
 *
 * @see https://utils.feathersjs.com/helpers/pick-paths.html
 */
export declare const pickPaths: <T extends Record<PropertyKey, any>>(
  object: T | null | undefined,
  paths: readonly PropertyPath[],
) => Partial<T>
ArgumentTypeDescription
objectT | null | undefined
pathsreadonly PropertyPath[]

Coming from lodash ​

A drop-in for _.pick: a nested path builds a nested result, a path that does not resolve is skipped, and a key that exists but holds undefined is kept.

To pick fields of context.data or context.result inside a hook, prefer the pick transformer.

Released under the MIT License.