Skip to content

Commit

Permalink
refactor: opt diff
Browse files Browse the repository at this point in the history
  • Loading branch information
gogoyqj committed Apr 4, 2023
1 parent ac4119a commit 649bf99
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/diff/diff.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
/**
* @file: diff and patch
* @author: yangqianjun
* @Date: 2019-12-31 13:48:32
* @LastEditors: yangqianjun
* @LastEditTime: 2020-02-06 11:57:31
*/

// @IMP: f*ck
import * as jsondiffpatch from 'jsondiffpatch';
import lodash from 'lodash';

// @IMP: 确保浏览器和node使用相同的版本
/** ensure node.js and browser side using jsondiffpatch with the same version */
// eslint-disable-next-line @typescript-eslint/no-var-requires
const realDiff: typeof jsondiffpatch = require('../../static/jsondiffpatch.umd');
const splitor = '__@__@__';

export function diffAndPatch(curVersion: {}, newVersion: {}) {
const delta = realDiff.diff(curVersion, newVersion);
return {
/** all changes from curVersion to newVersion evaluated by jsondiffpatch */
delta,
/** 按需更新 */
/** selected changes by keys */
selectDelta: (keys: string[][]) => {
const initialMap: { [key: string]: any } = {};
const setKeyMap: { [key: string]: any } = {};
Expand Down Expand Up @@ -56,10 +48,10 @@ export function diffAndPatch(curVersion: {}, newVersion: {}) {
}, {});
return newDelta;
},
/** 更新 */
patch: (d: typeof delta) => {
if (d) {
realDiff.patch(curVersion, d);
/** patch with selected changes to newVersion */
patch: (changes: typeof delta) => {
if (changes) {
realDiff.patch(curVersion, changes);
return curVersion;
}
return void 0;
Expand Down

0 comments on commit 649bf99

Please sign in to comment.