File tree 1 file changed +26
-11
lines changed
1 file changed +26
-11
lines changed Original file line number Diff line number Diff line change 1
- type R = Record < string , unknown > ;
2
-
3
1
/**
4
- * I really don't know how to typedef this. It just creates a new object that
5
- * has all the values copied over except for any keys that are defined in the
6
- * omitKeys param.
2
+ * Create a new object that does not contain the provided keys.
3
+ *
4
+ * @example
5
+ * Simple Examples
6
+ * ```ts
7
+ * const object = {
8
+ * a: "",
9
+ * b: 3,
10
+ * c: false,
11
+ * 4: null,
12
+ * } as const;
13
+ *
14
+ * expect(omit(object, ["a"])).toEqual({
15
+ * b: 3,
16
+ * c: false,
17
+ * 4: null,
18
+ * });
19
+ * expect(omit(object, ["a", "c", "d"])).toEqual({ b: 3 });
20
+ * ```
7
21
*
8
22
* @internal
9
23
* @param object - The object to remove keys from
@@ -12,17 +26,18 @@ type R = Record<string, unknown>;
12
26
*/
13
27
export function omit < T extends object , K extends keyof T > (
14
28
object : T ,
15
- omitKeys : K [ ] | string [ ]
29
+ omitKeys : readonly ( K | string ) [ ]
16
30
) : Omit < T , K > {
17
31
if ( ! omitKeys . length ) {
18
32
return object ;
19
33
}
20
34
21
- return Object . keys ( object ) . reduce ( ( updated , key ) => {
22
- if ( ! ( omitKeys as string [ ] ) . includes ( key ) ) {
23
- ( updated as R ) [ key ] = ( object as R ) [ key ] ;
35
+ const result : Record < string , unknown > = { } ;
36
+ for ( const key in object ) {
37
+ if ( ! omitKeys . includes ( key as unknown as K ) ) {
38
+ result [ key ] = object [ key ] ;
24
39
}
40
+ }
25
41
26
- return updated ;
27
- } , { } ) as Omit < T , K > ;
42
+ return result as Omit < T , K > ;
28
43
}
You can’t perform that action at this time.
0 commit comments