mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-23 19:32:14 +00:00
Fixed Object.freeze throwing error with objectOmit (#46)
Signed-off-by: Avior <github@avior.me>
This commit is contained in:
parent
3524264b68
commit
9f608a52fc
@ -158,6 +158,18 @@ describe('Object Clone Tests', () => {
|
||||
;(clone[2][0] as string) = 'zero'
|
||||
expect(clone).not.toEqual(obj)
|
||||
})
|
||||
|
||||
|
||||
it('should clone with Object.freeze', () => {
|
||||
const obj = Object.freeze({
|
||||
pouet: 'first',
|
||||
toto: 'second'
|
||||
})
|
||||
const clone = objectClone<Record<string, string>>(obj)
|
||||
expect(clone).toEqual(obj)
|
||||
clone.pouet = 'third'
|
||||
expect(clone).not.toEqual(obj)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Object Set Tests', () => {
|
||||
@ -237,7 +249,8 @@ describe('Object Clean Tests', () => {
|
||||
const obj = {a: '', b: null, c: undefined}
|
||||
objectClean(obj)
|
||||
expect(obj).toEqual({a: '', b: null})
|
||||
|
||||
})
|
||||
it('should not clean when options.cleanUndefined is false', () => {
|
||||
const obj2 = {a: '', b: null, c: undefined}
|
||||
objectClean(obj2, {cleanUndefined: false})
|
||||
expect(obj2).toEqual({a: '', b: null, c: undefined})
|
||||
@ -268,6 +281,10 @@ describe('Object Omit Tests', () => {
|
||||
const obj = {a: 'a', b: 'c', c: 'b'}
|
||||
expect(objectOmit(obj, 'b', 'd')).toEqual({a: 'a', c: 'b'})
|
||||
})
|
||||
it('should work with Object.freeze', () => {
|
||||
const obj = {a: 'a', b: 'c', c: 'b'}
|
||||
expect(objectOmit(Object.freeze(obj), 'b', 'd')).toEqual({a: 'a', c: 'b'})
|
||||
})
|
||||
})
|
||||
|
||||
describe('Is Object Tests', () => {
|
||||
|
@ -109,7 +109,7 @@ export function cloneObject<T = Record<string, any>>(obj: T): T {
|
||||
* @param obj the object to clone
|
||||
* @returns the clone of the object
|
||||
*/
|
||||
export function objectClone<T = Record<string, any>>(obj: T): T {
|
||||
export function objectClone<T = Record<string, any>>(obj: T, options?: {deep?: boolean}): T {
|
||||
mustBeObject(obj)
|
||||
if (Array.isArray(obj)) {
|
||||
const arr: Array<any> = []
|
||||
@ -124,7 +124,7 @@ export function objectClone<T = Record<string, any>>(obj: T): T {
|
||||
}
|
||||
const clone: Partial<T> = {}
|
||||
objectLoop(obj, (value, key) => {
|
||||
if (typeof value === 'object' && value != null) {
|
||||
if (typeof value === 'object' && value != null && (typeof options?.deep === 'undefined' || options.deep)) {
|
||||
clone[key as Extract<keyof T, string>] = objectClone(value)
|
||||
return
|
||||
}
|
||||
@ -228,7 +228,7 @@ export function objectClean(obj: Record<string, any>, options?: {cleanUndefined?
|
||||
* @returns the cloned object
|
||||
*/
|
||||
export function objectOmit<T extends Record<string, any> = Record<string, any>>(obj: T, ...keys: Array<string>): T {
|
||||
const cloned = obj
|
||||
const cloned = objectClone(obj, {deep: false})
|
||||
for (const key of keys) {
|
||||
if (key in cloned) {
|
||||
delete cloned[key]
|
||||
|
Loading…
x
Reference in New Issue
Block a user