trekhleb / trekhleb/javascript-algorithms
levenshteinDistance does not support unicode characters
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 197k
- Forks
- 31k
- PR merge metrics
- No merged PRs in 30d
Description
Details:
levenshteinDistance does not support unicode characters outside of the BMP range properly.
Step to reproduce: expect(levenshteinDistance('\u{1f431}', '')).toBe(1) // got 2
Fix
I can issue a PR with the fix https://github.com/dubzzz/javascript-algorithms/commit/856ba360134956cca2bed165929a4d4409537201
How did I find it?
Thanks to property based testing framework fast-check.
The property was the following:
import fc from 'fast-check';
fc.assert(
fc.property(
fc.fullUnicodeString(), fc.fullUnicodeString(),
fc.fullUnicodeString(), fc.fullUnicodeString(),
fc.fullUnicodeString(),
(aBegin, aEnd, bBegin, bEnd, common) =>
levenshteinDistance(aBegin + common + aEnd, bBegin + common + bEnd)
<= Math.max([...aBegin].length, [...bBegin].length) + Math.max([...aEnd].length, [...bEnd].length)
)
)
Or:
for any
aandb- strings
such thata = aBegin + common + aEndandb = bBegin + common + bEnd
levenshteinDistance(a, b)is at mostmax(numChars(aBegin), numChars(bBegin)) + max(numChars(aEnd), numChars(bEnd))
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the levenshteinDistance implementation and its existing tests. Reproduce the astral Unicode case from the issue, then add regression coverage for it and verify that the distance for '\u{1f431}' versus an empty string is 1.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100