This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 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
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,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 } |
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,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') | ||
}) | ||
}) |