const breakpoints = { xxsm: '0px', xsm: '600px', sm: '720px', md: '840px', lg: '992px', xl: '1400px', xxl: '1920px', }; const switchValue = (value?: number | string, defaultValue?: string) => value ? (typeof value === 'number' ? `${value}px` : value) : defaultValue; // parses a px-based CSS width (number, '250px', or a bare numeric string) down to its px number // returns undefined for anything else (percentages, 'auto', etc.) so callers can fall back safely const parsePxValue = (value?: number | string): number | undefined => { if (value === undefined || value === null) return undefined; if (typeof value === 'number') return value; const match = /^(-?\d+(?:\.\d+)?)(px)?$/.exec(value.trim()); return match ? parseFloat(match[1]) : undefined; }; // shrinks a px-based width by `by` px, floored at 0; returns undefined if value isn't a parseable px/number const shrinkPxValue = (value: number | string | undefined, by: number): number | undefined => { const parsed = parsePxValue(value); return parsed === undefined ? undefined : Math.max(0, parsed - by); }; // adds escape character in place of special characters in a string const escapeRegExp = (string: string): string => { return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); }; export const generateRandomString = () => { return Math.random().toString(16).slice(2, 10); }; const mobileView = (content: string) => { const mobileFloor = `${breakpoints['sm']}`; return `@media(max-width:${mobileFloor}) { ${content} }`; }; const tabletView = (content: string) => { const tabletFloor = `${breakpoints['lg']}`; return `@media(max-width:${tabletFloor}) { ${content} }`; }; const overflowView = (content: string) => { const overflowFloor = `${breakpoints['xxl']}`; return `@media(min-width:${overflowFloor}) { ${content} }`; }; export const getNameInitials = (name: string = ''): string => { const words = name?.trim()?.split(' '); // if name has more than one word if (words.length > 1) { const [fName, ...rest] = words; return `${fName[0]}${rest?.pop()?.[0]}`.toUpperCase(); // return initials of fName and lName } // if name has single word const fName = words[0]; return fName[0]?.toUpperCase() || ''; }; export const stringToColor = function (str: string) { let hash = 0; for (let i = 0; i < str.length; i++) { hash = str.charCodeAt(i) + ((hash << 5) - hash); } let colour = '#'; for (let i = 0; i < 3; i++) { const value = (hash >> (i * 8)) & 0xff; colour += ('00' + value.toString(16)).substr(-2); } return colour; }; const isArray = (array: unknown) => Array.isArray(array); const isArrayEmpty = (array: Array) => array.length === 0; const getLastItem = (array: Array) => array[array.length - 1]; function debounce void>(func: T, delay: number) { let timeoutId: NodeJS.Timeout; return (...args: Parameters) => { clearTimeout(timeoutId); timeoutId = setTimeout(() => { func(...args); }, delay); }; } export { switchValue, parsePxValue, shrinkPxValue, escapeRegExp, mobileView, tabletView, overflowView, isArray, isArrayEmpty, getLastItem, debounce, };