Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
98mux opened this issue Nov 11, 2021 · 2 comments
Closed

Comments

@98mux
Copy link

98mux commented Nov 11, 2021

<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

@AsyncBanana
Copy link
Owner

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.

@AsyncBanana
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants