mirror of
https://github.com/dzeiocom/libs.git
synced 2025-06-07 08:39:56 +00:00
fix: when path is empty it should return the current object
Signed-off-by: Avior <git@avior.me>
This commit is contained in:
parent
8fc6f39b40
commit
fe2e5a3bdc
@ -511,11 +511,9 @@ describe('object get', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('object get should return undefined', () => {
|
it('object get should return undefined', () => {
|
||||||
expect(() => {
|
expect(objectGet({a: 'pouet'}, []))
|
||||||
objectGet({a: { b: [{ c: 'pouet' }]}}, [])
|
.toEqual({a: 'pouet'})
|
||||||
}).toThrow()
|
expect(objectGet({a: 'pouet'}, ''))
|
||||||
expect(() => {
|
.toEqual({a: 'pouet'})
|
||||||
objectGet({a: { b: [{ c: 'pouet' }]}}, '')
|
|
||||||
}).toThrow()
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -350,8 +350,9 @@ export function objectFind<T = any, K extends BasicObjectKeys = BasicObjectKeys>
|
|||||||
export function objectGet<T = any>(obj: object, path: Array<string | number | symbol> | string): T | undefined {
|
export function objectGet<T = any>(obj: object, path: Array<string | number | symbol> | string): T | undefined {
|
||||||
mustBeObject(obj)
|
mustBeObject(obj)
|
||||||
|
|
||||||
if (path === '' || Array.isArray(path) && path.length === 0) {
|
// if path is not defined or path is empty return the current object
|
||||||
throw new Error(`Path MUST at least have a value (${path})`)
|
if (!path || path === '' || Array.isArray(path) && path.length === 0) {
|
||||||
|
return obj as T
|
||||||
}
|
}
|
||||||
|
|
||||||
// transform path into an Array
|
// transform path into an Array
|
||||||
|
Loading…
x
Reference in New Issue
Block a user