@f/pick

1.1.4 • Public • Published

pick

Build status Git tag NPM version Code style

Return partial copy of object containing specified subset of keys.

Installation

$ npm install @f/pick

Usage

var pick = require('@f/pick')

var source = {a: 1, b: 2, c: 3, d: function () {}}
var src1 = {a: 2}
var src2 = {a: 1, b: 2}

pick(['a', 'c'], source)     // => {a: 1, c: 3}
pick('a', source)            // => {a: 1}
pick(isFunction, source)     // => {d: function () {}}
pick(['a', 'b'], src1, src2) // => {a: 2, b: 2}

API

pick(keys, ...objs)

  • keys - Key(s) to pick, or predicate function that receives (val, key).
  • ...objs - Source objects to pick from, the object in the leftmost position has the highest priority. Multiple objects do not work with predicate functions.

Returns: partial copy of obj

Progressive picking

This pick implementation has a somewhat unique feature - it can pick progressively from objects until the key is found. If you pass multiple objects it will, starting from the left hand side, use the first value in the list that is not undefined. E.g.

var src1 = {a: 2}
var src2 = {a: 1, b: 2}

pick(['a', 'b'], src1, src2) // => {a: 2, b: 2}

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @f/pick

Weekly Downloads

10

Version

1.1.4

License

MIT

Last publish

Collaborators

  • f