1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-06-16 04:29:21 +00:00

Added objectClean and isObject functions

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
2021-09-21 21:39:16 +02:00
parent daed0b82e6
commit 4efb33c0cb
2 changed files with 115 additions and 29 deletions

View File

@ -1,6 +1,13 @@
/// <reference types="jest" />
import { objectSize, objectMap, objectSort, objectEqual, objectKeys, objectSet, objectLoop, objectClone, objectValues } from '../src/ObjectUtil'
import { objectSize, objectMap, objectSort, objectEqual, objectKeys, objectSet, objectLoop, objectClone, objectValues, objectClean, isObject } from '../src/ObjectUtil'
describe('Throw if parameter is not an object', () => {
it('should works', () => {
// @ts-ignore
expect(objectKeys).toThrow()
})
})
describe('Object Map tests', () => {
it('should works', () => {
@ -136,32 +143,20 @@ describe('Object Clone Tests', () => {
expect(clone).not.toEqual(obj)
})
it('Should keep types', () => {
const obj = {
a: [],
b: '10',
c: 10,
d: {},
e: [10],
f: {g: 10}
}
it('should clone an Array', () => {
const obj = ['one', 'two']
const clone = objectClone(obj)
expect(clone).toEqual(obj)
clone[0] = 'three'
expect(clone).not.toEqual(obj)
})
it ('Should clone any types', () => {
const obj = {
a: [],
b: '10',
c: 10,
d: {},
e: [10],
f: {g: 10}
}
objectLoop(obj, (subObj) => {
const clone = objectClone(subObj)
expect(clone).toEqual(subObj)
})
it('should deeply clone an Array', () => {
const obj = ['one', 'two', ['three']]
const clone = objectClone(obj)
expect(clone).toEqual(obj)
;(clone[2][0] as string) = 'zero'
expect(clone).not.toEqual(obj)
})
})
@ -236,3 +231,46 @@ describe('Object Equal Test', () => {
})).toBe(true)
})
})
describe('Object Clean Tests', () => {
it('should clean undefined by default', () => {
const obj = {a: '', b: null, c: undefined}
objectClean(obj)
expect(obj).toEqual({a: '', b: null})
const obj2 = {a: '', b: null, c: undefined}
objectClean(obj2, {cleanUndefined: false})
expect(obj2).toEqual({a: '', b: null, c: undefined})
})
it('should clean null when set', () => {
const obj = {a: '', b: null, c: undefined}
objectClean(obj, {cleanNull: true})
expect(obj).toEqual({a: ''})
})
it('should clean deep by default', () => {
const obj = {a: '', b: null, c: undefined, d: {da: '', db: null, dc: undefined}}
objectClean(obj)
expect(obj).toEqual({a: '', b: null, d: {da: '', db: null}})
})
})
describe('Is Object Tests', () => {
it('null is not an "object"', () => {
expect(isObject(null)).toBe(false)
})
it('boolean is not an "object"', () => {
expect(isObject(true)).toBe(false)
})
it('undefined is not an "object"', () => {
expect(isObject(undefined)).toBe(false)
})
it('string is not an "object"', () => {
expect(isObject("null")).toBe(false)
})
it('number is not an "object"', () => {
expect(isObject(0)).toBe(false)
})
it('object is an "object"', () => {
expect(isObject({})).toBe(true)
})
})