import { isEqual } from 'ohash/utils' export function pick(data: Data, keys: Keys[]): Pick { const result = {} as Pick for (const key of keys) { result[key] = data[key] } return result } export function omit(data: Data, keys: Keys[]): Omit { const result = { ...data } for (const key of keys) { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete result[key] } return result as Omit } export function get(object: Record | undefined, path: (string | number)[] | string, defaultValue?: any): any { if (typeof path === 'string') { path = path.split('.').map((key) => { const numKey = Number(key) return Number.isNaN(numKey) ? key : numKey }) } let result: any = object for (const key of path) { if (result === undefined || result === null) { return defaultValue } result = result[key] } return result !== undefined ? result : defaultValue } export function set(object: Record, path: (string | number)[] | string, value: any): void { if (typeof path === 'string') { path = path.split('.').map((key) => { const numKey = Number(key) return Number.isNaN(numKey) ? key : numKey }) } path.reduce((acc, key, i) => { if (acc[key] === undefined) acc[key] = {} if (i === path.length - 1) acc[key] = value return acc[key] }, object) } export function looseToNumber(val: any): any { const n = Number.parseFloat(val) return Number.isNaN(n) ? val : n } export function compare(value?: T, currentValue?: T, comparator?: string | ((a: T, b: T) => boolean)) { if (value === undefined || currentValue === undefined) { return false } if (typeof value === 'string') { return value === currentValue } if (typeof comparator === 'function') { return comparator(value, currentValue) } if (typeof comparator === 'string') { return get(value!, comparator) === get(currentValue!, comparator) } return isEqual(value, currentValue) }