@nejs/basic-extensions 1.4.1 → 1.5.0
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/dist/@nejs/basic-extensions.bundle.1.4.1.js +2 -0
- package/dist/@nejs/basic-extensions.bundle.1.4.1.js.map +7 -0
- package/dist/cjs/asyncIterable.d.ts +3 -0
- package/dist/cjs/asyncIterable.js +203 -0
- package/dist/cjs/descriptor.d.ts +1 -1
- package/dist/cjs/descriptor.js +16 -3
- package/dist/cjs/globals.js +55 -67
- package/dist/cjs/index.d.ts +9 -2
- package/dist/cjs/index.js +27 -10
- package/dist/cjs/iterable.d.ts +3 -0
- package/dist/cjs/iterable.js +199 -0
- package/dist/cjs/objectextensions.js +9 -0
- package/dist/cjs/refset.d.ts +2 -0
- package/dist/cjs/refset.js +374 -0
- package/dist/cjs/symbolextensions.js +43 -0
- package/dist/cjs/weakrefextensions.d.ts +2 -0
- package/dist/cjs/weakrefextensions.js +18 -0
- package/dist/mjs/asyncIterable.d.ts +3 -0
- package/dist/mjs/asyncIterable.js +188 -0
- package/dist/mjs/descriptor.d.ts +1 -1
- package/dist/mjs/descriptor.js +14 -1
- package/dist/mjs/globals.js +55 -67
- package/dist/mjs/index.d.ts +9 -2
- package/dist/mjs/index.js +20 -10
- package/dist/mjs/iterable.d.ts +3 -0
- package/dist/mjs/iterable.js +184 -0
- package/dist/mjs/objectextensions.js +9 -0
- package/dist/mjs/refset.d.ts +2 -0
- package/dist/mjs/refset.js +352 -0
- package/dist/mjs/symbolextensions.js +43 -0
- package/dist/mjs/weakrefextensions.d.ts +2 -0
- package/dist/mjs/weakrefextensions.js +15 -0
- package/package.json +2 -2
- package/src/asyncIterable.js +208 -0
- package/src/descriptor.js +16 -1
- package/src/globals.js +58 -78
- package/src/index.js +35 -10
- package/src/iterable.js +203 -0
- package/src/objectextensions.js +12 -0
- package/src/refset.js +414 -0
- package/src/symbolextensions.js +46 -0
- package/src/weakrefextensions.js +18 -0
- package/tests/asyncIterable.test.js +44 -0
- package/tests/iterable.test.js +45 -0
- package/tests/refset.test.js +58 -0
- package/dist/@nejs/basic-extensions.bundle.1.4.0.js +0 -2
- package/dist/@nejs/basic-extensions.bundle.1.4.0.js.map +0 -7
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { Extension } from '@nejs/extension';
|
|
2
|
+
/**
|
|
3
|
+
* RefSet class extends the standard Set object to manage a collection of
|
|
4
|
+
* WeakRef objects. It provides additional functionality such as objectification
|
|
5
|
+
* of values and various utility methods.
|
|
6
|
+
*
|
|
7
|
+
* Unlike standard Sets or Arrays, RefSet stores weak references to objects,
|
|
8
|
+
* allowing them to be garbage-collected if there are no other references to
|
|
9
|
+
* them. This behavior is different from Arrays and standard Sets, which
|
|
10
|
+
* maintain strong references to their elements.
|
|
11
|
+
*
|
|
12
|
+
* @extends Set
|
|
13
|
+
*/
|
|
14
|
+
class RefSet extends Set {
|
|
15
|
+
/**
|
|
16
|
+
* Private field to track whether the RefSet should objectify primitive
|
|
17
|
+
* values.
|
|
18
|
+
*
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
#objectifyValues = false;
|
|
22
|
+
/**
|
|
23
|
+
* Method to control whether the RefSet should objectify its values. When
|
|
24
|
+
* objectifying, primitive values (number, string, boolean, bigint) are
|
|
25
|
+
* converted to their respective object types, which allows them to be used as
|
|
26
|
+
* WeakRef targets.
|
|
27
|
+
*
|
|
28
|
+
* @param {boolean} setObjectification - Flag to enable or disable
|
|
29
|
+
* objectification.
|
|
30
|
+
* @returns {RefSet} - The current RefSet instance to allow method chaining.
|
|
31
|
+
*/
|
|
32
|
+
objectifying(setObjectification = true) {
|
|
33
|
+
this.objectifyValues = setObjectification;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Returns the state indicating whether or not `RefSet` will attempt to
|
|
38
|
+
* convert non-valid primitives into targets that are valid input for
|
|
39
|
+
* new `WeakRef` object instances. If this value is `false` then no
|
|
40
|
+
* *objectification* will occur.
|
|
41
|
+
*
|
|
42
|
+
* @returns {boolean} The current state of objectifyValues.
|
|
43
|
+
*/
|
|
44
|
+
get objectifyValues() {
|
|
45
|
+
return this.#objectifyValues;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Setting this value to true, will cause all added values to the Set to
|
|
49
|
+
* be analyzed for validity as a candidate to be wrapped in a `WeakRef`
|
|
50
|
+
* object. If true, and if possible, the object will be turned into an
|
|
51
|
+
* `Object` variant first. This will also enable less rigid variable
|
|
52
|
+
* comparison in the `.has()` method (i.e. `==` instead of `===`).
|
|
53
|
+
*
|
|
54
|
+
* @param {boolean} value - The new state to set for objectifyValues.
|
|
55
|
+
*/
|
|
56
|
+
set objectifyValues(value) {
|
|
57
|
+
this.#objectifyValues = !!value;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Overrides the add method of Set. Adds a value to the RefSet, converting it
|
|
61
|
+
* to a WeakRef. Throws an error if the value is not a valid WeakRef target
|
|
62
|
+
* (e.g., null, undefined, or a registered symbol). If `objectifyValues` is
|
|
63
|
+
* enabled, an attempt to convert primitives to their object variants will be
|
|
64
|
+
* made. These are numbers, strings, boolean values and big integers.
|
|
65
|
+
*
|
|
66
|
+
* @param {*} value - The value to be added to the RefSet.
|
|
67
|
+
* @throws {TypeError} If the value is not a valid WeakRef target.
|
|
68
|
+
*/
|
|
69
|
+
add(value) {
|
|
70
|
+
// Objectify the value if needed
|
|
71
|
+
if (this.#objectifyValues && (typeof value === 'number' ||
|
|
72
|
+
typeof value === 'string' ||
|
|
73
|
+
typeof value === 'boolean' ||
|
|
74
|
+
typeof value === 'bigint')) {
|
|
75
|
+
value = Object(value);
|
|
76
|
+
}
|
|
77
|
+
// Check if the value is an object, and if it's a symbol, ensure it's not registered
|
|
78
|
+
if (typeof value === 'symbol' && Symbol.keyFor(value) !== undefined) {
|
|
79
|
+
throw new TypeError('RefSet cannot accept registered symbols as values');
|
|
80
|
+
}
|
|
81
|
+
if (typeof value !== 'object' && typeof value !== 'symbol') {
|
|
82
|
+
throw new TypeError('RefSet values must be objects, non-registered symbols, or objectified primitives');
|
|
83
|
+
}
|
|
84
|
+
// If the value is null or undefined, throw an error
|
|
85
|
+
if (value === null || value === undefined) {
|
|
86
|
+
throw new TypeError('RefSet values cannot be null or undefined');
|
|
87
|
+
}
|
|
88
|
+
super.add(new WeakRef(value));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Adds multiple values to the RefSet. The supplied `values` should be
|
|
92
|
+
* iterable and truthy. This function defers to `.add()` for its logic so
|
|
93
|
+
* each value from the supplied collection of values will also be subject
|
|
94
|
+
* to the criteria of that function.
|
|
95
|
+
*
|
|
96
|
+
* @param {Iterable} values - An iterable of values to add to the RefSet.
|
|
97
|
+
* @throws {TypeError} If the supplied values are falsey or non-iterable.
|
|
98
|
+
*/
|
|
99
|
+
addAll(values) {
|
|
100
|
+
if (!values ||
|
|
101
|
+
(typeof values !== 'object') ||
|
|
102
|
+
!Reflect.has(values, Symbol.iterator)) {
|
|
103
|
+
throw new TypeError('The supplied values are either falsey or non-iterable');
|
|
104
|
+
}
|
|
105
|
+
for (const value of values) {
|
|
106
|
+
this.add(value);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Removes all elements from the RefSet that have been garbage collected
|
|
111
|
+
* (i.e., their WeakRef no longer points to an object).
|
|
112
|
+
*
|
|
113
|
+
* @returns {RefSet} - The current RefSet instance to allow method chaining.
|
|
114
|
+
*/
|
|
115
|
+
clean() {
|
|
116
|
+
for (const ref of this) {
|
|
117
|
+
if (!ref.deref()) {
|
|
118
|
+
this.delete(ref);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Executes a provided function once for each value in the RefSet. The callback
|
|
125
|
+
* function receives the dereferenced value, the value again (as RefSet doesn't
|
|
126
|
+
* use keys), and the RefSet itself. This method provides a way to iterate over
|
|
127
|
+
* and apply operations to the values stored in the RefSet, taking into account
|
|
128
|
+
* that they are weak references and may have been garbage-collected.
|
|
129
|
+
*
|
|
130
|
+
* @param {Function} forEachFn - Function to execute for each element. It
|
|
131
|
+
* takes three arguments: element, element (again, as RefSet has no key), and
|
|
132
|
+
* the RefSet itself.
|
|
133
|
+
* @param {*} thisArg - Value to use as `this` when executing `forEachFn`.
|
|
134
|
+
*/
|
|
135
|
+
entries() {
|
|
136
|
+
const refEntries = super.entries();
|
|
137
|
+
return refEntries
|
|
138
|
+
.map(([_, ref]) => [ref.deref(), ref.deref()])
|
|
139
|
+
.filter(([_, value]) => !!value);
|
|
140
|
+
}
|
|
141
|
+
forEach(forEachFn, thisArg) {
|
|
142
|
+
const set = this;
|
|
143
|
+
super.forEach(function (ref) {
|
|
144
|
+
const value = ref.deref();
|
|
145
|
+
if (!value) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
forEachFn.call(thisArg, value, value, set);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Returns an iterator for the values in the RefSet. Each value is
|
|
153
|
+
* dereferenced from its WeakRef before being returned. This method allows
|
|
154
|
+
* iterating over he set's values, similar to how one would iterate over
|
|
155
|
+
* values in a standard Set or Array, but with the understanding that the
|
|
156
|
+
* values are weakly referenced and may no longer exist (in which case
|
|
157
|
+
* they are skipped).
|
|
158
|
+
*
|
|
159
|
+
* @returns {Iterator} An iterator for the values.
|
|
160
|
+
*/
|
|
161
|
+
values() {
|
|
162
|
+
const values = [];
|
|
163
|
+
for (const value of this) {
|
|
164
|
+
const dereferenced = value.deref();
|
|
165
|
+
if (dereferenced) {
|
|
166
|
+
values.push(dereferenced);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return values;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Returns an iterator for the keys of the RefSet. In RefSet, keys and
|
|
173
|
+
* values are identical, so this method behaves the same as `values()`. It
|
|
174
|
+
* provides compatibility with the standard Set interface and allows use in
|
|
175
|
+
* contexts where keys are expected, despite RefSet not differentiating
|
|
176
|
+
* between keys and values.
|
|
177
|
+
*
|
|
178
|
+
* @returns {Iterator} An iterator for the keys.
|
|
179
|
+
*/
|
|
180
|
+
keys() {
|
|
181
|
+
return this.values();
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Determines whether an element with the specified value exists in the
|
|
185
|
+
* `RefSet`. For non-objectified sets, this method checks if the dereferenced
|
|
186
|
+
* values of the set include the specified value.
|
|
187
|
+
*
|
|
188
|
+
* For objectified sets, it uses the `contains` method which accounts for
|
|
189
|
+
* the objectification. This method differs from standard Set `has` in that
|
|
190
|
+
* it works with weak references and considers objectification settings.
|
|
191
|
+
*
|
|
192
|
+
* @param {*} value - The value to check for presence in the RefSet.
|
|
193
|
+
* @returns {boolean} - True if an element with the specified value exists
|
|
194
|
+
* in the RefSet, false otherwise.
|
|
195
|
+
*/
|
|
196
|
+
has(value) {
|
|
197
|
+
if (this.#objectifyValues) {
|
|
198
|
+
return this.contains(value);
|
|
199
|
+
}
|
|
200
|
+
for (const item of this.values()) {
|
|
201
|
+
if (item === value) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Checks if the RefSet contains a value that is equal to the specified
|
|
209
|
+
* value. This method is used primarily in objectified RefSets to determine
|
|
210
|
+
* the presence of a value, taking into account objectification. It differs
|
|
211
|
+
* from the `has` method in that it's tailored for sets that have
|
|
212
|
+
* transformed their primitive values into objects, whereas `has` is more
|
|
213
|
+
* general-purpose.
|
|
214
|
+
*
|
|
215
|
+
* @param {*} value - The value to search for in the RefSet.
|
|
216
|
+
* @returns {boolean} - True if the RefSet contains the value, false otherwise.
|
|
217
|
+
*/
|
|
218
|
+
contains(value) {
|
|
219
|
+
return !!(Array.from(this.values())
|
|
220
|
+
.filter(dereferencedValue => {
|
|
221
|
+
return value == dereferencedValue;
|
|
222
|
+
})
|
|
223
|
+
.length);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Creates a new array with all elements that pass the test implemented by
|
|
227
|
+
* the provided function. This method iterates over each element,
|
|
228
|
+
* dereferences it, and applies the filter function. Unlike Array `filter`,
|
|
229
|
+
* the callback receives the dereferenced value and not an index or array,
|
|
230
|
+
* reflecting the non-indexed nature of RefSet. Useful for selectively
|
|
231
|
+
* creating arrays from the set based on certain conditions, especially when
|
|
232
|
+
* dealing with weak references.
|
|
233
|
+
*
|
|
234
|
+
* @param {Function} filterFn - Function to test each element of the RefSet.
|
|
235
|
+
* The function receives the dereferenced value.
|
|
236
|
+
* @param {*} thisArg - Value to use as `this` when executing `filterFn`.
|
|
237
|
+
* @returns {Array} - A new array with the elements that pass the test.
|
|
238
|
+
*/
|
|
239
|
+
filter(filterFn, thisArg) {
|
|
240
|
+
const results = [];
|
|
241
|
+
for (const value of this) {
|
|
242
|
+
const dereferenced = value?.deref();
|
|
243
|
+
if (dereferenced) {
|
|
244
|
+
const include = filterFn.call(thisArg, dereferenced, NaN, this);
|
|
245
|
+
if (include) {
|
|
246
|
+
results.push(dereferenced);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return results;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Returns the value of the first element in the RefSet that satisfies the
|
|
254
|
+
* provided testing function. Similar to Array `find`, this method iterates
|
|
255
|
+
* over the RefSet, dereferencing each value and applying the testing
|
|
256
|
+
* function. The non-indexed nature of RefSet is considered, as the
|
|
257
|
+
* callback does not receive an index. This method is useful for finding a
|
|
258
|
+
* specific element based on a condition.
|
|
259
|
+
*
|
|
260
|
+
* @param {*} thisArg - Value to use as this when executing findFn.
|
|
261
|
+
* @returns {*} - The value of the first element in the RefSet that satisfies
|
|
262
|
+
* the testing function, or undefined if none found.
|
|
263
|
+
* @returns
|
|
264
|
+
*/
|
|
265
|
+
find(findFn, thisArg) {
|
|
266
|
+
for (const value of this) {
|
|
267
|
+
const dereferenced = value?.deref();
|
|
268
|
+
if (dereferenced) {
|
|
269
|
+
const found = findFn.call(thisArg, dereferenced, NaN, this);
|
|
270
|
+
if (found) {
|
|
271
|
+
return dereferenced;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Creates a new array or `RefSet` with the results of calling a provided
|
|
279
|
+
* function on every element in the calling `RefSet`. This method dereferences
|
|
280
|
+
* each value, applies the `mapFn`, and collects the results. If `toRefSet` is
|
|
281
|
+
* `true`, a new `RefSet` is returned; otherwise, an array. This method
|
|
282
|
+
* differs from `Array.map` in handling weak references and the potential to
|
|
283
|
+
* return a new `RefSet` instead of an array.
|
|
284
|
+
*
|
|
285
|
+
* @param {Function} mapFn - Function that produces an element of the new
|
|
286
|
+
* array or `RefSet`, taking three arguments.
|
|
287
|
+
* @param {*} thisArg - Value to use as this when executing mapFn.
|
|
288
|
+
* @param {boolean} toRefSet - Determines if the output should be a new
|
|
289
|
+
* `RefSet` (`true`) or an array (`false`).
|
|
290
|
+
* @param {boolean} mirrorObjectification - If `true` and `toRefSet` is
|
|
291
|
+
* `true`, the new `RefSet` mirrors the objectification setting of the
|
|
292
|
+
* original.
|
|
293
|
+
* @returns {Array|RefSet} - A new array or `RefSet` with each element being
|
|
294
|
+
* the result of the `mapFn`.
|
|
295
|
+
*/
|
|
296
|
+
map(mapFn, thisArg, toRefSet, mirrorObjectification) {
|
|
297
|
+
const mapped = [];
|
|
298
|
+
let validRefSetOutput = true;
|
|
299
|
+
let validRefSetOutputIfObjectified = true;
|
|
300
|
+
for (const value of this) {
|
|
301
|
+
const dereferenced = value?.deref();
|
|
302
|
+
if (dereferenced) {
|
|
303
|
+
const mappedItem = mapFn.call(thisArg, dereferenced, NaN, this);
|
|
304
|
+
if (validRefSetOutput || validRefSetOutputIfObjectified) {
|
|
305
|
+
const weakReferenceable = this.#validWeakRefTarget(mappedItem);
|
|
306
|
+
if (!weakReferenceable) {
|
|
307
|
+
validRefSetOutput = false;
|
|
308
|
+
if (validRefSetOutputIfObjectified) {
|
|
309
|
+
validRefSetOutputIfObjectified =
|
|
310
|
+
this.#validWeakRefTarget(Object(mappedItem));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
mapped.push(mappedItem);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (toRefSet) {
|
|
318
|
+
if (validRefSetOutput) {
|
|
319
|
+
return new RefSet(mapped).objectifying(mirrorObjectification ? this.objectifyValues : false);
|
|
320
|
+
}
|
|
321
|
+
if (validRefSetOutputIfObjectified) {
|
|
322
|
+
return new RefSet(mapped.map(value => {
|
|
323
|
+
return this.#validWeakRefTarget(value) ? value : Object(value);
|
|
324
|
+
})).objectifying();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return mapped;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Ensures that the constructor of this object instance's name
|
|
331
|
+
* is returned if the string tag for this instance is queried
|
|
332
|
+
*
|
|
333
|
+
* @returns {string} the name of the class
|
|
334
|
+
*/
|
|
335
|
+
get [Symbol.toStringTag]() {
|
|
336
|
+
return this.constructor.name;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Private method to check if a given value is a valid target for a WeakRef.
|
|
340
|
+
*
|
|
341
|
+
* @param {*} value - The value to check for validity as a WeakRef target.
|
|
342
|
+
* @returns {boolean} - True if the value is a valid WeakRef target,
|
|
343
|
+
* false otherwise.
|
|
344
|
+
* @private
|
|
345
|
+
*/
|
|
346
|
+
#validWeakRefTarget(value) {
|
|
347
|
+
return !((typeof value === 'symbol' && Symbol.keyFor(value) === undefined) ||
|
|
348
|
+
(typeof value !== 'object' && typeof value !== 'symbol') ||
|
|
349
|
+
(value === null || value === undefined));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
export const RefSetExtensions = new Extension(RefSet);
|
|
@@ -18,4 +18,47 @@ export const SymbolExtensions = new Patch(Symbol, {
|
|
|
18
18
|
isSymbol(value) {
|
|
19
19
|
return value && (typeof value === 'symbol');
|
|
20
20
|
},
|
|
21
|
+
/**
|
|
22
|
+
* Returns true if the supplied value is a Symbol created using
|
|
23
|
+
* `Symbol.for()`.
|
|
24
|
+
*
|
|
25
|
+
* @param {any} value assumption is that the supplied value is of type
|
|
26
|
+
* 'symbol' however, unless `allowOnlySymbols` is set to `true`, `false`
|
|
27
|
+
* will be returned for any non-symbol values.
|
|
28
|
+
* @param {boolean} allowOnlySymbols true if an error should be thrown
|
|
29
|
+
* if the supplied value is not of type 'symbol'
|
|
30
|
+
* @returns true if the symbol is registered, meaning, none of the spec
|
|
31
|
+
* static symbols (`toStringTag`, `iterator`, etc...), and no symbols
|
|
32
|
+
* created by passing a value directly to the Symbol function, such as
|
|
33
|
+
* `Symbol('name')`
|
|
34
|
+
*/
|
|
35
|
+
isRegistered(value, allowOnlySymbols = false) {
|
|
36
|
+
if (!Symbol.isSymbol(value)) {
|
|
37
|
+
if (allowOnlySymbols) {
|
|
38
|
+
throw new TypeError('allowOnlySymbols specified; value is not a symbol');
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return Symbol.keyFor(value) !== undefined;
|
|
43
|
+
},
|
|
44
|
+
/**
|
|
45
|
+
* A function that returns true if the symbol is not registered, meaning,
|
|
46
|
+
* any of the spec static symbols (`toStringTag`, `iterator`, etc...), and
|
|
47
|
+
* any symbols created by passing a value directly to the `Symbol` function,
|
|
48
|
+
* such as `Symbol('name')`.
|
|
49
|
+
*
|
|
50
|
+
* @param {any} value assumption is that the supplied value is of type
|
|
51
|
+
* 'symbol' however, unless allowOnlySymbols is set to true, false will
|
|
52
|
+
* be returned for any non-symbol values.
|
|
53
|
+
* @param {boolean} allowOnlySymbols true if an error should be thrown
|
|
54
|
+
* if the supplied value is not of type 'symbol'
|
|
55
|
+
* @returns true if the symbol is not registered, meaning, any of the
|
|
56
|
+
* spec static symbols (`toStringTag`, `iterator`, etc...), and any symbols
|
|
57
|
+
* created by passing a value directly to the `Symbol` function, such as
|
|
58
|
+
* `Symbol('name')`
|
|
59
|
+
* @returns
|
|
60
|
+
*/
|
|
61
|
+
isNonRegistered(value, allowOnlySymbols = false) {
|
|
62
|
+
return !Symbol.isRegistered(value, allowOnlySymbols);
|
|
63
|
+
},
|
|
21
64
|
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Patch } from '@nejs/extension';
|
|
2
|
+
export const WeakRefExtensions = new Patch(WeakRef, {
|
|
3
|
+
/**
|
|
4
|
+
* A static method to check if a given value is a valid target for a WeakRef.
|
|
5
|
+
*
|
|
6
|
+
* @param {*} value - The value to check for validity as a WeakRef target.
|
|
7
|
+
* @returns {boolean} - True if the value is a valid WeakRef target,
|
|
8
|
+
* false otherwise.
|
|
9
|
+
*/
|
|
10
|
+
isValidReference(value) {
|
|
11
|
+
return !((typeof value === 'symbol' && Symbol.keyFor(value) === undefined) ||
|
|
12
|
+
(typeof value !== 'object' && typeof value !== 'symbol') ||
|
|
13
|
+
(value === null || value === undefined));
|
|
14
|
+
},
|
|
15
|
+
});
|
package/package.json
CHANGED
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
"test": "jest"
|
|
47
47
|
},
|
|
48
48
|
"type": "module",
|
|
49
|
-
"version": "1.
|
|
49
|
+
"version": "1.5.0",
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@nejs/extension": "^1.2.1"
|
|
52
52
|
},
|
|
53
|
-
"browser": "dist/@nejs/basic-extensions.bundle.1.4.
|
|
53
|
+
"browser": "dist/@nejs/basic-extensions.bundle.1.4.1.js"
|
|
54
54
|
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { Extension } from '@nejs/extension'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The AsyncIterable class extends the concept of Iterable to asynchronous
|
|
5
|
+
* operations. It allows creating iterable objects where each element can be
|
|
6
|
+
* an asynchronous entity, like a Promise. This class is particularly useful
|
|
7
|
+
* when dealing with asynchronous data sources, such as API responses, file
|
|
8
|
+
* reading in chunks, or any other data that is not immediately available but
|
|
9
|
+
* arrives over time.
|
|
10
|
+
*/
|
|
11
|
+
class AsyncIterable {
|
|
12
|
+
/**
|
|
13
|
+
* Private field to store the elements of the async iterable.
|
|
14
|
+
* @private
|
|
15
|
+
*/
|
|
16
|
+
#elements = [];
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Constructs an instance of AsyncIterable. Similar to Iterable, it can be
|
|
20
|
+
* initialized with either an iterable object or individual elements. The
|
|
21
|
+
* elements can be promises, direct values, or a mix of both. If the first
|
|
22
|
+
* argument is an iterable, the instance is initialized with the elements
|
|
23
|
+
* from the iterable, followed by any additional arguments. If the first
|
|
24
|
+
* argument is not an iterable, all arguments are treated as individual
|
|
25
|
+
* elements.
|
|
26
|
+
*
|
|
27
|
+
* @param {Iterable|Promise|*} elementsOrFirstElement - An iterable object,
|
|
28
|
+
* a Promise, or the first element.
|
|
29
|
+
* @param {...Promise|*} moreElements - Additional elements if the first
|
|
30
|
+
* argument is not an iterable.
|
|
31
|
+
*/
|
|
32
|
+
constructor(elementsOrFirstElement, ...moreElements) {
|
|
33
|
+
if (
|
|
34
|
+
elementsOrFirstElement != null &&
|
|
35
|
+
typeof elementsOrFirstElement[Symbol.iterator] === 'function'
|
|
36
|
+
) {
|
|
37
|
+
this.#elements = [...elementsOrFirstElement, ...moreElements];
|
|
38
|
+
} else {
|
|
39
|
+
this.#elements = [elementsOrFirstElement, ...moreElements];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Implements the async iterable protocol. When an instance of AsyncIterable
|
|
45
|
+
* is used in a `for await...of` loop, this async generator function is
|
|
46
|
+
* invoked. It yields each element as a Promise, allowing asynchronous
|
|
47
|
+
* iteration. Elements that are not Promises are automatically wrapped in
|
|
48
|
+
* a resolved Promise to ensure consistency.
|
|
49
|
+
*
|
|
50
|
+
* @returns {AsyncGenerator} An async generator that yields each element as
|
|
51
|
+
* a Promise.
|
|
52
|
+
*/
|
|
53
|
+
async *[Symbol.asyncIterator]() {
|
|
54
|
+
for (const element of this.#elements) {
|
|
55
|
+
// Treat each element as a promise. If it's not, it's automatically
|
|
56
|
+
// wrapped as a resolved promise.
|
|
57
|
+
yield Promise.resolve(element);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Ensures that the constructor of this object instance's name
|
|
63
|
+
* is returned if the string tag for this instance is queried
|
|
64
|
+
*
|
|
65
|
+
* @returns {string} the name of the class
|
|
66
|
+
*/
|
|
67
|
+
get [Symbol.toStringTag]() {
|
|
68
|
+
return this.constructor.name
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Being able to create a compliant `AsyncIterator` around any type of
|
|
73
|
+
* iterable object. This can be wrapped around any type of object that
|
|
74
|
+
* has a `[Symbol.asyncIterator]` property assigned to a generator
|
|
75
|
+
* function.
|
|
76
|
+
*/
|
|
77
|
+
static AsyncIterator = class AsyncIterator {
|
|
78
|
+
/**
|
|
79
|
+
* Creates a new `AsyncIterator` object instance.
|
|
80
|
+
*
|
|
81
|
+
* @param {object} asyncIterable any object that has a
|
|
82
|
+
* `[Symbol.asyncIterable]` property assigned to a generator function.
|
|
83
|
+
*/
|
|
84
|
+
constructor(asyncIterable) {
|
|
85
|
+
if (!asyncIterable || !Reflect.has(asyncIterable, Symbol.asyncIterator)) {
|
|
86
|
+
throw new TypeError(
|
|
87
|
+
'Value used to instantiate AsyncIterator is not an async iterable'
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this.#asyncIterable = asyncIterable;
|
|
92
|
+
this.#asyncIterator = asyncIterable[Symbol.asyncIterator]();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Returns a new `Array` derived from the iterable this object
|
|
97
|
+
* wraps.
|
|
98
|
+
*
|
|
99
|
+
* @returns {array} a new `Array` generated from the wrapped
|
|
100
|
+
* iterable. The method is generated from using an async for of
|
|
101
|
+
* loop.
|
|
102
|
+
*/
|
|
103
|
+
async asArray() {
|
|
104
|
+
const array = []
|
|
105
|
+
|
|
106
|
+
for await (const value of this) {
|
|
107
|
+
array.push(value)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return array
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Returns the actual iterable object passed to the constructor that
|
|
115
|
+
* created this instance.
|
|
116
|
+
*
|
|
117
|
+
* @returns {object} the object containing the `[Symbol.iterator]`
|
|
118
|
+
*/
|
|
119
|
+
get asyncIterable() {
|
|
120
|
+
return this.#asyncIterable
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The function retrieves the next value in the iterator. If the
|
|
125
|
+
* the iterator has run its course, `reset()` can be invoked to
|
|
126
|
+
* reset the pointer to the beginning of the iteration.
|
|
127
|
+
*
|
|
128
|
+
* @returns {any} the next value
|
|
129
|
+
*/
|
|
130
|
+
async next() {
|
|
131
|
+
const result = await this.#asyncIterator.next();
|
|
132
|
+
if (result.done) {
|
|
133
|
+
return { value: undefined, done: true };
|
|
134
|
+
} else {
|
|
135
|
+
return { value: result.value, done: false };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Resets the async iterator to the beginning allowing it to be
|
|
141
|
+
* iterated over again.
|
|
142
|
+
*/
|
|
143
|
+
async reset() {
|
|
144
|
+
this.#asyncIterator = this.#asyncIterable[Symbol.asyncIterator]();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The existence of this symbol on the object instances, indicates that
|
|
149
|
+
* it can be used in `for(.. of ..)` loops and its values can be
|
|
150
|
+
* extracted from calls to `Array.from()`
|
|
151
|
+
*
|
|
152
|
+
* @returns {AsyncIterable} this is returned since this object is already
|
|
153
|
+
* conforming to the expected JavaScript AsyncIterator interface
|
|
154
|
+
*/
|
|
155
|
+
[Symbol.asyncIterator]() {
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Ensures that the constructor of this object instance's name
|
|
161
|
+
* is returned if the string tag for this instance is queried
|
|
162
|
+
*
|
|
163
|
+
* @returns {string} the name of the class
|
|
164
|
+
*/
|
|
165
|
+
get [Symbol.toStringTag]() {
|
|
166
|
+
return this.constructor.name;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The object from which its iterator functionality is derived.
|
|
171
|
+
*
|
|
172
|
+
* @type {object}
|
|
173
|
+
* @private
|
|
174
|
+
*/
|
|
175
|
+
#asyncIterable = null;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The results of a call to the iterable's `[Symbol.asyncIterator]`
|
|
179
|
+
* generator function.
|
|
180
|
+
*
|
|
181
|
+
* @type {object}
|
|
182
|
+
* @private
|
|
183
|
+
*/
|
|
184
|
+
#asyncIterator = null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Checks if a given value is an async iterable. This method determines if
|
|
189
|
+
* the provided value has a `Symbol.asyncIterator` property that is an async
|
|
190
|
+
* generator function. It's a precise way to identify if the value conforms
|
|
191
|
+
* to the async iterable protocol using an async generator function.
|
|
192
|
+
*
|
|
193
|
+
* Note: This method specifically checks for async generator functions. Some
|
|
194
|
+
* async iterables might use regular async functions that return an async
|
|
195
|
+
* iterator, which this method won't identify.
|
|
196
|
+
*
|
|
197
|
+
* @param {*} value - The value to be checked for async iterability.
|
|
198
|
+
* @returns {boolean} - Returns true if the value is an async iterable
|
|
199
|
+
* implemented using an async generator function, false otherwise.
|
|
200
|
+
*/
|
|
201
|
+
static isAsyncIterable(value) {
|
|
202
|
+
const type = Object.prototype.toString.call(value?.[Symbol.asyncIterator]);
|
|
203
|
+
return type === '[object AsyncGeneratorFunction]';
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export const AsyncIterableExtensions = new Extension(AsyncIterable)
|
|
208
|
+
export const AsyncIteratorExtensions = new Extension(AsyncIterable.AsyncIterator)
|
package/src/descriptor.js
CHANGED
|
@@ -192,6 +192,11 @@ class Descriptor {
|
|
|
192
192
|
(this.#desc || {}).set = value
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
[Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
|
|
196
|
+
const type = this.isAccessor ? ' (Accessor)' : this.isData ? ' (Data)' : ''
|
|
197
|
+
return `Descriptor${type} ${inspect(this.#desc, {...options, depth})}`
|
|
198
|
+
}
|
|
199
|
+
|
|
195
200
|
/**
|
|
196
201
|
* Shorthand for Object.getOwnPropertyDescriptor()
|
|
197
202
|
*
|
|
@@ -258,6 +263,16 @@ class Descriptor {
|
|
|
258
263
|
}
|
|
259
264
|
}
|
|
260
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Ensures that the constructor of this object instance's name
|
|
268
|
+
* is returned if the string tag for this instance is queried
|
|
269
|
+
*
|
|
270
|
+
* @returns {string} the name of the class
|
|
271
|
+
*/
|
|
272
|
+
get [Symbol.toStringTag]() {
|
|
273
|
+
return this.constructor.name
|
|
274
|
+
}
|
|
275
|
+
|
|
261
276
|
/**
|
|
262
277
|
* The function `getData` retrieves the value of a property from an object if it
|
|
263
278
|
* exists and is a data property.
|
|
@@ -570,4 +585,4 @@ class Descriptor {
|
|
|
570
585
|
}
|
|
571
586
|
}
|
|
572
587
|
|
|
573
|
-
export const
|
|
588
|
+
export const DescriptorExtensions = new Extension(Descriptor)
|