@lowentry/utils 1.25.3 → 2.0.2

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,124 +0,0 @@
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
- /** @type {Map<string, Set<Function>>} */
7
- #events;
8
-
9
-
10
- /**
11
- * Creates a new EventEmitter instance.
12
- */
13
- constructor()
14
- {
15
- this.#events = new Map();
16
- }
17
-
18
-
19
- /**
20
- * Registers a listener for a specific event.
21
- *
22
- * @param {string} event - The name of the event to listen for.
23
- * @param {Function} listener - The function to call when the event is emitted.
24
- * @returns {{remove:(()=>void)}}
25
- */
26
- on(event, listener)
27
- {
28
- if(!this.#events.has(event))
29
- {
30
- this.#events.set(event, new Set());
31
- }
32
- this.#events.get(event)?.add(listener);
33
-
34
- return {
35
- remove:
36
- () => this.off(event, listener),
37
- };
38
- }
39
-
40
- /**
41
- * Registers a listener for a specific event, this listener will be called only once.
42
- *
43
- * @param {string} event - The name of the event to listen for.
44
- * @param {Function} listener - The function to call when the event is emitted.
45
- * @returns {{remove:()=>void}}
46
- */
47
- once(event, listener)
48
- {
49
- const wrapper = (/** @type {*[]} */ ...args) =>
50
- {
51
- this.off(event, wrapper);
52
- listener(...args);
53
- };
54
-
55
- this.on(event, wrapper);
56
-
57
- return {
58
- remove:
59
- () => this.off(event, wrapper),
60
- };
61
- }
62
-
63
- /**
64
- * Removes a listener for a specific event.
65
- *
66
- * @param {string} event - The name of the event to stop listening for.
67
- * @param {Function} listener - The function to remove from the event listeners.
68
- */
69
- off(event, listener)
70
- {
71
- const listeners = this.#events.get(event);
72
- if(listeners)
73
- {
74
- listeners.delete(listener);
75
- if(listeners.size === 0)
76
- {
77
- this.#events.delete(event);
78
- }
79
- }
80
- }
81
-
82
- /**
83
- * Emits an event, calling all registered listeners with the provided arguments.
84
- *
85
- * @param {string} event - The name of the event to emit.
86
- * @param {...*} args - The arguments to pass to the listeners.
87
- */
88
- emit(event, ...args)
89
- {
90
- const listeners = this.#events.get(event);
91
- if(listeners)
92
- {
93
- const snapshot = [...listeners];
94
- for(const listener of snapshot)
95
- {
96
- try
97
- {
98
- listener(...args);
99
- }
100
- catch(err)
101
- {
102
- console.error(`Error in listener for "${event}":`, err);
103
- }
104
- }
105
- }
106
- }
107
-
108
- /**
109
- * Clears all listeners for a specific event or all events if no event is specified.
110
- *
111
- * @param {string} [event] - The name of the event to clear listeners for. If not provided, all events will be cleared.
112
- */
113
- clear(event)
114
- {
115
- if(event !== undefined)
116
- {
117
- this.#events.delete(event);
118
- }
119
- else
120
- {
121
- this.#events.clear();
122
- }
123
- }
124
- }
@@ -1,145 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
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
- }
@@ -1,235 +0,0 @@
1
- import {LeUtils} from '../LeUtils.js';
2
-
3
-
4
- /**
5
- * A TreeSet is a set of elements, sorted by a comparator.
6
- * Binary search is used to find elements, which makes it very fast to find elements.
7
- *
8
- * The comparator is also used to determine if two values are equal to each other.
9
- * 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.
10
- */
11
- export class TreeSet
12
- {
13
- /** @type {*[]} */
14
- #elements;
15
-
16
- /** @type {(valueA:*, valueB:*) => number} */
17
- #comparator;
18
-
19
-
20
- /**
21
- * Creates a new TreeSet with the given elements and comparator.
22
- *
23
- * @param {*[]} [elements=[]] - The initial elements of the set.
24
- * @param {(valueA:*, valueB:*) => number} [comparator=LeUtils.compare] - The comparator function to use for sorting.
25
- */
26
- constructor(elements, comparator)
27
- {
28
- this.#comparator = comparator || LeUtils.compare;
29
- this.#elements = elements || [];
30
- this.#elements.sort(this.#comparator);
31
- }
32
-
33
-
34
- /**
35
- *
36
- *
37
- * @param {*} value - The value to search for in the set.
38
- * @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).
39
- * @private
40
- */
41
- binarySearch(value)
42
- {
43
- let low = 0;
44
- let high = this.#elements.length - 1;
45
- while(low <= high)
46
- {
47
- const mid = Math.floor((low + high) / 2);
48
- const midValue = this.#elements[mid];
49
- const cmp = this.#comparator(midValue, value);
50
- if(cmp < 0)
51
- {
52
- low = mid + 1;
53
- }
54
- else if(cmp > 0)
55
- {
56
- high = mid - 1;
57
- }
58
- else
59
- {
60
- return {found:true, index:mid, value:midValue};
61
- }
62
- }
63
- return {found:false, index:low, value:undefined};
64
- };
65
-
66
-
67
- /**
68
- * Returns the elements of the set.
69
- */
70
- getElements()
71
- {
72
- return this.#elements;
73
- }
74
-
75
- /**
76
- * Returns the comparator of the set.
77
- *
78
- * @returns {(valueA:*, valueB:*) => number}
79
- */
80
- getComparator()
81
- {
82
- return this.#comparator;
83
- }
84
-
85
- /**
86
- * Returns the size of the set.
87
- *
88
- * @returns {number}
89
- */
90
- size()
91
- {
92
- return this.#elements.length;
93
- }
94
-
95
- /**
96
- * Returns true if the set is empty, false otherwise.
97
- *
98
- * @returns {boolean}
99
- */
100
- isEmpty()
101
- {
102
- return (this.#elements.length <= 0);
103
- }
104
-
105
- /**
106
- * Returns true if the set contains a value that is equal to the given value, returns false otherwise.
107
- *
108
- * @param {*} value - The value to check for in the set.
109
- * @return {boolean} - True if the set contains the value, false otherwise.
110
- */
111
- contains(value)
112
- {
113
- return this.binarySearch(value).found;
114
- }
115
-
116
- /**
117
- * Returns the first element of the set, or undefined if it is empty.
118
- *
119
- * @return {*|undefined} - The first element of the set, or undefined if it is empty.
120
- */
121
- first()
122
- {
123
- return (this.#elements.length > 0) ? this.#elements[0] : undefined;
124
- }
125
-
126
- /**
127
- * Returns the last element of the set, or undefined if it is empty.
128
- *
129
- * @return {*|undefined} - The last element of the set, or undefined if it is empty.
130
- */
131
- last()
132
- {
133
- return (this.#elements.length > 0) ? this.#elements[this.#elements.length - 1] : undefined;
134
- }
135
-
136
- /**
137
- * Removes and returns the first element of the set, or undefined if it is empty.
138
- *
139
- * @returns {*|undefined} - The first element of the set, or undefined if it is empty.
140
- */
141
- pollFirst()
142
- {
143
- return (this.#elements.length > 0) ? this.#elements.splice(0, 1)[0] : undefined;
144
- }
145
-
146
- /**
147
- * Removes and returns the last element of the set, or undefined if it is empty.
148
- *
149
- * @returns {*|undefined} - The last element of the set, or undefined if it is empty.
150
- */
151
- pollLast()
152
- {
153
- return (this.#elements.length > 0) ? this.#elements.splice(this.#elements.length - 1, 1)[0] : undefined;
154
- }
155
-
156
- /**
157
- * Adds the given value to the set. Will only do so if no equal value already exists.
158
- * @param {*} value - The value to add to the set.
159
- */
160
- add(value)
161
- {
162
- const result = this.binarySearch(value);
163
- if(result.found)
164
- {
165
- return;
166
- }
167
- this.#elements.splice(result.index, 0, value);
168
- }
169
-
170
- /**
171
- * Adds all the given values to the set. Will only do so if no equal value already exists.
172
- *
173
- * @param {*} values - The values to add to the set.
174
- */
175
- addAll(values)
176
- {
177
- LeUtils.each(values, this.add.bind(this));
178
- }
179
-
180
- /**
181
- * Returns an equal value that's already in the tree set, or undefined if no equal values in it exist.
182
- *
183
- * @param {*} value - The value to search for in the set.
184
- * @return {*|undefined} - The equal value if found, or undefined if not found.
185
- */
186
- getEqualValue(value)
187
- {
188
- const result = this.binarySearch(value);
189
- if(result.found)
190
- {
191
- return result.value;
192
- }
193
- return undefined;
194
- }
195
-
196
- /**
197
- * 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.
198
- *
199
- * @param {*} value - The value to search for in the set.
200
- * @return {*} - The equal value if found, or the added value if not found.
201
- */
202
- getEqualValueOrAdd(value)
203
- {
204
- const result = this.binarySearch(value);
205
- if(result.found)
206
- {
207
- return result.value;
208
- }
209
- this.#elements.splice(result.index, 0, value);
210
- return value;
211
- }
212
-
213
- /**
214
- * Returns a string representation of the TreeSet.
215
- *
216
- * @returns {string} - A string representation of the TreeSet, including its elements and comparator.
217
- */
218
- toString()
219
- {
220
- return `TreeSet{elements:${this.#elements}, comparator:${this.#comparator}}`;
221
- }
222
-
223
- /**
224
- * Returns a JSON representation of the TreeSet.
225
- *
226
- * @returns {Object} - An object containing the elements and comparator of the TreeSet.
227
- */
228
- toJSON()
229
- {
230
- return {
231
- elements: this.#elements,
232
- comparator:this.#comparator.toString(),
233
- };
234
- }
235
- }
package/src/index.js DELETED
@@ -1,6 +0,0 @@
1
- export {LeUtils} from './LeUtils.js';
2
- export {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, BOOL, BOOL_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
3
- export {EventEmitter} from './classes/EventEmitter.js';
4
- export {LinkedList} from './classes/LinkedList.js';
5
- export {SerializableMap} from './classes/SerializableMap.js';
6
- export {TreeSet} from './classes/TreeSet.js';
package/tsconfig.d.ts DELETED
@@ -1 +0,0 @@
1
- /// <reference lib="esnext" />
package/tsconfig.json DELETED
@@ -1,39 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "allowJs": true,
4
- "checkJs": true,
5
- "strict": true,
6
- "noImplicitAny": false,
7
- "noImplicitThis": false,
8
- "alwaysStrict": true,
9
- "resolveJsonModule": true,
10
- "esModuleInterop": true,
11
- "allowSyntheticDefaultImports": true,
12
- "jsx": "react-jsx",
13
- "target": "esnext",
14
- "module": "esnext",
15
- "moduleResolution": "node",
16
- "skipLibCheck": true,
17
- "types": [],
18
- "noEmit": false,
19
- "declaration": true,
20
- "declarationMap": false,
21
- "outDir": "./build",
22
- "removeComments": false,
23
- "stripInternal": true,
24
- "emitDeclarationOnly": true
25
- },
26
- "include": [
27
- "tsconfig.d.ts",
28
- "src"
29
- ],
30
- "exclude": [
31
- "node_modules"
32
- ],
33
- "typeAcquisition": {
34
- "include": [
35
- "react",
36
- "react-dom"
37
- ]
38
- }
39
- }