import type { ClassNames } from './types'; type BasicObj = { [key: string]: any }; export function forEach( obj: Input | undefined, fn: (value: Input[Key], key: string) => void, ): void { for (const key in obj) { fn(obj[key], key); } } export function omit( obj: Input | undefined, omitKeys: Array, ): Omit { let result: any = {}; for (const key in obj) { if (omitKeys.indexOf(key as any) === -1) { result[key] = obj[key]; } } return result; } export function mapKeys( obj: Input | undefined, fn: (value: Input[Key], key: string) => string, ): Record { let result: any = {}; for (const key in obj) { result[fn(obj[key], key)] = obj[key]; } return result; } export function isEqual(a: any, b: any) { if (typeof a !== typeof b) { return false; } if (typeof a === 'object') { const keys1 = Object.keys(a); const keys2 = Object.keys(b); if (keys1.length !== keys2.length) { return false; } for (const key in a) { if (!isEqual(a[key], b[key])) { return false; } } return true; } else { return a === b; } } function composeStylesIntoSet( set: Set, ...classNames: Array ) { for (const className of classNames) { if (className.length === 0) { continue; } if (typeof className === 'string') { if (className.includes(' ')) { composeStylesIntoSet(set, ...className.trim().split(' ')); } else { set.add(className); } } else if (Array.isArray(className)) { composeStylesIntoSet(set, ...className); } } } export function dedupeAndJoinClassList(classNames: Array) { const set: Set = new Set(); composeStylesIntoSet(set, ...classNames); return Array.from(set).join(' '); } // https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript export function escapeRegex(string: string) { return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); }