Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
✨ Add collection.sortBy
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Aug 31, 2017
1 parent 9a6145d commit 939b543
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/collection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import { map } from './map'
import { orderBy } from './orderBy'
import { reject } from './reject'
import { shuffle } from './shuffle'
import { sortBy } from './sortBy'

/**
* Collection functions.
* @namespace collection
* @since 0.1.8
*/
export {
filter,
map,
orderBy,
reject,
shuffle,
sortBy,
}
19 changes: 19 additions & 0 deletions src/collection/sortBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import _sortBy from 'lodash/sortBy'
import { convert } from 'util/convert'

/**
* Replaces by an array of sorted by <code>iteratees</code>.
* @function
* @memberof collection
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {Array[]|Function[]|Object[]|string[]} [iteratees=[{@link https://lodash.com/docs#identity|lodash.identity}]] The iteratees to sort by.
* @return {Object} Returns the updated object.
* @see {@link https://lodash.com/docs#sortBy|lodash.sortBy} for more information.
* @example
* sortBy({ nested: { prop: [{ name: 'Yvo', age: 2 }, { name: 'Nico', age: 666 }, { name: 'Nico', age: 30 }] } }, 'nested.prop', ['name', 'age'])
* // => { nested: { prop: [{ name: 'Nico', age: 30 }, { name: 'Nico', age: 666 }, { name: 'Yvo', age: 2 }] } }
* @since 0.3.0
*/
const sortBy = convert(_sortBy)
export { sortBy, sortBy as default }
41 changes: 41 additions & 0 deletions src/collection/sortBy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-env jest */
import { immutaTest } from 'test.utils'
import { sortBy } from './sortBy'

describe('SortBy', () => {
it('should sort array by name and age', () => {
immutaTest((input, path) => {
const output = sortBy(input, path, ['name', 'age'])
expect(output).toEqual({
nested: {
prop: [{
name: 'Nico',
age: 30,
}, {
name: 'Nico',
age: 666,
}, {
name: 'Yvo',
age: 2,
}],
},
other: {},
})
return output
}, {
nested: {
prop: [{
name: 'Yvo',
age: 2,
}, {
name: 'Nico',
age: 666,
}, {
name: 'Nico',
age: 30,
}],
},
other: {},
}, 'nested.prop')
})
})

0 comments on commit 939b543

Please sign in to comment.