refactor: rundown must be in order

This commit is contained in:
Carlos Valente
2024-04-16 22:22:13 +02:00
committed by Carlos Valente
parent bef69e7d0f
commit 8f249b9d51
4 changed files with 7 additions and 57 deletions
@@ -1,4 +1,4 @@
import { insertAtIndex, reorderArray, sortArrayByProperty } from './arrayUtils.js';
import { insertAtIndex, reorderArray } from './arrayUtils.js';
describe('insertAtIndex', () => {
it('should insert an item at the beginning of the array', () => {
@@ -52,37 +52,3 @@ describe('reorderArray', () => {
expect(result).toEqual(['b', 'c', 'a']);
});
});
describe('sortArrayByProperty()', () => {
it('sort array 1-5', () => {
const arr1 = [{ timeStart: 1 }, { timeStart: 5 }, { timeStart: 3 }, { timeStart: 2 }, { timeStart: 4 }];
const arr1Expected = [{ timeStart: 1 }, { timeStart: 2 }, { timeStart: 3 }, { timeStart: 4 }, { timeStart: 5 }];
const sorted = sortArrayByProperty(arr1, 'timeStart');
expect(sorted).toStrictEqual(arr1Expected);
});
it('sort array 1-5 with null', () => {
const arr1 = [
{ timeStart: 1 },
{ timeStart: 5 },
{ timeStart: 3 },
{ timeStart: 2 },
{ timeStart: 4 },
{ timeStart: null },
];
const arr1Expected = [
{ timeStart: null },
{ timeStart: 1 },
{ timeStart: 2 },
{ timeStart: 3 },
{ timeStart: 4 },
{ timeStart: 5 },
];
const sorted = sortArrayByProperty(arr1, 'timeStart');
expect(sorted).toStrictEqual(arr1Expected);
});
});
@@ -48,18 +48,3 @@ export function reorderArray<T>(array: T[], fromIndex: number, toIndex: number)
modifiedArray.splice(toIndex, 0, reorderedItem);
return modifiedArray;
}
/**
* @description Sorts an array of objects by given property
* @param {array} arr - array to be sorted
* @param {string} property - property to compare
* @returns {array} copy of array sorted in ascending order
*/
export const sortArrayByProperty = <T>(arr: T[], property: string): T[] => {
return [...arr].sort((a, b) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore -- its ok
return a[property] - b[property];
});
};