natural-compare-lite
Advanced tools
Comparing version 1.1.0 to 1.2.0
28
index.js
@@ -5,4 +5,4 @@ | ||
/* | ||
* @version 1.1.0 | ||
* @date 2014-09-30 | ||
* @version 1.2.0 | ||
* @date 2014-10-02 | ||
* @stability 3 - Stable | ||
@@ -14,27 +14,29 @@ * @author Lauri Rooden (https://github.com/litejs/natural-compare-lite) | ||
String.naturalCompare = function(a, b, alphabet) { | ||
alphabet = String.alphabet | ||
String.naturalCompare = function(a, b) { | ||
function getCode(str, pos, code) { | ||
code = str.charCodeAt(pos) || 0 | ||
return code < 45 || code > 127 ? code | ||
: code < 46 ? 65 | ||
code = alphabet && alphabet.indexOf(str.charAt(pos)) | ||
return code > -1 ? code + 76 : ((code = str.charCodeAt(pos) || 0), code < 45 || code > 127) ? code // | ||
: code < 46 ? 65 // - | ||
: code < 48 ? code - 1 | ||
: code < 58 ? code + 18 | ||
: code < 58 ? code + 18 // 0-9 | ||
: code < 65 ? code - 11 | ||
: code < 91 ? code + 11 | ||
: code < 91 ? code + 11 // A-Z | ||
: code < 97 ? code - 37 | ||
: code < 123 ? code + 5 | ||
: code < 123 ? code + 5 // a-z | ||
: code - 63 | ||
} | ||
if (a != b) for (var i, codeA, codeB = 1, posA = 0, posB = 0; codeB;) { | ||
if ((a+="") != (b+="")) for (var i, codeA, codeB = 1, posA = 0, posB = 0; codeB;) { | ||
codeA = getCode(a, posA++) | ||
codeB = getCode(b, posB++) | ||
if (codeA < 76 && codeA > 65 && codeB < 76 && codeB > 65) { | ||
if (!i && codeA < 76 && codeB < 76 && codeA > 66 && codeB > 66) { | ||
for (i = posA; codeA = getCode(a, posA), codeA < 76 && codeA > 65; posA++); | ||
codeA = (a.slice(i - 1, posA) | 0) + 1 | ||
codeA = +a.slice(i - 1, posA) | ||
for (i = posB; codeB = getCode(b, posB), codeB < 76 && codeB > 65; posB++); | ||
codeB = (b.slice(i - 1, posB) | 0) + 1 | ||
codeB = +b.slice(i - 1, posB) | ||
} | ||
i = codeA == 66 || codeB == 66 | ||
@@ -41,0 +43,0 @@ if (codeA != codeB) return (codeA < codeB) ? -1 : 1 |
/*! litejs.com/MIT-LICENSE.txt */ | ||
String.naturalCompare=function(f,g){function h(b,c,a){a=b.charCodeAt(c)||0;return 45>a||127<a?a:46>a?65:48>a?a-1:58>a?a+18:65>a?a-11:91>a?a+11:97>a?a-37:123>a?a+5:a-63}if(f!=g)for(var k,c,b=1,d=0,e=0;b;){c=h(f,d++);b=h(g,e++);if(76>c&&65<c&&76>b&&65<b){for(k=d;c=h(f,d),76>c&&65<c;d++);c=(f.slice(k-1,d)|0)+1;for(k=e;b=h(g,e),76>b&&65<b;e++);b=(g.slice(k-1,e)|0)+1}if(c!=b)return c<b?-1:1}return 0}; | ||
String.naturalCompare=function(e,f,l){function k(b,c,a){a=l&&l.indexOf(b.charAt(c));return-1<a?a+76:(a=b.charCodeAt(c)||0,45>a||127<a)?a:46>a?65:48>a?a-1:58>a?a+18:65>a?a-11:91>a?a+11:97>a?a-37:123>a?a+5:a-63}l=String.alphabet;if((e+="")!=(f+=""))for(var d,c,b=1,g=0,h=0;b;){c=k(e,g++);b=k(f,h++);if(!d&&76>c&&76>b&&66<c&&66<b){for(d=g;c=k(e,g),76>c&&65<c;g++);c=+e.slice(d-1,g);for(d=h;b=k(f,h),76>b&&65<b;h++);b=+f.slice(d-1,h)}d=66==c||66==b;if(c!=b)return c<b?-1:1}return 0}; |
{ | ||
"name": "natural-compare-lite", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"stability": 3, | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -7,9 +7,6 @@ | ||
[7]: https://ci.testling.com/litejs/natural-compare-lite.png | ||
[8]: https://ci.testling.com/litejs/natural-compare-lite | ||
@version 1.1.0 | ||
@date 2014-09-30 | ||
@version 1.2.0 | ||
@date 2014-10-02 | ||
@stability 3 - Stable | ||
@@ -33,5 +30,5 @@ | ||
String.naturalCompare returns a number indicating | ||
whether a reference string comes before or after or is the same | ||
as the given string in sort order. | ||
String.naturalCompare returns a number indicating | ||
whether a reference string comes before or after or is the same | ||
as the given string in sort order. | ||
Use it with builtin sort() function. | ||
@@ -94,11 +91,23 @@ | ||
- Removes leading zeros so "a 1" and "a 001" are equal. | ||
- Works well with dates in ISO format eg "Rev 2012-07-26.doc". | ||
### Browser Support | ||
### Custom alphabet | ||
[![browser support][7]][8] | ||
It is possible to configure a custom alphabet | ||
to achieve a desired order. | ||
```javascript | ||
// Estonian alphabet | ||
String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy" | ||
["t", "z", "x", "õ"].sort(String.naturalCompare) | ||
// ["z", "t", "õ", "x"] | ||
// Russian alphabet | ||
String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя" | ||
["Ё", "А", "Б"].sort(String.naturalCompare) | ||
// ["А", "Б", "Ё"] | ||
``` | ||
External links | ||
@@ -109,3 +118,3 @@ -------------- | ||
- [Package on npm](https://npmjs.org/package/natural-compare-lite) | ||
- [jsperf test](http://jsperf.com/natural-sort-2/7) | ||
- [jsperf test](http://jsperf.com/natural-sort-2/12) | ||
@@ -112,0 +121,0 @@ |
require("../") | ||
var arr = ["1.001","1.002","1.010","1.02","1.1","1.3"] | ||
require("testman"). | ||
@@ -26,2 +26,3 @@ describe ("String.naturalCompare"). | ||
equal( String.naturalCompare("3", "2"), 1 ). | ||
equal( String.naturalCompare("9", "2"), 1 ). | ||
equal( String.naturalCompare("1", "a"), -1 ). | ||
@@ -41,11 +42,6 @@ equal( String.naturalCompare("a1", "a1"), 0 ). | ||
equal( String.naturalCompare("a1a", "a11a"), -1 ). | ||
equal( String.naturalCompare("a 1", "a 001"), 0 ). | ||
equal( String.naturalCompare("a 1", "a 002"), -1 ). | ||
equal( String.naturalCompare("a 2", "a 001"), 1 ). | ||
it ( "should work with 0 in string" ). | ||
equal( String.naturalCompare("a 0 a", "a 0 b"), -1 ). | ||
equal( String.naturalCompare("a 0 a", "a 00 b"), -1 ). | ||
equal( String.naturalCompare("a 00 a", "a 0 b"), -1 ). | ||
equal( String.naturalCompare("a 0 b", "a 0 a"), 1 ). | ||
equal( String.naturalCompare("a 00 b", "a 0 a"), 1 ). | ||
it ( "should compare positive and negative number" ). | ||
@@ -59,7 +55,13 @@ equal( String.naturalCompare("a 1", "a -1"), 1 ). | ||
equal( String.naturalCompare("a -1", "a -1"), 0 ). | ||
it ( "should preserve leading zeros on decimal fractions.", { skip: "Not implemented" } ). | ||
equal( String.naturalCompare("1.01", "1.001"), -1 ). | ||
equal( String.naturalCompare("1.001", "1.01"), 1 ). | ||
// 1.001 < 1.002 < 1.010 < 1.02 < 1.1 < 1.3 | ||
it ( "should preserve leading zeros on decimal fractions."). | ||
equal( String.naturalCompare("1.01", "1.001"), 1 ). | ||
equal( String.naturalCompare("1.001", "1.01"), -1 ). | ||
equal(arr.sort(String.naturalCompare).join(","), "1.001,1.002,1.010,1.02,1.1,1.3"). | ||
equal(arr.reverse().sort(String.naturalCompare).join(","), "1.001,1.002,1.010,1.02,1.1,1.3"). | ||
it ( "should accept alphabet."). | ||
run(function() { | ||
String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy" | ||
}). | ||
equal(["a", "ä", "B", "Š", "X", "A", "õ", "z", "1", "2", "9", "10"].sort(String.naturalCompare).join(""), "12910ABŠXazõä"). | ||
done() | ||
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
9848
99
125