@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
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"tags": {
|
|
3
|
+
"allowUnknownTags": true
|
|
4
|
+
},
|
|
5
|
+
"source": {
|
|
6
|
+
"include": [
|
|
7
|
+
"src"
|
|
8
|
+
]
|
|
9
|
+
},
|
|
10
|
+
"plugins": ["plugins/markdown"],
|
|
11
|
+
"templates": {
|
|
12
|
+
"cleverLinks": false,
|
|
13
|
+
"monospaceLinks": false,
|
|
14
|
+
"default": {
|
|
15
|
+
"outputSourceFiles": true
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"opts": {
|
|
19
|
+
"encoding": "utf8",
|
|
20
|
+
"destination": "docs",
|
|
21
|
+
"recurse": true,
|
|
22
|
+
"template": "node_modules/docdash"
|
|
23
|
+
},
|
|
24
|
+
"markdown.hardwrap": true,
|
|
25
|
+
"markdown": {
|
|
26
|
+
"hardwrap": true
|
|
27
|
+
},
|
|
28
|
+
"metadata": {
|
|
29
|
+
"title": "@nejs/basic-extensions docs"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -3,10 +3,17 @@
|
|
|
3
3
|
"description": "Basic but commonly used extensions",
|
|
4
4
|
"devDependencies": {
|
|
5
5
|
"@jest/expect": "^29.7.0",
|
|
6
|
+
"clean-documentation-theme": "^0.5.2",
|
|
7
|
+
"docdash": "^2.0.2",
|
|
8
|
+
"documentation": "^14.0.2",
|
|
9
|
+
"documentation-devseed-theme": "^0.1.7",
|
|
6
10
|
"esbuild": "^0.19.10",
|
|
11
|
+
"esdoc": "^1.1.0",
|
|
12
|
+
"esdoc-standard-plugin": "^1.0.0",
|
|
7
13
|
"jest": "^29.7.0",
|
|
8
14
|
"jest-cli": "^29.7.0",
|
|
9
15
|
"jest-localstorage-mock": "^2.4.26",
|
|
16
|
+
"jsdoc": "^4.0.2",
|
|
10
17
|
"prompts": "^2.4.2",
|
|
11
18
|
"rimraf": "^5.0.5",
|
|
12
19
|
"typescript": "^5.2.2"
|
|
@@ -42,13 +49,15 @@
|
|
|
42
49
|
"build": "bin/clean && bin/esbuild && bin/build",
|
|
43
50
|
"browser": "bin/esbuild",
|
|
44
51
|
"clean": "bin/clean",
|
|
52
|
+
"jsdoc": "jsdoc -c jsdoc-config.json -p -a all -R README.md",
|
|
53
|
+
"documentation": "documentation build src/** -f html --github -o docs && documentation readme src/index.js --section=API",
|
|
45
54
|
"module": "bin/build",
|
|
46
55
|
"test": "jest"
|
|
47
56
|
},
|
|
48
57
|
"type": "module",
|
|
49
|
-
"version": "1.
|
|
58
|
+
"version": "1.7.0",
|
|
50
59
|
"dependencies": {
|
|
51
|
-
"@nejs/extension": "^1.
|
|
60
|
+
"@nejs/extension": "^2.1.0"
|
|
52
61
|
},
|
|
53
|
-
"browser": "dist/@nejs/basic-extensions.bundle.1.6.
|
|
62
|
+
"browser": "dist/@nejs/basic-extensions.bundle.1.6.1.js"
|
|
54
63
|
}
|
package/src/globals.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Patch } from '@nejs/extension'
|
|
2
2
|
import { FunctionExtensions } from './functionextensions.js'
|
|
3
3
|
|
|
4
|
-
const { isClass, isFunction } = FunctionExtensions.
|
|
4
|
+
const { isClass, isFunction } = FunctionExtensions.patches
|
|
5
5
|
const CustomInspect = Symbol.for('nodejs.util.inspect.custom')
|
|
6
6
|
|
|
7
7
|
export const GlobalFunctionsAndProps = new Patch(globalThis, {
|
package/src/index.js
CHANGED
|
@@ -1,97 +1,93 @@
|
|
|
1
1
|
import { FunctionExtensions } from './functionextensions.js'
|
|
2
|
-
import { ObjectExtensions } from './objectextensions.js'
|
|
2
|
+
import { ObjectExtensions, ObjectPrototypeExtensions } from './objectextensions.js'
|
|
3
|
+
import { MapPrototypeExtensions } from './mapextensions.js'
|
|
3
4
|
import { ReflectExtensions } from './reflectextensions.js'
|
|
4
5
|
import { StringExtensions } from './stringextensions.js'
|
|
5
6
|
import { SymbolExtensions } from './symbolextensions.js'
|
|
6
7
|
import { ArrayPrototypeExtensions } from './arrayextensions.js'
|
|
7
|
-
import { DescriptorExtensions } from './descriptor.js'
|
|
8
|
+
import { DescriptorExtensions, Descriptor } from './newClasses/descriptor.js'
|
|
8
9
|
import { GlobalFunctionsAndProps } from './globals.js'
|
|
9
|
-
import { RefSetExtensions } from './refset.js'
|
|
10
|
+
import { RefSetExtensions } from './newClasses/refset.js'
|
|
11
|
+
import { RefMapExtensions } from './newClasses/refmap.js'
|
|
10
12
|
|
|
11
13
|
import {
|
|
12
14
|
AsyncIteratorExtensions,
|
|
13
15
|
AsyncIterableExtensions
|
|
14
|
-
} from './asyncIterable.js'
|
|
16
|
+
} from './newClasses/asyncIterable.js'
|
|
15
17
|
|
|
16
18
|
import {
|
|
17
19
|
IteratorExtensions,
|
|
18
20
|
IterableExtensions
|
|
19
|
-
} from './iterable.js'
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
DescriptorExtensions,
|
|
36
|
-
AsyncIterableExtensions,
|
|
37
|
-
AsyncIteratorExtensions,
|
|
38
|
-
IterableExtensions,
|
|
39
|
-
IteratorExtensions,
|
|
40
|
-
RefSetExtensions,
|
|
41
|
-
]
|
|
42
|
-
|
|
43
|
-
export function enableAll(owners) {
|
|
44
|
-
const list = owners || Owners
|
|
45
|
-
|
|
46
|
-
if (!list) {
|
|
47
|
-
throw new Error('Unable to enable features without owners list')
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
list.forEach(owner => {
|
|
51
|
-
Patch.enableFor(owner)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
enableNetNew()
|
|
21
|
+
} from './newClasses/iterable.js'
|
|
22
|
+
|
|
23
|
+
const Patches = new Map([
|
|
24
|
+
[Object, ObjectExtensions],
|
|
25
|
+
[Function, FunctionExtensions],
|
|
26
|
+
[Reflect, ReflectExtensions],
|
|
27
|
+
[String, StringExtensions],
|
|
28
|
+
[Symbol, SymbolExtensions],
|
|
29
|
+
|
|
30
|
+
[Object.prototype, ObjectPrototypeExtensions],
|
|
31
|
+
[Array.prototype, ArrayPrototypeExtensions],
|
|
32
|
+
[Map.prototype, MapPrototypeExtensions],
|
|
33
|
+
[globalThis, GlobalFunctionsAndProps],
|
|
34
|
+
])
|
|
35
|
+
|
|
36
|
+
const Extensions = {
|
|
37
|
+
[DescriptorExtensions.key]: DescriptorExtensions,
|
|
38
|
+
[AsyncIterableExtensions.key]: AsyncIterableExtensions,
|
|
39
|
+
[AsyncIteratorExtensions.key]: AsyncIteratorExtensions,
|
|
40
|
+
[IterableExtensions.key]: IterableExtensions,
|
|
41
|
+
[IteratorExtensions.key]: IteratorExtensions,
|
|
42
|
+
[RefSetExtensions.key]: RefSetExtensions,
|
|
43
|
+
[RefMapExtensions.key]: RefMapExtensions,
|
|
55
44
|
}
|
|
56
45
|
|
|
57
|
-
|
|
58
|
-
NetNew.forEach(extension => { extension.apply() })
|
|
59
|
-
}
|
|
46
|
+
const Controls = {}
|
|
60
47
|
|
|
61
|
-
|
|
62
|
-
|
|
48
|
+
Object.assign(Controls, {
|
|
49
|
+
enableAll() {
|
|
50
|
+
Controls.enablePatches()
|
|
51
|
+
Controls.enableExtensions()
|
|
52
|
+
},
|
|
63
53
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
54
|
+
enablePatches() {
|
|
55
|
+
Patches.forEach((extension) => { extension.apply() })
|
|
56
|
+
},
|
|
67
57
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
58
|
+
enableExtensions() {
|
|
59
|
+
Object.values(Extensions).forEach((extension) => { extension.apply() })
|
|
60
|
+
},
|
|
71
61
|
|
|
72
|
-
|
|
73
|
-
|
|
62
|
+
disableAll(owners) {
|
|
63
|
+
Controls.disablePatches()
|
|
64
|
+
Controls.disableExtensions()
|
|
65
|
+
},
|
|
74
66
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
67
|
+
disablePatches() {
|
|
68
|
+
Patches.forEach((extension) => { extension.revert() })
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
disableExtensions() {
|
|
72
|
+
Object.values(Extensions).forEach((extension) => { extension.revert() })
|
|
73
|
+
},
|
|
74
|
+
})
|
|
78
75
|
|
|
79
76
|
export const all = (() => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
ReflectExtensions,
|
|
84
|
-
StringExtensions,
|
|
85
|
-
SymbolExtensions,
|
|
86
|
-
ArrayPrototypeExtensions,
|
|
87
|
-
|
|
88
|
-
GlobalFunctionsAndProps,
|
|
89
|
-
DescriptorExtensions,
|
|
77
|
+
const extensions = [
|
|
78
|
+
...Array.from(Patches.values()),
|
|
79
|
+
...Array.from(Object.values(Extensions)),
|
|
90
80
|
]
|
|
91
81
|
|
|
92
82
|
const dest = extensions.reduce((accumulator, extension) => {
|
|
93
83
|
Reflect.ownKeys(extension.patchEntries).reduce((_, key) => {
|
|
94
|
-
|
|
84
|
+
const entry = extension.patchEntries[key]
|
|
85
|
+
|
|
86
|
+
if (entry.isAccessor)
|
|
87
|
+
accumulator[key] = new Descriptor(entry.descriptor)
|
|
88
|
+
else
|
|
89
|
+
accumulator[key] = entry.computed
|
|
90
|
+
|
|
95
91
|
return accumulator
|
|
96
92
|
}, accumulator)
|
|
97
93
|
|
|
@@ -101,20 +97,17 @@ export const all = (() => {
|
|
|
101
97
|
return dest
|
|
102
98
|
})()
|
|
103
99
|
|
|
100
|
+
const results = {
|
|
101
|
+
...Controls,
|
|
102
|
+
extensions: Extensions,
|
|
103
|
+
patches: Patches,
|
|
104
|
+
all,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export default results
|
|
104
108
|
|
|
105
109
|
export {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
SymbolExtensions,
|
|
111
|
-
ArrayPrototypeExtensions,
|
|
112
|
-
|
|
113
|
-
GlobalFunctionsAndProps,
|
|
114
|
-
DescriptorExtensions,
|
|
115
|
-
AsyncIterableExtensions,
|
|
116
|
-
AsyncIteratorExtensions,
|
|
117
|
-
IterableExtensions,
|
|
118
|
-
IteratorExtensions,
|
|
119
|
-
RefSetExtensions,
|
|
120
|
-
}
|
|
110
|
+
Extensions,
|
|
111
|
+
Patches,
|
|
112
|
+
Controls,
|
|
113
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Patch } from '@nejs/extension';
|
|
2
|
+
|
|
3
|
+
export const MapPrototypeExtensions = new Patch(Map.prototype, {
|
|
4
|
+
/**
|
|
5
|
+
* The function `getKey` returns the key associated with a given value
|
|
6
|
+
* in a map.
|
|
7
|
+
*
|
|
8
|
+
* @param {any} value - The value parameter is the value that you want to
|
|
9
|
+
* find the corresponding key for in the map.
|
|
10
|
+
* @param [strict=true] - The "strict" parameter is a boolean value that
|
|
11
|
+
* determines whether strict equality (===) or loose equality (==) should
|
|
12
|
+
* be used when comparing the "value" parameter with the values in the
|
|
13
|
+
* entries of the object. If "strict" is set to true, strict equality will
|
|
14
|
+
* be used.
|
|
15
|
+
* @returns the key associated with the given value. If a matching key is
|
|
16
|
+
* found, it is returned. If no matching key is found, null is returned.
|
|
17
|
+
*/
|
|
18
|
+
getKey(value, strict = true) {
|
|
19
|
+
for (const [key, entryValue] of this) {
|
|
20
|
+
if (
|
|
21
|
+
(strict && value === entryValue) &&
|
|
22
|
+
(!strict && value == entryValue)
|
|
23
|
+
) {
|
|
24
|
+
return key
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
})
|
|
@@ -8,7 +8,7 @@ import { Extension } from '@nejs/extension'
|
|
|
8
8
|
* reading in chunks, or any other data that is not immediately available but
|
|
9
9
|
* arrives over time.
|
|
10
10
|
*/
|
|
11
|
-
class AsyncIterable {
|
|
11
|
+
export class AsyncIterable {
|
|
12
12
|
/**
|
|
13
13
|
* Private field to store the elements of the async iterable.
|
|
14
14
|
* @private
|
|
@@ -69,140 +69,140 @@ class AsyncIterable {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* function.
|
|
72
|
+
* Checks if a given value is an async iterable. This method determines if
|
|
73
|
+
* the provided value has a `Symbol.asyncIterator` property that is an async
|
|
74
|
+
* generator function. It's a precise way to identify if the value conforms
|
|
75
|
+
* to the async iterable protocol using an async generator function.
|
|
76
|
+
*
|
|
77
|
+
* Note: This method specifically checks for async generator functions. Some
|
|
78
|
+
* async iterables might use regular async functions that return an async
|
|
79
|
+
* iterator, which this method won't identify.
|
|
80
|
+
*
|
|
81
|
+
* @param {*} value - The value to be checked for async iterability.
|
|
82
|
+
* @returns {boolean} - Returns true if the value is an async iterable
|
|
83
|
+
* implemented using an async generator function, false otherwise.
|
|
76
84
|
*/
|
|
77
|
-
static
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
}
|
|
85
|
+
static isAsyncIterable(value) {
|
|
86
|
+
const type = Object.prototype.toString.call(value?.[Symbol.asyncIterator]);
|
|
87
|
+
return type === '[object AsyncGeneratorFunction]';
|
|
88
|
+
}
|
|
89
|
+
}
|
|
94
90
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Being able to create a compliant `AsyncIterator` around any type of
|
|
93
|
+
* iterable object. This can be wrapped around any type of object that
|
|
94
|
+
* has a `[Symbol.asyncIterator]` property assigned to a generator
|
|
95
|
+
* function.
|
|
96
|
+
*/
|
|
97
|
+
export class AsyncIterator {
|
|
98
|
+
/**
|
|
99
|
+
* Creates a new `AsyncIterator` object instance.
|
|
100
|
+
*
|
|
101
|
+
* @param {object} asyncIterable any object that has a
|
|
102
|
+
* `[Symbol.asyncIterable]` property assigned to a generator function.
|
|
103
|
+
*/
|
|
104
|
+
constructor(asyncIterable) {
|
|
105
|
+
if (!asyncIterable || !Reflect.has(asyncIterable, Symbol.asyncIterator)) {
|
|
106
|
+
throw new TypeError(
|
|
107
|
+
'Value used to instantiate AsyncIterator is not an async iterable'
|
|
108
|
+
);
|
|
111
109
|
}
|
|
112
110
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
*
|
|
117
|
-
* @returns {object} the object containing the `[Symbol.iterator]`
|
|
118
|
-
*/
|
|
119
|
-
get asyncIterable() {
|
|
120
|
-
return this.#asyncIterable
|
|
121
|
-
}
|
|
111
|
+
this.#asyncIterable = asyncIterable;
|
|
112
|
+
this.#asyncIterator = asyncIterable[Symbol.asyncIterator]();
|
|
113
|
+
}
|
|
122
114
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return { value: undefined, done: true };
|
|
134
|
-
} else {
|
|
135
|
-
return { value: result.value, done: false };
|
|
136
|
-
}
|
|
137
|
-
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns a new `Array` derived from the iterable this object
|
|
117
|
+
* wraps.
|
|
118
|
+
*
|
|
119
|
+
* @returns {array} a new `Array` generated from the wrapped
|
|
120
|
+
* iterable. The method is generated from using an async for of
|
|
121
|
+
* loop.
|
|
122
|
+
*/
|
|
123
|
+
async asArray() {
|
|
124
|
+
const array = []
|
|
138
125
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
* iterated over again.
|
|
142
|
-
*/
|
|
143
|
-
async reset() {
|
|
144
|
-
this.#asyncIterator = this.#asyncIterable[Symbol.asyncIterator]();
|
|
126
|
+
for await (const value of this) {
|
|
127
|
+
array.push(value)
|
|
145
128
|
}
|
|
146
129
|
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
}
|
|
130
|
+
return array
|
|
131
|
+
}
|
|
158
132
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Returns the actual iterable object passed to the constructor that
|
|
135
|
+
* created this instance.
|
|
136
|
+
*
|
|
137
|
+
* @returns {object} the object containing the `[Symbol.iterator]`
|
|
138
|
+
*/
|
|
139
|
+
get asyncIterable() {
|
|
140
|
+
return this.#asyncIterable
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The function retrieves the next value in the iterator. If the
|
|
145
|
+
* the iterator has run its course, `reset()` can be invoked to
|
|
146
|
+
* reset the pointer to the beginning of the iteration.
|
|
147
|
+
*
|
|
148
|
+
* @returns {any} the next value
|
|
149
|
+
*/
|
|
150
|
+
async next() {
|
|
151
|
+
const result = await this.#asyncIterator.next();
|
|
152
|
+
if (result.done) {
|
|
153
|
+
return { value: undefined, done: true };
|
|
154
|
+
} else {
|
|
155
|
+
return { value: result.value, done: false };
|
|
167
156
|
}
|
|
157
|
+
}
|
|
168
158
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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;
|
|
159
|
+
/**
|
|
160
|
+
* Resets the async iterator to the beginning allowing it to be
|
|
161
|
+
* iterated over again.
|
|
162
|
+
*/
|
|
163
|
+
async reset() {
|
|
164
|
+
this.#asyncIterator = this.#asyncIterable[Symbol.asyncIterator]();
|
|
185
165
|
}
|
|
186
166
|
|
|
187
167
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* to the async iterable protocol using an async generator function.
|
|
168
|
+
* The existence of this symbol on the object instances, indicates that
|
|
169
|
+
* it can be used in `for(.. of ..)` loops and its values can be
|
|
170
|
+
* extracted from calls to `Array.from()`
|
|
192
171
|
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
|
|
172
|
+
* @returns {AsyncIterable} this is returned since this object is already
|
|
173
|
+
* conforming to the expected JavaScript AsyncIterator interface
|
|
174
|
+
*/
|
|
175
|
+
[Symbol.asyncIterator]() {
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Ensures that the constructor of this object instance's name
|
|
181
|
+
* is returned if the string tag for this instance is queried
|
|
196
182
|
*
|
|
197
|
-
* @
|
|
198
|
-
* @returns {boolean} - Returns true if the value is an async iterable
|
|
199
|
-
* implemented using an async generator function, false otherwise.
|
|
183
|
+
* @returns {string} the name of the class
|
|
200
184
|
*/
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return type === '[object AsyncGeneratorFunction]';
|
|
185
|
+
get [Symbol.toStringTag]() {
|
|
186
|
+
return this.constructor.name;
|
|
204
187
|
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The object from which its iterator functionality is derived.
|
|
191
|
+
*
|
|
192
|
+
* @type {object}
|
|
193
|
+
* @private
|
|
194
|
+
*/
|
|
195
|
+
#asyncIterable = null;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The results of a call to the iterable's `[Symbol.asyncIterator]`
|
|
199
|
+
* generator function.
|
|
200
|
+
*
|
|
201
|
+
* @type {object}
|
|
202
|
+
* @private
|
|
203
|
+
*/
|
|
204
|
+
#asyncIterator = null;
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
export const AsyncIterableExtensions = new Extension(AsyncIterable)
|
|
208
|
-
export const AsyncIteratorExtensions = new Extension(
|
|
208
|
+
export const AsyncIteratorExtensions = new Extension(AsyncIterator)
|