UNPKG

object-pickby

Version:

Creates an object composed of the picked object properties. Written in Typescript for ES2019+ environments.

40 lines (31 loc) 1.3 kB
# object-pickby [![codecov](https://codecov.io/gh/tinovyatkin/object-pick/branch/master/graph/badge.svg)](https://codecov.io/gh/tinovyatkin/object-pick) It's similar to `only` or `lodash.pick` and `lodash.pickBy`, but written for today's world with TypeScript targeting `Object.fromEntries` supporting environments: - https://node.green/#ES2019-features--Object-fromEntries (Node.js >= 12.4) - https://caniuse.com/#search=fromEntries - https://polyfill.io/v3/polyfill.min.js?features=Object.getOwnPropertyDescriptors,Object.fromEntries - core-js polyfilled with Babel Contrary of all above it also works with arrays and Symbols: ```ts import { pick, pickBy } from 'object-pickby'; it('works with symbols', () => { expect( pick({ boo: 'bar', foo: 'eee', [Symbol.for('eee')]: 'aaaa' }, [ 'boo', Symbol.for('eee'), ]), ).toEqual({ boo: 'bar', [Symbol.for('eee')]: 'aaaa' }); }); it('works with array and negative indexes', () => { expect(pick([1, 2, 3, 4], [1, -2])).toEqual([2, 3]); }); it('picks items by function, providing accumulated array to predicate', () => { expect( pickBy( [1, 'slon', 2, 'slon', 3, 'foo'], (val, idx, acc) => typeof val === 'string' && idx < 4 && !acc.includes(val), ), ).toEqual(['slon']); }); ``` # License: MIT