Add common/src/without.ts

This commit is contained in:
Jerko Steiner 2019-03-24 10:47:53 +08:00
parent d5c4be2ac1
commit e50dd99206
3 changed files with 31 additions and 0 deletions

View File

@ -12,3 +12,4 @@ export * from './IUserTeam'
export * from './ReadonlyRecord'
export * from './URLFormatter'
export * from './indexBy'
export * from './without'

View 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',
})
})
})

View 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>>)
}