50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
|
|
export function mergeDiffArray(pristine, current) {
|
|
if (!Array.isArray(pristine)) {
|
|
return current;
|
|
}
|
|
if (pristine.length !== current.length) {
|
|
return current;
|
|
}
|
|
for (let i = 0; i < pristine.length; i++) {
|
|
if (JSON.stringify(current[i]) !== JSON.stringify(pristine[i])) {
|
|
return current;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function mergeDiffObject(pristine, current) {
|
|
if ('object' !== typeof pristine) {
|
|
return current;
|
|
}
|
|
const diff = {};
|
|
for (const i in current) {
|
|
const value = mergeDiff(pristine[i], current[i]);
|
|
if (undefined !== value) {
|
|
diff[i] = value;
|
|
}
|
|
}
|
|
if (0 === Object.keys(diff).length) {
|
|
return undefined;
|
|
}
|
|
return diff;
|
|
}
|
|
|
|
export function mergeDiffPrimitive(pristine, current) {
|
|
if (pristine !== current) {
|
|
return current;
|
|
}
|
|
}
|
|
|
|
export function mergeDiff(pristine, current) {
|
|
if (Array.isArray(current)) {
|
|
return mergeDiffArray(pristine, current);
|
|
}
|
|
else if ('object' === typeof current) {
|
|
return mergeDiffObject(pristine, current);
|
|
}
|
|
else {
|
|
return mergeDiffPrimitive(pristine, current);
|
|
}
|
|
}
|