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

Implement fix + tests (#14)

Signed-off-by: Florian BOUILLON <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-02-08 10:53:17 +01:00 committed by GitHub
parent ba1f4f441a
commit 980caaf3b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 5 deletions

View File

@ -1,6 +1,6 @@
/// <reference types="jest" /> /// <reference types="jest" />
import { objectSize, objectToArray, objectMap, objectSort, cloneObject, objectEqual, objectKeys, objectSet, objectLoop } from '../src/ObjectUtil' import { objectSize, objectToArray, objectMap, objectSort, cloneObject, objectEqual, objectKeys, objectSet, objectLoop, objectClone } from '../src/ObjectUtil'
describe('Object Map tests', () => { describe('Object Map tests', () => {
it('should works', () => { it('should works', () => {
@ -106,6 +106,34 @@ describe('Object Clone Tests', () => {
clone.toto = 'third' clone.toto = 'third'
expect(clone).not.toEqual(obj) expect(clone).not.toEqual(obj)
}) })
it('Should keep types', () => {
const obj = {
a: [],
b: '10',
c: 10,
d: {},
e: [10],
f: {g: 10}
}
const clone = objectClone(obj)
expect(clone).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)
})
})
}) })
describe('Object Set Tests', () => { describe('Object Set Tests', () => {
@ -171,4 +199,11 @@ describe('Object Equal Test', () => {
it('should not be differently equal', () => { it('should not be differently equal', () => {
expect(objectEqual({pouet: true}, {})).toBe(false) expect(objectEqual({pouet: true}, {})).toBe(false)
}) })
it('should handle every types', () => {
expect(objectEqual({
a: [10, {b: 'c'}], d: '1', e: 2, f: true, g: null, h: undefined
}, {
a: [10, {b: 'c'}], d: '1', e: 2, f: true, g: null, h: undefined
})).toBe(true)
})
}) })

View File

@ -19,10 +19,8 @@ export function objectMap<T = any, J = any>(obj: Record<string, T>, fn: (value:
* @param fn the function to run for each childs * @param fn the function to run for each childs
*/ */
export function objectLoop<T = any>(obj: Record<string, T>, fn: (value: T, key: string) => boolean | void): boolean { export function objectLoop<T = any>(obj: Record<string, T>, fn: (value: T, key: string) => boolean | void): boolean {
for (const key in obj) { const keys = objectKeys(obj)
if (!Object.prototype.hasOwnProperty.call(obj, key)) { for (const key of keys) {
continue
}
const stop = fn(obj[key], key) const stop = fn(obj[key], key)
if (stop === false) { if (stop === false) {
return false return false
@ -87,6 +85,17 @@ 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') {
const v = obj
return v
}
if (Array.isArray(obj)) {
const arr: Array<any> = []
for (const item of obj) {
arr.push(objectClone(item))
}
return arr as unknown as T
}
const clone: Partial<T> = {} const clone: Partial<T> = {}
objectLoop(obj, (value, key) => { objectLoop(obj, (value, key) => {
if (typeof value === 'object' && value != null) { if (typeof value === 'object' && value != null) {