Skip to content

Commit

Permalink
feat(object): add objectOmit
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Oct 12, 2023
1 parent 0931532 commit 1fd156a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { deepMerge, deepMergeWithArray, objectMap } from './object'
import { deepMerge, deepMergeWithArray, objectMap, objectOmit } from './object'

it('objectMap', () => {
expect(objectMap({}, (...args) => args)).toEqual({})
Expand Down Expand Up @@ -77,3 +77,12 @@ describe('deepMergeWithArray', () => {
expect(deepMergeWithArray({}, obj1, obj2)).toEqual({ a: ['A', 'B', 'C'], b: ['D'] })
})
})

it('objectOmit', () => {
const obj = {
foo: 'bar',
baz: 'qux',
}

expect(objectOmit(obj, ['foo'])).toEqual({ baz: 'qux' })
})
9 changes: 9 additions & 0 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ export function objectPick<O extends object, T extends keyof O>(obj: O, keys: T[
}, {} as Pick<O, T>)
}

/**
* Creates a new object without the specified keys.
*
* @category Object
*/
export function objectOmit<O extends object, T extends keyof O>(obj: O, keys: T[], omitUndefined = false): Omit<O, T> {
return objectPick(obj, objectKeys(obj).filter((k) => !keys.includes(k as any)) as any, omitUndefined)
}

/**
* Clears undefined fields from an object. It mutates the object.
*
Expand Down

0 comments on commit 1fd156a

Please sign in to comment.