Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Handling of tiny strings not functioning as expected #6

Closed
Jezternz opened this issue Mar 24, 2017 · 5 comments
Closed

Handling of tiny strings not functioning as expected #6

Jezternz opened this issue Mar 24, 2017 · 5 comments
Labels

Comments

@Jezternz
Copy link

For some reason when both strings are 1 or less characters long, compareTwoStrings will return Number.NaN instead of expected 1 or 0.

Examples:

compareTwoStrings("", "") === Number.NaN
compareTwoStrings("a", "a") === Number.NaN
compareTwoStrings("a", "") === Number.NaN
compareTwoStrings("aa", "aa") === 1
compareTwoStrings("aa", "") === 0

This is a problem as Number.NaN is always greater then other numbers,
eg the following will always return false, even though expected true:
compareTwoStrings("", "") > 0.9

I have temporarily got around this in my own project, by simply using:

if(a.length <= 1 && b.length <= 1)return a === b ? 1 : 0;
return compareTwoStrings(a, b);

Cheers,
Josh

@aceakash aceakash added the bug label Mar 24, 2017
@felixfbecker
Copy link

I have the same problem, for two empty (and therefor identical) strings it returns NaN instead of 1.

@aceakash
Copy link
Owner

@felixfbecker @Jezternz thanks for reporting, fixed in 1.2.0

@joaomilho
Copy link

Well, even though compareTwoStrings('h', 'h') is fixed (returns 1), it still seems incorrect:

compareTwoStrings('h', 'hm') // 0

@aceakash
Copy link
Owner

This library implements the Sørensen–Dice coefficient algorithm. It doesn't work for very small strings.

@joaomilho
Copy link

fair enough :D

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

No branches or pull requests

4 participants