Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

callback-pluck

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

callback-pluck

Use callback-pluck instead of `_.pluck`

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

callback-pluck

NPM version Build Status Coveralls Status Dependency Status DevDependency Status

Use callback-pluck instead of _.pluck

Callback pluck is a callback generator with clean API for ES5 iterations methods. It allows you to use _.pluck idea in more clean way via generating required callbacks for iteration methods (map, filter, every and some).

Deprecation note

Use es6 arrow functions instead, they are native and simpler solution.

arr.map(p('age'));    // [36, 40, 22, 22]
arr.map(i => i.age);  // [36, 40, 22, 22]

Install

npm install --save-dev callback-pluck

Usage

var p = require('callback-pluck');
var arr = [
  { 'name': 'Barney',  'age': 36, sex: 'male', married: true },
  { 'name': 'Fred',    'age': 40, sex: 'male', married: true },
  { 'name': 'Glenn',   'age': 22, sex: 'male', married: false },
  { 'name': 'Stephen', 'age': 22, sex: 'male', married: true }
];

Callback pluck with [ ].map

arr.map(p('name')); // ['Barney', 'Fred', 'Glenn', 'Stephen']
arr.map(p('age'));  // [36, 40, 22, 22]

Callback pluck with [ ].filter

Simple filtering with string parameter:

arr.filter(p('married')); // [{ 'name': 'Barney',  …, married: true },
                          //  { 'name': 'Fred',    ¬, married: true },
                          //  { 'name': 'Stephen', …, married: true }]

Simple filtering with string parameter and additional false parameter:

arr.filter(p('married', false)); // { 'name': 'Glenn', …, married: false },

Advanced filtering via property value:

arr.filter(p({ age: 22  })); // [{ 'name': 'Glenn',   'age': 22, … },
                             //  { 'name': 'Stephen', 'age': 22, … }]

Advanced negative filtering via property value and additional false parameter:

arr.filter(p({ age: 22 }, false)); // [{ 'name': 'Barney',  'age': 36, … },
                                   //  { 'name': 'Fred',    'age': 40, … }]

Callback pluck with [ ].every

arr.every(p('married')); // false
arr.every(p({ sex: 'male' })); // true
arr.every(p({ married: true })); // false

Callback pluck with [ ].some

arr.some(p('married')); // true
arr.some(p({ married: true })); // true
arr.some(p({ name: 'Barney' })); // true
arr.some(p({ sex: 'female' })); // false

Note

Advanced filtering via property value doesn't do deep equal, It take only first property from Object.keys(inputObject) to keep logic clean and simple.

License

MIT © Vladimir Starkov

Keywords

FAQs

Package last updated on 28 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc