js-nested-pick
TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published

js-nested-pick

Creates an object composed of the picked object properties from a nested object using a dot path, select specific fields based conditions with filter arrays with MongoDB queries

Can be used in js/ts projects in node/browser.

Support select number of fields from nested object with dot and object with query:

How to use:

const pick = require('js-nested-pick');

const data = {
    name: 'Dan',
    age: 30,
}

const values = pick(data, ['name']) // {name: 'Dan'}
  • Simple select
    ['name', 'age']
    Select name and age from object

  • Deep select with dot
    ['user.firstname']
    Select user.name from inner object

  • Ommit fileds
    ['-password']
    Select all fields except the password

  • Deep ommit fileds with dot
    ['-user.address']
    Select all fields except the user address

  • Deep select/ ommit using object with path
    ['*', {path: 'user', select: ['-paasword']}]
    Select all fields, but in the user object, remove the password field

  • Select field in root object base of query on the data - using sift (read sift docs to see the options)
    [{when: {isPublic: true}, then: ['*'], otherwise: ['title']}]
    When the data.isPublic equal to true then in the data.posts select all fields, otherwise select only the title (post can be object or array)

  • Select field in inner object or array base of query on the data - using sift (read sift docs to see the options)
    [{path: 'post', when: {isPublic: true}, then: ['*'], otherwise: ['title']}]
    When the data.isPublic equal to true then in the data.posts select all fields, otherwise select only the title (post can be object or array)

  • Select field in inner object or array base of query on the item data - using sift (read sift docs to see the options)
    [{path: 'posts', where: {isPublic: true}, then: ['*'], otherwise: ['title']}]
    When the post.isPublic equal to true then select all post fields, otherwise select only the title (post can be object or array)

In-browser Playgrounds

Edit frosty-bash-sgj4j

Dependencies:

  1. lodash/get
  2. lodash/set
  3. lodash/omit
  4. sift

Test with jest enter image description here

Package Sidebar

Install

npm i js-nested-pick

Weekly Downloads

8

Version

1.1.3

License

MIT

Unpacked Size

26.3 kB

Total Files

12

Last publish

Collaborators

  • doronn