@lowentry/utils 1.16.2 → 1.18.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 +202 -1
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LeUtils.js +41 -0
- package/src/classes/LinkedList.js +145 -0
- package/src/classes/SerializableMap.js +17 -0
- package/src/index.js +2 -0
- package/tests/sort.test.js +33 -0
package/package.json
CHANGED
package/src/LeUtils.js
CHANGED
|
@@ -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
|
*
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
class LinkedListNode
|
|
2
|
+
{
|
|
3
|
+
/** @type {*} */
|
|
4
|
+
value;
|
|
5
|
+
|
|
6
|
+
/** @type {LinkedListNode|null} */
|
|
7
|
+
next = null;
|
|
8
|
+
|
|
9
|
+
/** @type {LinkedListNode|null} */
|
|
10
|
+
previous = null;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {*} value
|
|
14
|
+
*/
|
|
15
|
+
constructor(value)
|
|
16
|
+
{
|
|
17
|
+
this.value = value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class LinkedList
|
|
22
|
+
{
|
|
23
|
+
/** @type {LinkedListNode|null} */
|
|
24
|
+
#head = null;
|
|
25
|
+
|
|
26
|
+
/** @type {LinkedListNode|null} */
|
|
27
|
+
#tail = null;
|
|
28
|
+
|
|
29
|
+
/** @type {number} */
|
|
30
|
+
#size = 0;
|
|
31
|
+
|
|
32
|
+
constructor()
|
|
33
|
+
{
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns the number of elements in the list.
|
|
38
|
+
*
|
|
39
|
+
* @returns {number}
|
|
40
|
+
*/
|
|
41
|
+
get size()
|
|
42
|
+
{
|
|
43
|
+
return this.#size;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Adds a new value to the beginning of the list.
|
|
48
|
+
*
|
|
49
|
+
* @param {*} value
|
|
50
|
+
*/
|
|
51
|
+
unshift(value)
|
|
52
|
+
{
|
|
53
|
+
const newNode = new LinkedListNode(value);
|
|
54
|
+
if(this.#head === null)
|
|
55
|
+
{
|
|
56
|
+
this.#head = newNode;
|
|
57
|
+
this.#tail = newNode;
|
|
58
|
+
}
|
|
59
|
+
else
|
|
60
|
+
{
|
|
61
|
+
newNode.next = this.#head;
|
|
62
|
+
this.#head.previous = newNode;
|
|
63
|
+
this.#head = newNode;
|
|
64
|
+
}
|
|
65
|
+
this.#size++;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Adds a new value to the end of the list.
|
|
70
|
+
*
|
|
71
|
+
* @param {*} value
|
|
72
|
+
*/
|
|
73
|
+
push(value)
|
|
74
|
+
{
|
|
75
|
+
const newNode = new LinkedListNode(value);
|
|
76
|
+
if(this.#tail === null)
|
|
77
|
+
{
|
|
78
|
+
this.#head = newNode;
|
|
79
|
+
this.#tail = newNode;
|
|
80
|
+
}
|
|
81
|
+
else
|
|
82
|
+
{
|
|
83
|
+
newNode.previous = this.#tail;
|
|
84
|
+
this.#tail.next = newNode;
|
|
85
|
+
this.#tail = newNode;
|
|
86
|
+
}
|
|
87
|
+
this.#size++;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Removes the first value from the list and returns it.
|
|
92
|
+
*
|
|
93
|
+
* @returns {*|undefined}
|
|
94
|
+
*/
|
|
95
|
+
shift()
|
|
96
|
+
{
|
|
97
|
+
if(this.#head === null)
|
|
98
|
+
{
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const value = this.#head.value;
|
|
103
|
+
this.#head = this.#head.next;
|
|
104
|
+
|
|
105
|
+
if(this.#head !== null)
|
|
106
|
+
{
|
|
107
|
+
this.#head.previous = null;
|
|
108
|
+
}
|
|
109
|
+
else
|
|
110
|
+
{
|
|
111
|
+
this.#tail = null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this.#size--;
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Removes the last value from the list and returns it.
|
|
120
|
+
*
|
|
121
|
+
* @returns {*|undefined}
|
|
122
|
+
*/
|
|
123
|
+
pop()
|
|
124
|
+
{
|
|
125
|
+
if(this.#tail === null)
|
|
126
|
+
{
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const value = this.#tail.value;
|
|
131
|
+
this.#tail = this.#tail.previous;
|
|
132
|
+
|
|
133
|
+
if(this.#tail !== null)
|
|
134
|
+
{
|
|
135
|
+
this.#tail.next = null;
|
|
136
|
+
}
|
|
137
|
+
else
|
|
138
|
+
{
|
|
139
|
+
this.#head = null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.#size--;
|
|
143
|
+
return value;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SerializableMap class extends the native Map to provide a JSON representation.
|
|
3
|
+
*
|
|
4
|
+
* This class can only have string keys, as JSON does not support non-string keys.
|
|
5
|
+
*/
|
|
6
|
+
export class SerializableMap extends Map
|
|
7
|
+
{
|
|
8
|
+
/**
|
|
9
|
+
* Returns a JSON representation of the map.
|
|
10
|
+
*
|
|
11
|
+
* @returns {object}
|
|
12
|
+
*/
|
|
13
|
+
toJSON()
|
|
14
|
+
{
|
|
15
|
+
return Object.fromEntries(this);
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export {LeUtils} from './LeUtils.js';
|
|
2
2
|
export {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
|
|
3
|
+
export {LinkedList} from './classes/LinkedList.js';
|
|
4
|
+
export {SerializableMap} from './classes/SerializableMap.js';
|
|
@@ -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
|
+
});
|