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

pluck

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pluck - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

2

component.json

@@ -5,3 +5,3 @@ {

"description": "pluck property values from arrays or objects",
"version": "0.0.2",
"version": "0.0.3",
"keywords": ["array", "pluck", "array values"],

@@ -8,0 +8,0 @@ "dependencies": {

/**
* Deps.
* Expose `pluck`.
*/
var toFunction = require('to-function');
module.exports = pluck;
/**
* Expose `pluck`.
* Passes directly to `select` or returns a
* function that accepts the object for that will
* be passed along with the earlier `path` to `select`.
*
* @param {String} path
* @param {Object|Array|Undefined} [object]
* @return {Function}
*/
module.exports = pluck;
function pluck(path, object){
if (arguments.length === 2) return select(path, object);
return function(o){
return select(path, o);
};
}
/**
* Pluck.
* Decides whether a single value needs to
* be passed to `get` or many need to be mapped
* in the case of arrays.
*
* @param {String} attr
* @param {Object|Array} obj
* @return {Function}
* @private
* @param {String} path
* @param {Object|Array} object
* @return {Mixed}
*/
function pluck(attr){
var fn = toFunction(attr);
return function(obj){
return Array.isArray(obj)
? obj.map(function(val){
return fn(val);
})
: fn(obj);
};
function select(path, object){
return !Array.isArray(object)
? get(path, object)
: object.map(function(o){
return get(path, o);
});
}
/**
* Returns value from `object` from the `path`.
*
* Note: purposefully loosely checking
* against null to catch undefined and null.
*
* @private
* @param {String} path
* @param {Object} object
* @return {Mixed}
*/
function get(path, object){
if (!(path && object)) return;
var parts = path.split('.');
var len = parts.length;
var value = object;
for (var i = 0; i < len; i++) {
value = value[parts[i]];
if (value == null) break;
}
return value;
}
{
"name": "pluck",
"version": "0.0.2",
"version": "0.0.3",
"description": "pluck values from an object path",
"keywords": [],
"keywords": ["pluck"],
"author": "Garrett Johnson <gjohnson@redventures.com>",
"license": "MIT",
"dependencies": {
"to-function": "*"
},
"dependencies": {},
"devDependencies": {

@@ -12,0 +10,0 @@ "mocha": "*",

@@ -39,3 +39,3 @@ [![Build Status](https://secure.travis-ci.org/gjohnson/pluck.png?branch=master)](http://travis-ci.org/gjohnson/pluck)

Pluck from simple objects.
Pluck from plain objects.

@@ -45,2 +45,4 @@ ```javascript

var firstName = pluck('name.first');
var item = {

@@ -56,10 +58,4 @@ name: {

## API
### pluck(path)
Creates the property lookup function.
## License
MIT
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