Skip to content

Commit

Permalink
fix: smaller utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed May 1, 2023
1 parent f05ab79 commit b347c97
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/form-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export function functionalUpdate<TInput, TOutput = TInput>(
}

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

const intPrefix = '__int__'
const intReplace = `${intPrefix}$1`

function makePathArray(str: string) {
if (typeof str !== 'string') {
throw new Error()
}

return str
.replace('[', '.')
.replace(']', '')
.replace(reFindNumbers0, '__int__$1')
.replace(reFindNumbers1, '.__int__$1.')
.replace(reFindNumbers2, '__int__$1.')
.replace(reFindNumbers3, '.__int__$1')
.replace(reFindNumbers0, intReplace)
.replace(reFindNumbers1, `.${intReplace}.`)
.replace(reFindNumbers2, `${intReplace}.`)
.replace(reFindNumbers3, `.${intReplace}`)
.replace(reFindMultiplePeriods, '.')
.split('.')
.map((d) => {
if (d.indexOf('__int__') === 0) {
return parseInt(d.substring('__int__'.length), 10)
if (d.indexOf(intPrefix) === 0) {
return parseInt(d.substring(intPrefix.length), 10)
}
return d
})
Expand Down

0 comments on commit b347c97

Please sign in to comment.