Skip to content

Commit

Permalink
feat(js-core): add sort()
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Aug 12, 2022
1 parent c347ebf commit 94d255a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
17 changes: 17 additions & 0 deletions projects/js-core/src/lib/sort.spec.ts
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]);
});
});
10 changes: 10 additions & 0 deletions projects/js-core/src/lib/sort.ts
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);
}
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/collection/sort-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { map } from './map';
* - Micro-dash: 692 bytes
*/
export function sortBy<T>(
collection: Nil | ObjectWith<T> | readonly T[],
collection: ObjectWith<T> | readonly T[] | Nil,
iteratees: Array<ValueIteratee<T, any>> | ValueIteratee<T, any>,
): T[] {
const fns = castArray(iteratees);
Expand Down

0 comments on commit 94d255a

Please sign in to comment.