object-pickby
Version:
Creates an object composed of the picked object properties. Written in Typescript for ES2022+ environments.
50 lines (35 loc) • 1.25 kB
Markdown
[](https://codecov.io/gh/tinovyatkin/object-pick)
Tiny TypeScript alternative to `only`, `lodash.pick`, and `lodash.pickBy`.
Built for ES2019+ environments.
```sh
npm install object-pickby
```
`pick` works with plain objects, arrays (including negative indices), and symbol
keys. `pickBy` supports both objects and arrays; the predicate receives the
accumulator as the third argument.
```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(
[],
(val, idx, acc) => typeof val === "string" && idx < 4 && !acc.includes(val),
),
).toEqual(["slon"]);
});
```
This package targets Node.js >= 12.4 / ES2019+ environments.
MIT