@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowentry/utils",
3
- "version": "1.16.1",
3
+ "version": "1.17.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/LeUtils.js CHANGED
@@ -1049,7 +1049,7 @@ export const LeUtils = {
1049
1049
  {
1050
1050
  const flattenToArrayRecursive = (result, elements, optionalSkipHasOwnPropertyCheck) =>
1051
1051
  {
1052
- if(!LeUtils.supportsEach(elements))
1052
+ if(!LeUtils.supportsEach(elements) || (typeof elements === 'string'))
1053
1053
  {
1054
1054
  result.push(elements);
1055
1055
  return;
@@ -1062,7 +1062,7 @@ export const LeUtils = {
1062
1062
 
1063
1063
  return (elements, optionalSkipHasOwnPropertyCheck = false) =>
1064
1064
  {
1065
- if(!LeUtils.supportsEach(elements))
1065
+ if(!LeUtils.supportsEach(elements) || (typeof elements === 'string'))
1066
1066
  {
1067
1067
  return [elements];
1068
1068
  }
@@ -1127,6 +1127,47 @@ export const LeUtils = {
1127
1127
  return 0;
1128
1128
  },
1129
1129
 
1130
+ /**
1131
+ * Compares two strings in a natural way, meaning that it will compare numbers in the strings as actual numbers.
1132
+ *
1133
+ * 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".
1134
+ *
1135
+ * @param {string} a
1136
+ * @param {string} b
1137
+ * @returns {number}
1138
+ */
1139
+ compareNaturalStrings:
1140
+ (a, b) =>
1141
+ {
1142
+ const re = /(\d+|\D+)/g; // split into runs of digits or non-digits
1143
+ const aTokens = a.match(re) ?? [];
1144
+ const bTokens = b.match(re) ?? [];
1145
+
1146
+ const len = Math.min(aTokens.length, bTokens.length);
1147
+ for(let i = 0; i < len; i++)
1148
+ {
1149
+ const x = aTokens[i];
1150
+ const y = bTokens[i];
1151
+ if(x === y)
1152
+ {
1153
+ continue;
1154
+ }
1155
+
1156
+ // if both are numbers, compare as numbers
1157
+ const nx = parseInt(x, 10);
1158
+ const ny = parseInt(y, 10);
1159
+ if(!isNaN(nx) && !isNaN(ny))
1160
+ {
1161
+ return nx - ny;
1162
+ }
1163
+
1164
+ // otherwise compare lexically
1165
+ return x < y ? -1 : 1;
1166
+ }
1167
+
1168
+ return aTokens.length - bTokens.length;
1169
+ },
1170
+
1130
1171
  /**
1131
1172
  * Compares two strings generated by LeUtils.timestamp(). Primarily used for sorting.
1132
1173
  *
@@ -4,6 +4,16 @@ import {LeUtils} from '../src/index.js';
4
4
  const wait = ms => LeUtils.promiseTimeout(ms ?? 100);
5
5
 
6
6
 
7
+ describe('LeUtils.flattenToArray crash tests', () =>
8
+ {
9
+ test('flattenToArray on strings', () =>
10
+ {
11
+ const res = LeUtils.flattenToArray('test123');
12
+ expect(res).toEqual(['test123']);
13
+ });
14
+ });
15
+
16
+
7
17
  describe('LeUtils.each()', () =>
8
18
  {
9
19
  test('array order', () =>
@@ -0,0 +1,33 @@
1
+ import {describe, test, expect, jest} from '@jest/globals';
2
+ import {LeUtils} from '../src/index.js';
3
+
4
+ const wait = ms => LeUtils.promiseTimeout(ms ?? 100);
5
+
6
+
7
+ describe('LeUtils.compareNaturalStrings', () =>
8
+ {
9
+ test('should return 0 for equal paths', () =>
10
+ {
11
+ expect(LeUtils.compareNaturalStrings('path/to/file.txt', 'path/to/file.txt')).toBe(0);
12
+ });
13
+
14
+ test('should return -1 for first path being "less than" second', () =>
15
+ {
16
+ expect(LeUtils.compareNaturalStrings('path/to/a.txt', 'path/to/b.txt')).toBeLessThan(0);
17
+ });
18
+
19
+ test('should return 1 for first path being "greater than" second', () =>
20
+ {
21
+ expect(LeUtils.compareNaturalStrings('path/to/b.txt', 'path/to/a.txt')).toBeGreaterThan(0);
22
+ });
23
+
24
+ test('test/5/test.txt should be less than test/10/test.txt', () =>
25
+ {
26
+ expect(LeUtils.compareNaturalStrings('test/5/test.txt', 'test/10/test.txt')).toBeLessThan(0);
27
+ });
28
+
29
+ test('test5.txt should be less than test10.txt', () =>
30
+ {
31
+ expect(LeUtils.compareNaturalStrings('test5.txt', 'test10.txt')).toBeLessThan(0);
32
+ });
33
+ });