@nejs/basic-extensions 1.6.1 → 1.7.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/.esdoc.json +9 -0
- package/README.md +2025 -11
- package/bin/clean +6 -0
- package/dist/@nejs/basic-extensions.bundle.1.6.1.js +4 -0
- package/dist/@nejs/basic-extensions.bundle.1.6.1.js.map +7 -0
- package/dist/cjs/arrayextensions.js +1 -0
- package/dist/cjs/arrayextensions.js.map +1 -0
- package/dist/cjs/functionextensions.js +1 -0
- package/dist/cjs/functionextensions.js.map +1 -0
- package/dist/cjs/globals.js +2 -1
- package/dist/cjs/globals.js.map +1 -0
- package/dist/cjs/index.d.ts +10 -19
- package/dist/cjs/index.js +69 -76
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mapextensions.d.ts +2 -0
- package/dist/cjs/mapextensions.js +30 -0
- package/dist/cjs/mapextensions.js.map +1 -0
- package/dist/cjs/newClasses/asyncIterable.d.ts +123 -0
- package/dist/cjs/{asyncIterable.js → newClasses/asyncIterable.js} +7 -4
- package/dist/cjs/newClasses/asyncIterable.js.map +1 -0
- package/dist/cjs/newClasses/descriptor.d.ts +401 -0
- package/dist/cjs/{descriptor.js → newClasses/descriptor.js} +150 -80
- package/dist/cjs/newClasses/descriptor.js.map +1 -0
- package/dist/cjs/newClasses/iterable.d.ts +125 -0
- package/dist/cjs/{iterable.js → newClasses/iterable.js} +36 -10
- package/dist/cjs/newClasses/iterable.js.map +1 -0
- package/dist/cjs/newClasses/refmap.d.ts +238 -0
- package/dist/cjs/newClasses/refmap.js +433 -0
- package/dist/cjs/newClasses/refmap.js.map +1 -0
- package/dist/cjs/newClasses/refset.d.ts +186 -0
- package/dist/cjs/{refset.js → newClasses/refset.js} +4 -2
- package/dist/cjs/newClasses/refset.js.map +1 -0
- package/dist/cjs/objectextensions.d.ts +7 -6
- package/dist/cjs/objectextensions.js +85 -43
- package/dist/cjs/objectextensions.js.map +1 -0
- package/dist/cjs/reflectextensions.js +16 -12
- package/dist/cjs/reflectextensions.js.map +1 -0
- package/dist/cjs/stringextensions.js +1 -0
- package/dist/cjs/stringextensions.js.map +1 -0
- package/dist/cjs/symbolextensions.js +3 -1
- package/dist/cjs/symbolextensions.js.map +1 -0
- package/dist/cjs/weakrefextensions.js +1 -0
- package/dist/cjs/weakrefextensions.js.map +1 -0
- package/dist/mjs/arrayextensions.js +1 -0
- package/dist/mjs/arrayextensions.js.map +1 -0
- package/dist/mjs/functionextensions.js +1 -0
- package/dist/mjs/functionextensions.js.map +1 -0
- package/dist/mjs/globals.js +2 -1
- package/dist/mjs/globals.js.map +1 -0
- package/dist/mjs/index.d.ts +10 -19
- package/dist/mjs/index.js +67 -60
- package/dist/mjs/index.js.map +1 -0
- package/dist/mjs/mapextensions.d.ts +2 -0
- package/dist/mjs/mapextensions.js +27 -0
- package/dist/mjs/mapextensions.js.map +1 -0
- package/dist/mjs/newClasses/asyncIterable.d.ts +123 -0
- package/dist/mjs/{asyncIterable.js → newClasses/asyncIterable.js} +106 -105
- package/dist/mjs/newClasses/asyncIterable.js.map +1 -0
- package/dist/mjs/newClasses/descriptor.d.ts +401 -0
- package/dist/mjs/{descriptor.js → newClasses/descriptor.js} +129 -67
- package/dist/mjs/newClasses/descriptor.js.map +1 -0
- package/dist/mjs/newClasses/iterable.d.ts +125 -0
- package/dist/mjs/newClasses/iterable.js +199 -0
- package/dist/mjs/newClasses/iterable.js.map +1 -0
- package/dist/mjs/newClasses/refmap.d.ts +238 -0
- package/dist/mjs/newClasses/refmap.js +417 -0
- package/dist/mjs/newClasses/refmap.js.map +1 -0
- package/dist/mjs/newClasses/refset.d.ts +186 -0
- package/dist/mjs/{refset.js → newClasses/refset.js} +3 -2
- package/dist/mjs/newClasses/refset.js.map +1 -0
- package/dist/mjs/objectextensions.d.ts +7 -6
- package/dist/mjs/objectextensions.js +84 -42
- package/dist/mjs/objectextensions.js.map +1 -0
- package/dist/mjs/reflectextensions.js +16 -12
- package/dist/mjs/reflectextensions.js.map +1 -0
- package/dist/mjs/stringextensions.js +1 -0
- package/dist/mjs/stringextensions.js.map +1 -0
- package/dist/mjs/symbolextensions.js +3 -1
- package/dist/mjs/symbolextensions.js.map +1 -0
- package/dist/mjs/weakrefextensions.js +1 -0
- package/dist/mjs/weakrefextensions.js.map +1 -0
- package/docs/assets/anchor.js +350 -0
- package/docs/assets/bass-addons.css +12 -0
- package/docs/assets/bass.css +544 -0
- package/docs/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/docs/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/docs/assets/fonts/LICENSE.txt +93 -0
- package/docs/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/docs/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/docs/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/docs/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/docs/assets/fonts/source-code-pro.css +23 -0
- package/docs/assets/github.css +123 -0
- package/docs/assets/site.js +168 -0
- package/docs/assets/split.css +15 -0
- package/docs/assets/split.js +782 -0
- package/docs/assets/style.css +147 -0
- package/docs/index.html +13060 -0
- package/jsdoc-config.json +31 -0
- package/package.json +12 -3
- package/src/globals.js +1 -1
- package/src/index.js +75 -82
- package/src/mapextensions.js +30 -0
- package/src/{asyncIterable.js → newClasses/asyncIterable.js} +117 -117
- package/src/{descriptor.js → newClasses/descriptor.js} +137 -74
- package/src/newClasses/iterable.js +221 -0
- package/src/newClasses/refmap.js +483 -0
- package/src/{refset.js → newClasses/refset.js} +2 -2
- package/src/objectextensions.js +97 -46
- package/src/reflectextensions.js +16 -14
- package/src/symbolextensions.js +2 -1
- package/tests/index.test.js +1 -1
- package/tests/{asyncIterable.test.js → newClasses/asyncIterable.test.js} +3 -4
- package/tests/newClasses/descriptor.test.js +252 -0
- package/tests/{iterable.test.js → newClasses/iterable.test.js} +2 -4
- package/tests/newClasses/refmap.test.js +69 -0
- package/tests/{refset.test.js → newClasses/refset.test.js} +2 -4
- package/tests/objectextensions.test.js +128 -0
- package/tsconfig.base.json +2 -1
- package/dist/@nejs/basic-extensions.bundle.1.6.0.js +0 -2
- package/dist/@nejs/basic-extensions.bundle.1.6.0.js.map +0 -7
- package/dist/cjs/asyncIterable.d.ts +0 -3
- package/dist/cjs/descriptor.d.ts +0 -2
- package/dist/cjs/iterable.d.ts +0 -3
- package/dist/cjs/refset.d.ts +0 -2
- package/dist/mjs/asyncIterable.d.ts +0 -3
- package/dist/mjs/descriptor.d.ts +0 -2
- package/dist/mjs/iterable.d.ts +0 -3
- package/dist/mjs/iterable.js +0 -184
- package/dist/mjs/refset.d.ts +0 -2
- package/src/iterable.js +0 -203
package/dist/mjs/iterable.js
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import { Extension } from '@nejs/extension';
|
|
2
|
-
/**
|
|
3
|
-
* The Iterable class is designed to provide a convenient way to create synchronous
|
|
4
|
-
* iterable objects. It can be initialized with either an array or individual elements.
|
|
5
|
-
* This class implements the iterable protocol, allowing instances to be used with
|
|
6
|
-
* `for...of` loops and other standard JavaScript iteration mechanisms.
|
|
7
|
-
*/
|
|
8
|
-
class Iterable {
|
|
9
|
-
/**
|
|
10
|
-
* Private field to store the elements of the iterable.
|
|
11
|
-
* @private
|
|
12
|
-
*/
|
|
13
|
-
#elements = [];
|
|
14
|
-
/**
|
|
15
|
-
* Constructs an instance of Iterable. It can be initialized with either an
|
|
16
|
-
* iterable object (such as an array, Set, Map, string, or any object
|
|
17
|
-
* implementing the iterable protocol) or individual elements. If the first
|
|
18
|
-
* argument is an iterable, the class instance is initialized with the
|
|
19
|
-
* elements from the iterable, followed by any additional arguments. If the
|
|
20
|
-
* first argument is not an iterable, all arguments are treated as individual
|
|
21
|
-
* elements.
|
|
22
|
-
*
|
|
23
|
-
* @param {Iterable|*} elementsOrFirstElement - An iterable object or the
|
|
24
|
-
* first element.
|
|
25
|
-
* @param {...*} moreElements - Additional elements if the first argument is
|
|
26
|
-
* not an iterable.
|
|
27
|
-
*/
|
|
28
|
-
constructor(elementsOrFirstElement, ...moreElements) {
|
|
29
|
-
if (elementsOrFirstElement != null &&
|
|
30
|
-
typeof elementsOrFirstElement[Symbol.iterator] === 'function') {
|
|
31
|
-
this.#elements = [...elementsOrFirstElement, ...moreElements];
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
this.#elements = [elementsOrFirstElement, ...moreElements];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Implements the iterable protocol. When an instance of Iterable is used
|
|
39
|
-
* in a `for...of` loop or spread syntax, this generator function is invoked
|
|
40
|
-
* to yield the elements one by one in a synchronous manner.
|
|
41
|
-
*
|
|
42
|
-
* @returns {Generator} A generator that yields each element of the iterable.
|
|
43
|
-
*/
|
|
44
|
-
*[Symbol.iterator]() {
|
|
45
|
-
for (const element of this.#elements) {
|
|
46
|
-
yield element;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Provides access to the elements as a standard array. Useful for scenarios
|
|
51
|
-
* where array methods and behaviors are needed.
|
|
52
|
-
*
|
|
53
|
-
* @returns {Array} An array containing all the elements of the iterable.
|
|
54
|
-
*/
|
|
55
|
-
get asArray() {
|
|
56
|
-
return this.#elements;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Ensures that the constructor of this object instance's name
|
|
60
|
-
* is returned if the string tag for this instance is queried
|
|
61
|
-
*
|
|
62
|
-
* @returns {string} the name of the class
|
|
63
|
-
*/
|
|
64
|
-
get [Symbol.toStringTag]() {
|
|
65
|
-
return this.constructor.name;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Being able to create a compliant `Iterator` around any type of iterable
|
|
69
|
-
* object. This can be wrapped around any type of object that has a
|
|
70
|
-
* `[Symbol.iterator]` property assigned to a generator function.
|
|
71
|
-
*/
|
|
72
|
-
static Iterator = class Iterator {
|
|
73
|
-
/**
|
|
74
|
-
* Creates a new `Iterator` object instance.
|
|
75
|
-
*
|
|
76
|
-
* @param {object} iterable any object that has a `[Symbol.iterator]`
|
|
77
|
-
* property assigned to a generator function.
|
|
78
|
-
*/
|
|
79
|
-
constructor(iterable) {
|
|
80
|
-
if (!iterable || !Reflect.has(iterable, Symbol.iterator)) {
|
|
81
|
-
throw new TypeError('Value used to instantiate Iterator is not iterable');
|
|
82
|
-
}
|
|
83
|
-
this.#iterable = iterable;
|
|
84
|
-
this.#iterator = iterable[Symbol.iterator]();
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Returns a new `Array` derived from the iterable this object
|
|
88
|
-
* wraps.
|
|
89
|
-
*
|
|
90
|
-
* @returns {array} a new `Array` generated from the wrapped
|
|
91
|
-
* iterable. The method is generated from `Array.from()`
|
|
92
|
-
*/
|
|
93
|
-
get asArray() {
|
|
94
|
-
return Array.from(this.#iterable);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Returns the actual iterable object passed to the constructor that
|
|
98
|
-
* created this instance.
|
|
99
|
-
*
|
|
100
|
-
* @returns {object} the object containing the `[Symbol.iterator]`
|
|
101
|
-
*/
|
|
102
|
-
get iterable() {
|
|
103
|
-
return this.#iterable;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* The function retrieves the next value in the iterator. If the
|
|
107
|
-
* the iterator has run its course, `reset()` can be invoked to
|
|
108
|
-
* reset the pointer to the beginning of the iteration.
|
|
109
|
-
*
|
|
110
|
-
* @returns {any} the next value
|
|
111
|
-
*/
|
|
112
|
-
next() {
|
|
113
|
-
const result = this.#iterator.next();
|
|
114
|
-
if (result.done) {
|
|
115
|
-
return { value: undefined, done: true };
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
return { value: result.value, done: false };
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Resets the iterator to the beginning allowing it to be
|
|
123
|
-
* iterated over again.
|
|
124
|
-
*/
|
|
125
|
-
reset() {
|
|
126
|
-
this.#iterator = this.#iterable[Symbol.iterator]();
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* The existence of this symbol on the object instances, indicates that
|
|
130
|
-
* it can be used in `for(.. of ..)` loops and its values can be
|
|
131
|
-
* extracted from calls to `Array.from()`
|
|
132
|
-
*
|
|
133
|
-
* @returns {Iterator} this is returned since this object is already
|
|
134
|
-
* conforming to the expected JavaScript Iterator interface
|
|
135
|
-
*/
|
|
136
|
-
[Symbol.iterator]() {
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Ensures that the constructor of this object instance's name
|
|
141
|
-
* is returned if the string tag for this instance is queried
|
|
142
|
-
*
|
|
143
|
-
* @returns {string} the name of the class
|
|
144
|
-
*/
|
|
145
|
-
get [Symbol.toStringTag]() {
|
|
146
|
-
return this.constructor.name;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* The object from which its iterator functionality is derived.
|
|
150
|
-
*
|
|
151
|
-
* @type {object}
|
|
152
|
-
* @private
|
|
153
|
-
*/
|
|
154
|
-
#iterable = null;
|
|
155
|
-
/**
|
|
156
|
-
* The results of a call to the iterable's `[Symbol.iterator]`
|
|
157
|
-
* generator function.
|
|
158
|
-
*
|
|
159
|
-
* @type {object}
|
|
160
|
-
* @private
|
|
161
|
-
*/
|
|
162
|
-
#iterator = null;
|
|
163
|
-
};
|
|
164
|
-
/**
|
|
165
|
-
* Checks if a given value is an iterable. This method determines if the
|
|
166
|
-
* provided value has a `Symbol.iterator` property that is a generator
|
|
167
|
-
* function. It's a precise way to identify if the value conforms to the
|
|
168
|
-
* iterable protocol using a generator function.
|
|
169
|
-
*
|
|
170
|
-
* Note: This method specifically checks for generator functions. Some
|
|
171
|
-
* iterables might use regular functions that return an iterator, which
|
|
172
|
-
* this method won't identify.
|
|
173
|
-
*
|
|
174
|
-
* @param {*} value - The value to be checked for iterability.
|
|
175
|
-
* @returns {boolean} - Returns true if the value is an iterable implemented
|
|
176
|
-
* using a generator function, false otherwise.
|
|
177
|
-
*/
|
|
178
|
-
static isIterable(value) {
|
|
179
|
-
const type = Object.prototype.toString.call(value?.[Symbol.iterator]);
|
|
180
|
-
return type === '[object GeneratorFunction]';
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
export const IterableExtensions = new Extension(Iterable);
|
|
184
|
-
export const IteratorExtensions = new Extension(Iterable.Iterator);
|
package/dist/mjs/refset.d.ts
DELETED
package/src/iterable.js
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
import { Extension } from '@nejs/extension'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The Iterable class is designed to provide a convenient way to create synchronous
|
|
5
|
-
* iterable objects. It can be initialized with either an array or individual elements.
|
|
6
|
-
* This class implements the iterable protocol, allowing instances to be used with
|
|
7
|
-
* `for...of` loops and other standard JavaScript iteration mechanisms.
|
|
8
|
-
*/
|
|
9
|
-
class Iterable {
|
|
10
|
-
/**
|
|
11
|
-
* Private field to store the elements of the iterable.
|
|
12
|
-
* @private
|
|
13
|
-
*/
|
|
14
|
-
#elements = [];
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Constructs an instance of Iterable. It can be initialized with either an
|
|
18
|
-
* iterable object (such as an array, Set, Map, string, or any object
|
|
19
|
-
* implementing the iterable protocol) or individual elements. If the first
|
|
20
|
-
* argument is an iterable, the class instance is initialized with the
|
|
21
|
-
* elements from the iterable, followed by any additional arguments. If the
|
|
22
|
-
* first argument is not an iterable, all arguments are treated as individual
|
|
23
|
-
* elements.
|
|
24
|
-
*
|
|
25
|
-
* @param {Iterable|*} elementsOrFirstElement - An iterable object or the
|
|
26
|
-
* first element.
|
|
27
|
-
* @param {...*} moreElements - Additional elements if the first argument is
|
|
28
|
-
* not an iterable.
|
|
29
|
-
*/
|
|
30
|
-
constructor(elementsOrFirstElement, ...moreElements) {
|
|
31
|
-
if (
|
|
32
|
-
elementsOrFirstElement != null &&
|
|
33
|
-
typeof elementsOrFirstElement[Symbol.iterator] === 'function'
|
|
34
|
-
) {
|
|
35
|
-
this.#elements = [...elementsOrFirstElement, ...moreElements];
|
|
36
|
-
} else {
|
|
37
|
-
this.#elements = [elementsOrFirstElement, ...moreElements];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Implements the iterable protocol. When an instance of Iterable is used
|
|
43
|
-
* in a `for...of` loop or spread syntax, this generator function is invoked
|
|
44
|
-
* to yield the elements one by one in a synchronous manner.
|
|
45
|
-
*
|
|
46
|
-
* @returns {Generator} A generator that yields each element of the iterable.
|
|
47
|
-
*/
|
|
48
|
-
*[Symbol.iterator]() {
|
|
49
|
-
for (const element of this.#elements) {
|
|
50
|
-
yield element;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Provides access to the elements as a standard array. Useful for scenarios
|
|
56
|
-
* where array methods and behaviors are needed.
|
|
57
|
-
*
|
|
58
|
-
* @returns {Array} An array containing all the elements of the iterable.
|
|
59
|
-
*/
|
|
60
|
-
get asArray() {
|
|
61
|
-
return this.#elements;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Ensures that the constructor of this object instance's name
|
|
66
|
-
* is returned if the string tag for this instance is queried
|
|
67
|
-
*
|
|
68
|
-
* @returns {string} the name of the class
|
|
69
|
-
*/
|
|
70
|
-
get [Symbol.toStringTag]() {
|
|
71
|
-
return this.constructor.name
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Being able to create a compliant `Iterator` around any type of iterable
|
|
76
|
-
* object. This can be wrapped around any type of object that has a
|
|
77
|
-
* `[Symbol.iterator]` property assigned to a generator function.
|
|
78
|
-
*/
|
|
79
|
-
static Iterator = class Iterator {
|
|
80
|
-
/**
|
|
81
|
-
* Creates a new `Iterator` object instance.
|
|
82
|
-
*
|
|
83
|
-
* @param {object} iterable any object that has a `[Symbol.iterator]`
|
|
84
|
-
* property assigned to a generator function.
|
|
85
|
-
*/
|
|
86
|
-
constructor(iterable) {
|
|
87
|
-
if (!iterable || !Reflect.has(iterable, Symbol.iterator)) {
|
|
88
|
-
throw new TypeError(
|
|
89
|
-
'Value used to instantiate Iterator is not iterable'
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
this.#iterable = iterable;
|
|
94
|
-
this.#iterator = iterable[Symbol.iterator]();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Returns a new `Array` derived from the iterable this object
|
|
99
|
-
* wraps.
|
|
100
|
-
*
|
|
101
|
-
* @returns {array} a new `Array` generated from the wrapped
|
|
102
|
-
* iterable. The method is generated from `Array.from()`
|
|
103
|
-
*/
|
|
104
|
-
get asArray() {
|
|
105
|
-
return Array.from(this.#iterable)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Returns the actual iterable object passed to the constructor that
|
|
110
|
-
* created this instance.
|
|
111
|
-
*
|
|
112
|
-
* @returns {object} the object containing the `[Symbol.iterator]`
|
|
113
|
-
*/
|
|
114
|
-
get iterable() {
|
|
115
|
-
return this.#iterable
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The function retrieves the next value in the iterator. If the
|
|
120
|
-
* the iterator has run its course, `reset()` can be invoked to
|
|
121
|
-
* reset the pointer to the beginning of the iteration.
|
|
122
|
-
*
|
|
123
|
-
* @returns {any} the next value
|
|
124
|
-
*/
|
|
125
|
-
next() {
|
|
126
|
-
const result = this.#iterator.next();
|
|
127
|
-
if (result.done) {
|
|
128
|
-
return { value: undefined, done: true };
|
|
129
|
-
} else {
|
|
130
|
-
return { value: result.value, done: false };
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Resets the iterator to the beginning allowing it to be
|
|
136
|
-
* iterated over again.
|
|
137
|
-
*/
|
|
138
|
-
reset() {
|
|
139
|
-
this.#iterator = this.#iterable[Symbol.iterator]();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* The existence of this symbol on the object instances, indicates that
|
|
144
|
-
* it can be used in `for(.. of ..)` loops and its values can be
|
|
145
|
-
* extracted from calls to `Array.from()`
|
|
146
|
-
*
|
|
147
|
-
* @returns {Iterator} this is returned since this object is already
|
|
148
|
-
* conforming to the expected JavaScript Iterator interface
|
|
149
|
-
*/
|
|
150
|
-
[Symbol.iterator]() {
|
|
151
|
-
return this;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Ensures that the constructor of this object instance's name
|
|
156
|
-
* is returned if the string tag for this instance is queried
|
|
157
|
-
*
|
|
158
|
-
* @returns {string} the name of the class
|
|
159
|
-
*/
|
|
160
|
-
get [Symbol.toStringTag]() {
|
|
161
|
-
return this.constructor.name
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* The object from which its iterator functionality is derived.
|
|
166
|
-
*
|
|
167
|
-
* @type {object}
|
|
168
|
-
* @private
|
|
169
|
-
*/
|
|
170
|
-
#iterable = null;
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* The results of a call to the iterable's `[Symbol.iterator]`
|
|
174
|
-
* generator function.
|
|
175
|
-
*
|
|
176
|
-
* @type {object}
|
|
177
|
-
* @private
|
|
178
|
-
*/
|
|
179
|
-
#iterator = null;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Checks if a given value is an iterable. This method determines if the
|
|
184
|
-
* provided value has a `Symbol.iterator` property that is a generator
|
|
185
|
-
* function. It's a precise way to identify if the value conforms to the
|
|
186
|
-
* iterable protocol using a generator function.
|
|
187
|
-
*
|
|
188
|
-
* Note: This method specifically checks for generator functions. Some
|
|
189
|
-
* iterables might use regular functions that return an iterator, which
|
|
190
|
-
* this method won't identify.
|
|
191
|
-
*
|
|
192
|
-
* @param {*} value - The value to be checked for iterability.
|
|
193
|
-
* @returns {boolean} - Returns true if the value is an iterable implemented
|
|
194
|
-
* using a generator function, false otherwise.
|
|
195
|
-
*/
|
|
196
|
-
static isIterable(value) {
|
|
197
|
-
const type = Object.prototype.toString.call(value?.[Symbol.iterator]);
|
|
198
|
-
return type === '[object GeneratorFunction]';
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export const IterableExtensions = new Extension(Iterable)
|
|
203
|
-
export const IteratorExtensions = new Extension(Iterable.Iterator)
|