Skip to content

Commit b347c97

Browse files
committed
fix: smaller utils
1 parent f05ab79 commit b347c97

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

packages/form-core/src/utils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ export function functionalUpdate<TInput, TOutput = TInput>(
1414
}
1515

1616
export function getBy(obj: any, path: any) {
17-
if (!path) {
18-
throw new Error('A path string is required to use getBy')
19-
}
2017
const pathArray = makePathArray(path)
2118
const pathObj = pathArray
2219
return pathObj.reduce((current: any, pathPart: any) => {
@@ -73,19 +70,26 @@ const reFindNumbers2 = /^(\d*)\./gm
7370
const reFindNumbers3 = /\.(\d*$)/gm
7471
const reFindMultiplePeriods = /\.{2,}/gm
7572

73+
const intPrefix = '__int__'
74+
const intReplace = `${intPrefix}$1`
75+
7676
function makePathArray(str: string) {
77+
if (typeof str !== 'string') {
78+
throw new Error()
79+
}
80+
7781
return str
7882
.replace('[', '.')
7983
.replace(']', '')
80-
.replace(reFindNumbers0, '__int__$1')
81-
.replace(reFindNumbers1, '.__int__$1.')
82-
.replace(reFindNumbers2, '__int__$1.')
83-
.replace(reFindNumbers3, '.__int__$1')
84+
.replace(reFindNumbers0, intReplace)
85+
.replace(reFindNumbers1, `.${intReplace}.`)
86+
.replace(reFindNumbers2, `${intReplace}.`)
87+
.replace(reFindNumbers3, `.${intReplace}`)
8488
.replace(reFindMultiplePeriods, '.')
8589
.split('.')
8690
.map((d) => {
87-
if (d.indexOf('__int__') === 0) {
88-
return parseInt(d.substring('__int__'.length), 10)
91+
if (d.indexOf(intPrefix) === 0) {
92+
return parseInt(d.substring(intPrefix.length), 10)
8993
}
9094
return d
9195
})

0 commit comments

Comments
 (0)