flitbit / diff

Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect result when diff'ing simple array

ParaSwarm opened this issue · comments

I seem to be getting an error from a simple string array comparison. Version 0.3.7.

Ignoring all the redundant DiffEdits, I get a "DiffArray" which shows a new addition of string "USA" to the array, when clearly "USA" exists in both arrays, and item "AXG" was the item added to the edited array.

If this is an actual bug that can be replicated, it seems pretty urgent since it affects the core functionality / purpose of the library.

`

     let lol1 = [
        "ASN", "AST", "AWS", "FIN", "FOR", "GIC", "GIM", "GIN", "GIV", "MIN", "MSI", "PMG", "POC", "POL", "SFO",
        "SWY", "UQE", "USA"
    ];

    let lol2 = [
        "AXG", "ASN", "AST", "AWS", "FIN", "FOR", "GIC", "GIM", "GIN", "GIV", "MIN", "MSI", "PMG", "POC", "POL",
        "SFO", "SWY", "UQE", "USA"
    ];

    let changes = diff(lol1, lol2) as deepDiff.IDiff[];
    console.log(changes);

`

image

This is by design. From the README:

Changes to arrays are recorded simplistically. We care most about the shape of the structure; therefore we don't take the time to determine if an object moved from one slot in the array to another. Instead, we only record the structural differences.

So when the lib looks at index 0, it sees an edit, same all the way to index 17, because the shorter array was 18 items long. Then on index 18, it sees index 18 didn't exist in the previous object and so records a new item, "USA".