diff --git a/src/logic/sort.js b/src/logic/sort.js index 73ed026..c5ee979 100644 --- a/src/logic/sort.js +++ b/src/logic/sort.js @@ -1,14 +1,14 @@ import { getIn } from '../utils/immutabilityHelpers.js' +import naturalSort from 'javascript-natural-sort' /** * Sort the items of an array - * @param {Array} array The array to be sorted - * @param {Path} path Nested path to the property on which to sort the contents - * @param {1 | -1} direction Pass 1 to sort ascending, -1 to sort descending + * @param {Array} array The array to be sorted + * @param {Path} [path=[]] Nested path to the property on which to sort the contents + * @param {1 | -1} [direction=1] Pass 1 to sort ascending, -1 to sort descending * @return {Array} Returns a sorted shallow copy of the array */ -// TODO: write unit tests for sortArray -export function sortArray (array, path, direction) { +export function sortArray (array, path = [], direction = 1) { function comparator (a, b) { const valueA = getIn(a, path) const valueB = getIn(b, path) @@ -41,12 +41,11 @@ export function sortArray (array, path, direction) { /** * Sort the keys of an object - * @param {Object} object The object to be sorted - * @param {1 | -1} direction Pass 1 to sort ascending, -1 to sort descending + * @param {Object} object The object to be sorted + * @param {1 | -1} [direction=1] Pass 1 to sort ascending, -1 to sort descending * @return {Object} Returns a sorted shallow copy of the object */ -// TODO: write unit tests for sortObjectKeys -export function sortObjectKeys (object, direction) { +export function sortObjectKeys (object, direction = 1) { const keys = Object.keys(object) keys.sort((keyA, keyB) => { return direction * naturalSort(keyA, keyB) diff --git a/src/logic/sort.test.js b/src/logic/sort.test.js new file mode 100644 index 0000000..36e1ee7 --- /dev/null +++ b/src/logic/sort.test.js @@ -0,0 +1,34 @@ +import assert from 'assert' +import { sortArray, sortObjectKeys } from './sort.js' + +describe('sort', () => { + + it('should sort array', () => { + assert.deepStrictEqual(sortArray([ 2, 3, 1 ]), [1, 2, 3]) + assert.deepStrictEqual(sortArray([ 2, 3, 1 ], undefined, -1), [3, 2, 1]) + }) + + it('should sort array using natural sort', () => { + assert.deepStrictEqual(sortArray([ '10', '2', '1' ]), ['1', '2', '10']) + }) + + it('should sort array by nested properties', () => { + const a = {data: { value: 1 }} + const b = {data: { value: 2 }} + const c = {data: { value: 3 }} + + assert.deepStrictEqual(sortArray([ b, c, a ], ['data', 'value']), [a, b, c]) + assert.deepStrictEqual(sortArray([ b, a, c ], ['data', 'value']), [a, b, c]) + assert.deepStrictEqual(sortArray([ b, a, c ], ['data', 'value'], 1), [a, b, c]) + assert.deepStrictEqual(sortArray([ b, a, c ], ['data', 'value'], -1), [c, b, a]) + }) + + it('should sort object keys', () => { + const object = { b: 1, c: 1, a: 1 } + + assert.deepStrictEqual(Object.keys(sortObjectKeys(object)), ['a', 'b', 'c']) + assert.deepStrictEqual(Object.keys(sortObjectKeys(object, 1)), ['a', 'b', 'c']) + assert.deepStrictEqual(Object.keys(sortObjectKeys(object, -1)), ['c', 'b', 'a']) + }) + +})