Skip to content

code recommend  #1

@noonhorse

Description

@noonhorse

mp-store/src/diff.js

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))
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions