-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(src): modernize
omit()
& move to helpers
directory
Resolves: #45 Signed-off-by: Derek Lewis <DerekNonGeneric@inf.is>
- Loading branch information
1 parent
001e81d
commit fcf321c
Showing
3 changed files
with
39 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |