2.0.3 • Published 1 year ago

prop-pick v2.0.3

Weekly downloads
70
License
ISC
Repository
github
Last release
1 year ago

PropPick

Easily filter properties from objects or Map instances.

npm npm bundle size


Filtering from an object:

import pick from "prop-pick";

const obj = {
  a: 1,
  b: 2,
  c: 3,
};

pick("a c", obj); //=> { a: 1 , c: 3 }

Filtering from nested objects:

import pick from "prop-pick";

const tournament = {
  result: {
    winner: "Anna",
    time: "01:27:32",
  },
  date: "2020-01-03",
  "played at": "Manhattan",
};

// wrap the key in quotes if it contains spaces or special characters
pick(
  `
    result: {
        time
    },
    "played at"
  `,
  tournament
); //=> { result: { time: '01:27:32' }, "played at": 'Manhattan' }

Filtering from a Map instance:

import pick from "prop-pick";

const map = new Map();
map.set("a", 1);
map.set("b", 2);
map.set("c", 3);

pick("a c", map); //=> { a: 1 , c: 3 }

Returning a Map instance:

import pick from "prop-pick";

const obj = {
  a: 1,
  b: 2,
  c: 3,
};

pick("a c", obj, { map: true }); //=> Map(2) { 'a' => 1, 'c' => 3 }

Returning a flat result:

import pick from "prop-pick";

const tournament = {
  result: {
    winner: "Anna",
    time: "01:27:32",
  },
  date: "2020-01-03",
  "played at": "Manhattan",
};

// wrap the key in quotes if it contains spaces or special characters
pick(
  `
    result: {
      winner
    },
    'played at'
  `,
  tournament,
  { flat: true }
); //=> { winner: 'Anna', 'played at': 'Manhattan' }

Renaming the keys:

Use the greater than (>) operator to rename a key.

import pick from "prop-pick";

const person = {
  name: "John",
  age: 33,
  job: "Designer",
  city: "New York",
};

// renaming 'name' to 'firstname' and 'job' to 'occupation'
pick(
  `
    name > firstname,
    job > occupation,
    age
  `,
  person
); //=> { firstname: 'John', occupation: 'Designer', age: 33 }

Setting the return type:

Since return type cannot be inferred, use Generics to set it yourself (only Object and Map types are allowed).

import pick from "prop-pick";

const obj = {
  a: 1,
  b: 2,
  c: 3,
};

pick<{ b: 2 }>("b", obj); //=> { b: 2 }
2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.3.3

3 years ago

1.3.2

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago