Sha256: d675dfc821d7a5238a5bea13d8d3faf0791ca90880834f8a61fb6df6a4e34996

Contents?: true

Size: 701 Bytes

Versions: 19

Compression:

Stored size: 701 Bytes

Contents

module.exports = function naturalCmp(str1, str2) {
  if (str1 == str2) return 0;
  if (!str1) return -1;
  if (!str2) return 1;

  var cmpRegex = /(\.\d+|\d+|\D+)/g,
    tokens1 = String(str1).match(cmpRegex),
    tokens2 = String(str2).match(cmpRegex),
    count = Math.min(tokens1.length, tokens2.length);

  for (var i = 0; i < count; i++) {
    var a = tokens1[i],
      b = tokens2[i];

    if (a !== b) {
      var num1 = +a;
      var num2 = +b;
      if (num1 === num1 && num2 === num2) {
        return num1 > num2 ? 1 : -1;
      }
      return a < b ? -1 : 1;
    }
  }

  if (tokens1.length != tokens2.length)
    return tokens1.length - tokens2.length;

  return str1 < str2 ? -1 : 1;
};

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ela-4.1.6 node_modules/underscore.string/naturalCmp.js
ela-4.1.5 node_modules/underscore.string/naturalCmp.js
ela-4.1.4 node_modules/underscore.string/naturalCmp.js
ela-4.1.3 node_modules/underscore.string/naturalCmp.js
ela-4.1.2 node_modules/underscore.string/naturalCmp.js
ela-4.1.1 node_modules/underscore.string/naturalCmp.js
ela-4.1.0 node_modules/underscore.string/naturalCmp.js
ela-4.0.0 node_modules/underscore.string/naturalCmp.js
ela-3.4.3 node_modules/underscore.string/naturalCmp.js
ela-3.4.2 node_modules/underscore.string/naturalCmp.js
ela-3.4.0 node_modules/underscore.string/naturalCmp.js
ela-3.3.1 node_modules/underscore.string/naturalCmp.js
ela-3.3.0 node_modules/underscore.string/naturalCmp.js
ela-3.2.0 node_modules/underscore.string/naturalCmp.js
ela-3.1.1 node_modules/underscore.string/naturalCmp.js
ela-3.1.0 node_modules/underscore.string/naturalCmp.js
ela-3.0.0 node_modules/underscore.string/naturalCmp.js
ela-2.0.0 node_modules/underscore.string/naturalCmp.js
ela-1.1.0 node_modules/underscore.string/naturalCmp.js