Add unit tests for sorting functions
This commit is contained in:
parent
aeb3374406
commit
ec581ad674
|
@ -1,14 +1,14 @@
|
||||||
import { getIn } from '../utils/immutabilityHelpers.js'
|
import { getIn } from '../utils/immutabilityHelpers.js'
|
||||||
|
import naturalSort from 'javascript-natural-sort'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort the items of an array
|
* Sort the items of an array
|
||||||
* @param {Array} array The array to be sorted
|
* @param {Array} array The array to be sorted
|
||||||
* @param {Path} path Nested path to the property on which to sort the contents
|
* @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 {1 | -1} [direction=1] Pass 1 to sort ascending, -1 to sort descending
|
||||||
* @return {Array} Returns a sorted shallow copy of the array
|
* @return {Array} Returns a sorted shallow copy of the array
|
||||||
*/
|
*/
|
||||||
// TODO: write unit tests for sortArray
|
export function sortArray (array, path = [], direction = 1) {
|
||||||
export function sortArray (array, path, direction) {
|
|
||||||
function comparator (a, b) {
|
function comparator (a, b) {
|
||||||
const valueA = getIn(a, path)
|
const valueA = getIn(a, path)
|
||||||
const valueB = getIn(b, path)
|
const valueB = getIn(b, path)
|
||||||
|
@ -41,12 +41,11 @@ export function sortArray (array, path, direction) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort the keys of an object
|
* Sort the keys of an object
|
||||||
* @param {Object} object The object to be sorted
|
* @param {Object} object The object to be sorted
|
||||||
* @param {1 | -1} direction Pass 1 to sort ascending, -1 to sort descending
|
* @param {1 | -1} [direction=1] Pass 1 to sort ascending, -1 to sort descending
|
||||||
* @return {Object} Returns a sorted shallow copy of the object
|
* @return {Object} Returns a sorted shallow copy of the object
|
||||||
*/
|
*/
|
||||||
// TODO: write unit tests for sortObjectKeys
|
export function sortObjectKeys (object, direction = 1) {
|
||||||
export function sortObjectKeys (object, direction) {
|
|
||||||
const keys = Object.keys(object)
|
const keys = Object.keys(object)
|
||||||
keys.sort((keyA, keyB) => {
|
keys.sort((keyA, keyB) => {
|
||||||
return direction * naturalSort(keyA, keyB)
|
return direction * naturalSort(keyA, keyB)
|
||||||
|
|
|
@ -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'])
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
Loading…
Reference in New Issue