-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace 'localeCompare' with function independent from locale (#2876)
Fixes #2869
- Loading branch information
1 parent
edbe218
commit 16d2535
Showing
6 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import naturalCompare from '../naturalCompare'; | ||
|
||
describe('naturalCompare', () => { | ||
it('Handles empty strings', () => { | ||
expect(naturalCompare('', '')).to.equal(0); | ||
|
||
expect(naturalCompare('', 'a')).to.equal(-1); | ||
expect(naturalCompare('', '1')).to.equal(-1); | ||
|
||
expect(naturalCompare('a', '')).to.equal(1); | ||
expect(naturalCompare('1', '')).to.equal(1); | ||
}); | ||
|
||
it('Handles strings of different length', () => { | ||
expect(naturalCompare('A', 'A')).to.equal(0); | ||
expect(naturalCompare('A1', 'A1')).to.equal(0); | ||
|
||
expect(naturalCompare('A', 'AA')).to.equal(-1); | ||
expect(naturalCompare('A1', 'A1A')).to.equal(-1); | ||
|
||
expect(naturalCompare('AA', 'A')).to.equal(1); | ||
expect(naturalCompare('A1A', 'A1')).to.equal(1); | ||
}); | ||
|
||
it('Handles numbers', () => { | ||
expect(naturalCompare('0', '0')).to.equal(0); | ||
expect(naturalCompare('1', '1')).to.equal(0); | ||
|
||
expect(naturalCompare('1', '2')).to.equal(-1); | ||
expect(naturalCompare('2', '1')).to.equal(1); | ||
|
||
expect(naturalCompare('2', '11')).to.equal(-1); | ||
expect(naturalCompare('11', '2')).to.equal(1); | ||
}); | ||
|
||
it('Handles numbers with leading zeros', () => { | ||
expect(naturalCompare('00', '00')).to.equal(0); | ||
expect(naturalCompare('0', '00')).to.equal(-1); | ||
expect(naturalCompare('00', '0')).to.equal(1); | ||
|
||
expect(naturalCompare('02', '11')).to.equal(-1); | ||
expect(naturalCompare('11', '02')).to.equal(1); | ||
|
||
expect(naturalCompare('011', '200')).to.equal(-1); | ||
expect(naturalCompare('200', '011')).to.equal(1); | ||
}); | ||
|
||
it('Handles numbers embedded into names', () => { | ||
expect(naturalCompare('a0a', 'a0a')).to.equal(0); | ||
expect(naturalCompare('a0a', 'a9a')).to.equal(-1); | ||
expect(naturalCompare('a9a', 'a0a')).to.equal(1); | ||
|
||
expect(naturalCompare('a00a', 'a00a')).to.equal(0); | ||
expect(naturalCompare('a00a', 'a09a')).to.equal(-1); | ||
expect(naturalCompare('a09a', 'a00a')).to.equal(1); | ||
|
||
expect(naturalCompare('a0a1', 'a0a1')).to.equal(0); | ||
expect(naturalCompare('a0a1', 'a0a9')).to.equal(-1); | ||
expect(naturalCompare('a0a9', 'a0a1')).to.equal(1); | ||
|
||
expect(naturalCompare('a10a11a', 'a10a11a')).to.equal(0); | ||
expect(naturalCompare('a10a11a', 'a10a19a')).to.equal(-1); | ||
expect(naturalCompare('a10a19a', 'a10a11a')).to.equal(1); | ||
|
||
expect(naturalCompare('a10a11a', 'a10a11a')).to.equal(0); | ||
expect(naturalCompare('a10a11a', 'a10a11b')).to.equal(-1); | ||
expect(naturalCompare('a10a11b', 'a10a11a')).to.equal(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Returns a number indicating whether a reference string comes before, or after, | ||
* or is the same as the given string in natural sort order. | ||
* | ||
* See: https://en.wikipedia.org/wiki/Natural_sort_order | ||
* | ||
*/ | ||
export default function naturalCompare(aStr: string, bStr: string): number { | ||
let aIdx = 0; | ||
let bIdx = 0; | ||
|
||
while (aIdx < aStr.length && bIdx < bStr.length) { | ||
let aChar = aStr.charCodeAt(aIdx); | ||
let bChar = bStr.charCodeAt(bIdx); | ||
|
||
if (isDigit(aChar) && isDigit(bChar)) { | ||
let aNum = 0; | ||
do { | ||
++aIdx; | ||
aNum = aNum * 10 + aChar - DIGIT_0; | ||
aChar = aStr.charCodeAt(aIdx); | ||
} while (isDigit(aChar) && aNum > 0); | ||
|
||
let bNum = 0; | ||
do { | ||
++bIdx; | ||
bNum = bNum * 10 + bChar - DIGIT_0; | ||
bChar = bStr.charCodeAt(bIdx); | ||
} while (isDigit(bChar) && bNum > 0); | ||
|
||
if (aNum < bNum) { | ||
return -1; | ||
} | ||
|
||
if (aNum > bNum) { | ||
return 1; | ||
} | ||
} else { | ||
if (aChar < bChar) { | ||
return -1; | ||
} | ||
if (aChar > bChar) { | ||
return 1; | ||
} | ||
++aIdx; | ||
++bIdx; | ||
} | ||
} | ||
|
||
return aStr.length - bStr.length; | ||
} | ||
|
||
const DIGIT_0 = 48; | ||
const DIGIT_9 = 57; | ||
|
||
function isDigit(code: number): boolean { | ||
return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters