Skip to content

Commit b6b68d0

Browse files
committed
Light refactoring
1 parent f6c4d86 commit b6b68d0

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/index.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ function toeObj<TData extends Record<string, any>>(
3737
for (const key of Object.keys(data)) {
3838
const value = data[key];
3939
if (keys.includes(key)) {
40-
// Guaranteed to have at least one entry
41-
const filteredErrors = errors.filter((e) => e.path[depth] === key);
42-
43-
if (value === null) {
44-
// CONSIDER: error wrap? E.g. so it's `instanceof Error`?
45-
const error = filteredErrors[0];
40+
if (value == null) {
41+
const error = errors.find((e) => e.path[depth] === key);
4642
// This is where the error is!
4743
// obj[key] = value;
4844
Object.defineProperty(obj, key, {
@@ -52,12 +48,12 @@ function toeObj<TData extends Record<string, any>>(
5248
},
5349
});
5450
} else {
51+
// Guaranteed to have at least one entry
52+
const filteredErrors = errors.filter((e) => e.path[depth] === key);
5553
// Recurse
56-
if (Array.isArray(value)) {
57-
obj[key] = toeArr(value, depth + 1, filteredErrors) as any;
58-
} else {
59-
obj[key] = toeObj(value, depth + 1, filteredErrors);
60-
}
54+
obj[key] = Array.isArray(value)
55+
? (toeArr(value, depth + 1, filteredErrors) as any)
56+
: toeObj(value, depth + 1, filteredErrors);
6157
}
6258
} else {
6359
obj[key] = value;
@@ -77,12 +73,8 @@ function toeArr<TData>(
7773
for (let index = 0, l = data.length; index < l; index++) {
7874
const value = data[index];
7975
if (keys.includes(index)) {
80-
// Guaranteed to have at least one entry
81-
const filteredErrors = errors.filter((e) => e.path[depth] === index);
82-
83-
if (value === null) {
84-
// CONSIDER: error wrap? E.g. so it's `instanceof Error`?
85-
const error = filteredErrors[0];
76+
if (value == null) {
77+
const error = errors.find((e) => e.path[depth] === index);
8678
// This is where the error is!
8779
// arr[index] = value;
8880
Object.defineProperty(arr, index, {
@@ -92,12 +84,12 @@ function toeArr<TData>(
9284
},
9385
});
9486
} else {
87+
// Guaranteed to have at least one entry
88+
const filteredErrors = errors.filter((e) => e.path[depth] === index);
9589
// Recurse
96-
if (Array.isArray(value)) {
97-
arr[index] = toeArr(value, depth + 1, filteredErrors);
98-
} else {
99-
arr[index] = toeObj(value as any, depth + 1, filteredErrors);
100-
}
90+
arr[index] = Array.isArray(value)
91+
? toeArr(value, depth + 1, filteredErrors)
92+
: toeObj(value as any, depth + 1, filteredErrors);
10193
}
10294
} else {
10395
arr[index] = value;

0 commit comments

Comments
 (0)