whisk
Advanced tools
Comparing version 0.0.1 to 0.0.2
var pluck = require('../pluck'); | ||
var people = [ | ||
{ name: 'Bob', age: 35 }, | ||
{ name: 'Thelma', age: 32 }, | ||
{ name: 'Roger', age: 50 } | ||
{ name: 'Bob', age: 35, address: { country: 'Australia' } }, | ||
{ name: 'Thelma', age: 32, address: { country: 'New Zealand' } }, | ||
{ name: 'Roger', age: 50, address: { country: 'Fiji' } } | ||
]; | ||
console.log(people.map(pluck('name'))); | ||
// --> [ 'Bob', 'Thelma', 'Roger' ] | ||
// --> [ 'Bob', 'Thelma', 'Roger' ] | ||
console.log(people.map(pluck('address.country'))); | ||
// --> [ 'Australia', 'New Zealand', 'Fiji' ] |
{ | ||
"name": "whisk", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Functional operation helpers (underscore-like) for working with map, filter, reduce, etc", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
19
pluck.js
@@ -8,14 +8,17 @@ /** | ||
### pluck todo | ||
**/ | ||
module.exports = function(path) { | ||
var parts = (path || '').split('.'); | ||
var maxIdx = parts.length - 1; | ||
- Permit the extraction of multiple properties (should probably be a | ||
separate function?) | ||
return function(item) { | ||
var partIdx = 0; | ||
var val = item; | ||
- Ability to pluck nested properties (e.g. `address.street`) | ||
do { | ||
val = val && val[parts[partIdx++]]; | ||
} while (val && partIdx <= maxIdx); | ||
**/ | ||
module.exports = function(path) { | ||
return function(item) { | ||
return item && item[path]; | ||
return val; | ||
}; | ||
}; |
@@ -18,5 +18,5 @@ # whisk | ||
var people = [ | ||
{ name: 'Bob', age: 35 }, | ||
{ name: 'Thelma', age: 32 }, | ||
{ name: 'Roger', age: 50 } | ||
{ name: 'Bob', age: 35, address: { country: 'Australia' } }, | ||
{ name: 'Thelma', age: 32, address: { country: 'New Zealand' } }, | ||
{ name: 'Roger', age: 50, address: { country: 'Fiji' } } | ||
]; | ||
@@ -26,11 +26,7 @@ | ||
// --> [ 'Bob', 'Thelma', 'Roger' ] | ||
console.log(people.map(pluck('address.country'))); | ||
// --> [ 'Australia', 'New Zealand', 'Fiji' ] | ||
``` | ||
### pluck todo | ||
- Permit the extraction of multiple properties (should probably be a | ||
separate function?) | ||
- Ability to pluck nested properties (e.g. `address.street`) | ||
## License(s) | ||
@@ -37,0 +33,0 @@ |
var test = require('tape'); | ||
var pluck = require('../pluck'); | ||
var people = [ | ||
{ name: 'Bob', age: 35 }, | ||
{ name: 'Thelma', age: 32 }, | ||
{ name: 'Roger', age: 50 } | ||
{ name: 'Bob', age: 35, address: { country: 'Australia' } }, | ||
{ name: 'Thelma', age: 32, address: { country: 'New Zealand' } }, | ||
{ name: 'Roger', age: 50, address: { country: 'Fiji' } } | ||
]; | ||
@@ -25,2 +25,20 @@ | ||
); | ||
}); | ||
test('extract nested property', function(t) { | ||
t.plan(1); | ||
t.deepEqual( | ||
people.map(pluck('address.country')), | ||
['Australia', 'New Zealand', 'Fiji'], | ||
'extracted address country' | ||
); | ||
}); | ||
test('extract missing nested property yields undefined', function(t) { | ||
t.plan(1); | ||
t.deepEqual( | ||
people.map(pluck('address.postcode')), | ||
[undefined, undefined, undefined], | ||
'undefined results received as expected' | ||
); | ||
}); |
4871
10
74
54