Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

string-natural-compare

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-natural-compare - npm Package Compare versions

Comparing version 1.1.1 to 2.0.0

README.md

2

LICENSE.txt
The MIT License (MIT)
Copyright (c) 2015 Nathan Woltman
Copyright (c) 2015-2016 Nathan Woltman

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

@@ -1,141 +0,111 @@

/**
* Natural Compare
* https://github.com/woollybogger/string-natural-compare
*
* @version 1.1.1
* @copyright 2015 Nathan Woltman
* @license MIT https://github.com/woollybogger/string-natural-compare/blob/master/LICENSE.txt
*/
'use strict';
(function() {
'use strict';
var alphabet;
var alphabetIndexMap;
var alphabetIndexMapLength = 0;
var alphabet;
var alphabetIndexMap;
var alphabetIndexMapLength = 0;
function isNumberCode(code) {
return code >= 48 && code <= 57;
}
function isNumberCode(code) {
return code >= 48 && code <= 57;
}
function naturalCompare(a, b) {
var lengthA = (a += '').length;
var lengthB = (b += '').length;
var aIndex = 0;
var bIndex = 0;
function naturalCompare(a, b) {
var lengthA = (a += '').length;
var lengthB = (b += '').length;
var aIndex = 0;
var bIndex = 0;
var alphabetIndexA;
var alphabetIndexB;
while (aIndex < lengthA && bIndex < lengthB) {
var charCodeA = a.charCodeAt(aIndex);
var charCodeB = b.charCodeAt(bIndex);
while (aIndex < lengthA && bIndex < lengthB) {
var charCodeA = a.charCodeAt(aIndex);
var charCodeB = b.charCodeAt(bIndex);
if (isNumberCode(charCodeA)) {
if (!isNumberCode(charCodeB)) {
return charCodeA - charCodeB;
}
if (isNumberCode(charCodeA)) {
if (!isNumberCode(charCodeB)) {
return charCodeA - charCodeB;
}
var numStartA = aIndex;
var numStartB = bIndex;
var numStartA = aIndex;
var numStartB = bIndex;
while (charCodeA === 48 && ++numStartA < lengthA) {
charCodeA = a.charCodeAt(numStartA);
}
while (charCodeB === 48 && ++numStartB < lengthB) {
charCodeB = b.charCodeAt(numStartB);
}
while (charCodeA === 48 && ++numStartA < lengthA) {
charCodeA = a.charCodeAt(numStartA);
}
while (charCodeB === 48 && ++numStartB < lengthB) {
charCodeB = b.charCodeAt(numStartB);
}
var numEndA = numStartA;
var numEndB = numStartB;
var numEndA = numStartA;
var numEndB = numStartB;
while (numEndA < lengthA && isNumberCode(a.charCodeAt(numEndA))) {
++numEndA;
}
while (numEndB < lengthB && isNumberCode(b.charCodeAt(numEndB))) {
++numEndB;
}
while (numEndA < lengthA && isNumberCode(a.charCodeAt(numEndA))) {
++numEndA;
}
while (numEndB < lengthB && isNumberCode(b.charCodeAt(numEndB))) {
++numEndB;
}
var difference = numEndA - numStartA - numEndB + numStartB; // numA length - numB length
if (difference) {
return difference;
}
var numLengthA = numEndA - numStartA;
var numLengthB = numEndB - numStartB;
if (numLengthA < numLengthB) {
return -1;
while (numStartA < numEndA) {
difference = a.charCodeAt(numStartA++) - b.charCodeAt(numStartB++);
if (difference) {
return difference;
}
if (numLengthA > numLengthB) {
return 1;
}
if (numLengthA) {
var numA = a.slice(numStartA, numEndA);
var numB = b.slice(numStartB, numEndB);
if (numA < numB) {
return -1;
}
if (numA > numB) {
return 1;
}
}
aIndex = numEndA;
bIndex = numEndB;
continue;
}
if (charCodeA !== charCodeB) {
if (
alphabetIndexMapLength &&
charCodeA < alphabetIndexMapLength &&
charCodeB < alphabetIndexMapLength &&
(alphabetIndexA = alphabetIndexMap[charCodeA]) !== -1 &&
(alphabetIndexB = alphabetIndexMap[charCodeB]) !== -1
) {
return alphabetIndexA - alphabetIndexB;
}
aIndex = numEndA;
bIndex = numEndB;
continue;
}
return charCodeA - charCodeB;
if (charCodeA !== charCodeB) {
if (
charCodeA < alphabetIndexMapLength &&
charCodeB < alphabetIndexMapLength &&
alphabetIndexMap[charCodeA] !== -1 &&
alphabetIndexMap[charCodeB] !== -1
) {
return alphabetIndexMap[charCodeA] - alphabetIndexMap[charCodeB];
}
++aIndex;
++bIndex;
return charCodeA - charCodeB;
}
return lengthA - lengthB;
++aIndex;
++bIndex;
}
Object.defineProperties(String, {
alphabet: {
get: function() {
return alphabet;
},
set: function(value) {
alphabet = value;
alphabetIndexMap = [];
var i = 0;
if (alphabet) {
for (; i < alphabet.length; i++) {
alphabetIndexMap[alphabet.charCodeAt(i)] = i;
}
return lengthA - lengthB;
}
naturalCompare.caseInsensitive = naturalCompare.i = function(a, b) {
return naturalCompare(('' + a).toLowerCase(), ('' + b).toLowerCase());
};
Object.defineProperties(naturalCompare, {
alphabet: {
get: function() {
return alphabet;
},
set: function(value) {
alphabet = value;
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;
}
alphabetIndexMapLength = alphabetIndexMap.length;
for (i = 0; i < alphabetIndexMapLength; i++) {
if (alphabetIndexMap[i] === undefined) {
alphabetIndexMap[i] = -1;
}
},
}
},
naturalCompare: {
value: naturalCompare,
configurable: true,
writable: true,
},
naturalCaseCompare: {
value: function(a, b) {
return naturalCompare(('' + a).toLowerCase(), ('' + b).toLowerCase());
},
configurable: true,
writable: true,
},
});
},
});
})();
module.exports = naturalCompare;
{
"name": "string-natural-compare",
"version": "1.1.1",
"version": "2.0.0",
"description": "Compare alphanumeric strings the same way a human would, using a natural order algorithm",
"author": "Nathan Woltman <nwoltman@outlook.com>",
"license": "MIT",
"main": "natural-compare.js",
"files": [
"natural-compare.js",
"LICENSE.txt"
],
"repository": {
"type": "git",
"url": "https://github.com/nwoltman/string-natural-compare"
},
"bugs": {
"url": "https://github.com/nwoltman/string-natural-compare/issues"
},
"homepage": "https://github.com/nwoltman/string-natural-compare",
"devDependencies": {
"coveralls": "^2.11.9",
"grunt": "~1.0.1",
"grunt-eslint": "^18.0.0",
"grunt-mocha-istanbul": "^4.0.2",
"grunt-mocha-test": "^0.12.7",
"istanbul": "^0.4.3",
"jit-grunt": "^0.10.0",
"mocha": "^2.3.2",
"should": "^8.0.2"
},
"scripts": {
"test": "grunt"
},
"repository": {
"type": "git",
"url": "https://github.com/woollybogger/string-natural-compare"
},
"keywords": [

@@ -20,5 +41,3 @@ "string",

"natcmp",
"natcomp",
"strnatcmp",
"strnatcomp",
"sort",

@@ -28,27 +47,3 @@ "natsort",

"alphanumeric"
],
"author": "Nathan Woltman <nwoltman@outlook.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/woollybogger/string-natural-compare/issues"
},
"homepage": "https://github.com/woollybogger/string-natural-compare",
"config": {
"blanket": {
"pattern": [
"string-natural-compare/natural-compare.js"
]
}
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-jshint": "^0.11.2",
"grunt-contrib-uglify": "0.9.1",
"grunt-jscs": "~2.0.0",
"grunt-jsonlint": "^1.0.4",
"grunt-mocha-cov": "~0.4.0",
"mocha": "^2.2.5",
"should": "^7.0.2"
}
]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc