Skip to content

Commit

Permalink
fix(utils): omit uses readonly prefix for key list
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaursen committed Jun 10, 2021
1 parent 7fb8b94 commit d3e1ee8
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions packages/utils/src/omit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
type R = Record<string, unknown>;

/**
* I really don't know how to typedef this. It just creates a new object that
* has all the values copied over except for any keys that are defined in the
* omitKeys param.
* Create a new object that does not contain the provided keys.
*
* @example
* Simple Examples
* ```ts
* const object = {
* a: "",
* b: 3,
* c: false,
* 4: null,
* } as const;
*
* expect(omit(object, ["a"])).toEqual({
* b: 3,
* c: false,
* 4: null,
* });
* expect(omit(object, ["a", "c", "d"])).toEqual({ b: 3 });
* ```
*
* @internal
* @param object - The object to remove keys from
Expand All @@ -12,17 +26,18 @@ type R = Record<string, unknown>;
*/
export function omit<T extends object, K extends keyof T>(
object: T,
omitKeys: K[] | string[]
omitKeys: readonly (K | string)[]
): Omit<T, K> {
if (!omitKeys.length) {
return object;
}

return Object.keys(object).reduce((updated, key) => {
if (!(omitKeys as string[]).includes(key)) {
(updated as R)[key] = (object as R)[key];
const result: Record<string, unknown> = {};
for (const key in object) {
if (!omitKeys.includes(key as unknown as K)) {
result[key] = object[key];
}
}

return updated;
}, {}) as Omit<T, K>;
return result as Omit<T, K>;
}

0 comments on commit d3e1ee8

Please sign in to comment.