Skip to content

Commit

Permalink
馃悶 fix useFieldArray clear error issue (#1099)
Browse files Browse the repository at this point in the history
* fix #1097 with unset issue around array errors

* fix issue around clear array of error

* enhenced version

* imporved version

* improve code

* clean up code and add mmore unit tests

* remove unused import
  • Loading branch information
bluebill1049 committed Feb 26, 2020
1 parent c5d7a6b commit 6372985
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
41 changes: 40 additions & 1 deletion src/utils/unset.test.ts
Expand Up @@ -31,6 +31,45 @@ test('should unset the object', () => {
},
},
});

const test1 = {
test: [{ min: 'required' }],
};

expect(unset(test1, ['test[0].min'])).toEqual({});

const test2 = {
test: {
min: 'test',
},
};

expect(unset(test2, ['test.min'])).toEqual({});

const test3 = {
test: {
bill: {
min: 'test',
},
},
};

expect(unset(test3, ['test.bill.min'])).toEqual({});

const test4 = {
test: {
bill: {
min: [{ test }],
},
test: 'ha',
},
};

expect(unset(test4, ['test.bill.min[0].test'])).toEqual({
test: {
test: 'ha',
},
});
});

test('should unset multiple path', () => {
Expand Down Expand Up @@ -66,7 +105,7 @@ test('should return empty object when inner object is empty object', () => {
},
};

expect(unset(test, ['data.firstName'])).toEqual({ data: {} });
expect(unset(test, ['data.firstName'])).toEqual({});
});

test('should clear empty array', () => {
Expand Down
40 changes: 37 additions & 3 deletions src/utils/unset.ts
Expand Up @@ -2,6 +2,8 @@ import isArray from './isArray';
import isUndefined from './isUndefined';
import isKey from './isKey';
import stringToPath from './stringToPath';
import isEmptyObject from './isEmptyObject';
import isObject from './isObject';

function castPath(value: string) {
return isArray(value) ? value : stringToPath(value);
Expand Down Expand Up @@ -45,10 +47,42 @@ function parent(object: any, path: string | string[]) {

function baseUnset(object: any, path: string) {
const updatePath = isKey(path) ? [path] : castPath(path);
object = parent(object, updatePath);

const childObject = parent(object, updatePath);
const key = updatePath[updatePath.length - 1];
return !(object != null) || delete object[key];
const result = !(childObject != null) || delete childObject[key];
let previousObjRef = undefined;

for (let k = 0; k < updatePath.slice(0, -1).length; k++) {
let index = -1;
let objectRef = undefined;
const currentPaths = updatePath.slice(0, -(k + 1));
const currentPathsLength = currentPaths.length - 1;

if (k > 0) {
previousObjRef = object;
}

while (++index < currentPaths.length) {
const item = currentPaths[index];
objectRef = objectRef ? objectRef[item] : object[item];

if (currentPathsLength === index) {
if (isObject(objectRef) && isEmptyObject(objectRef)) {
previousObjRef ? delete previousObjRef[item] : delete object[item];
} else if (
isArray(objectRef) &&
!objectRef.filter(data => isObject(data) && !isEmptyObject(data))
.length
) {
delete previousObjRef[item];
}
}

previousObjRef = objectRef;
}
}

return result;
}

export default function unset(object: any, paths: string[]) {
Expand Down

0 comments on commit 6372985

Please sign in to comment.