string-natural-compare
Advanced tools
Comparing version 0.1.0 to 1.0.0
16
index.js
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 0.1.0 | ||
* @version 1.0.0 | ||
* @copyright 2015 Nathan Woltman | ||
@@ -17,4 +17,4 @@ * @license MIT https://github.com/woollybogger/string-natural-compare/blob/master/LICENSE.txt | ||
function isNumber(c) { | ||
return c >= '0' && c <= '9'; | ||
function isNumberCode(code) { | ||
return code >= 48 && code <= 57; | ||
} | ||
@@ -34,4 +34,4 @@ | ||
if (isNumber(charA)) { | ||
if (!isNumber(charB)) { | ||
if (isNumberCode(charA.charCodeAt(0))) { | ||
if (!isNumberCode(charB.charCodeAt(0))) { | ||
return charA < charB ? -1 : 1; | ||
@@ -49,6 +49,6 @@ } | ||
while (numEndA < lengthA && isNumber(a[numEndA])) { | ||
while (numEndA < lengthA && isNumberCode(a.charCodeAt(numEndA))) { | ||
++numEndA; | ||
} | ||
while (numEndB < lengthB && isNumber(b[numEndB])) { | ||
while (numEndB < lengthB && isNumberCode(b.charCodeAt(numEndB))) { | ||
++numEndB; | ||
@@ -126,3 +126,3 @@ } | ||
value: function(a, b) { | ||
return naturalCompare((a + '').toLowerCase(), (b + '').toLowerCase()); | ||
return naturalCompare(('' + a).toLowerCase(), ('' + b).toLowerCase()); | ||
}, | ||
@@ -129,0 +129,0 @@ configurable: true, |
{ | ||
"name": "string-natural-compare", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Compare alphanumeric strings the same way a human would, using a natural order algorithm", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
# Natural Compare | ||
# String Natural Compare | ||
@@ -9,6 +9,28 @@ Compare alphanumeric strings the same way a human would, using a natural order algorithm | ||
[![Coverage Status](https://coveralls.io/repos/woollybogger/string-natural-compare/badge.svg?branch=master)](https://coveralls.io/r/woollybogger/string-natural-compare?branch=master) | ||
[![Dependency Status](https://david-dm.org/woollybogger/string-natural-compare.svg)](https://david-dm.org/woollybogger/string-natural-compare) | ||
[![devDependency Status](https://david-dm.org/woollybogger/string-natural-compare/dev-status.svg)](https://david-dm.org/woollybogger/string-natural-compare#info=devDependencies) | ||
``` | ||
Standard sorting: Natural order sorting: | ||
img1.png img1.png | ||
img10.png img2.png | ||
img12.png img10.png | ||
img2.png img12.png | ||
``` | ||
This module makes two functions available on the global `String` object: | ||
+ `String.naturalCompare` (case-sensitive) | ||
+ `String.naturalCaseCompare` (case-insensitive) | ||
These functions return a number indicating whether one string should come before, after, or is the same as another string. | ||
They can be easily used with the native [`.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) array method. | ||
### Fast and Robust | ||
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. | ||
+ [jsPerf - natsort()](http://jsperf.com/natsort/2) | ||
+ [jsPerf - natsort() with custom alphabet](http://jsperf.com/natsort-custom-alphabet) | ||
## Installation | ||
@@ -30,4 +52,2 @@ | ||
Get the package: | ||
```sh | ||
@@ -41,11 +61,9 @@ bower install string-natural-compare | ||
<script src="/bower_components/string-natural-compare/dist/natural-compare.min.js"></script> | ||
``` | ||
``` | ||
#### Plain HTML: | ||
**Note:** IE8 and lower not supported. | ||
[Download](https://rawgit.com/woollybogger/string-natural-compare/master/dist/natural-compare.min.js) the script and include it in your page: | ||
#### Download: | ||
```html | ||
<script src="natural-compare.min.js"></script> | ||
``` | ||
Production and development versions can be found [here](https://github.com/woollybogger/string-natural-compare/tree/master/dist) and can be included in your page similar to the HTML example above. | ||
@@ -55,9 +73,2 @@ | ||
Natural Compare makes two functions available on the global `String` object: | ||
+ `String.naturalCompare` (case-sensitive) | ||
+ `String.naturalCaseCompare` (case-insensitive) | ||
Examples: | ||
```js | ||
@@ -64,0 +75,0 @@ // Simple case-sensitive sorting |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10065
0
138