Download src/lib/util.js from no1b4me/statslist: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/no1b4me/statslist/resolve/main/src/lib/util.js
- Command line
-
hf download hf://spaces/no1b4me/statslist/src/lib/util.js
-
curl -L -o util.js https://huggingface.co/spaces/no1b4me/statslist/resolve/main/src/lib/util.js
1.45 kB
| import {setTimeout} from 'timers/promises'; | |
| export function numberPad(number, count){ | |
| return `${number}`.padStart(count || 2, 0); | |
| } | |
| export function parseWords(str){ | |
| return str.replace(/[^a-zA-Z0-9]+/g, ' ').split(' ').filter(Boolean); | |
| } | |
| export function sortBy(...keys){ | |
| return (a, b) => { | |
| if(typeof(keys[0]) == 'string')keys = [keys]; | |
| for(const [key, reverse] of keys){ | |
| if(a[key] > b[key])return reverse ? -1 : 1; | |
| if(a[key] < b[key])return reverse ? 1 : -1; | |
| } | |
| return 0; | |
| } | |
| } | |
| export function bytesToSize(bytes){ | |
| const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | |
| if (bytes === 0) return '0 Byte'; | |
| const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); | |
| return (Math.round(bytes / Math.pow(1024, i) * 100) / 100) + ' ' + sizes[i]; | |
| } | |
| export function wait(ms){ | |
| return setTimeout(ms); | |
| } | |
| export function isVideo(filename){ | |
| return [ | |
| "3g2", | |
| "3gp", | |
| "avi", | |
| "flv", | |
| "mkv", | |
| "mk3d", | |
| "mov", | |
| "mp2", | |
| "mp4", | |
| "m4v", | |
| "mpe", | |
| "mpeg", | |
| "mpg", | |
| "mpv", | |
| "webm", | |
| "wmv", | |
| "ogm", | |
| "ts", | |
| "m2ts" | |
| ].includes(filename?.split('.').pop()); | |
| } | |
| export async function promiseTimeout(promise, ms){ | |
| const ac = new AbortController(); | |
| const waitPromise = setTimeout(ms, null, { signal: ac.signal }).then(() => Promise.reject(`Max execution time reached ${ms}`)); | |
| return Promise.race([waitPromise, promise.finally(() => { | |
| ac.abort(); | |
| })]); | |
| } |