(cherry picked from commit c9ae46a9ad)
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
co-authored by
Ryan McKinley
parent
258f3eae32
commit
fee11227da
@@ -98,6 +98,7 @@ describe('setOptionImmutably', () => {
|
||||
${{}} | ${'a.b[2]'} | ${'x'} | ${{ a: { b: [undefined, undefined, 'x'] } }}
|
||||
${{}} | ${'a[0]'} | ${1} | ${{ a: [1] }}
|
||||
${{}} | ${'a[0].b.c'} | ${1} | ${{ a: [{ b: { c: 1 } }] }}
|
||||
${{ a: [{ b: 1 }] }} | ${'a[0].c'} | ${2} | ${{ a: [{ b: 1, c: 2 }] }}
|
||||
`('property value:${value', ({ source, path, value, expected }) => {
|
||||
expect(setOptionImmutably(source, path, value)).toEqual(expected);
|
||||
});
|
||||
|
||||
@@ -75,6 +75,7 @@ export function setOptionImmutably<T extends object>(options: T, path: string |
|
||||
let current = (options as Record<string, any>)[propKey];
|
||||
const arr = Array.isArray(current) ? [...current] : [];
|
||||
if (splat.length) {
|
||||
current = arr[index];
|
||||
if (current == null || typeof current !== 'object') {
|
||||
current = {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user