feat: merge and mergeDiff
This commit is contained in:
parent
009e69ffd6
commit
8360da72cb
|
@ -69,5 +69,12 @@ export class TickingPromise extends Promise {
|
|||
}
|
||||
|
||||
export {EventEmitterMixin as EventEmitter} from './event-emitter';
|
||||
export {merge} from './merge';
|
||||
export {
|
||||
mergeDiff,
|
||||
mergeDiffArray,
|
||||
mergeDiffObject,
|
||||
mergeDiffPrimitive,
|
||||
} from './merge-diff';
|
||||
export {fastApply} from './fast-apply';
|
||||
export {PropertyMixin as Property} from './property';
|
||||
|
|
49
packages/core/merge-diff.js
Normal file
49
packages/core/merge-diff.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
10
packages/core/merge.js
Normal file
10
packages/core/merge.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
import mergeWith from 'lodash.mergewith';
|
||||
|
||||
export function merge(...args) {
|
||||
args.push((l, r) => {
|
||||
if (Array.isArray(l)) {
|
||||
return r;
|
||||
}
|
||||
});
|
||||
return mergeWith.apply(null, args);
|
||||
}
|
|
@ -1,7 +1,10 @@
|
|||
{
|
||||
"name": "@avocado/core",
|
||||
"version": "1.0.5",
|
||||
"version": "1.0.6",
|
||||
"main": "index.js",
|
||||
"author": "cha0s",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"lodash.mergewith": "4.6.1"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user