-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pluck): pluck method implementation and tests
Creates a collection composed of the picked properties from each `object` of the passed collection.
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import map from './map'; | ||
import pick from './pick'; | ||
|
||
/** | ||
* Creates a collection composed of the picked properties from each `object` of the passed collection. | ||
* | ||
* @since 5.0.0 | ||
* @category Collection | ||
* @param {Array} collection The collection to iterate over. | ||
* @param {...(string|string[])} [paths] The property paths of each item to pick. | ||
* @returns {Array} Returns the new array. | ||
* @example | ||
* | ||
* const collection = [ { 'a': 1, 'b': '2', 'c': 3 }, { 'a': 4, 'b': 5, 'c': 6 } ] | ||
* | ||
* pluck(collection, ['a', 'c']) | ||
* // => [ { 'a': 1, 'c': 3 }, { 'a': 4, 'c': 6 } ] | ||
*/ | ||
function pluck(collection, ...paths) { | ||
return map(collection, (item) => pick(item, paths)); | ||
} | ||
|
||
export default pluck; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import lodashStable from 'lodash'; | ||
import { toArgs } from './utils'; | ||
import pluck from '../src/pluck'; | ||
|
||
describe('pluck', () => { | ||
const args = toArgs(['a', 'c']); | ||
const collection = [ | ||
{ a: 1, b: '2', c: 3 }, | ||
{ a: 4, b: 5, c: 6 }, | ||
]; | ||
const nested = [{ a: 1, b: { c: 2, d: 3 } }]; | ||
|
||
it('should flatten `paths`', () => { | ||
expect(pluck(collection, 'a', 'c')).toEqual([ | ||
{ a: 1, c: 3 }, | ||
{ a: 4, c: 6 }, | ||
]); | ||
expect(pluck(collection, ['a', 'd'], 'c')).toEqual([ | ||
{ a: 1, c: 3 }, | ||
{ a: 4, c: 6 }, | ||
]); | ||
}); | ||
|
||
it('should support deep paths', () => { | ||
expect(pluck(nested, 'b.c')).toEqual([{ b: { c: 2 } }]); | ||
}); | ||
|
||
it('should support path arrays', () => { | ||
const collection = [{ 'a.b': 1, a: { b: 2 } }]; | ||
const actual = pluck(collection, [['a.b']]); | ||
|
||
expect(actual).toEqual([{ 'a.b': 1 }]); | ||
}); | ||
|
||
it('should pluck a key over a path', () => { | ||
const collection = [{ 'a.b': 1, a: { b: 2 } }]; | ||
|
||
lodashStable.each(['a.b', ['a.b']], (path) => { | ||
expect(pluck(collection, path)).toEqual([{ 'a.b': 1 }]); | ||
}); | ||
}); | ||
|
||
it('should coerce `paths` to strings', () => { | ||
expect(pluck([{ 0: 'a', 1: 'b' }], 0)).toEqual([{ 0: 'a' }]); | ||
}); | ||
|
||
it('should return an empty collection when `collection` is nullish', () => { | ||
lodashStable.each([null, undefined], (value) => { | ||
expect(pluck([value], 'valueOf')).toEqual([{}]); | ||
}); | ||
}); | ||
|
||
it('should work with a primitive `collection`', () => { | ||
expect(pluck('', 'slice')).toEqual([{ slice: ''.slice }]); | ||
}); | ||
|
||
it('should work with `arguments` collection `paths`', () => { | ||
expect(pluck(collection, args)).toEqual([{ a: 1, c: 3 }]); | ||
}); | ||
}); |