mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-22 10:52:11 +00:00
fix: allow to use another type than an object when possible
Some checks are pending
CodeQL / Analyze (javascript) (push) Has started running
Some checks are pending
CodeQL / Analyze (javascript) (push) Has started running
Signed-off-by: Avior <git@avior.me>
This commit is contained in:
parent
1dba6cf74e
commit
78f75ec7d1
@ -515,5 +515,9 @@ describe('object get', () => {
|
|||||||
.toEqual({a: 'pouet'})
|
.toEqual({a: 'pouet'})
|
||||||
expect(objectGet({a: 'pouet'}, ''))
|
expect(objectGet({a: 'pouet'}, ''))
|
||||||
.toEqual({a: 'pouet'})
|
.toEqual({a: 'pouet'})
|
||||||
|
expect(objectGet('pouet', ''))
|
||||||
|
.toEqual('pouet')
|
||||||
|
expect(objectGet('pouet', []))
|
||||||
|
.toEqual('pouet')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
4
packages/object-util/package-lock.json
generated
4
packages/object-util/package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@dzeio/object-util",
|
"name": "@dzeio/object-util",
|
||||||
"version": "1.8.2",
|
"version": "1.8.3",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@dzeio/object-util",
|
"name": "@dzeio/object-util",
|
||||||
"version": "1.8.2",
|
"version": "1.8.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29",
|
"@types/jest": "^29",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@dzeio/object-util",
|
"name": "@dzeio/object-util",
|
||||||
"version": "1.8.2",
|
"version": "1.8.3",
|
||||||
"description": "Utility functions to manipulate an object",
|
"description": "Utility functions to manipulate an object",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -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
|
* @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 {
|
export function objectGet<T = any>(obj: any, path: Array<string | number | symbol> | string): T | undefined {
|
||||||
mustBeObject(obj)
|
|
||||||
|
|
||||||
// if path is not defined or path is empty return the current object
|
// if path is not defined or path is empty return the current object
|
||||||
if (!path || path === '' || Array.isArray(path) && path.length === 0) {
|
if (!path || path === '' || Array.isArray(path) && path.length === 0) {
|
||||||
return obj as T
|
return obj as T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mustBeObject(obj)
|
||||||
|
|
||||||
// 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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user