diff --git a/.changeset/olive-cloths-beam.md b/.changeset/olive-cloths-beam.md new file mode 100644 index 000000000..760963e48 --- /dev/null +++ b/.changeset/olive-cloths-beam.md @@ -0,0 +1,5 @@ +--- +'@tanstack/form-core': patch +--- + +optimize makePathArray util function diff --git a/packages/form-core/src/utils.ts b/packages/form-core/src/utils.ts index 71a6ddb69..071b87427 100644 --- a/packages/form-core/src/utils.ts +++ b/packages/form-core/src/utils.ts @@ -141,17 +141,6 @@ export function deleteBy(obj: any, _path: any) { return doDelete(obj) } -const reLineOfOnlyDigits = /^(\d+)$/gm -// the second dot must be in a lookahead or the engine -// will skip subsequent numbers (like foo.0.1.) -const reDigitsBetweenDots = /\.(\d+)(?=\.)/gm -const reStartWithDigitThenDot = /^(\d+)\./gm -const reDotWithDigitsToEnd = /\.(\d+$)/gm -const reMultipleDots = /\.{2,}/gm - -const intPrefix = '__int__' -const intReplace = `${intPrefix}$1` - /** * @private */ @@ -164,31 +153,16 @@ export function makePathArray(str: string | Array) { throw new Error('Path must be a string.') } - return ( - str - // Leading `[` may lead to wrong parsing down the line - // (Example: '[0][1]' should be '0.1', not '.0.1') - .replace(/(^\[)|]/gm, '') - .replace(/\[/g, '.') - .replace(reLineOfOnlyDigits, intReplace) - .replace(reDigitsBetweenDots, `.${intReplace}.`) - .replace(reStartWithDigitThenDot, `${intReplace}.`) - .replace(reDotWithDigitsToEnd, `.${intReplace}`) - .replace(reMultipleDots, '.') - .split('.') - .map((d) => { - if (d.startsWith(intPrefix)) { - const numStr = d.substring(intPrefix.length) - const num = parseInt(numStr, 10) - - if (String(num) === numStr) { - return num - } - return numStr - } - return d - }) - ) + // Leading `[` may lead to wrong parsing (e.g. '[0][1]' → '0.1', not '.0.1') + return str + .replace(/(^\[)|]/g, '') + .replace(/\[/g, '.') + .replace(/\.{2,}/g, '.') + .split('.') + .map((segment) => { + const num = parseInt(segment, 10) + return String(num) === segment ? num : segment + }) } /**