Skip to content

Commit 9c2f9b4

Browse files
committed
Merge branch 'undefined-default-value' of github.com:LeCarbonator/tanstack-form into undefined-default-value
2 parents 3b07e51 + fb1a06f commit 9c2f9b4

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

packages/form-core/src/utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,24 @@ export function functionalUpdate<TInput, TOutput = TInput>(
3131
* Get a value from an object using a path, including dot notation.
3232
* @private
3333
*/
34-
export function getBy(obj: unknown, path: string | (string | number)[]): { found: boolean; value: any } {
34+
export function getBy(
35+
obj: unknown,
36+
path: string | (string | number)[],
37+
): { found: boolean; value: any } {
3538
const pathObj = makePathArray(path)
3639
let current: unknown = obj
3740
for (const pathPart of pathObj) {
3841
// path is trying to access props of undefined/null, so it doesn't exist
3942
if (typeof current === 'undefined' || current === null) {
4043
return { found: false, value: undefined }
4144
}
42-
if (typeof current === 'object' && (pathPart in current)) {
45+
if (typeof current === 'object' && pathPart in current) {
4346
current = current[pathPart as never]
4447
} else {
4548
return { found: false, value: undefined }
4649
}
4750
}
48-
return { found: true, value: current };
51+
return { found: true, value: current }
4952
}
5053

5154
/**

packages/form-core/tests/utils.spec.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ describe('getBy', () => {
2121
mother: {
2222
name: 'Lisa',
2323
},
24-
siblings: [{
25-
name: undefined
26-
}]
24+
siblings: [
25+
{
26+
name: undefined,
27+
},
28+
],
2729
}
2830

2931
it('should get subfields by path', () => {
30-
const name = getBy(structure, 'name');
32+
const name = getBy(structure, 'name')
3133
expect(name.value).toBe(structure.name)
3234
expect(name.found).toBe(true)
3335

34-
const motherName = getBy(structure, 'mother.name');
36+
const motherName = getBy(structure, 'mother.name')
3537
expect(motherName.value).toBe(structure.mother.name)
3638
expect(motherName.found).toBe(true)
3739
})
@@ -47,22 +49,18 @@ describe('getBy', () => {
4749
})
4850

4951
it('should get nested array subfields by path', () => {
50-
const hobbies0 = getBy(structure, 'kids[0].hobbies[0]');
51-
expect(hobbies0.value).toBe(
52-
structure.kids[0]!.hobbies[0],
53-
)
52+
const hobbies0 = getBy(structure, 'kids[0].hobbies[0]')
53+
expect(hobbies0.value).toBe(structure.kids[0]!.hobbies[0])
5454
expect(hobbies0.found).toBe(true)
5555

56-
const hobbies1 = getBy(structure, 'kids[0].hobbies[1]');
57-
expect(hobbies1.value).toBe(
58-
structure.kids[0]!.hobbies[1],
59-
)
56+
const hobbies1 = getBy(structure, 'kids[0].hobbies[1]')
57+
expect(hobbies1.value).toBe(structure.kids[0]!.hobbies[1])
6058
expect(hobbies1.found).toBe(true)
6159
})
6260

6361
it('should differentiate between explicit undefined vs. no path', () => {
64-
const sibling0 = getBy(structure, 'siblings[0].name');
65-
const sibling1 = getBy(structure, 'siblings[1].name');
62+
const sibling0 = getBy(structure, 'siblings[0].name')
63+
const sibling1 = getBy(structure, 'siblings[1].name')
6664

6765
expect(sibling0.value).toBeUndefined()
6866
expect(sibling1.value).toBeUndefined()

0 commit comments

Comments
 (0)