From 89a00d05808d279e6459bf34fc9bd07c3a946f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B5=D0=BD=D1=82=D0=B8=D0=BD=20=D0=A1?= =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2?= <62594983+teleskop150750@users.noreply.github.com> Date: Fri, 7 Nov 2025 15:37:01 +0300 Subject: [PATCH 1/4] wip: refactor utils --- packages/form-core/src/utils.ts | 34 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/form-core/src/utils.ts b/packages/form-core/src/utils.ts index 2e8273b5b..2ecad9fe2 100644 --- a/packages/form-core/src/utils.ts +++ b/packages/form-core/src/utils.ts @@ -32,14 +32,14 @@ export function functionalUpdate( * @private */ export function getBy(obj: any, path: any) { - const pathObj = makePathArray(path) - return pathObj.reduce((current: any, pathPart: any) => { - if (current === null) return null - if (typeof current !== 'undefined') { - return current[pathPart] - } - return undefined - }, obj) + let current = obj + + for (const pathPart of makePathArray(path)) { + if (current === null || current === undefined) return current + current = current[pathPart] + } + + return current } /** @@ -48,13 +48,14 @@ export function getBy(obj: any, path: any) { */ export function setBy(obj: any, _path: any, updater: Updater) { const path = makePathArray(_path) + let pathIndex = 0 + const lastPathIndex = path.length - 1 function doSet(parent?: any): any { - if (!path.length) { - return functionalUpdate(updater, parent) - } + if (pathIndex > lastPathIndex) return functionalUpdate(updater, parent) - const key = path.shift() + const key = path[pathIndex]! + pathIndex += 1 if ( typeof key === 'string' || @@ -62,8 +63,11 @@ export function setBy(obj: any, _path: any, updater: Updater) { ) { if (typeof parent === 'object') { if (parent === null) { - parent = {} + return { + [key]: doSet(), + } } + return { ...parent, [key]: doSet(parent[key]), @@ -77,12 +81,12 @@ export function setBy(obj: any, _path: any, updater: Updater) { if (Array.isArray(parent) && typeof key === 'number') { const prefix = parent.slice(0, key) return [ - ...(prefix.length ? prefix : new Array(key)), + ...(prefix.length ? prefix : Array.from({ length: key })), doSet(parent[key]), ...parent.slice(key + 1), ] } - return [...new Array(key), doSet()] + return [...Array.from({ length: key }), doSet()] } return doSet(obj) From f0535eb62ef1a606f081f98a291a28a7fcd6e18a Mon Sep 17 00:00:00 2001 From: teleskop150750 Date: Fri, 7 Nov 2025 16:37:50 +0300 Subject: [PATCH 2/4] chore --- packages/form-core/src/utils.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/form-core/src/utils.ts b/packages/form-core/src/utils.ts index 2ecad9fe2..a66bf98df 100644 --- a/packages/form-core/src/utils.ts +++ b/packages/form-core/src/utils.ts @@ -32,6 +32,7 @@ export function functionalUpdate( * @private */ export function getBy(obj: any, path: any) { + if (obj === null || obj === undefined) return obj let current = obj for (const pathPart of makePathArray(path)) { @@ -52,10 +53,12 @@ export function setBy(obj: any, _path: any, updater: Updater) { const lastPathIndex = path.length - 1 function doSet(parent?: any): any { - if (pathIndex > lastPathIndex) return functionalUpdate(updater, parent) + if (pathIndex > lastPathIndex) { + return functionalUpdate(updater, parent) + } const key = path[pathIndex]! - pathIndex += 1 + pathIndex++ if ( typeof key === 'string' || @@ -63,11 +66,8 @@ export function setBy(obj: any, _path: any, updater: Updater) { ) { if (typeof parent === 'object') { if (parent === null) { - return { - [key]: doSet(), - } + parent = {} } - return { ...parent, [key]: doSet(parent[key]), @@ -77,16 +77,15 @@ export function setBy(obj: any, _path: any, updater: Updater) { [key]: doSet(), } } - if (Array.isArray(parent) && typeof key === 'number') { const prefix = parent.slice(0, key) return [ - ...(prefix.length ? prefix : Array.from({ length: key })), + ...(prefix.length ? prefix : new Array(key)), doSet(parent[key]), ...parent.slice(key + 1), ] } - return [...Array.from({ length: key }), doSet()] + return [...new Array(key), doSet()] } return doSet(obj) From 57985f0268ef9032e055a7cad2ebd0eadf680455 Mon Sep 17 00:00:00 2001 From: teleskop150750 Date: Fri, 7 Nov 2025 16:41:47 +0300 Subject: [PATCH 3/4] chore --- packages/form-core/src/utils.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/form-core/src/utils.ts b/packages/form-core/src/utils.ts index a66bf98df..ff9190a8a 100644 --- a/packages/form-core/src/utils.ts +++ b/packages/form-core/src/utils.ts @@ -1,4 +1,3 @@ -import { defaultValidationLogic } from './ValidationLogic' import type { ValidationLogicProps } from './ValidationLogic' import type { FieldValidators } from './FieldApi' import type { FormValidators } from './FormApi' @@ -80,12 +79,12 @@ export function setBy(obj: any, _path: any, updater: Updater) { if (Array.isArray(parent) && typeof key === 'number') { const prefix = parent.slice(0, key) return [ - ...(prefix.length ? prefix : new Array(key)), + ...(prefix.length ? prefix : Array.from({ length: key })), doSet(parent[key]), ...parent.slice(key + 1), ] } - return [...new Array(key), doSet()] + return [...Array.from({ length: key }), doSet()] } return doSet(obj) From a928619885527bd202913adaaef4da5fd34f7790 Mon Sep 17 00:00:00 2001 From: teleskop150750 Date: Fri, 7 Nov 2025 16:46:41 +0300 Subject: [PATCH 4/4] chore --- packages/form-core/src/utils.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/form-core/src/utils.ts b/packages/form-core/src/utils.ts index ff9190a8a..9c7c289ce 100644 --- a/packages/form-core/src/utils.ts +++ b/packages/form-core/src/utils.ts @@ -96,11 +96,13 @@ export function setBy(obj: any, _path: any, updater: Updater) { */ export function deleteBy(obj: any, _path: any) { const path = makePathArray(_path) + const lastPathIndex = path.length - 1 + let pathIndex = 0 function doDelete(parent: any): any { if (!parent) return - if (path.length === 1) { - const finalPath = path[0]! + if (pathIndex === lastPathIndex) { + const finalPath = path[lastPathIndex]! if (Array.isArray(parent) && typeof finalPath === 'number') { return parent.filter((_, i) => i !== finalPath) } @@ -108,7 +110,8 @@ export function deleteBy(obj: any, _path: any) { return rest } - const key = path.shift() + const key = path[pathIndex]! + pathIndex++ if (typeof key === 'string') { if (typeof parent === 'object') { @@ -126,7 +129,7 @@ export function deleteBy(obj: any, _path: any) { } const prefix = parent.slice(0, key) return [ - ...(prefix.length ? prefix : new Array(key)), + ...(prefix.length ? prefix : Array.from({ length: key })), doDelete(parent[key]), ...parent.slice(key + 1), ]