export function leftPad(str: string | number, length: number, fill = '0') { let current = String(str); while (current.length < length) { current = `${fill}${str}`; } return current; } export const tuple = (...args: T) => args; export function toArray(val: T | T[]): T[] { if (val === null || val === undefined) { return []; } return Array.isArray(val) ? val : [val]; } export default function getDataOrAriaProps(props: any) { const retProps: any = {}; Object.keys(props).forEach(key => { if ( (key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-' || key === 'role' || key === 'name') && key.substr(0, 7) !== 'data-__' ) { retProps[key] = props[key]; } }); return retProps; } export function getValue(values: null | undefined | (T | null)[], index: number): T | null { return values ? values[index] : null; } type UpdateValue = (prev: T) => T; export function updateValues( values: [T | null, T | null] | null, value: T | UpdateValue, index: number, ): R { const newValues: [T | null, T | null] = [getValue(values, 0), getValue(values, 1)]; newValues[index] = typeof value === 'function' ? (value as UpdateValue)(newValues[index]) : value; if (!newValues[0] && !newValues[1]) { return null as unknown as R; } return newValues as unknown as R; }