mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-12 10:53:51 +00:00
refactor: create utility for nested templates
This commit is contained in:
committed by
Carlos Valente
parent
7baa6a4ab9
commit
aa929fd0dc
@@ -0,0 +1,87 @@
|
||||
import { deleteAtIndex, insertAtIndex, reorderArray } 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('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', () => {
|
||||
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']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 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.toSpliced(index, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorders two objects in an array
|
||||
* @param array
|
||||
* @param fromIndex
|
||||
* @param toIndex
|
||||
*/
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Extracts a value from a nested object using a dot-separated path
|
||||
*/
|
||||
export function getPropertyFromPath<T extends object>(path: string, obj: T): any | undefined {
|
||||
const keys = path.split('.');
|
||||
let result: any = obj;
|
||||
|
||||
// iterate through variable parts, and look for the property in the given object
|
||||
for (const key of keys) {
|
||||
if (result && typeof result === 'object' && key in result) {
|
||||
result = result[key];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { getPropertyFromPath } from './objectUtils';
|
||||
|
||||
describe('getNestedFromTemplate', () => {
|
||||
it('should return the value of a nested property', () => {
|
||||
expect(getPropertyFromPath('a', { a: 1 })).toBe(1);
|
||||
expect(getPropertyFromPath('a.b', { a: { b: 1 } })).toBe(1);
|
||||
expect(getPropertyFromPath('a.b.c', { a: { b: { c: 1 } } })).toBe(1);
|
||||
});
|
||||
|
||||
it('should guard against values that do not exist', () => {
|
||||
expect(getPropertyFromPath('c', {})).toBe(undefined);
|
||||
expect(getPropertyFromPath('c', { a: 1 })).toBe(undefined);
|
||||
expect(getPropertyFromPath('a.c', { a: { b: 1 } })).toBeUndefined();
|
||||
expect(getPropertyFromPath('c.a', { a: { b: 1 } })).toBeUndefined();
|
||||
expect(getPropertyFromPath('a.b.b', { a: { b: { c: 1 } } })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user