Skip to content

Commit

Permalink
fix: merge custom undefined to target data
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Jun 8, 2022
1 parent ebfe250 commit 2a1d83f
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion packages/http-mock-gen/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,38 @@ const customizer: AssignCustomizer = (value, srcValue) => {
}
};

const mergeUndefined = (temp: any, custom: any) => {
if (lodash.isObject(custom)) {
if (lodash.isArray(custom)) {
custom.map((item, index) => {
if (lodash.isObject(item)) {
mergeUndefined(temp[index], item);
} else {
if (item === undefined) {
temp[index] = undefined;
}
}
});
} else {
Object.keys(custom).map((key) => {
const item = custom[key];
if (lodash.isObject(item)) {
mergeUndefined(temp[key], item);
} else {
if (item === undefined) {
temp[key] = undefined;
}
}
});
}
}
return temp;
};

// 自定义数据和默认数据合并
export const mergeObject = (object: any, source: any) => {
return lodash.mergeWith(object, source, customizer);
const tempData = lodash.mergeWith(lodash.cloneDeep(object), source, customizer);
// 自定义undefined数据覆盖默认数据
const result = mergeUndefined(tempData, object);
return result;
};

0 comments on commit 2a1d83f

Please sign in to comment.