Skip to content

omitPaths ​

Category
Export size
min 1.62 kB · gzip 0.89 kB
See also

Returns a copy of object without the given paths. Paths may be nested ('user.email'), in which case the objects on the way are copied so the source keeps its key; untouched branches stay shared by reference. Arrays and class instances on the path are copied too — nothing here writes into what it was handed.

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 { 
omitPaths
} from 'feathers-utils/helpers';

Example ​

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

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

Type declaration ​

Show Type Declarations
ts
/**
 * Returns a copy of `object` without the given paths. Paths may be nested
 * (`'user.email'`), in which case the objects on the way are copied so the
 * source keeps its key; untouched branches stay shared by reference. Arrays and
 * class instances on the path are copied too — nothing here writes into what it
 * was handed.
 *
 * 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
 *
 *
 * omitPaths({ id: 1, user: { email: '[email protected]', name: 'a' } }, ['user.email'])
 * // => { id: 1, user: { name: 'a' } }
 * ```
 *
 * @see https://utils.feathersjs.com/helpers/omit-paths.html
 */
export declare const omitPaths: <T extends Record<PropertyKey, any>>(
  object: T | null | undefined,
  paths: readonly PropertyPath[],
) => Partial<T>
ArgumentTypeDescription
objectT | null | undefined
pathsreadonly PropertyPath[]

Coming from lodash ​

Given a nested path, _.omit deep-clones its whole input before deleting; omitPaths copies only the objects on the omitted paths. The result is the same, but untouched branches stay the same reference — cheaper, and safe for values compared by identity (params.connection, an ObjectId). Unlike _.omit, inherited enumerable properties are not copied into the result.

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

Released under the MIT License.