Git Product home page Git Product logo

pluck-deep's Introduction

Build Status

pluck-deep

Pluck values of a collection given a 'dot' separated string

module.exports = function pluckDeep(coll, selector) { ... }

Install

npm i -S pluck-deep

Usage

deep-pluck accepts an array or object as an initial argument.

You can pick a value or array of values by passing in a selector.

Selectors

For the purpose of deep-pluck a selector is a string with object value accessors separated with a .. In other words, you can retrieve a value just like you would on any object, except that these selectors will automatically map an array of values for you.

// Example selectors
'planet.earth.size' // returns a single value set a size
'planet.saturn.moons.name' // Will return an array of moon names

See below examples and tests for more selectors

Value plucking

var sel = 'mercury.venus';
var o = {
  mercury: {
    venus: 'earth'
  }
};

var v = pluckDeep(o, sel);
assert.equal(v, o.mercury.venus);

Array plucking

var sel = 'saturn.moons.name';
var o = {
  saturn: {
    moons: [
      { name: 'titan' },
      { name: 'enceladus' },
      { name: 'rhea' }
    ]
  }
};

var arr = pluckDeep(o, sel);
var expect = o.saturn.moons.map(prop('name'));
assert.deepEqual(arr, expect);

Filterable

Add a filterable statment to filter out return values based on some comparable

var sel = 'system.planets.name[type=dwarf]';
var o = {
  system: {
    planets: [
      {
        name: 'earth',
        type: 'ocean',
      },
      {
        name: 'pluto',
        type: 'dwarf',
      }
    ]
  }
};

var v = pluckDeep(o, sel);
assert(Array.isArray(v));
assert.equal(v.length, 1);
assert.equal(v[0], 'pluto');

// With an object
var sel = 'rings[type=gas]';
var venus = {
  type: 'gas',
  rings: false
};

var v = pluckDeep(venus, sel);
assert.equal(v, false);

Changelog

0.2.2

  • Use regex for filters

0.2.1

  • Fixed typo in test

0.2.0

  • Added filterables

0.1.1

  • Added an extra santity test

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.