1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-07-25 14:29:51 +00:00

feat(object-util): add an objectGet function

Signed-off-by: Avior <git@avior.me>
This commit is contained in:
2024-06-03 17:30:02 +02:00
parent 3d74438086
commit 0062c02255
2 changed files with 98 additions and 17 deletions

View File

@ -1,6 +1,6 @@
/// <reference types="jest" />
import { isObject, objectClean, objectClone, objectEqual, objectFind, objectKeys, objectLoop, objectMap, objectOmit, objectRemap, objectSet, objectSize, objectSort, objectValues } from '../src/ObjectUtil'
import { isObject, objectClean, objectClone, objectEqual, objectFind, objectGet, objectKeys, objectLoop, objectMap, objectOmit, objectRemap, objectSet, objectSize, objectSort, objectValues } from '../src/ObjectUtil'
describe('Throw if parameter is not an object', () => {
it('should works', () => {
@ -486,3 +486,27 @@ describe('object find', () => {
})).toEqual(undefined)
})
})
describe('object get', () => {
it('should deeply get an object value', () => {
expect(objectGet({a: { b: [{ c: 'pouet' }]}}, ['a', 'b', 0, 'c']))
.toEqual('pouet')
})
it('should deeply get an object value from a string', () => {
expect(objectGet({a: { b: [{ c: 'pouet' }]}}, 'a.b.0.c'))
.toEqual('pouet')
})
it('return undefined if key is too profound', () => {
expect(objectGet({a: { b: [{ c: 'pouet' }]}}, 'a.b.0.c.d'))
.toEqual(undefined)
})
it('return the object if key is too shallow', () => {
expect(objectGet({a: { b: [{ c: 'pouet' }]}}, 'a.b.0'))
.toEqual({ c: 'pouet' })
})
it('return undefined if key is invalid', () => {
expect(objectGet({a: { b: [{ c: 'pouet' }]}}, 'a.c.0'))
.toEqual(undefined)
})
})