mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-22 10:52:11 +00:00
fix(object-util): objectGet with an empty path throwing the incorrect error
Signed-off-by: Avior <git@avior.me>
This commit is contained in:
parent
50710c7816
commit
dc8b25b803
@ -509,4 +509,13 @@ describe('object get', () => {
|
|||||||
expect(objectGet({a: { b: [{ c: 'pouet' }]}}, 'a.c.0'))
|
expect(objectGet({a: { b: [{ c: 'pouet' }]}}, 'a.c.0'))
|
||||||
.toEqual(undefined)
|
.toEqual(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('object get should return undefined', () => {
|
||||||
|
expect(() => {
|
||||||
|
objectGet({a: { b: [{ c: 'pouet' }]}}, [])
|
||||||
|
}).toThrow()
|
||||||
|
expect(() => {
|
||||||
|
objectGet({a: { b: [{ c: 'pouet' }]}}, '')
|
||||||
|
}).toThrow()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -350,6 +350,10 @@ 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) {
|
||||||
|
throw new Error(`Path MUST at least have a value (${path})`)
|
||||||
|
}
|
||||||
|
|
||||||
// transform path into an Array
|
// transform path into an Array
|
||||||
if (typeof path === 'string') {
|
if (typeof path === 'string') {
|
||||||
path = path.split('.').map((it) => /^\d+$/g.test(it) ? Number.parseInt(it) : it)
|
path = path.split('.').map((it) => /^\d+$/g.test(it) ? Number.parseInt(it) : it)
|
||||||
@ -377,7 +381,7 @@ export function objectGet<T = any>(obj: object, path: Array<string | number | sy
|
|||||||
pointer = (pointer as any)[key]
|
pointer = (pointer as any)[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`it should never be here ! (${JSON.stringify(obj)}, ${path}, ${JSON.stringify(pointer)})`)
|
throw new Error(`it should never get there ! (${JSON.stringify(obj)}, ${path}, ${JSON.stringify(pointer)})`)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user