Skip to content

Commit ad2f854

Browse files
committed
perf: avoid redundant traversable check
1 parent 6090fd2 commit ad2f854

File tree

4 files changed

+12
-32
lines changed

4 files changed

+12
-32
lines changed

src/methods/get-field-error.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { FormValue, FormState, FieldPath } from '../types';
2-
import { isTraversable } from '../utils';
32

43
/**
54
* Get the first error for a field (if there is one).
@@ -8,19 +7,15 @@ export function getFieldError<V extends FormValue, P extends FieldPath<V>>(
87
formState: FormState<V>,
98
fieldPath: P,
109
): string | null {
11-
const { value: formValue } = formState;
1210
const { errorFieldPaths } = formState.__internal.fieldStates;
1311

1412
if (errorFieldPaths.has(fieldPath)) {
1513
return errorFieldPaths.get(fieldPath)?.[0] ?? null;
1614
}
1715

18-
// No need to check descendants if the value is not an object or array.
19-
if (isTraversable(formValue)) {
20-
for (const [errorFieldPath, errors] of errorFieldPaths) {
21-
if (errorFieldPath.startsWith(fieldPath)) {
22-
return errors[0] ?? null;
23-
}
16+
for (const [errorFieldPath, errors] of errorFieldPaths) {
17+
if (errorFieldPath.startsWith(fieldPath)) {
18+
return errors[0] ?? null;
2419
}
2520
}
2621

src/methods/get-field-errors.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { FormValue, FormState, FieldPath } from '../types';
2-
import { isTraversable } from '../utils';
32

43
/**
54
* Get all errors for a field.
@@ -8,19 +7,15 @@ export function getFieldErrors<
87
V extends FormValue,
98
P extends FieldPath<V>,
109
>(formState: FormState<V>, fieldPath: P): string[] {
11-
const { value: formValue } = formState;
1210
const { errorFieldPaths } = formState.__internal.fieldStates;
1311

1412
if (errorFieldPaths.has(fieldPath)) {
1513
return errorFieldPaths.get(fieldPath) ?? [];
1614
}
1715

18-
// No need to check descendants if the value is not an object or array.
19-
if (isTraversable(formValue)) {
20-
for (const [errorFieldPath, errors] of errorFieldPaths) {
21-
if (errorFieldPath.startsWith(fieldPath)) {
22-
return errors;
23-
}
16+
for (const [errorFieldPath, errors] of errorFieldPaths) {
17+
if (errorFieldPath.startsWith(fieldPath)) {
18+
return errors;
2419
}
2520
}
2621

src/methods/is-field-dirty.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { FieldPath, FormState, FormValue } from '../types';
2-
import { isTraversable } from '../utils';
32

43
/**
54
* Whether the form field is dirty.
@@ -15,19 +14,15 @@ export function isFieldDirty<V extends FormValue, P extends FieldPath<V>>(
1514
formState: FormState<V>,
1615
fieldPath: P,
1716
): boolean {
18-
const { value: formValue } = formState;
1917
const { dirtyFieldPaths } = formState.__internal.fieldStates;
2018

2119
if (dirtyFieldPaths.has(fieldPath)) {
2220
return true;
2321
}
2422

25-
// No need to check descendants if the value is not an object or array.
26-
if (isTraversable(formValue)) {
27-
for (const dirtyFieldPath of dirtyFieldPaths) {
28-
if (dirtyFieldPath.startsWith(fieldPath)) {
29-
return true;
30-
}
23+
for (const dirtyFieldPath of dirtyFieldPaths) {
24+
if (dirtyFieldPath.startsWith(fieldPath)) {
25+
return true;
3126
}
3227
}
3328

src/methods/is-field-touched.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { FormValue, FormState, FieldPath } from '../types';
2-
import { isTraversable } from '../utils';
32

43
/**
54
* Whether the form field is touched.
@@ -15,19 +14,15 @@ export function isFieldTouched<
1514
V extends FormValue,
1615
P extends FieldPath<V>,
1716
>(formState: FormState<V>, fieldPath: P): boolean {
18-
const { value: formValue } = formState;
1917
const { touchedFieldPaths } = formState.__internal.fieldStates;
2018

2119
if (touchedFieldPaths.has(fieldPath)) {
2220
return true;
2321
}
2422

25-
// No need to check descendants if the value is not an object or array.
26-
if (isTraversable(formValue)) {
27-
for (const touchedFieldPath of touchedFieldPaths) {
28-
if (touchedFieldPath.startsWith(fieldPath)) {
29-
return true;
30-
}
23+
for (const touchedFieldPath of touchedFieldPaths) {
24+
if (touchedFieldPath.startsWith(fieldPath)) {
25+
return true;
3126
}
3227
}
3328

0 commit comments

Comments
 (0)