From e6868888186b5bddc83939fc778ad0567f6e8078 Mon Sep 17 00:00:00 2001 From: Dumitru Uzun Date: Thu, 22 Oct 2015 23:55:50 +0300 Subject: [PATCH 1/2] AMD and CommonJs support - Universal Module Definition pattern --- naturalSort.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/naturalSort.js b/naturalSort.js index 8bdc024..4321e96 100644 --- a/naturalSort.js +++ b/naturalSort.js @@ -2,6 +2,21 @@ * Natural Sort algorithm for Javascript - Version 0.8.1 - Released under MIT license * Author: Jim Palmer (based on chunking idea from Dave Koelle) */ +;(function (name, global, undefined) { + var UNDEFINED = undefined + '' + , FUNCTION = 'function' + ; +( + typeof define !== FUNCTION || !define.amd ? typeof module != UNDEFINED && module.exports + // CommonJS + ? function (deps, factory) { module.exports = factory(); } + // Browser + : function (deps, factory) { global[name] = factory(); } + // AMD + : define +) +/*define*/([], function factory() { + function naturalSort (a, b) { var re = /(^([+\-]?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?(?=\D|\s|$))|^0x[\da-fA-F]+$|\d+)/g, sre = /^\s+|\s+$/g, // trim pre-post whitespace @@ -48,3 +63,8 @@ function naturalSort (a, b) { else if (oFxNcL > oFyNcL) { return 1; } } } + + return naturalSort; +}); +} +('naturalSort', typeof self == 'undefined' ? typeof global == 'undefined' ? this : global : self)); From df9634cadc598509771bbc30fcbc64c8e3075a16 Mon Sep 17 00:00:00 2001 From: Dumitru Uzun Date: Fri, 23 Oct 2015 00:17:08 +0300 Subject: [PATCH 2/2] Declare regexps and functions outside of naturalSort() - speed increased by ~12% --- naturalSort.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/naturalSort.js b/naturalSort.js index 4321e96..0845102 100644 --- a/naturalSort.js +++ b/naturalSort.js @@ -16,30 +16,32 @@ : define ) /*define*/([], function factory() { - -function naturalSort (a, b) { var re = /(^([+\-]?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?(?=\D|\s|$))|^0x[\da-fA-F]+$|\d+)/g, sre = /^\s+|\s+$/g, // trim pre-post whitespace snre = /\s+/g, // normalize all whitespace to single ' ' character dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, hre = /^0x[0-9a-f]+$/i, ore = /^0/, + b0re = /^\0/, + e0re = /\0$/, i = function(s) { return (naturalSort.insensitive && ('' + s).toLowerCase() || '' + s).replace(sre, ''); }, + normChunk = function(s, l) { + // normalize spaces; find floats not starting with '0', string or 0 if not defined (Clint Priest) + return (!s.match(ore) || l == 1) && parseFloat(s) || s.replace(snre, ' ').replace(sre, '') || 0; + }; + +function naturalSort (a, b) { // convert all to strings strip whitespace - x = i(a), - y = i(b), + var x = i(a) || '', + y = i(b) || '', // chunk/tokenize - xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), - yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + xN = x.replace(re, '\0$1\0').replace(e0re,'').replace(b0re,'').split('\0'), + yN = y.replace(re, '\0$1\0').replace(e0re,'').replace(b0re,'').split('\0'), // numeric, hex or date detection xD = parseInt(x.match(hre), 16) || (xN.length !== 1 && Date.parse(x)), yD = parseInt(y.match(hre), 16) || xD && y.match(dre) && Date.parse(y) || null, - normChunk = function(s, l) { - // normalize spaces; find floats not starting with '0', string or 0 if not defined (Clint Priest) - return (!s.match(ore) || l == 1) && parseFloat(s) || s.replace(snre, ' ').replace(sre, '') || 0; - }, oFxNcL, oFyNcL; // first try and sort Hex codes or Dates if (yD) {