jws-frontend/src/utils/arr.ts

58 lines
1.3 KiB
TypeScript

const arr = { move, removeAt, insertAt };
export function move<T extends any>(arr: T[], idx: number, offset: number) {
arr.splice(idx + offset, 0, ...arr.splice(idx, 1));
}
export function removeAt<T extends any>(arr: T[], idx: number) {
return arr.splice(idx, 1);
}
export function insertAt<T extends any>(arr: T[], idx: number, item: T) {
return arr.splice(idx, 0, item);
}
export default arr;
export function deepEquals(
obj1: Record<string, any>,
obj2: Record<string, any>,
): boolean {
// If both objects are the same reference, they are deeply equal
if (obj1 === obj2) {
return true;
}
// If either object is not an object or null, they are not deeply equal
if (
typeof obj1 !== 'object' ||
obj1 === null ||
typeof obj2 !== 'object' ||
obj2 === null
) {
return false;
}
// Get the set of keys for both objects
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
// If the number of keys is different, they are not deeply equal
if (keys1.length !== keys2.length) {
return false;
}
// Check if all keys in obj1 exist in obj2
for (const key of keys1) {
if (!keys2.includes(key)) {
return false;
}
// Recursively check if the values for each key are deeply equal
if (!deepEquals(obj1[key], obj2[key])) {
return false;
}
}
return true;
}