export function mapObject(obj: { [key: string]: V }, valueTransform: null, keyTransform: (key: K) => string | number): Record export function mapObject(obj: { [key: string]: V }, valueTransform: (value: V) => R, keyTransform?: (key: K) => string | number): Record export function mapObject( obj: { [key: string]: any }, valueTransform: null | ((value: any) => any), keyTransform?: (key: any) => string | number ): any { if (!obj) return obj; const nob: Record = {}; Object.entries(obj).forEach(([k, v]) => { const key = keyTransform ? keyTransform(k as any) : k; const value = valueTransform ? valueTransform(v as any) : v; nob[key] = value; }); return nob; } /** * Accepts an object and maps any numeric keys to `_`-prefixed strings */ export const mapNumerics = (obj: any, prefix: string = '_') => mapObject(obj, null, (k) => prefix + k); /** * Accepts an object returned from mapNumerics and maps back to numeric keys */ export const unmapNumerics = (obj: any, prefix: string = '_') => mapObject(obj, null, (k) => { if (k.startsWith(prefix)) k = k.slice(prefix.length); const val = parseInt(k); if (isNaN(val)) console.warn(`Could not unmap ${k}`); return val; }); export const copySimpleObj = (obj: T): T => JSON.parse(JSON.stringify(obj)); export const pick = (obj: T, keys: K[]): Pick => { const picked: any = {}; for (let k of keys) { if (k in obj) picked[k] = obj[k]; } return picked; } export const formDataArray = >(arr: T) => { const obj: { [K in keyof T]: T[K] } = {} as any; for (let i = 0; i < arr.length; i++) { obj[i] = arr[i]; } return obj; } export function addHiddenProp(object: any, propName: PropertyKey, value: any) { Object.defineProperty(object, propName, { enumerable: false, writable: true, configurable: true, value }) } export function getInheritedHiddenProp(target: any, propName: PropertyKey, type: 'prototypical'): Record export function getInheritedHiddenProp(target: any, propName: PropertyKey, type: (target, inherit: T) => T): T export function getInheritedHiddenProp(target: any, propName: PropertyKey, defaultValue: Record | 'object'): Record export function getInheritedHiddenProp(target: any, propName: PropertyKey, defaultValue: T[] | 'array'): T[] export function getInheritedHiddenProp(target: any, propName: PropertyKey, defaultValue: 'set'): Set export function getInheritedHiddenProp(target: any, propName: PropertyKey, defaultValue) { if (!Object.prototype.hasOwnProperty.call(target, propName)) { const inherited = target[propName] || (typeof defaultValue != 'string' ? defaultValue : null); let newValue; if (typeof defaultValue == 'function') { newValue = defaultValue(target, inherited); } else if (Array.isArray(defaultValue) || defaultValue == 'array') { newValue = inherited ? [...inherited] : []; } else if (defaultValue == 'set') { newValue = new Set(inherited); } else if (typeof defaultValue == 'object' || defaultValue == 'object') { newValue = inherited ? { ...inherited } : {}; } else if (defaultValue == 'prototypical') { newValue = {}; if (inherited) Object.setPrototypeOf(newValue, inherited); } addHiddenProp(target, propName, newValue) } return target[propName]; }