Comparing version 0.0.2 to 0.0.3
@@ -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": { |
71
index.js
/** | ||
* 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 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
11020
0
438
0
59
- Removedto-function@*
- Removedcomponent-props@1.1.1(transitive)
- Removedto-function@2.0.6(transitive)