@lowentry/utils 1.20.1 → 1.22.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/build/LeUtils.d.ts +0 -15
- package/build/classes/EventEmitter.d.ts +46 -0
- package/build/classes/TreeSet.d.ts +115 -0
- package/build/index.d.ts +2 -0
- package/index.d.ts +163 -15
- package/index.js +447 -221
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LeUtils.js +14 -189
- package/src/classes/EventEmitter.js +124 -0
- package/src/classes/TreeSet.js +235 -0
- package/src/index.js +2 -0
- package/tests/event-emitter.test.js +179 -0
package/build/LeUtils.d.ts
CHANGED
|
@@ -119,21 +119,6 @@ export namespace LeUtils {
|
|
|
119
119
|
function localStorageRemove(id: string): void;
|
|
120
120
|
function isCurrentHostPrivate(): boolean;
|
|
121
121
|
function isGivenHostPrivate(host: string): boolean;
|
|
122
|
-
function createTreeSet(elements: any[], comparator: (valueA: any, valueB: any) => number): {
|
|
123
|
-
getElements: (() => any[]);
|
|
124
|
-
getComparator: (() => ((valueA: any, valueB: any) => number));
|
|
125
|
-
size: (() => number);
|
|
126
|
-
isEmpty: (() => boolean);
|
|
127
|
-
contains: ((value: any) => boolean);
|
|
128
|
-
first: (() => any | undefined);
|
|
129
|
-
last: (() => any | undefined);
|
|
130
|
-
pollFirst: (() => any | undefined);
|
|
131
|
-
pollLast: (() => any | undefined);
|
|
132
|
-
add: ((value: any) => void);
|
|
133
|
-
addAll: ((values: any) => void);
|
|
134
|
-
getEqualValue: ((value: any) => any);
|
|
135
|
-
getEqualValueOrAdd: ((value: any) => any);
|
|
136
|
-
};
|
|
137
122
|
function createTransactionalValue(value?: any): {
|
|
138
123
|
value: any;
|
|
139
124
|
changes: {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple event emitter class that allows you to register listeners for events, emit events, and remove listeners.
|
|
3
|
+
*/
|
|
4
|
+
export class EventEmitter {
|
|
5
|
+
/**
|
|
6
|
+
* Registers a listener for a specific event.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} event - The name of the event to listen for.
|
|
9
|
+
* @param {Function} listener - The function to call when the event is emitted.
|
|
10
|
+
* @returns {{remove:(()=>void)}}
|
|
11
|
+
*/
|
|
12
|
+
on(event: string, listener: Function): {
|
|
13
|
+
remove: (() => void);
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Registers a listener for a specific event, this listener will be called only once.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} event - The name of the event to listen for.
|
|
19
|
+
* @param {Function} listener - The function to call when the event is emitted.
|
|
20
|
+
* @returns {{remove:()=>void}}
|
|
21
|
+
*/
|
|
22
|
+
once(event: string, listener: Function): {
|
|
23
|
+
remove: () => void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Removes a listener for a specific event.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} event - The name of the event to stop listening for.
|
|
29
|
+
* @param {Function} listener - The function to remove from the event listeners.
|
|
30
|
+
*/
|
|
31
|
+
off(event: string, listener: Function): void;
|
|
32
|
+
/**
|
|
33
|
+
* Emits an event, calling all registered listeners with the provided arguments.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} event - The name of the event to emit.
|
|
36
|
+
* @param {...*} args - The arguments to pass to the listeners.
|
|
37
|
+
*/
|
|
38
|
+
emit(event: string, ...args: any[]): void;
|
|
39
|
+
/**
|
|
40
|
+
* Clears all listeners for a specific event or all events if no event is specified.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} [event] - The name of the event to clear listeners for. If not provided, all events will be cleared.
|
|
43
|
+
*/
|
|
44
|
+
clear(event?: string): void;
|
|
45
|
+
#private;
|
|
46
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A TreeSet is a set of elements, sorted by a comparator.
|
|
3
|
+
* Binary search is used to find elements, which makes it very fast to find elements.
|
|
4
|
+
*
|
|
5
|
+
* The comparator is also used to determine if two values are equal to each other.
|
|
6
|
+
* This way, you can have values that aren't the same be treated as if they are. This can be used to deal with issues such as floating point errors for example.
|
|
7
|
+
*/
|
|
8
|
+
export class TreeSet {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new TreeSet with the given elements and comparator.
|
|
11
|
+
*
|
|
12
|
+
* @param {*[]} [elements=[]] - The initial elements of the set.
|
|
13
|
+
* @param {(valueA:*, valueB:*) => number} [comparator=LeUtils.compare] - The comparator function to use for sorting.
|
|
14
|
+
*/
|
|
15
|
+
constructor(elements?: any[], comparator?: (valueA: any, valueB: any) => number);
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
* @param {*} value - The value to search for in the set.
|
|
20
|
+
* @returns {{found:boolean, index:number, value:*|undefined}} - An object containing whether the value was found, the index where it would be inserted, and the value at that index (if found).
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
23
|
+
private binarySearch;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the elements of the set.
|
|
26
|
+
*/
|
|
27
|
+
getElements(): any[];
|
|
28
|
+
/**
|
|
29
|
+
* Returns the comparator of the set.
|
|
30
|
+
*
|
|
31
|
+
* @returns {(valueA:*, valueB:*) => number}
|
|
32
|
+
*/
|
|
33
|
+
getComparator(): (valueA: any, valueB: any) => number;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the size of the set.
|
|
36
|
+
*
|
|
37
|
+
* @returns {number}
|
|
38
|
+
*/
|
|
39
|
+
size(): number;
|
|
40
|
+
/**
|
|
41
|
+
* Returns true if the set is empty, false otherwise.
|
|
42
|
+
*
|
|
43
|
+
* @returns {boolean}
|
|
44
|
+
*/
|
|
45
|
+
isEmpty(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Returns true if the set contains a value that is equal to the given value, returns false otherwise.
|
|
48
|
+
*
|
|
49
|
+
* @param {*} value - The value to check for in the set.
|
|
50
|
+
* @return {boolean} - True if the set contains the value, false otherwise.
|
|
51
|
+
*/
|
|
52
|
+
contains(value: any): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Returns the first element of the set, or undefined if it is empty.
|
|
55
|
+
*
|
|
56
|
+
* @return {*|undefined} - The first element of the set, or undefined if it is empty.
|
|
57
|
+
*/
|
|
58
|
+
first(): any | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Returns the last element of the set, or undefined if it is empty.
|
|
61
|
+
*
|
|
62
|
+
* @return {*|undefined} - The last element of the set, or undefined if it is empty.
|
|
63
|
+
*/
|
|
64
|
+
last(): any | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Removes and returns the first element of the set, or undefined if it is empty.
|
|
67
|
+
*
|
|
68
|
+
* @returns {*|undefined} - The first element of the set, or undefined if it is empty.
|
|
69
|
+
*/
|
|
70
|
+
pollFirst(): any | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Removes and returns the last element of the set, or undefined if it is empty.
|
|
73
|
+
*
|
|
74
|
+
* @returns {*|undefined} - The last element of the set, or undefined if it is empty.
|
|
75
|
+
*/
|
|
76
|
+
pollLast(): any | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* Adds the given value to the set. Will only do so if no equal value already exists.
|
|
79
|
+
* @param {*} value - The value to add to the set.
|
|
80
|
+
*/
|
|
81
|
+
add(value: any): void;
|
|
82
|
+
/**
|
|
83
|
+
* Adds all the given values to the set. Will only do so if no equal value already exists.
|
|
84
|
+
*
|
|
85
|
+
* @param {*} values - The values to add to the set.
|
|
86
|
+
*/
|
|
87
|
+
addAll(values: any): void;
|
|
88
|
+
/**
|
|
89
|
+
* Returns an equal value that's already in the tree set, or undefined if no equal values in it exist.
|
|
90
|
+
*
|
|
91
|
+
* @param {*} value - The value to search for in the set.
|
|
92
|
+
* @return {*|undefined} - The equal value if found, or undefined if not found.
|
|
93
|
+
*/
|
|
94
|
+
getEqualValue(value: any): any | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Returns an equal value that's already in the tree set. If no equal values in it exist, the given value will be added and returned.
|
|
97
|
+
*
|
|
98
|
+
* @param {*} value - The value to search for in the set.
|
|
99
|
+
* @return {*} - The equal value if found, or the added value if not found.
|
|
100
|
+
*/
|
|
101
|
+
getEqualValueOrAdd(value: any): any;
|
|
102
|
+
/**
|
|
103
|
+
* Returns a string representation of the TreeSet.
|
|
104
|
+
*
|
|
105
|
+
* @returns {string} - A string representation of the TreeSet, including its elements and comparator.
|
|
106
|
+
*/
|
|
107
|
+
toString(): string;
|
|
108
|
+
/**
|
|
109
|
+
* Returns a JSON representation of the TreeSet.
|
|
110
|
+
*
|
|
111
|
+
* @returns {Object} - An object containing the elements and comparator of the TreeSet.
|
|
112
|
+
*/
|
|
113
|
+
toJSON(): any;
|
|
114
|
+
#private;
|
|
115
|
+
}
|
package/build/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { LeUtils } from "./LeUtils.js";
|
|
2
|
+
export { EventEmitter } from "./classes/EventEmitter.js";
|
|
2
3
|
export { LinkedList } from "./classes/LinkedList.js";
|
|
3
4
|
export { SerializableMap } from "./classes/SerializableMap.js";
|
|
5
|
+
export { TreeSet } from "./classes/TreeSet.js";
|
|
4
6
|
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";
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,52 @@
|
|
|
1
1
|
export declare function ARRAY(value: any): any[];
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* A simple event emitter class that allows you to register listeners for events, emit events, and remove listeners.
|
|
5
|
+
*/
|
|
6
|
+
export declare class EventEmitter {
|
|
7
|
+
/**
|
|
8
|
+
* Registers a listener for a specific event.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} event - The name of the event to listen for.
|
|
11
|
+
* @param {Function} listener - The function to call when the event is emitted.
|
|
12
|
+
* @returns {{remove:(()=>void)}}
|
|
13
|
+
*/
|
|
14
|
+
on(event: string, listener: Function): {
|
|
15
|
+
remove: (() => void);
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Registers a listener for a specific event, this listener will be called only once.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} event - The name of the event to listen for.
|
|
21
|
+
* @param {Function} listener - The function to call when the event is emitted.
|
|
22
|
+
* @returns {{remove:()=>void}}
|
|
23
|
+
*/
|
|
24
|
+
once(event: string, listener: Function): {
|
|
25
|
+
remove: () => void;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Removes a listener for a specific event.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} event - The name of the event to stop listening for.
|
|
31
|
+
* @param {Function} listener - The function to remove from the event listeners.
|
|
32
|
+
*/
|
|
33
|
+
off(event: string, listener: Function): void;
|
|
34
|
+
/**
|
|
35
|
+
* Emits an event, calling all registered listeners with the provided arguments.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} event - The name of the event to emit.
|
|
38
|
+
* @param {...*} args - The arguments to pass to the listeners.
|
|
39
|
+
*/
|
|
40
|
+
emit(event: string, ...args: any[]): void;
|
|
41
|
+
/**
|
|
42
|
+
* Clears all listeners for a specific event or all events if no event is specified.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} [event] - The name of the event to clear listeners for. If not provided, all events will be cleared.
|
|
45
|
+
*/
|
|
46
|
+
clear(event?: string): void;
|
|
47
|
+
#private;
|
|
48
|
+
}
|
|
49
|
+
|
|
3
50
|
export declare function FLOAT(value: any): number;
|
|
4
51
|
|
|
5
52
|
export declare function FLOAT_ANY(...values: any): number;
|
|
@@ -143,21 +190,6 @@ export declare namespace LeUtils {
|
|
|
143
190
|
export function localStorageRemove(id: string): void;
|
|
144
191
|
export function isCurrentHostPrivate(): boolean;
|
|
145
192
|
export function isGivenHostPrivate(host: string): boolean;
|
|
146
|
-
export function createTreeSet(elements: any[], comparator: (valueA: any, valueB: any) => number): {
|
|
147
|
-
getElements: (() => any[]);
|
|
148
|
-
getComparator: (() => ((valueA: any, valueB: any) => number));
|
|
149
|
-
size: (() => number);
|
|
150
|
-
isEmpty: (() => boolean);
|
|
151
|
-
contains: ((value: any) => boolean);
|
|
152
|
-
first: (() => any | undefined);
|
|
153
|
-
last: (() => any | undefined);
|
|
154
|
-
pollFirst: (() => any | undefined);
|
|
155
|
-
pollLast: (() => any | undefined);
|
|
156
|
-
add: ((value: any) => void);
|
|
157
|
-
addAll: ((values: any) => void);
|
|
158
|
-
getEqualValue: ((value: any) => any);
|
|
159
|
-
getEqualValueOrAdd: ((value: any) => any);
|
|
160
|
-
};
|
|
161
193
|
export function createTransactionalValue(value?: any): {
|
|
162
194
|
value: any;
|
|
163
195
|
changes: {
|
|
@@ -299,4 +331,120 @@ export declare function STRING(value: any): string;
|
|
|
299
331
|
|
|
300
332
|
export declare function STRING_ANY(...values: any): string;
|
|
301
333
|
|
|
334
|
+
/**
|
|
335
|
+
* A TreeSet is a set of elements, sorted by a comparator.
|
|
336
|
+
* Binary search is used to find elements, which makes it very fast to find elements.
|
|
337
|
+
*
|
|
338
|
+
* The comparator is also used to determine if two values are equal to each other.
|
|
339
|
+
* This way, you can have values that aren't the same be treated as if they are. This can be used to deal with issues such as floating point errors for example.
|
|
340
|
+
*/
|
|
341
|
+
export declare class TreeSet {
|
|
342
|
+
/**
|
|
343
|
+
* Creates a new TreeSet with the given elements and comparator.
|
|
344
|
+
*
|
|
345
|
+
* @param {*[]} [elements=[]] - The initial elements of the set.
|
|
346
|
+
* @param {(valueA:*, valueB:*) => number} [comparator=LeUtils.compare] - The comparator function to use for sorting.
|
|
347
|
+
*/
|
|
348
|
+
constructor(elements?: any[], comparator?: (valueA: any, valueB: any) => number);
|
|
349
|
+
/**
|
|
350
|
+
*
|
|
351
|
+
*
|
|
352
|
+
* @param {*} value - The value to search for in the set.
|
|
353
|
+
* @returns {{found:boolean, index:number, value:*|undefined}} - An object containing whether the value was found, the index where it would be inserted, and the value at that index (if found).
|
|
354
|
+
* @private
|
|
355
|
+
*/
|
|
356
|
+
private binarySearch;
|
|
357
|
+
/**
|
|
358
|
+
* Returns the elements of the set.
|
|
359
|
+
*/
|
|
360
|
+
getElements(): any[];
|
|
361
|
+
/**
|
|
362
|
+
* Returns the comparator of the set.
|
|
363
|
+
*
|
|
364
|
+
* @returns {(valueA:*, valueB:*) => number}
|
|
365
|
+
*/
|
|
366
|
+
getComparator(): (valueA: any, valueB: any) => number;
|
|
367
|
+
/**
|
|
368
|
+
* Returns the size of the set.
|
|
369
|
+
*
|
|
370
|
+
* @returns {number}
|
|
371
|
+
*/
|
|
372
|
+
size(): number;
|
|
373
|
+
/**
|
|
374
|
+
* Returns true if the set is empty, false otherwise.
|
|
375
|
+
*
|
|
376
|
+
* @returns {boolean}
|
|
377
|
+
*/
|
|
378
|
+
isEmpty(): boolean;
|
|
379
|
+
/**
|
|
380
|
+
* Returns true if the set contains a value that is equal to the given value, returns false otherwise.
|
|
381
|
+
*
|
|
382
|
+
* @param {*} value - The value to check for in the set.
|
|
383
|
+
* @return {boolean} - True if the set contains the value, false otherwise.
|
|
384
|
+
*/
|
|
385
|
+
contains(value: any): boolean;
|
|
386
|
+
/**
|
|
387
|
+
* Returns the first element of the set, or undefined if it is empty.
|
|
388
|
+
*
|
|
389
|
+
* @return {*|undefined} - The first element of the set, or undefined if it is empty.
|
|
390
|
+
*/
|
|
391
|
+
first(): any | undefined;
|
|
392
|
+
/**
|
|
393
|
+
* Returns the last element of the set, or undefined if it is empty.
|
|
394
|
+
*
|
|
395
|
+
* @return {*|undefined} - The last element of the set, or undefined if it is empty.
|
|
396
|
+
*/
|
|
397
|
+
last(): any | undefined;
|
|
398
|
+
/**
|
|
399
|
+
* Removes and returns the first element of the set, or undefined if it is empty.
|
|
400
|
+
*
|
|
401
|
+
* @returns {*|undefined} - The first element of the set, or undefined if it is empty.
|
|
402
|
+
*/
|
|
403
|
+
pollFirst(): any | undefined;
|
|
404
|
+
/**
|
|
405
|
+
* Removes and returns the last element of the set, or undefined if it is empty.
|
|
406
|
+
*
|
|
407
|
+
* @returns {*|undefined} - The last element of the set, or undefined if it is empty.
|
|
408
|
+
*/
|
|
409
|
+
pollLast(): any | undefined;
|
|
410
|
+
/**
|
|
411
|
+
* Adds the given value to the set. Will only do so if no equal value already exists.
|
|
412
|
+
* @param {*} value - The value to add to the set.
|
|
413
|
+
*/
|
|
414
|
+
add(value: any): void;
|
|
415
|
+
/**
|
|
416
|
+
* Adds all the given values to the set. Will only do so if no equal value already exists.
|
|
417
|
+
*
|
|
418
|
+
* @param {*} values - The values to add to the set.
|
|
419
|
+
*/
|
|
420
|
+
addAll(values: any): void;
|
|
421
|
+
/**
|
|
422
|
+
* Returns an equal value that's already in the tree set, or undefined if no equal values in it exist.
|
|
423
|
+
*
|
|
424
|
+
* @param {*} value - The value to search for in the set.
|
|
425
|
+
* @return {*|undefined} - The equal value if found, or undefined if not found.
|
|
426
|
+
*/
|
|
427
|
+
getEqualValue(value: any): any | undefined;
|
|
428
|
+
/**
|
|
429
|
+
* Returns an equal value that's already in the tree set. If no equal values in it exist, the given value will be added and returned.
|
|
430
|
+
*
|
|
431
|
+
* @param {*} value - The value to search for in the set.
|
|
432
|
+
* @return {*} - The equal value if found, or the added value if not found.
|
|
433
|
+
*/
|
|
434
|
+
getEqualValueOrAdd(value: any): any;
|
|
435
|
+
/**
|
|
436
|
+
* Returns a string representation of the TreeSet.
|
|
437
|
+
*
|
|
438
|
+
* @returns {string} - A string representation of the TreeSet, including its elements and comparator.
|
|
439
|
+
*/
|
|
440
|
+
toString(): string;
|
|
441
|
+
/**
|
|
442
|
+
* Returns a JSON representation of the TreeSet.
|
|
443
|
+
*
|
|
444
|
+
* @returns {Object} - An object containing the elements and comparator of the TreeSet.
|
|
445
|
+
*/
|
|
446
|
+
toJSON(): any;
|
|
447
|
+
#private;
|
|
448
|
+
}
|
|
449
|
+
|
|
302
450
|
export { }
|