Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix colliding fuzzy matches #8

Open
wants to merge 2 commits into from

3 participants

@unconed

Diffing the following two arrays shows "c" incorrectly being added to the first object.

a.json:

[
{ "name": "Foo", "a": 3, "b": 1 }
]

b.json

[
{ "name": "Foo", "a": 3, "b": 1 },
{ "name": "Foo", "a": 3, "b": 1, "c": 1 }
]

In scalarize, it maps a target array's objects to matching source objects, but it doesn't check whether the mapping exists already.

    else if fuzzyOriginals && (bestMatch = findMatchingObject(item, index, fuzzyOriginals)) && bestMatch.score > 40
      originals[bestMatch.key] = item;

The proposed change passes all existing mappings into findMatchingObject and does the check there so that an alternate match can be found.

@unconed

After some more real world testing, it seems better to just drop fuzzy match collisions altogether. Otherwise, you risk a low-scoring match hogging an item for which a much higher scoring match exists, and this can cascade down the array to bloat the diff.

@andreyvit
Owner

I see. Can you please add a runnable test for that as well? And yes, as long as the existing tests pass and new tests are added, I'll be happy to merge any improvements. :-)

@unconed

I can't figure out how to run your tests. Either way, I've provided the bug report, the isolated example, and the patch. Do you really need it with a cherry on top too?

@unconed unconed closed this
@andreyvit
Owner

Yes, please. :P

Seriously though, did npm test not work for you? I probably won't have time to update this package soon, so I rely on contributors like you to move it forward.

@andreyvit andreyvit reopened this
@tsibley

Running npm test in a git clone produces errors about not finding ./node_modules/mocha/bin/mocha. Once I install mocha locally with npm install mocha and re-run npm test, I get:

> json-diff@0.3.1 test /Users/trsibley/src/json-diff
> ./node_modules/mocha/bin/mocha

  0 passing (3ms)

which implies it's not running or even finding either of the test scripts. If I try to run mocha directly with ./node_modules/mocha/bin/mocha test/*.coffee, I get errors because mocha 1.7.x doesn't support coffee-script by default or something.

Anyway, after much trial and error, I get npm test working with the changes in PR #10.

@andreyvit
Owner

Hey, so do you guys want to provide a test for this so that the pull request can be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 30, 2014
  1. @unconed

    Fix colliding fuzzy matches

    unconed authored
  2. @unconed
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  lib/index.iced
View
2  lib/index.iced
@@ -60,7 +60,7 @@ scalarize = (array, originals, fuzzyOriginals) ->
for item, index in array
if isScalar item
item
- else if fuzzyOriginals && (bestMatch = findMatchingObject(item, index, fuzzyOriginals)) && bestMatch.score > 40
+ else if fuzzyOriginals && (bestMatch = findMatchingObject(item, index, fuzzyOriginals)) && bestMatch.score > 40 && !originals[bestMatch.key]?
originals[bestMatch.key] = item
bestMatch.key
else
Something went wrong with that request. Please try again.