string-natural-compare
Advanced tools
Comparing version 1.0.0 to 1.1.0
76
index.js
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 1.0.0 | ||
* @version 1.1.0 | ||
* @copyright 2015 Nathan Woltman | ||
@@ -16,2 +16,3 @@ * @license MIT https://github.com/woollybogger/string-natural-compare/blob/master/LICENSE.txt | ||
var alphabetIndexMap; | ||
var alphabetIndexMapLength = 0; | ||
@@ -25,21 +26,25 @@ function isNumberCode(code) { | ||
var lengthB = (b += '').length; | ||
var i = 0; | ||
var j = 0; | ||
var aIndex = 0; | ||
var bIndex = 0; | ||
var alphabetIndexA; | ||
var alphabetIndexB; | ||
while (i < lengthA && j < lengthB) { | ||
var charA = a[i]; | ||
var charB = b[j]; | ||
while (aIndex < lengthA && bIndex < lengthB) { | ||
var charCodeA = a.charCodeAt(aIndex); | ||
var charCodeB = b.charCodeAt(bIndex); | ||
if (isNumberCode(charA.charCodeAt(0))) { | ||
if (!isNumberCode(charB.charCodeAt(0))) { | ||
return charA < charB ? -1 : 1; | ||
if (isNumberCode(charCodeA)) { | ||
if (!isNumberCode(charCodeB)) { | ||
return charCodeA - charCodeB; | ||
} | ||
var numStartA = i; | ||
var numStartB = j; | ||
var numStartA = aIndex; | ||
var numStartB = bIndex; | ||
while (a[numStartA] === '0' && ++numStartA < lengthA); | ||
while (b[numStartB] === '0' && ++numStartB < lengthB); | ||
while (charCodeA === 48 && ++numStartA < lengthA) { | ||
charCodeA = a.charCodeAt(numStartA); | ||
} | ||
while (charCodeB === 48 && ++numStartB < lengthB) { | ||
charCodeB = b.charCodeAt(numStartB); | ||
} | ||
@@ -78,23 +83,23 @@ var numEndA = numStartA; | ||
i = numEndA; | ||
j = numEndB; | ||
aIndex = numEndA; | ||
bIndex = numEndB; | ||
continue; | ||
} | ||
if ( | ||
alphabet && | ||
(alphabetIndexA = alphabetIndexMap[charA]) !== undefined && | ||
(alphabetIndexB = alphabetIndexMap[charB]) !== undefined | ||
) { | ||
if ((alphabetIndexA -= alphabetIndexB)) { | ||
return alphabetIndexA; | ||
if (charCodeA !== charCodeB) { | ||
if ( | ||
alphabetIndexMapLength && | ||
charCodeA < alphabetIndexMapLength && | ||
charCodeB < alphabetIndexMapLength && | ||
(alphabetIndexA = alphabetIndexMap[charCodeA]) !== -1 && | ||
(alphabetIndexB = alphabetIndexMap[charCodeB]) !== -1 | ||
) { | ||
return alphabetIndexA - alphabetIndexB; | ||
} | ||
} else if (charA < charB) { | ||
return -1; | ||
} else if (charA > charB) { | ||
return 1; | ||
return charCodeA - charCodeB; | ||
} | ||
++i; | ||
++j; | ||
++aIndex; | ||
++bIndex; | ||
} | ||
@@ -112,7 +117,14 @@ | ||
alphabet = value; | ||
alphabetIndexMap = {}; | ||
if (!alphabet) return; | ||
for (var i = 0; i < alphabet.length; i++) { | ||
alphabetIndexMap[alphabet[i]] = i; | ||
alphabetIndexMap = []; | ||
var i = 0; | ||
if (alphabet) { | ||
for (; i < alphabet.length; i++) { | ||
alphabetIndexMap[alphabet.charCodeAt(i)] = i; | ||
} | ||
} | ||
alphabetIndexMapLength = alphabetIndexMap.length; | ||
for (i = 0; i < alphabetIndexMapLength; i++) { | ||
if (i in alphabetIndexMap) continue; | ||
alphabetIndexMap[i] = -1; | ||
} | ||
} | ||
@@ -119,0 +131,0 @@ }, |
{ | ||
"name": "string-natural-compare", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Compare alphanumeric strings the same way a human would, using a natural order algorithm", | ||
@@ -50,5 +50,5 @@ "main": "index.js", | ||
"grunt-mocha-cov": "~0.4.0", | ||
"mocha": "^2.2.4", | ||
"mocha": "^2.2.5", | ||
"should": "^6.0.1" | ||
} | ||
} |
@@ -29,3 +29,3 @@ # String Natural Compare | ||
This module uses an extremely performant and robust algorithm to compare alphanumeric strings. It does not convert numeric substrings into JavaScript numbers, so it can compare strings containing very large numeric substrings (i.e. exceeding what can be contained in a 64-bit integer). The algorithm has been optimized to be very fast, even when a [custom alphabet](#custom-alphabet) has been configured. | ||
This module uses a performant and robust algorithm to compare alphanumeric strings. It does not convert numeric substrings into JavaScript numbers, so it can compare strings containing very large numeric substrings (i.e. exceeding what can be contained in a 64-bit integer). The algorithm has been optimized to be very fast, even when a [custom alphabet](#custom-alphabet) has been configured. | ||
@@ -84,6 +84,6 @@ + [jsPerf - natsort()](http://jsperf.com/natsort/2) | ||
// Compare very large numbers as strings (or strings containing long, numeric substrings) | ||
// Compare strings containing large numbers | ||
String.naturalCompare( | ||
'1165874568735487968325787328996865', | ||
'1165874568735487968325787328996864' | ||
'265812277985321589735871687040841' | ||
); | ||
@@ -99,5 +99,8 @@ // -> 1 | ||
// Sort by street, then by room | ||
// Sort by street (case-insensitive), then by room (case-sensitive) | ||
a.sort(function(a, b) { | ||
return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room); | ||
return ( | ||
String.naturalCompare(a.street.toLowerCase(), b.street.toLowerCase()) || | ||
String.naturalCompare(a.room, b.room) | ||
); | ||
}); | ||
@@ -117,6 +120,6 @@ | ||
a = a.map(function(car) { | ||
car.sort_key = (car.make + ' ' + car.model).toLowerCase(); | ||
car.sortKey = (car.make + ' ' + car.model).toLowerCase(); | ||
}); | ||
a.sort(function(a, b) { | ||
return String.naturalCompare(a.sort_key, b.sort_key); | ||
return String.naturalCompare(a.sortKey, b.sortKey); | ||
}); | ||
@@ -137,6 +140,6 @@ ``` | ||
String.alphabet = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя'; | ||
['Ё', 'А', 'Б'].sort(String.naturalCompare); | ||
// -> ['А', 'Б', 'Ё'] | ||
['Ё', 'А', 'б', 'Б'].sort(String.naturalCompare); | ||
// -> ['А', 'Б', 'Ё', 'б'] | ||
``` | ||
**Note:** Putting numbers in the custom alphabet can cause undefined behaviour. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10593
120
141