Type-safe shallow and deep pick/omit for JavaScript objects
npm install @philiprehberger/pick-omitType-safe shallow and deep pick/omit utilities for JavaScript objects
npm install @philiprehberger/pick-omit
import { pick, omit } from '@philiprehberger/pick-omit';
pick({ a: 1, b: 2, c: 3 }, 'a', 'c'); // { a: 1, c: 3 }
omit({ a: 1, b: 2, c: 3 }, 'b'); // { a: 1, c: 3 }
import { deepPick, deepOmit } from '@philiprehberger/pick-omit';
const user = { name: 'Alice', address: { city: 'NYC', zip: '10001' } };
deepPick(user, 'name', 'address.city'); // { name: 'Alice', address: { city: 'NYC' } }
deepOmit(user, 'address.zip'); // { name: 'Alice', address: { city: 'NYC' } }
import { pickBy, omitBy } from '@philiprehberger/pick-omit';
pickBy({ a: 1, b: null, c: 'ok' }, (v) => v != null); // { a: 1, c: 'ok' }
omitBy({ a: 1, b: null, c: 'ok' }, (v) => v == null); // { a: 1, c: 'ok' }
typeofimport { pickByType } from '@philiprehberger/pick-omit';
const obj = { name: 'Alice', age: 30, active: true };
pickByType(obj, 'string'); // { name: 'Alice' }
pickByType(obj, 'number'); // { age: 30 }
pickByType(obj, 'boolean'); // { active: true }
import { flatten, unflatten } from '@philiprehberger/pick-omit';
const nested = { user: { name: 'Alice', tags: ['admin', 'staff'] } };
const flat = flatten(nested);
// { 'user.name': 'Alice', 'user.tags.0': 'admin', 'user.tags.1': 'staff' }
unflatten(flat);
// { user: { name: 'Alice', tags: ['admin', 'staff'] } }
flatten({ a: { b: 1 } }, '/'); // { 'a/b': 1 }
| Method | Description |
|---|---|
pick(obj, ...keys) | Returns a new object with only the specified keys |
omit(obj, ...keys) | Returns a new object without the specified keys |
deepPick(obj, ...paths) | Picks nested values using dot-notation paths |
deepOmit(obj, ...paths) | Removes nested values using dot-notation paths |
pickBy(obj, predicate) | Picks entries where the predicate returns true |
omitBy(obj, predicate) | Omits entries where the predicate returns true |
pickByType(obj, typeName) | Keeps properties whose typeof matches typeName |
flatten(obj, separator?) | Converts a nested object/array into a flat dot-notation map |
unflatten(obj, separator?) | Inverse of flatten; numeric segments become array indices |
npm install
npm run build
npm test
If you find this project useful: