refactor: normalise data (#756)

* chore: remove legal from bundle

* refactor: create normalised dataset

* refactor: cuesheet uses flat rundown

* refactor: multi-selection

* refactor: prevent stale data on server restart

* refactor: increase ID size

* chore: instrument operation

* chore: update csv tests

* fix: resolve directory to test-db (#758)
This commit is contained in:
Carlos Valente
2024-02-03 21:43:17 +01:00
committed by GitHub
parent 47a519bac1
commit 1890fc49d7
62 changed files with 1838 additions and 3341 deletions
@@ -0,0 +1,88 @@
import { insertAtIndex, reorderArray, sortArrayByProperty } from './arrayUtils.js';
describe('insertAtIndex', () => {
it('should insert an item at the beginning of the array', () => {
const array = [2, 3, 4];
const result = insertAtIndex(0, 1, array);
expect(result).toEqual([1, 2, 3, 4]);
});
it('should insert an item at the end of the array', () => {
const array = [1, 2, 3];
const result = insertAtIndex(3, 4, array);
expect(result).toEqual([1, 2, 3, 4]);
});
it('should insert an item in the middle of the array', () => {
const array = [1, 2, 4];
const result = insertAtIndex(2, 3, array);
expect(result).toEqual([1, 2, 3, 4]);
});
it('should return a new array and not modify the original array', () => {
const array = [1, 2, 3];
const result = insertAtIndex(1, 5, array);
expect(result).toEqual([1, 5, 2, 3]);
expect(array).toEqual([1, 2, 3]); // Original array should remain unchanged
});
});
describe('reorderArray', () => {
it('should reorder an item in the array', () => {
const array = ['a', 'b', 'c', 'd'];
const result = reorderArray(array, 1, 3);
expect(result).toEqual(['a', 'c', 'd', 'b']);
});
it('should return the original array if fromIndex and toIndex are the same', () => {
const array = ['a', 'b', 'c'];
const result = reorderArray(array, 1, 1);
expect(result).toEqual(array);
});
it('should handle reordering to the beginning of the array', () => {
const array = ['a', 'b', 'c'];
const result = reorderArray(array, 2, 0);
expect(result).toEqual(['c', 'a', 'b']);
});
it('should handle reordering to the end of the array', () => {
const array = ['a', 'b', 'c'];
const result = reorderArray(array, 0, 2);
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);
});
});
@@ -0,0 +1,65 @@
/**
* Inserts an item in an array at a given index
* @param index
* @param item
* @param array
*/
export function insertAtIndex<T>(index: number, item: T, array: T[]): T[] {
const modifiedArray = [...array];
// Insert at beginning
if (index === 0) {
modifiedArray.unshift(item);
}
// insert at end
else if (index >= modifiedArray.length) {
modifiedArray.push(item);
}
// insert in the middle
else {
modifiedArray.splice(index, 0, item);
}
return modifiedArray;
}
/**
* Deletes array element at a given index
* @param index
* @param array
*/
export function deleteAtIndex<T>(index: number, array: T[]) {
return array.filter((_, i) => i !== index);
}
export function reorderArray<T>(array: T[], fromIndex: number, toIndex: number) {
if (fromIndex === toIndex) {
return array; // No change needed, return the original array
}
const modifiedArray = [...array];
// delete in from
const [reorderedItem] = modifiedArray.splice(fromIndex, 1);
// reinsert item at to
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];
});
};