@lowentry/utils 1.16.1 → 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 +36 -2
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LeUtils.js +43 -2
- package/tests/each.test.js +10 -0
- package/tests/sort.test.js +33 -0
package/index.js
CHANGED
|
@@ -1341,7 +1341,7 @@ var LeUtils = {
|
|
|
1341
1341
|
*/
|
|
1342
1342
|
flattenToArray: function () {
|
|
1343
1343
|
var _flattenToArrayRecursive = function flattenToArrayRecursive(result, elements, optionalSkipHasOwnPropertyCheck) {
|
|
1344
|
-
if (!LeUtils.supportsEach(elements)) {
|
|
1344
|
+
if (!LeUtils.supportsEach(elements) || typeof elements === 'string') {
|
|
1345
1345
|
result.push(elements);
|
|
1346
1346
|
return;
|
|
1347
1347
|
}
|
|
@@ -1351,7 +1351,7 @@ var LeUtils = {
|
|
|
1351
1351
|
};
|
|
1352
1352
|
return function (elements) {
|
|
1353
1353
|
var optionalSkipHasOwnPropertyCheck = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
1354
|
-
if (!LeUtils.supportsEach(elements)) {
|
|
1354
|
+
if (!LeUtils.supportsEach(elements) || typeof elements === 'string') {
|
|
1355
1355
|
return [elements];
|
|
1356
1356
|
}
|
|
1357
1357
|
var result = [];
|
|
@@ -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
|
*
|