1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-04-22 02:42:13 +00:00

fix: allow to use another type than an object when possible
Some checks are pending
CodeQL / Analyze (javascript) (push) Has started running

Signed-off-by: Avior <git@avior.me>
This commit is contained in:
Florian Bouillon 2024-06-12 12:44:44 +02:00
parent 1dba6cf74e
commit 78f75ec7d1
4 changed files with 10 additions and 6 deletions

View File

@ -515,5 +515,9 @@ describe('object get', () => {
.toEqual({a: 'pouet'})
expect(objectGet({a: 'pouet'}, ''))
.toEqual({a: 'pouet'})
expect(objectGet('pouet', ''))
.toEqual('pouet')
expect(objectGet('pouet', []))
.toEqual('pouet')
})
})

View File

@ -1,12 +1,12 @@
{
"name": "@dzeio/object-util",
"version": "1.8.2",
"version": "1.8.3",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@dzeio/object-util",
"version": "1.8.2",
"version": "1.8.3",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29",

View File

@ -1,6 +1,6 @@
{
"name": "@dzeio/object-util",
"version": "1.8.2",
"version": "1.8.3",
"description": "Utility functions to manipulate an object",
"repository": {
"type": "git",

View File

@ -347,14 +347,14 @@ export function objectFind<T = any, K extends BasicObjectKeys = BasicObjectKeys>
*
* @returns the value if found or undefined if it was not found
*/
export function objectGet<T = any>(obj: object, path: Array<string | number | symbol> | string): T | undefined {
mustBeObject(obj)
export function objectGet<T = any>(obj: any, path: Array<string | number | symbol> | string): T | undefined {
// 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
}
mustBeObject(obj)
// transform path into an Array
if (typeof path === 'string') {
path = path.split('.').map((it) => /^\d+$/g.test(it) ? Number.parseInt(it) : it)