mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-05 06:19:11 +00:00
refactor: simplify logic
This commit is contained in:
committed by
Carlos Valente
parent
0dfef732bf
commit
760de608e3
@@ -1,4 +1,4 @@
|
|||||||
import { insertAtIndex, reorderArray } from './arrayUtils.js';
|
import { deleteAtIndex, insertAtIndex, reorderArray } from './arrayUtils.js';
|
||||||
|
|
||||||
describe('insertAtIndex', () => {
|
describe('insertAtIndex', () => {
|
||||||
it('should insert an item at the beginning of the array', () => {
|
it('should insert an item at the beginning of the array', () => {
|
||||||
@@ -27,6 +27,39 @@ describe('insertAtIndex', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('deleteAtIndex', () => {
|
||||||
|
it('should delete an item at the beginning of the array', () => {
|
||||||
|
const array = [1, 2, 3, 4];
|
||||||
|
const result = deleteAtIndex(0, array);
|
||||||
|
expect(result).toEqual([2, 3, 4]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete an item at the end of the array', () => {
|
||||||
|
const array = [1, 2, 3, 4];
|
||||||
|
const result = deleteAtIndex(3, array);
|
||||||
|
expect(result).toEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete an item in the middle of the array', () => {
|
||||||
|
const array = [1, 2, 3, 4];
|
||||||
|
const result = deleteAtIndex(2, array);
|
||||||
|
expect(result).toEqual([1, 2, 4]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a new array and not modify the original array', () => {
|
||||||
|
const array = [1, 2, 3, 4];
|
||||||
|
const result = deleteAtIndex(1, array);
|
||||||
|
expect(result).toEqual([1, 3, 4]);
|
||||||
|
expect(array).toEqual([1, 2, 3, 4]); // Original array should remain unchanged
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the original array if the index is out of bounds', () => {
|
||||||
|
const array = [1, 2, 3, 4];
|
||||||
|
const result = deleteAtIndex(10, array);
|
||||||
|
expect(result).toEqual([1, 2, 3, 4]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('reorderArray', () => {
|
describe('reorderArray', () => {
|
||||||
it('should reorder an item in the array', () => {
|
it('should reorder an item in the array', () => {
|
||||||
const array = ['a', 'b', 'c', 'd'];
|
const array = ['a', 'b', 'c', 'd'];
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export function insertAtIndex<T>(index: number, item: T, array: T[]): T[] {
|
|||||||
* @param array
|
* @param array
|
||||||
*/
|
*/
|
||||||
export function deleteAtIndex<T>(index: number, array: T[]) {
|
export function deleteAtIndex<T>(index: number, array: T[]) {
|
||||||
return array.filter((_, i) => i !== index);
|
return array.toSpliced(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user