mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 23:27:12 +01:00
Imported libraries, removed next-basics.
This commit is contained in:
parent
31266cb1ac
commit
113022ed17
44 changed files with 361 additions and 180 deletions
46
src/lib/utils.ts
Normal file
46
src/lib/utils.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
export function hook(
|
||||
_this: { [x: string]: any },
|
||||
method: string | number,
|
||||
callback: (arg0: any) => void,
|
||||
) {
|
||||
const orig = _this[method];
|
||||
|
||||
return (...args: any) => {
|
||||
callback.apply(_this, args);
|
||||
|
||||
return orig.apply(_this, args);
|
||||
};
|
||||
}
|
||||
|
||||
export function sleep(ms: number | undefined) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export function shuffleArray(a) {
|
||||
const arr = a.slice();
|
||||
for (let i = arr.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
const temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
export function chunkArray(arr: any[], size: number) {
|
||||
const chunks: any[] = [];
|
||||
|
||||
let index = 0;
|
||||
while (index < arr.length) {
|
||||
chunks.push(arr.slice(index, size + index));
|
||||
index += size;
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
export function ensureArray(arr?: any) {
|
||||
if (arr === undefined || arr === null) return [];
|
||||
if (Array.isArray(arr)) return arr;
|
||||
return [arr];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue