webamp/packages/skin-database/utils.ts
2021-10-17 20:27:24 -07:00

29 lines
761 B
TypeScript

export function truncate(str: string, len: number): string {
const overflow = str.length - len;
if (overflow < 0) {
return str;
}
const half = Math.floor((len - 1) / 2);
const start = str.slice(0, half);
const end = str.slice(-half);
return `${start} ########### ${end}`;
}
export function chunk<T>(items: T[], chunkSize: number): T[][] {
const chunks: T[][] = [];
for (let i = 0; i < items.length; i += chunkSize) {
chunks.push(items.slice(i, i + chunkSize));
}
return chunks;
}
export const MD5_REGEX = /([a-fA-F0-9]{32})/;
export const TWEET_SNOWFLAKE_REGEX = /([0-9]{19})/;
export function throwAfter(message, ms) {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error(message)), ms);
});
}