@lowentry/utils 1.22.1 → 1.23.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.
@@ -1,216 +0,0 @@
1
- import {describe, test, expect} from 'vitest';
2
- import {LeUtils, SerializableMap} from '../src/index.js';
3
-
4
-
5
- const KEY_TO_IGNORE = '@ignored@';
6
-
7
- const areMapsEqual = (map1, map2) => LeUtils.equalsMapLike(map1, map2);
8
- const areMapsEqualIgnoreKey = (map1, map2) => LeUtils.equalsMapLike(map1, map2, [KEY_TO_IGNORE]);
9
-
10
-
11
- describe('testing map comparisons', () =>
12
- {
13
- test('map compare true - different classes', () =>
14
- {
15
- const ownMap = new SerializableMap([['key1', 'value1'], ['key2', 'value2']]);
16
- const map = new Map([['key1', 'value1'], ['key2', 'value2']]);
17
- expect(areMapsEqual(ownMap, map)).toBe(true);
18
- });
19
- test('map compare true', () =>
20
- {
21
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
22
- const map = new Map([['key1', 'value1'], ['key2', 'value2']]);
23
- expect(areMapsEqual(ownMap, map)).toBe(true);
24
- });
25
- test('map compare false', () =>
26
- {
27
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
28
- const map = new Map([['key1', 'value1'], ['key2', 'value3']]);
29
- expect(areMapsEqual(ownMap, map)).toBe(false);
30
- });
31
- test('map compare true - different order', () =>
32
- {
33
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
34
- const map = new Map([['key2', 'value2'], ['key1', 'value1']]);
35
- expect(areMapsEqual(ownMap, map)).toBe(true);
36
- });
37
- test('map compare false - different order', () =>
38
- {
39
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
40
- const map = new Map([['key2', 'value3'], ['key1', 'value1']]);
41
- expect(areMapsEqual(ownMap, map)).toBe(false);
42
- });
43
- test('map compare true - with location', () =>
44
- {
45
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
46
- const map = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
47
- expect(areMapsEqual(ownMap, map)).toBe(true);
48
- });
49
- test('map compare false - with location', () =>
50
- {
51
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
52
- const map = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '456']]);
53
- expect(areMapsEqual(ownMap, map)).toBe(false);
54
- });
55
- test('map compare true - ignore location 1', () =>
56
- {
57
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
58
- const map = new Map([['key1', 'value1'], ['key2', 'value2']]);
59
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(true);
60
- });
61
- test('map compare true - ignore location 2', () =>
62
- {
63
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
64
- const map = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
65
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(true);
66
- });
67
- test('map compare false - ignore location 1', () =>
68
- {
69
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
70
- const map = new Map([['key1', 'value1'], ['key2', 'value3']]);
71
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(false);
72
- });
73
- test('map compare false - ignore location 2', () =>
74
- {
75
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
76
- const map = new Map([['key1', 'value1'], ['key2', 'value3'], [KEY_TO_IGNORE, '123']]);
77
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(false);
78
- });
79
- test('map compare true - empty map', () =>
80
- {
81
- const ownMap = new Map();
82
- const map = new Map();
83
- expect(areMapsEqual(ownMap, map)).toBe(true);
84
- });
85
- test('map compare false - empty map', () =>
86
- {
87
- const ownMap = new Map([['key1', 'value1']]);
88
- const map = new Map();
89
- expect(areMapsEqual(ownMap, map)).toBe(false);
90
- });
91
- test('map compare true - empty map with location', () =>
92
- {
93
- const ownMap = new Map([[KEY_TO_IGNORE, '123']]);
94
- const map = new Map([[KEY_TO_IGNORE, '123']]);
95
- expect(areMapsEqual(ownMap, map)).toBe(true);
96
- });
97
- test('map compare false - empty map with location', () =>
98
- {
99
- const ownMap = new Map([[KEY_TO_IGNORE, '123']]);
100
- const map = new Map([[KEY_TO_IGNORE, '456']]);
101
- expect(areMapsEqual(ownMap, map)).toBe(false);
102
- });
103
- test('map compare true - empty map ignore location', () =>
104
- {
105
- const ownMap = new Map([[KEY_TO_IGNORE, '123']]);
106
- const map = new Map();
107
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(true);
108
- });
109
- test('map compare false - empty map ignore location', () =>
110
- {
111
- const ownMap = new Map([[KEY_TO_IGNORE, '123']]);
112
- const map = new Map([['key1', 'value1']]);
113
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(false);
114
- });
115
- });
116
-
117
-
118
- describe('testing map-object comparisons', () =>
119
- {
120
- test('map-object compare true', () =>
121
- {
122
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
123
- const map = {'key1':'value1', 'key2':'value2'};
124
- expect(areMapsEqual(ownMap, map)).toBe(true);
125
- });
126
- test('map-object compare false', () =>
127
- {
128
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
129
- const map = {'key1':'value1', 'key2':'value3'};
130
- expect(areMapsEqual(ownMap, map)).toBe(false);
131
- });
132
- test('map-object compare true - different order', () =>
133
- {
134
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
135
- const map = {'key2':'value2', 'key1':'value1'};
136
- expect(areMapsEqual(ownMap, map)).toBe(true);
137
- });
138
- test('map-object compare false - different order', () =>
139
- {
140
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
141
- const map = {'key2':'value3', 'key1':'value1'};
142
- expect(areMapsEqual(ownMap, map)).toBe(false);
143
- });
144
- test('map-object compare true - with location', () =>
145
- {
146
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
147
- const map = {'key1':'value1', 'key2':'value2', [KEY_TO_IGNORE]:'123'};
148
- expect(areMapsEqual(ownMap, map)).toBe(true);
149
- });
150
- test('map-object compare false - with location', () =>
151
- {
152
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
153
- const map = {'key1':'value1', 'key2':'value2', [KEY_TO_IGNORE]:'456'};
154
- expect(areMapsEqual(ownMap, map)).toBe(false);
155
- });
156
- test('map-object compare true - ignore location 1', () =>
157
- {
158
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
159
- const map = {'key1':'value1', 'key2':'value2'};
160
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(true);
161
- });
162
- test('map-object compare true - ignore location 2', () =>
163
- {
164
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
165
- const map = {'key1':'value1', 'key2':'value2', [KEY_TO_IGNORE]:'123'};
166
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(true);
167
- });
168
- test('map-object compare false - ignore location 1', () =>
169
- {
170
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2'], [KEY_TO_IGNORE, '123']]);
171
- const map = {'key1':'value1', 'key2':'value3'};
172
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(false);
173
- });
174
- test('map-object compare false - ignore location 2', () =>
175
- {
176
- const ownMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
177
- const map = {'key1':'value1', 'key2':'value3', [KEY_TO_IGNORE]:'123'};
178
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(false);
179
- });
180
- test('map-object compare true - empty map', () =>
181
- {
182
- const ownMap = new Map();
183
- const map = {};
184
- expect(areMapsEqual(ownMap, map)).toBe(true);
185
- });
186
- test('map-object compare false - empty map', () =>
187
- {
188
- const ownMap = new Map([['key1', 'value1']]);
189
- const map = {};
190
- expect(areMapsEqual(ownMap, map)).toBe(false);
191
- });
192
- test('map-object compare true - empty map with location', () =>
193
- {
194
- const ownMap = new Map([[KEY_TO_IGNORE, '123']]);
195
- const map = {[KEY_TO_IGNORE]:'123'};
196
- expect(areMapsEqual(ownMap, map)).toBe(true);
197
- });
198
- test('map-object compare false - empty map with location', () =>
199
- {
200
- const ownMap = new Map([[KEY_TO_IGNORE, '123']]);
201
- const map = {[KEY_TO_IGNORE]:'456'};
202
- expect(areMapsEqual(ownMap, map)).toBe(false);
203
- });
204
- test('map-object compare true - empty map ignore location', () =>
205
- {
206
- const ownMap = new Map([[KEY_TO_IGNORE, '123']]);
207
- const map = {};
208
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(true);
209
- });
210
- test('map-object compare false - empty map ignore location', () =>
211
- {
212
- const ownMap = new Map([[KEY_TO_IGNORE, '123']]);
213
- const map = {'key1':'value1'};
214
- expect(areMapsEqualIgnoreKey(ownMap, map)).toBe(false);
215
- });
216
- });
@@ -1,179 +0,0 @@
1
- import {describe, test, expect} from 'vitest';
2
- import {EventEmitter} from '../src/index.js';
3
-
4
-
5
- describe('EventEmitter', () =>
6
- {
7
- test('should create an instance of EventEmitter', () =>
8
- {
9
- const emitter = new EventEmitter();
10
- expect(emitter).toBeInstanceOf(EventEmitter);
11
- });
12
-
13
- test('should emit and listen to events', () =>
14
- {
15
- const emitter = new EventEmitter();
16
- let called = 0;
17
-
18
- emitter.on('testEvent', () =>
19
- {
20
- called++;
21
- });
22
-
23
- emitter.emit('testEvent');
24
- expect(called).toBe(1);
25
- });
26
-
27
- test('should remove event listeners', () =>
28
- {
29
- const emitter = new EventEmitter();
30
- let called = 0;
31
-
32
- const listener = () =>
33
- {
34
- called++;
35
- };
36
-
37
- emitter.on('testEvent', listener);
38
- emitter.off('testEvent', listener);
39
-
40
- emitter.emit('testEvent');
41
- expect(called).toBe(0);
42
- });
43
-
44
- test('should handle multiple listeners for the same event', () =>
45
- {
46
- const emitter = new EventEmitter();
47
- let called1 = 0;
48
- let called2 = 0;
49
-
50
- const listener1 = () =>
51
- {
52
- called1++;
53
- };
54
- const listener2 = () =>
55
- {
56
- called2++;
57
- };
58
-
59
- emitter.on('testEvent', listener1);
60
- emitter.on('testEvent', listener2);
61
-
62
- emitter.emit('testEvent');
63
- expect(called1).toBe(1);
64
- expect(called2).toBe(1);
65
-
66
- emitter.off('testEvent', listener1);
67
- emitter.emit('testEvent');
68
- expect(called1).toBe(1);
69
- expect(called2).toBe(2);
70
- });
71
-
72
- test('should handle event names with special characters', () =>
73
- {
74
- const emitter = new EventEmitter();
75
- let called = 0;
76
-
77
- emitter.on('test-event!@#$', () =>
78
- {
79
- called++;
80
- });
81
-
82
- emitter.emit('test-event!@#$');
83
- expect(called).toBe(1);
84
- });
85
-
86
- test('should handle event names with spaces', () =>
87
- {
88
- const emitter = new EventEmitter();
89
- let called = 0;
90
-
91
- emitter.on('test event with spaces', () =>
92
- {
93
- called++;
94
- });
95
-
96
- emitter.emit('test event with spaces');
97
- expect(called).toBe(1);
98
- });
99
-
100
- test('should be able to remove a listener using the returned listener object', () =>
101
- {
102
- const emitter = new EventEmitter();
103
- let called = 0;
104
-
105
- const listener = emitter.on('test123', () =>
106
- {
107
- called++;
108
- });
109
-
110
- emitter.emit('test123');
111
- expect(called).toBe(1);
112
-
113
- listener.remove();
114
- emitter.emit('test123');
115
- expect(called).toBe(1);
116
- });
117
-
118
- test('should handle multiple events with the same name', () =>
119
- {
120
- const emitter = new EventEmitter();
121
- let called1 = 0;
122
- let called2 = 0;
123
-
124
- emitter.on('testEvent', () =>
125
- {
126
- called1++;
127
- });
128
-
129
- emitter.on('testEvent', () =>
130
- {
131
- called2++;
132
- });
133
-
134
- emitter.emit('testEvent');
135
- expect(called1).toBe(1);
136
- expect(called2).toBe(1);
137
-
138
- emitter.clear('testEvent');
139
- emitter.emit('testEvent');
140
- expect(called1).toBe(1);
141
- expect(called2).toBe(1);
142
- });
143
-
144
- test('should handle once() method', () =>
145
- {
146
- const emitter = new EventEmitter();
147
- let called = 0;
148
-
149
- emitter.once('testOnce', () =>
150
- {
151
- called++;
152
- });
153
-
154
- emitter.emit('testOnce');
155
- expect(called).toBe(1);
156
-
157
- emitter.emit('testOnce');
158
- expect(called).toBe(1);
159
- });
160
-
161
- test('should allow canceling a once listener using .remove()', () =>
162
- {
163
- const emitter = new EventEmitter();
164
- let called = 0;
165
-
166
- const listener = emitter.once('testOnceCancel', () =>
167
- {
168
- called++;
169
- });
170
-
171
- listener.remove();
172
-
173
- emitter.emit('testOnceCancel');
174
- expect(called).toBe(0);
175
-
176
- emitter.emit('testOnceCancel');
177
- expect(called).toBe(0);
178
- });
179
- });
@@ -1,33 +0,0 @@
1
- import {describe, test, expect} from 'vitest';
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
- });