mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-10 15:35:35 +00:00
feat: recreate dashboard page from previous version
This commit is contained in:
39
inertia/lib/array.ts
Normal file
39
inertia/lib/array.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import i18n from '~/i18n';
|
||||
|
||||
export function groupItemBy(array: any[], property: string) {
|
||||
const hash: any = {};
|
||||
const props = property.split('.');
|
||||
|
||||
for (const item of array) {
|
||||
const key = props.reduce((acc, prop) => acc && acc[prop], item);
|
||||
const hashKey =
|
||||
key !== undefined ? key : i18n.t('common:collection.collections');
|
||||
|
||||
if (!hash[hashKey]) {
|
||||
hash[hashKey] = [];
|
||||
}
|
||||
|
||||
hash[hashKey].push(item);
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
// Thanks S/O
|
||||
export function arrayMove<T>(
|
||||
arr: T[],
|
||||
previousIndex: number,
|
||||
nextIndex: number
|
||||
): T[] {
|
||||
const arrayCopy = [...arr];
|
||||
const [removedElement] = arrayCopy.splice(previousIndex, 1);
|
||||
|
||||
if (nextIndex >= arr.length) {
|
||||
// Pad the array with undefined elements if needed
|
||||
const padding = nextIndex - arr.length + 1;
|
||||
arrayCopy.push(...new Array(padding).fill(undefined));
|
||||
}
|
||||
|
||||
arrayCopy.splice(nextIndex, 0, removedElement);
|
||||
return arrayCopy;
|
||||
}
|
||||
Reference in New Issue
Block a user