Skip to content

Commit fd1a660

Browse files
committed
feat(get): update get function
1 parent 5c11574 commit fd1a660

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/formats.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ const DesignTokensType =
2424
}`
2525

2626
const getFunction =
27-
`const get = (obj, path, def) => (() => typeof path === 'string' ? path.replace(/\\[(\\d+)]/g,'.$1') : path.join('.'))()
28-
.split('.')
29-
.filter(Boolean)
30-
.every(step => ((obj = obj[step]) !== undefined)) ? obj : def`
27+
`const get = (obj, path, defValue) => {
28+
if (!path) return undefined
29+
const pathArray = Array.isArray(path) ? path : path.match(/([^[.\\]])+/g)
30+
const result = pathArray.reduce(
31+
(prevObj, key) => prevObj && prevObj[key],
32+
obj
33+
)
34+
return result === undefined ? defValue : result
35+
}`
3136

3237
export const treeWalker = (obj, typing: boolean = true) => {
3338
let type = Object.create(null)

0 commit comments

Comments
 (0)