1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-04-22 19:02:14 +00:00

Added objectClean and isObject functions

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

View File

@ -1,6 +1,13 @@
/// <reference types="jest" /> /// <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', () => { describe('Object Map tests', () => {
it('should works', () => { it('should works', () => {
@ -136,32 +143,20 @@ describe('Object Clone Tests', () => {
expect(clone).not.toEqual(obj) expect(clone).not.toEqual(obj)
}) })
it('Should keep types', () => { it('should clone an Array', () => {
const obj = { const obj = ['one', 'two']
a: [],
b: '10',
c: 10,
d: {},
e: [10],
f: {g: 10}
}
const clone = objectClone(obj) const clone = objectClone(obj)
expect(clone).toEqual(obj) expect(clone).toEqual(obj)
clone[0] = 'three'
expect(clone).not.toEqual(obj)
}) })
it ('Should clone any types', () => { it('should deeply clone an Array', () => {
const obj = { const obj = ['one', 'two', ['three']]
a: [], const clone = objectClone(obj)
b: '10', expect(clone).toEqual(obj)
c: 10, ;(clone[2][0] as string) = 'zero'
d: {}, expect(clone).not.toEqual(obj)
e: [10],
f: {g: 10}
}
objectLoop(obj, (subObj) => {
const clone = objectClone(subObj)
expect(clone).toEqual(subObj)
})
}) })
}) })
@ -236,3 +231,46 @@ describe('Object Equal Test', () => {
})).toBe(true) })).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)
})
})

View File

@ -9,6 +9,7 @@ export function objectMap<T = any, J = any>(
obj: Record<string, T>, obj: Record<string, T>,
fn: (value: T, key: string, index: number) => J fn: (value: T, key: string, index: number) => J
): Array<J> { ): Array<J> {
mustBeObject(obj)
const list: Array<J> = [] const list: Array<J> = []
objectLoop(obj, (item, key, index) => { objectLoop(obj, (item, key, index) => {
list.push(fn(item, key, index)) list.push(fn(item, key, index))
@ -25,6 +26,7 @@ export function objectLoop<T = any>(
obj: Record<string, T>, obj: Record<string, T>,
fn: (value: T, key: string, index: number) => boolean | void fn: (value: T, key: string, index: number) => boolean | void
): boolean { ): boolean {
mustBeObject(obj)
const keys = objectKeys(obj) const keys = objectKeys(obj)
for (let index = 0; index < keys.length; index++) { for (let index = 0; index < keys.length; index++) {
const key = keys[index] const key = keys[index]
@ -41,6 +43,7 @@ export function objectLoop<T = any>(
* @param obj the object to transform * @param obj the object to transform
*/ */
export function objectValues<T = any>(obj: Record<string, T>): Array<T> { export function objectValues<T = any>(obj: Record<string, T>): Array<T> {
mustBeObject(obj)
return Object.values(obj) return Object.values(obj)
} }
@ -48,6 +51,7 @@ export function objectValues<T = any>(obj: Record<string, T>): Array<T> {
* @deprecated use `objectValues` * @deprecated use `objectValues`
*/ */
export function objectToArray<T = any>(obj: Record<string, T>): Array<T> { export function objectToArray<T = any>(obj: Record<string, T>): Array<T> {
mustBeObject(obj)
return objectValues(obj) return objectValues(obj)
} }
@ -56,6 +60,7 @@ export function objectToArray<T = any>(obj: Record<string, T>): Array<T> {
* @param obj the object * @param obj the object
*/ */
export function objectKeys(obj: Record<string, any>): Array<string> { export function objectKeys(obj: Record<string, any>): Array<string> {
mustBeObject(obj)
return Object.keys(obj) return Object.keys(obj)
} }
@ -64,6 +69,7 @@ export function objectKeys(obj: Record<string, any>): Array<string> {
* @param obj the object * @param obj the object
*/ */
export function objectSize(obj: Record<string, any>): number { export function objectSize(obj: Record<string, any>): number {
mustBeObject(obj)
return objectKeys(obj).length return objectKeys(obj).length
} }
@ -78,6 +84,7 @@ export function objectSort<T extends Record<string, any> = Record<string, any>>(
obj: T, obj: T,
fn?: Array<keyof T> | ((a: keyof T, b: keyof T) => number) fn?: Array<keyof T> | ((a: keyof T, b: keyof T) => number)
): T { ): T {
mustBeObject(obj)
const ordered: any = {} const ordered: any = {}
let sortedKeys: Array<keyof T> = [] let sortedKeys: Array<keyof T> = []
if (Array.isArray(fn)) { if (Array.isArray(fn)) {
@ -95,6 +102,7 @@ export function objectSort<T extends Record<string, any> = Record<string, any>>(
* @deprecated use `objectClone` * @deprecated use `objectClone`
*/ */
export function cloneObject<T = Record<string, any>>(obj: T): T { export function cloneObject<T = Record<string, any>>(obj: T): T {
mustBeObject(obj)
return objectClone(obj) return objectClone(obj)
} }
@ -103,14 +111,15 @@ export function cloneObject<T = Record<string, any>>(obj: T): T {
* @param obj the object to clone * @param obj the object to clone
*/ */
export function objectClone<T = Record<string, any>>(obj: T): T { export function objectClone<T = Record<string, any>>(obj: T): T {
if (typeof obj !== 'object') { mustBeObject(obj)
const v = obj
return v
}
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
const arr: Array<any> = [] const arr: Array<any> = []
for (const item of obj) { for (const item of obj) {
arr.push(objectClone(item)) if (isObject(item)) {
arr.push(objectClone(item))
} else {
arr.push(item)
}
} }
return arr as unknown as T return arr as unknown as T
} }
@ -136,6 +145,7 @@ export function objectClone<T = Record<string, any>>(obj: T): T {
* @param value the value * @param value the value
*/ */
export function objectSet(obj: Record<string, any>, path: Array<string | number>, value: any): void { export function objectSet(obj: Record<string, any>, path: Array<string | number>, value: any): void {
mustBeObject(obj)
let pointer = obj let pointer = obj
for (let index = 0; index < path.length; index++) { for (let index = 0; index < path.length; index++) {
const key = path[index] const key = path[index]
@ -167,6 +177,8 @@ export function objectSet(obj: Record<string, any>, path: Array<string | number>
* @param y the second object * @param y the second object
*/ */
export function objectEqual(x: Record<string, any>, y: Record<string, any>): boolean { export function objectEqual(x: Record<string, any>, y: Record<string, any>): boolean {
mustBeObject(x)
mustBeObject(y)
if (objectSize(x) !== objectSize(y)) { if (objectSize(x) !== objectSize(y)) {
return false return false
} }
@ -186,6 +198,40 @@ export function objectEqual(x: Record<string, any>, y: Record<string, any>): boo
return res return res
} }
/**
* deeply compare objects and return if they are equal or not
* @param x the first object
* @param y the second object
*/
export function objectClean(obj: Record<string, any>, options?: {cleanUndefined?: boolean, cleanNull?: boolean, deep?: boolean}): void {
mustBeObject(obj)
objectLoop(obj, (item, key) => {
if ((typeof options?.cleanUndefined === 'undefined' || options?.cleanUndefined) && item === undefined) {
delete obj[key]
}
if (options?.cleanNull && item === null) {
delete obj[key]
}
if ((typeof options?.deep === 'undefined' || options?.deep) && isObject(item)) {
return objectClean(item, options)
}
})
}
export function isObject(item: any): item is Record<any, any> {
return typeof item === 'object' && item !== null
}
function mustBeObject(item: any): item is Record<any, any> {
if (isObject(item)) {
return true
} else {
throw new Error("Input is not an object!")
}
}
export default { export default {
objectMap, objectMap,
objectLoop, objectLoop,
@ -196,5 +242,7 @@ export default {
cloneObject, cloneObject,
objectClone, objectClone,
objectSet, objectSet,
objectEqual objectEqual,
objectClean,
isObject
} }