Add common/src/without.ts
This commit is contained in:
parent
d5c4be2ac1
commit
e50dd99206
@ -12,3 +12,4 @@ export * from './IUserTeam'
|
||||
export * from './ReadonlyRecord'
|
||||
export * from './URLFormatter'
|
||||
export * from './indexBy'
|
||||
export * from './without'
|
||||
|
||||
17
packages/common/src/without.test.ts
Normal file
17
packages/common/src/without.test.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import {without} from './without'
|
||||
|
||||
describe('without', () => {
|
||||
|
||||
it('filters a key out of a record', () => {
|
||||
const records = {
|
||||
k1: 'one',
|
||||
k2: 'two',
|
||||
k3: 'three',
|
||||
}
|
||||
expect(without(records, 'k1')).toEqual({
|
||||
k2: 'two',
|
||||
k3: 'three',
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
13
packages/common/src/without.ts
Normal file
13
packages/common/src/without.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export function without<T, R extends Record<string, T>, K extends keyof R>(
|
||||
items: R,
|
||||
key: K,
|
||||
): Pick<R, Exclude<keyof R, K>> {
|
||||
return Object.keys(items).reduce((obj, k) => {
|
||||
// tslint:disable-next-line
|
||||
if (key == k) {
|
||||
return obj
|
||||
}
|
||||
(obj as any)[k] = items[k]
|
||||
return obj
|
||||
}, {} as Pick<R, Exclude<keyof R, K>>)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user