@benderthecrime/pick
v1.0.0
Published
Returns a partial copy of an object containing only the keys specified.
Downloads
3
Maintainers
Readme
@benderthecrime/pick
Returns a partial copy of an object containing only the keys specified.
⚠️NOTE: This package is great for my limited use cases and there are probably better options for your production apps.
Installation
npm i @benderthecrime/pick
Usage
Require the module in your Node.js service:
const pick = require('@benderthecrime/pick')
const {pick, pickAll} = require('@benderthecrime/pick')
Use pick
when you want to skip missing keys in the returned Object:
const pick = require('@benderthecrime/pick')
const user = {firstName: 'Jane', lastName: 'Doe', password: 'supersecret7'}
const responseFields = pick(['email', 'firstName', 'lastName'], user) // {firstName: 'Jane', lastName: 'Doe'}
Alternatively, pick
can return a partially applied function:
const app = require('express')()
const pick = require('@benderthecrime/pick')
const pickUserResponseFields = pick(['firstName', 'lastName'])
app.get('/user/self', (_, res) => {
const user = {firstName: 'Jane', lastName: 'Doe', password: 'supersecret7'}
res.send(pickUserResponseFields(user))
})
Use pickAll
when you want to include missing keys in the returned Object:
const {pickAll} = require('@benderthecrime/pick')
const user = {firstName: 'Jane', lastName: 'Doe', password: 'supersecret7'}
const responseFields = pickAll(['email', 'firstName', 'lastName'], user) // {email: undefined, firstName: 'Jane', lastName: 'Doe'}
Similarly to pick
, pickAll
can also return a partial:
const app = require('express')()
const {pickAll} = require('@benderthecrime/pick')
const pickUserResponseFields = pickAll(['firstName', 'lastName'])
app.get('/user/self', (_, res) => {
const user = {firstName: 'Jane', lastName: 'Doe', password: 'supersecret7'}
res.send(pickUserResponseFields(user))
})