AsyncBanana / microdiff

A fast, zero dependency object and array comparison library. Significantly faster than most other deep comparison libraries and has full TypeScript support.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How about having index path for array be a number instead of a string?

98mux opened this issue · comments

<script>
import diff from "microdiff";

const array1 = [1];
const array2 = [2];
	
	
const obj1 = {0:1};
const obj2 = {0:2};

</script>

{JSON.stringify(diff(array1,array2))}
<br/>
{JSON.stringify(diff(obj1,obj2))}

Both diffs give the same result, which could be problematic:

[{"path":["0"],"type":"CHANGE","value":2}]
[{"path":["0"],"type":"CHANGE","value":2}]

(repl)

My proposal is to differenciate between object and array by having array indexes be a number instead of a string, so that the result would be this instead:

[{"path":[0],"type":"CHANGE","value":2}]
[{"path":["0"],"type":"CHANGE","value":2}]

So the path array will have this type:

 Array<string | number>

This would make it easier to create a patching function too

Good idea! I was planning on adding patching, and this would prevent accidental transformations between an object and an array. I will work on adding this and will hopefully release it in the next release.

In v1.2.0, array paths should be numbers instead of strings. If this does not work, let me know.