Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions packages/form-core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { defaultValidationLogic } from './ValidationLogic'
import type { ValidationLogicProps } from './ValidationLogic'
import type { FieldValidators } from './FieldApi'
import type { FormValidators } from './FormApi'
Expand Down Expand Up @@ -32,14 +31,15 @@ export function functionalUpdate<TInput, TOutput = TInput>(
* @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)
if (obj === null || obj === undefined) return obj
let current = obj

for (const pathPart of makePathArray(path)) {
if (current === null || current === undefined) return current
current = current[pathPart]
}

return current
}

/**
Expand All @@ -48,13 +48,16 @@ export function getBy(obj: any, path: any) {
*/
export function setBy(obj: any, _path: any, updater: Updater<any>) {
const path = makePathArray(_path)
let pathIndex = 0
const lastPathIndex = path.length - 1

function doSet(parent?: any): any {
if (!path.length) {
if (pathIndex > lastPathIndex) {
return functionalUpdate(updater, parent)
}

const key = path.shift()
const key = path[pathIndex]!
pathIndex++

if (
typeof key === 'string' ||
Expand All @@ -73,16 +76,15 @@ export function setBy(obj: any, _path: any, updater: Updater<any>) {
[key]: doSet(),
}
}

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)
Expand All @@ -94,19 +96,22 @@ export function setBy(obj: any, _path: any, updater: Updater<any>) {
*/
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)
}
const { [finalPath]: remove, ...rest } = parent
return rest
}

const key = path.shift()
const key = path[pathIndex]!
pathIndex++

if (typeof key === 'string') {
if (typeof parent === 'object') {
Expand All @@ -124,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),
]
Expand Down
Loading