-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Description
Lines 13 to 43 in be26f98
| function diffValues (left, right, path, patchs) { | |
| // filter functin and null | |
| if (typeof left === 'function' || left === null) { | |
| patchs.push(new Patch(REPLACE, path, right, left)) | |
| } else if (Array.isArray(left)) { | |
| // diff array | |
| if (Array.isArray(right)) { | |
| walkArray(left, right, path, patchs) | |
| } else { | |
| patchs.push(new Patch(REPLACE, path, right, left)) | |
| } | |
| } else if (typeof left === 'object') { | |
| // diff object | |
| if ( | |
| right !== null && | |
| typeof right === 'object' && | |
| !Array.isArray(right) | |
| ) { | |
| // filter Date object | |
| if (left instanceof Date || right instanceof Date) { | |
| patchs.push(new Patch(REPLACE, path, right, left)) | |
| } else { | |
| walkObject(left, right, path, patchs) | |
| } | |
| } else { | |
| patchs.push(new Patch(REPLACE, path, right, left)) | |
| } | |
| } else { | |
| patchs.push(new Patch(REPLACE, path, right, left)) | |
| } | |
| } |
这几行代码没看明白,你是不是这样实现更好。
这里面对array 和 object 考虑不够,
目的是diff ,建议细致思考 Array lenght 或 Object.keys lenght 不一样问题,
const ArrayType = '[object Array]'
const ObjectType = '[object Object]'
function type(obj) {
return Object.prototype.toString.call(obj)
}
function diffValues (left, right, path, patchs) {
if((type(left) === ArrayType && type(right) === ArrayType) || type(right) === ObjectType){
walkObject(left, right, path, patchs)
} else {
patchs.push(new Patch(REPLACE, path, right, left))
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels