Skip to content
This repository was archived by the owner on Aug 31, 2025. It is now read-only.

prazdevs/deep-pick-omit

Repository files navigation

Important

This project has a new home at codeberg.org/praz/deep-pick-omit. Everything (issues included) has been migrated and development/maintenance will continue over there.

deep-pick-omit

npm version bundlephobia license

Deep-pick and deep-omit objects with typesafe paths.

Basic usage

Both deepPick and deepOmit take an object and an array of dot-notation paths to respectively pick and omit from the object. By default, TypeScript will infer types for paths and error if a path does not exist in the object.

deepPick

import { deepPick } from 'deep-pick-omit'

const obj = {
  a: {
    b: 'this',
    c: 'not this'
  },
  d: 'this'
}

deepPick(obj, ['a.b', 'e'])
// -> { a: { b: 'this' }, d: 'this' }

deepPick(obj, ['f'])
// -> TypeScript Error: `f` is not a key of `obj`

deepOmit

import { deepOmit } from 'deep-pick-omit'

const obj = {
  a: {
    b: 'this',
    c: 'not this'
  },
  d: 'this'
}

deepOmit(obj, ['a.c'])
// -> { a: { b: 'this' }, d: 'this' }

deepOmit(obj, ['f'])
// -> TypeScript Error: `f` is not a key of `obj`

Note

Pathing through array values is not allowed typesafe path methods. See unsafe methods.

Unsafe methods

If paths type-safety is a problem for some edge cases, the package exposes the same methods without the type-checking on paths.

deepPickUnsafe

import { deepPickUnsafe } from 'deep-pick-omit'

const obj = {
  a: {
    c: 'not this'
  },
  d: 'this'
}

deepPickUnsafe(obj, ['d', 'f'])
// -> { d: 'this' }

deepOmitUnsafe

import { deepOmitUnsafe } from 'deep-pick-omit'

const obj = {
  a: {
    b: 'this',
    c: 'not this'
  },
  d: 'this'
}

deepOmitUnsafe(obj, ['a.c', 'f'])
// -> { a: { b: 'this' }, d: 'this' }

License

MIT — Made with 💖.

About

⛏️ Deep-pick and deep-omit objects with typesafe paths.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •