Giter Club home page Giter Club logo

callback-pluck's Introduction

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

callback-pluck's People

Contributors

iamstarkov avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.