Skip to content

Commit

Permalink
refactor(src): modernize omit() & move to helpers directory
Browse files Browse the repository at this point in the history
Resolves: #45
Signed-off-by: Derek Lewis <DerekNonGeneric@inf.is>
  • Loading branch information
DerekNonGeneric committed Aug 18, 2022
1 parent 001e81d commit fcf321c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
29 changes: 29 additions & 0 deletions src/helpers/omit.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(
opt_originalObject: Record<ESPropertyKey, T> = {},
opt_keysToOmit: ESPropertyKey[] = new Array<ESPropertyKey>()
): Record<ESPropertyKey, T> => {
return Object.keys(opt_originalObject).reduce(
(accumulator: Record<ESPropertyKey, T>, 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;
17 changes: 0 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,6 @@ export function deepMerge(target: Object, source: Object, depth = 10): Object {
return target;
}

/**
* @param {!Record<string, number | RegExp>} o An object to remove properties from.
* @param {!Array<string>} props A list of properties to remove from the Object.
* @returns {!Record<string, number | RegExp>} An object with the given properties removed.
*/
export function omit(
o: Record<string, number | RegExp>,
props: Array<string>
): Record<string, number | RegExp> {
return Object.keys(o).reduce((acc: Record<string, number | RegExp>, key) => {
if (!props.includes(key)) {
acc[key] = o[key];
}
return acc;
}, {});
}

/**
* @param {!Record<string, number | RegExp> | null | undefined} o1
* @param {!Record<string, number | RegExp> | null | undefined} o2
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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<string, PropertyDescriptor>;

0 comments on commit fcf321c

Please sign in to comment.