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:
@ -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)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user