import { arrayReduce } from '@datahub/utils/array/index'; import { isObject } from '@datahub/utils/validators/object'; /** * Aliases the exclusion / diff conditional type that specifies that an object * contains properties from T, that are not in K * From T pick a set of properties that are not in K * @alias */ export type Omit = Pick>; /** * Aliases a record with properties of type keyof T and null values * Make all values in T null * @template T type to nullify * @alias */ export type Nullify = Record; /** * Checks that an object has it own enumerable props * @param {any} object the object to the be tested */ export const hasEnumerableKeys = (object: object | unknown): boolean => Boolean(isObject(object) && Object.keys(object).length); /** * Non mutative object attribute deletion. Removes the specified keys from a copy of the object and returns the copy. * @template T the object type to drop keys from * @template K the keys to be dropped from the object * @param {T} o * @param {Array} droppedKeys * @return {Pick>} */ export const omit = (o: T, droppedKeys: Array): Omit => { const partialResult = Object.assign({}, o); return arrayReduce((partial: T, key: K) => { delete partial[key]; return partial; }, partialResult)(droppedKeys); }; /** * Extracts keys from a source to a new object * @template T the object to select keys from * @param {T} o the source object * @param {Array} pickedKeys * @return {Select} */ export const pick = (o: T, pickedKeys: Array): Pick => arrayReduce( (partial: T, key: K): Pick => pickedKeys.includes(key) ? Object.assign(partial, { [key]: o[key] }) : partial, {} as T )(pickedKeys); /** * Creates an object of type T with a set of properties of type null * @template T the type of the source object * @template K union of literal types of properties * @param {T} o instance of T to be set to null * @returns {Nullify} */ export const nullify = (o: T): Nullify => { const nullObj = {} as Nullify; const keys = Object.keys(o) as Array; return arrayReduce((nullObj, key: K) => Object.assign(nullObj, { [key]: null }), nullObj)(keys); };