@lowentry/utils 1.20.1 → 1.21.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.20.1",
3
+ "version": "1.21.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/LeUtils.js CHANGED
@@ -2988,7 +2988,21 @@ export const LeUtils = {
2988
2988
  isGivenHostPrivate:
2989
2989
  (host) =>
2990
2990
  {
2991
+ host = STRING(host).trim();
2992
+ if(!host)
2993
+ {
2994
+ return false;
2995
+ }
2996
+ try
2997
+ {
2998
+ host = (new URL(host)).hostname;
2999
+ }
3000
+ catch(e)
3001
+ {
3002
+ host = host.split(':')[0];
3003
+ }
2991
3004
  host = STRING(host).trim().toLowerCase();
3005
+
2992
3006
  if((host === 'localhost') || (host === '127.0.0.1'))
2993
3007
  {
2994
3008
  return true;
@@ -3003,195 +3017,6 @@ export const LeUtils = {
3003
3017
  ((parts[0] === '192') && (parts[1] === '168')); // 192.168.0.0 - 192.168.255.255
3004
3018
  },
3005
3019
 
3006
- /**
3007
- * Creates and returns a new TreeSet.
3008
- * A TreeSet is a set of elements, sorted by a comparator.
3009
- * Binary search is used to find elements, which makes it very fast to find elements.
3010
- *
3011
- * The comparator is also used to determine if two values are equal to each other.
3012
- * 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.
3013
- *
3014
- * @param {*[]} elements
3015
- * @param {(valueA:*, valueB:*) => number} comparator
3016
- * @returns {{getElements:(()=>*[]), getComparator:(()=>((valueA:*,valueB:*)=>number)), size:(()=>number), isEmpty:(()=>boolean), contains:((value:*)=>boolean), first:(()=>*|undefined), last:(()=>*|undefined), pollFirst:(()=>*|undefined), pollLast:(()=>*|undefined), add:((value:*)=>void), addAll:((values:*)=>void), getEqualValue:((value:*)=>*), getEqualValueOrAdd:((value:*)=>*)}}
3017
- */
3018
- createTreeSet:
3019
- (elements, comparator) =>
3020
- {
3021
- comparator = comparator || LeUtils.compare;
3022
- elements = elements || [];
3023
- elements.sort(comparator);
3024
-
3025
- /**
3026
- * Performs a binary search on the elements, and returns the result.
3027
- *
3028
- * @param {*} value
3029
- * @returns {{found: boolean, index: number, value: *|undefined}}
3030
- */
3031
- const binarySearch = (value) =>
3032
- {
3033
- let low = 0;
3034
- let high = elements.length - 1;
3035
- while(low <= high)
3036
- {
3037
- const mid = Math.floor((low + high) / 2);
3038
- const midValue = elements[mid];
3039
- const cmp = comparator(midValue, value);
3040
- if(cmp < 0)
3041
- {
3042
- low = mid + 1;
3043
- }
3044
- else if(cmp > 0)
3045
- {
3046
- high = mid - 1;
3047
- }
3048
- else
3049
- {
3050
- return {found:true, index:mid, value:midValue};
3051
- }
3052
- }
3053
- return {found:false, index:low, value:undefined};
3054
- };
3055
-
3056
- const treeSet = {
3057
- /**
3058
- * Returns the elements of the set.
3059
- *
3060
- * @returns {*[]}
3061
- */
3062
- getElements:
3063
- () => elements,
3064
-
3065
- /**
3066
- * Returns the comparator of the set.
3067
- *
3068
- * @returns {(valueA:*, valueB:*) => number}
3069
- */
3070
- getComparator:
3071
- () => comparator,
3072
-
3073
- /**
3074
- * Returns the size of the set.
3075
- *
3076
- * @returns {number}
3077
- */
3078
- size:
3079
- () => elements.length,
3080
-
3081
- /**
3082
- * Returns true if the set is empty, false otherwise.
3083
- *
3084
- * @returns {boolean}
3085
- */
3086
- isEmpty:
3087
- () => (elements.length <= 0),
3088
-
3089
- /**
3090
- * Returns true if the set contains a value that is equal to the given value, returns false otherwise.
3091
- *
3092
- * @param {*} value
3093
- * @returns {boolean}
3094
- */
3095
- contains:
3096
- (value) => binarySearch(value).found,
3097
-
3098
- /**
3099
- * Returns the first element of the set, or undefined if it is empty.
3100
- *
3101
- * @returns {*|undefined}
3102
- */
3103
- first:
3104
- () => (elements.length > 0) ? elements[0] : undefined,
3105
-
3106
- /**
3107
- * Returns the last element of the set, or undefined if it is empty.
3108
- *
3109
- * @returns {*|undefined}
3110
- */
3111
- last:
3112
- () => (elements.length > 0) ? elements[elements.length - 1] : undefined,
3113
-
3114
- /**
3115
- * Removes and returns the first element of the set, or undefined if it is empty.
3116
- *
3117
- * @returns {*|undefined}
3118
- */
3119
- pollFirst:
3120
- () => (elements.length > 0) ? elements.splice(0, 1)[0] : undefined,
3121
-
3122
- /**
3123
- * Removes and returns the last element of the set, or undefined if it is empty.
3124
- *
3125
- * @returns {*|undefined}
3126
- */
3127
- pollLast:
3128
- () => (elements.length > 0) ? elements.splice(elements.length - 1, 1)[0] : undefined,
3129
-
3130
- /**
3131
- * Adds the given value to the set. Will only do so if no equal value already exists.
3132
- *
3133
- * @param {*} value
3134
- */
3135
- add:
3136
- (value) =>
3137
- {
3138
- const result = binarySearch(value);
3139
- if(result.found)
3140
- {
3141
- return;
3142
- }
3143
- elements.splice(result.index, 0, value);
3144
- },
3145
-
3146
- /**
3147
- * Adds all the given values to the set. Will only do so if no equal value already exists.
3148
- *
3149
- * @param {*} values
3150
- */
3151
- addAll:
3152
- (values) =>
3153
- {
3154
- LeUtils.each(values, treeSet.add);
3155
- },
3156
-
3157
- /**
3158
- * Returns an equal value that's already in the tree set, or undefined if no equal values in it exist.
3159
- *
3160
- * @param {*} value
3161
- * @returns {*|undefined}
3162
- */
3163
- getEqualValue:
3164
- (value) =>
3165
- {
3166
- const result = binarySearch(value);
3167
- if(result.found)
3168
- {
3169
- return result.value;
3170
- }
3171
- return undefined;
3172
- },
3173
-
3174
- /**
3175
- * 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.
3176
- *
3177
- * @param {*} value
3178
- * @returns {*}
3179
- */
3180
- getEqualValueOrAdd:
3181
- (value) =>
3182
- {
3183
- const result = binarySearch(value);
3184
- if(result.found)
3185
- {
3186
- return result.value;
3187
- }
3188
- elements.splice(result.index, 0, value);
3189
- return value;
3190
- },
3191
- };
3192
- return treeSet;
3193
- },
3194
-
3195
3020
  /**
3196
3021
  * @typedef {Object} LeUtils_TransactionalValue
3197
3022
  * @property {*} value
@@ -0,0 +1,235 @@
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 CHANGED
@@ -2,3 +2,4 @@ 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
3
  export {LinkedList} from './classes/LinkedList.js';
4
4
  export {SerializableMap} from './classes/SerializableMap.js';
5
+ export {TreeSet} from './classes/TreeSet.js';