diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 9f2c583..60d69e1 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -12,3 +12,4 @@ export * from './IUserTeam' export * from './ReadonlyRecord' export * from './URLFormatter' export * from './indexBy' +export * from './without' diff --git a/packages/common/src/without.test.ts b/packages/common/src/without.test.ts new file mode 100644 index 0000000..c159d20 --- /dev/null +++ b/packages/common/src/without.test.ts @@ -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', + }) + }) + +}) diff --git a/packages/common/src/without.ts b/packages/common/src/without.ts new file mode 100644 index 0000000..50553a5 --- /dev/null +++ b/packages/common/src/without.ts @@ -0,0 +1,13 @@ +export function without, K extends keyof R>( + items: R, + key: K, +): Pick> { + 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>) +}