@lowentry/utils 1.16.2 → 1.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1406,6 +1406,40 @@ var LeUtils = {
1406
1406
  }
1407
1407
  return 0;
1408
1408
  },
1409
+ /**
1410
+ * Compares two strings in a natural way, meaning that it will compare numbers in the strings as actual numbers.
1411
+ *
1412
+ * This will correctly sort numeric parts so that "file5.txt" comes before "file10.txt", as well as that "file/5/test.txt" comes before "file/10/test.txt".
1413
+ *
1414
+ * @param {string} a
1415
+ * @param {string} b
1416
+ * @returns {number}
1417
+ */
1418
+ compareNaturalStrings: function compareNaturalStrings(a, b) {
1419
+ var _a$match, _b$match;
1420
+ var re = /(\d+|\D+)/g; // split into runs of digits or non-digits
1421
+ var aTokens = (_a$match = a.match(re)) !== null && _a$match !== void 0 ? _a$match : [];
1422
+ var bTokens = (_b$match = b.match(re)) !== null && _b$match !== void 0 ? _b$match : [];
1423
+ var len = Math.min(aTokens.length, bTokens.length);
1424
+ for (var i = 0; i < len; i++) {
1425
+ var x = aTokens[i];
1426
+ var y = bTokens[i];
1427
+ if (x === y) {
1428
+ continue;
1429
+ }
1430
+
1431
+ // if both are numbers, compare as numbers
1432
+ var nx = parseInt(x, 10);
1433
+ var ny = parseInt(y, 10);
1434
+ if (!isNaN(nx) && !isNaN(ny)) {
1435
+ return nx - ny;
1436
+ }
1437
+
1438
+ // otherwise compare lexically
1439
+ return x < y ? -1 : 1;
1440
+ }
1441
+ return aTokens.length - bTokens.length;
1442
+ },
1409
1443
  /**
1410
1444
  * Compares two strings generated by LeUtils.timestamp(). Primarily used for sorting.
1411
1445
  *