cfinke / Typo.js

A client-side JavaScript spellchecker that uses Hunspell-style dictionaries.
Other
501 stars 110 forks source link

Consistently sort suggestions across javascript engines #71

Closed SLKnutson closed 4 years ago

SLKnutson commented 4 years ago

In existing code, different javascript engines (versions of node, or different browsers) would return different suggestion orders for suggestions whose scores were tied.

One example word that exhibits this behavior with the built in dictionary is "spellling". When run through https://www.chrisfinke.com/files/typo-demo/ Suggestions in chrome: spelling, spilling, swelling, smelling, shelling Suggestions in firefox: spelling, spilling, selling, shelling, smelling

I traced this back to the sorter function, which was returning 1 in the event that the two suggestions had the same score. In the event of a tie, comparators are generally supposed to return 0. Returning 0 is not enough to get the same result on all javascript engines because only recently were browsers forced to sort stable (in the event of a tie, return the items in the same relative order as they were in the original list). So to ensure that the ordering is always the same, I arbitrarily used localeCompare on the words to break ties.

Thanks!

cfinke commented 4 years ago

Thanks for the patch; I've given you access to the repository so you can push this change and any others you deem helpful.

SLKnutson commented 4 years ago

Thanks! Is there any way we can publish these changes to npm?

cfinke commented 4 years ago

I've published the current state as version 1.1 on npm: https://www.npmjs.com/package/typo-js