-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): add pick fn, improve omit
- Loading branch information
Showing
5 changed files
with
115 additions
and
9 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
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 |
---|---|---|
@@ -1,18 +1,33 @@ | ||
export function omit<T extends object>( | ||
obj: T, | ||
keys: string | string[] | ||
): Partial<T> { | ||
const result = {}; | ||
for (const key of Object.keys(obj)) { | ||
if (typeof keys === 'string') { | ||
if (keys !== key) { | ||
export function omit<Obj extends object, Keys extends keyof Obj>( | ||
obj: Obj, | ||
keys: Keys | ||
): Omit<Obj, Keys>; | ||
|
||
export function omit<Obj extends object, Keys extends Array<keyof Obj>>( | ||
obj: Obj, | ||
keys: Keys | ||
): Omit<Obj, Keys[number]>; | ||
|
||
export function omit< | ||
Obj extends object, | ||
Keys extends keyof Obj | Array<keyof Obj> | ||
>( | ||
obj: Obj, | ||
keys: string | Array<keyof Obj> | ||
): Omit<Obj, Keys extends Array<keyof Obj> ? Keys[number] : Keys> { | ||
const result: any = {}; | ||
|
||
for (const key of Object.keys(obj) as Array<keyof Obj>) { | ||
if (Array.isArray(keys)) { | ||
if (!keys.includes(key)) { | ||
result[key] = obj[key]; | ||
} | ||
} else { | ||
if (!keys.includes(key)) { | ||
if (keys !== key) { | ||
result[key] = obj[key]; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} |
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,56 @@ | ||
import { pick } from './pick'; | ||
|
||
describe('pick', () => { | ||
it('returns an object with only the specified key when a single key is provided', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
const result = pick(obj, 'a'); | ||
expect(result).toEqual({ a: 1 }); | ||
}); | ||
|
||
it('returns an object with only the specified keys when an array of keys is provided', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
const result = pick(obj, ['a', 'c']); | ||
expect(result).toEqual({ a: 1, c: 3 }); | ||
}); | ||
|
||
it('returns an empty object when none of the specified keys are present', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
// @ts-expect-error - intentionally testing non-existent keys | ||
const result = pick(obj, ['d', 'e']); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('returns an empty object when the input object is empty', () => { | ||
const obj = {}; | ||
// @ts-expect-error - intentionally testing non-existent keys | ||
const result = pick(obj, ['a', 'b']); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('returns an empty object when the keys array is empty', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
const result = pick(obj, []); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('returns an empty object when the key string is empty', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
// @ts-expect-error - intentionally testing non-existent keys | ||
const result = pick(obj, ''); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('returns an object with only the specified key when the key is present multiple times', () => { | ||
// @ts-expect-error - intentionally testing duplicate keys | ||
const obj = { a: 1, b: 2, a: 3 }; | ||
const result = pick(obj, 'a'); | ||
expect(result).toEqual({ a: 3 }); | ||
}); | ||
|
||
it('returns an object with only the specified keys when the keys are present multiple times', () => { | ||
// @ts-expect-error - intentionally testing duplicate keys | ||
const obj = { a: 1, b: 2, a: 3, c: 4, c: 5 }; | ||
const result = pick(obj, ['a', 'c']); | ||
expect(result).toEqual({ a: 3, c: 5 }); | ||
}); | ||
}); |
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,33 @@ | ||
export function pick<Obj extends object, Keys extends keyof Obj>( | ||
obj: Obj, | ||
keys: Keys | ||
): Pick<Obj, Keys>; | ||
|
||
export function pick<Obj extends object, Keys extends Array<keyof Obj>>( | ||
obj: Obj, | ||
keys: Keys | ||
): Pick<Obj, Keys[number]>; | ||
|
||
export function pick< | ||
Obj extends object, | ||
Keys extends keyof Obj | Array<keyof Obj> | ||
>( | ||
obj: Obj, | ||
keys: Keys | ||
): Pick<Obj, Keys extends Array<keyof Obj> ? Keys[number] : Keys> { | ||
const result: any = {}; | ||
|
||
for (const key of Object.keys(obj) as Array<keyof Obj>) { | ||
if (Array.isArray(keys)) { | ||
if (keys.includes(key)) { | ||
result[key] = obj[key]; | ||
} | ||
} else { | ||
if (keys === key) { | ||
result[key] = obj[key]; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} |