From fcf321c49e73a2486866b8da238b2088edad68bd Mon Sep 17 00:00:00 2001 From: Derek Lewis Date: Wed, 17 Aug 2022 20:18:18 -0400 Subject: [PATCH] refactor(src): modernize `omit()` & move to `helpers` directory Resolves: https://github.com/openinf/util-object/issues/45 Signed-off-by: Derek Lewis --- src/helpers/omit.ts | 29 +++++++++++++++++++++++++++++ src/index.ts | 17 ----------------- src/types.ts | 10 ++++++++++ 3 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 src/helpers/omit.ts create mode 100644 src/types.ts diff --git a/src/helpers/omit.ts b/src/helpers/omit.ts new file mode 100644 index 0000000..f134a97 --- /dev/null +++ b/src/helpers/omit.ts @@ -0,0 +1,29 @@ +import type { ESPropertyKey } from '../types'; + +/** + * Returns a partial copy of an object omitting the keys specified. We do not copy. + * + * @template T + * @param opt_originalObject An object to remove properties from. The default value is an empty object literal. + * @param opt_keysToOmit The list of property keys to remove from the original Object. The default value is an empty array. + * @returns A new object without the omitted keys. An object with the given properties removed. + * The original object is not modified if provided. + */ +export const omit = ( + opt_originalObject: Record = {}, + opt_keysToOmit: ESPropertyKey[] = new Array() +): Record => { + return Object.keys(opt_originalObject).reduce( + (accumulator: Record, key) => { + if ( + !opt_keysToOmit.includes(key) && // if key is not in opt_keysToOmit + Object.is(Reflect.get(accumulator, key), undefined) // if value of key is `undefined` in accumulator (not in prototype chain) + ) + Reflect.set(accumulator, key, Reflect.get(opt_originalObject, key)); + return accumulator; + }, + {} // FIXME(@DerekNonGeneric): do we want an empty object literal to begin our accumulation? + ); +}; + +export default omit; diff --git a/src/index.ts b/src/index.ts index ac6da98..641345b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -121,23 +121,6 @@ export function deepMerge(target: Object, source: Object, depth = 10): Object { return target; } -/** - * @param {!Record} o An object to remove properties from. - * @param {!Array} props A list of properties to remove from the Object. - * @returns {!Record} An object with the given properties removed. - */ -export function omit( - o: Record, - props: Array -): Record { - return Object.keys(o).reduce((acc: Record, key) => { - if (!props.includes(key)) { - acc[key] = o[key]; - } - return acc; - }, {}); -} - /** * @param {!Record | null | undefined} o1 * @param {!Record | null | undefined} o2 diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..7359e5d --- /dev/null +++ b/src/types.ts @@ -0,0 +1,10 @@ +export type ESPropertyKey = symbol | string | number /* | bigint */; + +/** + * Safer version of `Function` which should not be called. + * Every function should be assignable to this, but this should not be assignable to every function. + */ +export type AnyFunction = (...args: never[]) => void; +export type AnyConstructor = new (...args: unknown[]) => unknown; + +export type PropertyDescriptorMap = Record;