-
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
28 additions
and
1 deletion.
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,17 @@ | ||
import { sort } from './sort'; | ||
|
||
describe('sort()', () => { | ||
it('handles nil collections', () => { | ||
expect(sort(null)).toEqual([]); | ||
expect(sort(undefined)).toEqual([]); | ||
}); | ||
|
||
it('should sort in ascending order', () => { | ||
expect(sort([3, 4, 1, 2])).toEqual([1, 2, 3, 4]); | ||
}); | ||
|
||
it('should work with an object for `collection`', () => { | ||
const actual = sort({ a: 2, b: 3, c: 1 }); | ||
expect(actual).toEqual([1, 2, 3]); | ||
}); | ||
}); |
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,10 @@ | ||
import { identity, sortBy } from '@s-libs/micro-dash'; | ||
|
||
/** | ||
* Creates an array of elements, sorted in ascending order as determined by the `<` and `>` operators. This method performs a stable sort, that is, it preserves the original sort order of equal elements. | ||
*/ | ||
export function sort<T>( | ||
collection: Record<string, T> | readonly T[] | null | undefined, | ||
): T[] { | ||
return sortBy(collection, identity); | ||
} |
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