1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-06-06 08:19:53 +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:
Florian Bouillon 2024-06-10 23:36:53 +02:00
parent 8fc6f39b40
commit fe2e5a3bdc
2 changed files with 7 additions and 8 deletions

View File

@ -511,11 +511,9 @@ describe('object get', () => {
})
it('object get should return undefined', () => {
expect(() => {
objectGet({a: { b: [{ c: 'pouet' }]}}, [])
}).toThrow()
expect(() => {
objectGet({a: { b: [{ c: 'pouet' }]}}, '')
}).toThrow()
expect(objectGet({a: 'pouet'}, []))
.toEqual({a: 'pouet'})
expect(objectGet({a: 'pouet'}, ''))
.toEqual({a: 'pouet'})
})
})

View File

@ -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 {
mustBeObject(obj)
if (path === '' || Array.isArray(path) && path.length === 0) {
throw new Error(`Path MUST at least have a value (${path})`)
// if path is not defined or path is empty return the current object
if (!path || path === '' || Array.isArray(path) && path.length === 0) {
return obj as T
}
// transform path into an Array