import { arrayReduce } from 'wherehows-web/utils/array'; 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 * @return {boolean} true if enumerable keys are present */ const hasEnumerableKeys = (object: any): 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>} */ 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} */ const pick = (o: T, pickedKeys: Array): Pick => arrayReduce( (partial: T, key: K): Pick => pickedKeys.includes(key) ? Object.assign(partial, { [key]: o[key] }) : partial, {} )(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} */ const nullify = (o: T): Nullify => { let nullObj = >{}; return arrayReduce((nullObj, key: K) => Object.assign(nullObj, { [key]: null }), nullObj)(>Object.keys(o)); }; export { isObject, hasEnumerableKeys, omit, pick, nullify };