@nejs/basic-extensions 2.21.5 → 2.22.6
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/README.md +78 -4
- package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
- package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
- package/dist/cjs/classes/index.cjs +11129 -0
- package/dist/cjs/classes/index.cjs.map +7 -0
- package/dist/cjs/index.cjs +15191 -0
- package/dist/cjs/index.cjs.map +7 -0
- package/dist/cjs/utils/index.cjs +3954 -0
- package/dist/cjs/utils/index.cjs.map +7 -0
- package/dist/esm/basic-extensions.mjs +25 -0
- package/dist/esm/basic-extensions.mjs.map +7 -0
- package/package.json +16 -22
- package/repl.bootstrap.js +4 -7
- package/repl.history +26 -26
- package/src/classes/enumeration.js +466 -0
- package/src/classes/index.js +5 -1
- package/src/index.js +3 -1
- package/src/regular.expression.extensions.js +0 -35
- package/src/utils/toolkit.js +699 -516
- package/tests/arrayextensions.test.js +3 -3
- package/tests/index.test.js +3 -1
- package/tests/newClasses/asyncIterable.test.js +3 -3
- package/tests/newClasses/deferred.test.js +3 -3
- package/tests/newClasses/descriptor.test.js +3 -3
- package/tests/newClasses/iterable.test.js +3 -3
- package/tests/newClasses/refmap.test.js +3 -3
- package/tests/newClasses/refset.test.js +3 -3
- package/tests/objectextensions.test.js +3 -3
- package/tests/setextensions.test.js +3 -3
- package/tests/stringextensions.test.js +3 -2
- package/tests/utils/descriptor.utils.test.js +1 -1
- package/tests/utils/toolkit.test.js +429 -163
- package/.esdoc.json +0 -9
- package/.vscode/settings.json +0 -5
- package/bin/build +0 -27
- package/bin/clean +0 -14
- package/bin/esbuild +0 -91
- package/bin/fixup +0 -13
- package/bin/repl.basics.js +0 -584
- package/bin/repl.signature.js +0 -63
- package/bin/version +0 -100
- package/dist/@nejs/basic-extensions.bundle.2.21.5.js +0 -25
- package/dist/@nejs/basic-extensions.bundle.2.21.5.js.map +0 -7
- package/dist/cjs/array.extensions.d.ts +0 -39
- package/dist/cjs/array.extensions.js +0 -477
- package/dist/cjs/array.extensions.js.map +0 -1
- package/dist/cjs/big.int.extension.d.ts +0 -31
- package/dist/cjs/big.int.extension.js +0 -273
- package/dist/cjs/big.int.extension.js.map +0 -1
- package/dist/cjs/classes/asyncIterable.d.ts +0 -126
- package/dist/cjs/classes/asyncIterable.js +0 -209
- package/dist/cjs/classes/asyncIterable.js.map +0 -1
- package/dist/cjs/classes/deferred.d.ts +0 -146
- package/dist/cjs/classes/deferred.js +0 -291
- package/dist/cjs/classes/deferred.js.map +0 -1
- package/dist/cjs/classes/descriptor.d.ts +0 -334
- package/dist/cjs/classes/descriptor.js +0 -537
- package/dist/cjs/classes/descriptor.js.map +0 -1
- package/dist/cjs/classes/enum.d.ts +0 -50
- package/dist/cjs/classes/enum.js +0 -405
- package/dist/cjs/classes/enum.js.map +0 -1
- package/dist/cjs/classes/index.d.ts +0 -15
- package/dist/cjs/classes/index.js +0 -63
- package/dist/cjs/classes/index.js.map +0 -1
- package/dist/cjs/classes/introspector.d.ts +0 -20
- package/dist/cjs/classes/introspector.js +0 -130
- package/dist/cjs/classes/introspector.js.map +0 -1
- package/dist/cjs/classes/iterable.d.ts +0 -169
- package/dist/cjs/classes/iterable.js +0 -268
- package/dist/cjs/classes/iterable.js.map +0 -1
- package/dist/cjs/classes/param.parser.d.ts +0 -221
- package/dist/cjs/classes/param.parser.js +0 -242
- package/dist/cjs/classes/param.parser.js.map +0 -1
- package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
- package/dist/cjs/classes/pluggable.proxy.js +0 -444
- package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
- package/dist/cjs/classes/property.d.ts +0 -79
- package/dist/cjs/classes/property.js +0 -284
- package/dist/cjs/classes/property.js.map +0 -1
- package/dist/cjs/classes/refmap.d.ts +0 -238
- package/dist/cjs/classes/refmap.js +0 -421
- package/dist/cjs/classes/refmap.js.map +0 -1
- package/dist/cjs/classes/refset.d.ts +0 -186
- package/dist/cjs/classes/refset.js +0 -370
- package/dist/cjs/classes/refset.js.map +0 -1
- package/dist/cjs/classes/symkeys.d.ts +0 -349
- package/dist/cjs/classes/symkeys.js +0 -510
- package/dist/cjs/classes/symkeys.js.map +0 -1
- package/dist/cjs/classes/type.d.ts +0 -56
- package/dist/cjs/classes/type.js +0 -405
- package/dist/cjs/classes/type.js.map +0 -1
- package/dist/cjs/function.extensions.d.ts +0 -12
- package/dist/cjs/function.extensions.js +0 -758
- package/dist/cjs/function.extensions.js.map +0 -1
- package/dist/cjs/global.this.d.ts +0 -2
- package/dist/cjs/global.this.js +0 -300
- package/dist/cjs/global.this.js.map +0 -1
- package/dist/cjs/index.d.ts +0 -31
- package/dist/cjs/index.js +0 -228
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/json.extensions.d.ts +0 -2
- package/dist/cjs/json.extensions.js +0 -109
- package/dist/cjs/json.extensions.js.map +0 -1
- package/dist/cjs/map.extensions.d.ts +0 -3
- package/dist/cjs/map.extensions.js +0 -143
- package/dist/cjs/map.extensions.js.map +0 -1
- package/dist/cjs/math.extension.d.ts +0 -14
- package/dist/cjs/math.extension.js +0 -71
- package/dist/cjs/math.extension.js.map +0 -1
- package/dist/cjs/number.extension.d.ts +0 -44
- package/dist/cjs/number.extension.js +0 -278
- package/dist/cjs/number.extension.js.map +0 -1
- package/dist/cjs/object.extensions.d.ts +0 -33
- package/dist/cjs/object.extensions.js +0 -1091
- package/dist/cjs/object.extensions.js.map +0 -1
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/proxy.extensions.d.ts +0 -2
- package/dist/cjs/proxy.extensions.js +0 -207
- package/dist/cjs/proxy.extensions.js.map +0 -1
- package/dist/cjs/reflect.extensions.d.ts +0 -14
- package/dist/cjs/reflect.extensions.js +0 -316
- package/dist/cjs/reflect.extensions.js.map +0 -1
- package/dist/cjs/regular.expression.extensions.d.ts +0 -2
- package/dist/cjs/regular.expression.extensions.js +0 -423
- package/dist/cjs/regular.expression.extensions.js.map +0 -1
- package/dist/cjs/set.extensions.d.ts +0 -40
- package/dist/cjs/set.extensions.js +0 -355
- package/dist/cjs/set.extensions.js.map +0 -1
- package/dist/cjs/string.extensions.d.ts +0 -23
- package/dist/cjs/string.extensions.js +0 -704
- package/dist/cjs/string.extensions.js.map +0 -1
- package/dist/cjs/symbol.extensions.d.ts +0 -11
- package/dist/cjs/symbol.extensions.js +0 -735
- package/dist/cjs/symbol.extensions.js.map +0 -1
- package/dist/cjs/utils/copy.object.d.ts +0 -408
- package/dist/cjs/utils/copy.object.js +0 -720
- package/dist/cjs/utils/copy.object.js.map +0 -1
- package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
- package/dist/cjs/utils/descriptor.utils.js +0 -889
- package/dist/cjs/utils/descriptor.utils.js.map +0 -1
- package/dist/cjs/utils/index.d.ts +0 -75
- package/dist/cjs/utils/index.js +0 -61
- package/dist/cjs/utils/index.js.map +0 -1
- package/dist/cjs/utils/stdout.d.ts +0 -742
- package/dist/cjs/utils/stdout.js +0 -1042
- package/dist/cjs/utils/stdout.js.map +0 -1
- package/dist/cjs/utils/toolkit.d.ts +0 -1898
- package/dist/cjs/utils/toolkit.js +0 -1378
- package/dist/cjs/utils/toolkit.js.map +0 -1
- package/dist/cjs/weakref.extensions.d.ts +0 -2
- package/dist/cjs/weakref.extensions.js +0 -19
- package/dist/cjs/weakref.extensions.js.map +0 -1
- package/dist/mjs/array.extensions.d.ts +0 -39
- package/dist/mjs/array.extensions.js +0 -474
- package/dist/mjs/array.extensions.js.map +0 -1
- package/dist/mjs/big.int.extension.d.ts +0 -31
- package/dist/mjs/big.int.extension.js +0 -270
- package/dist/mjs/big.int.extension.js.map +0 -1
- package/dist/mjs/classes/asyncIterable.d.ts +0 -126
- package/dist/mjs/classes/asyncIterable.js +0 -204
- package/dist/mjs/classes/asyncIterable.js.map +0 -1
- package/dist/mjs/classes/deferred.d.ts +0 -146
- package/dist/mjs/classes/deferred.js +0 -287
- package/dist/mjs/classes/deferred.js.map +0 -1
- package/dist/mjs/classes/descriptor.d.ts +0 -334
- package/dist/mjs/classes/descriptor.js +0 -533
- package/dist/mjs/classes/descriptor.js.map +0 -1
- package/dist/mjs/classes/enum.d.ts +0 -50
- package/dist/mjs/classes/enum.js +0 -400
- package/dist/mjs/classes/enum.js.map +0 -1
- package/dist/mjs/classes/index.d.ts +0 -15
- package/dist/mjs/classes/index.js +0 -46
- package/dist/mjs/classes/index.js.map +0 -1
- package/dist/mjs/classes/introspector.d.ts +0 -20
- package/dist/mjs/classes/introspector.js +0 -126
- package/dist/mjs/classes/introspector.js.map +0 -1
- package/dist/mjs/classes/iterable.d.ts +0 -169
- package/dist/mjs/classes/iterable.js +0 -263
- package/dist/mjs/classes/iterable.js.map +0 -1
- package/dist/mjs/classes/param.parser.d.ts +0 -221
- package/dist/mjs/classes/param.parser.js +0 -238
- package/dist/mjs/classes/param.parser.js.map +0 -1
- package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
- package/dist/mjs/classes/pluggable.proxy.js +0 -438
- package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
- package/dist/mjs/classes/property.d.ts +0 -79
- package/dist/mjs/classes/property.js +0 -280
- package/dist/mjs/classes/property.js.map +0 -1
- package/dist/mjs/classes/refmap.d.ts +0 -238
- package/dist/mjs/classes/refmap.js +0 -417
- package/dist/mjs/classes/refmap.js.map +0 -1
- package/dist/mjs/classes/refset.d.ts +0 -186
- package/dist/mjs/classes/refset.js +0 -366
- package/dist/mjs/classes/refset.js.map +0 -1
- package/dist/mjs/classes/symkeys.d.ts +0 -349
- package/dist/mjs/classes/symkeys.js +0 -506
- package/dist/mjs/classes/symkeys.js.map +0 -1
- package/dist/mjs/classes/type.d.ts +0 -56
- package/dist/mjs/classes/type.js +0 -401
- package/dist/mjs/classes/type.js.map +0 -1
- package/dist/mjs/function.extensions.d.ts +0 -12
- package/dist/mjs/function.extensions.js +0 -755
- package/dist/mjs/function.extensions.js.map +0 -1
- package/dist/mjs/global.this.d.ts +0 -2
- package/dist/mjs/global.this.js +0 -264
- package/dist/mjs/global.this.js.map +0 -1
- package/dist/mjs/index.d.ts +0 -31
- package/dist/mjs/index.js +0 -206
- package/dist/mjs/index.js.map +0 -1
- package/dist/mjs/json.extensions.d.ts +0 -2
- package/dist/mjs/json.extensions.js +0 -106
- package/dist/mjs/json.extensions.js.map +0 -1
- package/dist/mjs/map.extensions.d.ts +0 -3
- package/dist/mjs/map.extensions.js +0 -140
- package/dist/mjs/map.extensions.js.map +0 -1
- package/dist/mjs/math.extension.d.ts +0 -14
- package/dist/mjs/math.extension.js +0 -68
- package/dist/mjs/math.extension.js.map +0 -1
- package/dist/mjs/number.extension.d.ts +0 -44
- package/dist/mjs/number.extension.js +0 -275
- package/dist/mjs/number.extension.js.map +0 -1
- package/dist/mjs/object.extensions.d.ts +0 -33
- package/dist/mjs/object.extensions.js +0 -1088
- package/dist/mjs/object.extensions.js.map +0 -1
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/proxy.extensions.d.ts +0 -2
- package/dist/mjs/proxy.extensions.js +0 -204
- package/dist/mjs/proxy.extensions.js.map +0 -1
- package/dist/mjs/reflect.extensions.d.ts +0 -14
- package/dist/mjs/reflect.extensions.js +0 -313
- package/dist/mjs/reflect.extensions.js.map +0 -1
- package/dist/mjs/regular.expression.extensions.d.ts +0 -2
- package/dist/mjs/regular.expression.extensions.js +0 -420
- package/dist/mjs/regular.expression.extensions.js.map +0 -1
- package/dist/mjs/set.extensions.d.ts +0 -40
- package/dist/mjs/set.extensions.js +0 -352
- package/dist/mjs/set.extensions.js.map +0 -1
- package/dist/mjs/string.extensions.d.ts +0 -23
- package/dist/mjs/string.extensions.js +0 -701
- package/dist/mjs/string.extensions.js.map +0 -1
- package/dist/mjs/symbol.extensions.d.ts +0 -11
- package/dist/mjs/symbol.extensions.js +0 -732
- package/dist/mjs/symbol.extensions.js.map +0 -1
- package/dist/mjs/utils/copy.object.d.ts +0 -408
- package/dist/mjs/utils/copy.object.js +0 -702
- package/dist/mjs/utils/copy.object.js.map +0 -1
- package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
- package/dist/mjs/utils/descriptor.utils.js +0 -875
- package/dist/mjs/utils/descriptor.utils.js.map +0 -1
- package/dist/mjs/utils/index.d.ts +0 -75
- package/dist/mjs/utils/index.js +0 -45
- package/dist/mjs/utils/index.js.map +0 -1
- package/dist/mjs/utils/stdout.d.ts +0 -742
- package/dist/mjs/utils/stdout.js +0 -1037
- package/dist/mjs/utils/stdout.js.map +0 -1
- package/dist/mjs/utils/toolkit.d.ts +0 -1898
- package/dist/mjs/utils/toolkit.js +0 -1373
- package/dist/mjs/utils/toolkit.js.map +0 -1
- package/dist/mjs/weakref.extensions.d.ts +0 -2
- package/dist/mjs/weakref.extensions.js +0 -16
- package/dist/mjs/weakref.extensions.js.map +0 -1
- package/jsdoc-config.json +0 -31
- package/tsconfig.base.json +0 -28
- package/tsconfig.cjs.json +0 -8
- package/tsconfig.esm.json +0 -8
- package/vitest.config.js +0 -7
|
@@ -1,732 +0,0 @@
|
|
|
1
|
-
import { Patch, PatchToggle } from '@nejs/extension';
|
|
2
|
-
import { Symkeys } from './classes/symkeys.js';
|
|
3
|
-
import { JSONExtensions } from './json.extensions.js';
|
|
4
|
-
const JSONToggle = new PatchToggle(JSONExtensions);
|
|
5
|
-
const symkeys = new Symkeys('nejs');
|
|
6
|
-
/**
|
|
7
|
-
* `SymbolExtensions` is a patch for the JavaScript built-in `Symbol` class. It
|
|
8
|
-
* adds utility methods to the `Symbol` class without modifying the global namespace
|
|
9
|
-
* directly. This patch includes methods for key validation, object type checking,
|
|
10
|
-
* and retrieving the string tag of an object. These methods are useful for
|
|
11
|
-
* enhancing the capabilities of the standard `Symbol` class with additional
|
|
12
|
-
* utility functions.
|
|
13
|
-
*/
|
|
14
|
-
export const SymbolExtensions = new Patch(Symbol, {
|
|
15
|
-
/**
|
|
16
|
-
* Adds a new symbol to the Symkeys instance with the given name and
|
|
17
|
-
* associated data.
|
|
18
|
-
*
|
|
19
|
-
* This method generates a unique symbol based on the provided name,
|
|
20
|
-
* optional domain, separator, and token. It also allows embedding
|
|
21
|
-
* additional data into the symbol's name.
|
|
22
|
-
*
|
|
23
|
-
* @param {string} named - The base name for the new symbol.
|
|
24
|
-
* @param {Object} options - Additional options for the symbol.
|
|
25
|
-
* @param {*} [options.associate={}] - Data to associate with the symbol.
|
|
26
|
-
* @param {Object} [options.embed] - Optional data to embed in the symbol.
|
|
27
|
-
* @param {string} [options.useDomain] - Optional domain to include in the
|
|
28
|
-
* symbol's name.
|
|
29
|
-
* @param {string} [options.useSeparator] - Optional separator to use in
|
|
30
|
-
* the symbol's name.
|
|
31
|
-
* @param {string} [options.useToken] - Optional token to use for the
|
|
32
|
-
* symbol. If not provided, a random token is generated.
|
|
33
|
-
* @returns {Symbol} The newly created symbol.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* // Add a symbol with associated data
|
|
37
|
-
* const mySymbol = SymbolExtensions.add('myIdentifier', {
|
|
38
|
-
* associate: { foo: 'bar' },
|
|
39
|
-
* embed: { baz: 'qux' },
|
|
40
|
-
* useDomain: 'exampleDomain',
|
|
41
|
-
* useSeparator: '-',
|
|
42
|
-
* useToken: 'customToken',
|
|
43
|
-
* })
|
|
44
|
-
* console.log(mySymbol)
|
|
45
|
-
* // Symbol(@exampleDomain-myIdentifier {"baz":"qux"} #customToken)
|
|
46
|
-
*/
|
|
47
|
-
add(named, { associate = {}, embed, useToken, useDomain, useSeparator }) {
|
|
48
|
-
return this.keys.add(named, {
|
|
49
|
-
associate, embed, useToken, useDomain, useSeparator,
|
|
50
|
-
});
|
|
51
|
-
},
|
|
52
|
-
/**
|
|
53
|
-
* Deletes the data associated with a given symbol from the Symkeys
|
|
54
|
-
* instance.
|
|
55
|
-
*
|
|
56
|
-
* This method allows removal of the data that has been associated with a
|
|
57
|
-
* particular symbol in the Symkeys instance. It is useful when you want
|
|
58
|
-
* to clean up or remove stored information associated with a symbol.
|
|
59
|
-
*
|
|
60
|
-
* @param {Symbol} forSymbol - The symbol whose associated data is to be
|
|
61
|
-
* deleted.
|
|
62
|
-
* @param {*} [replaceWith=undefined] - Optionally, if `replaceWith` is
|
|
63
|
-
* not `undefined`, a new value can be set after the original is deleted
|
|
64
|
-
* @returns {boolean} - Returns true if an element in the Symkeys existed
|
|
65
|
-
* and has been removed, or false if the element does not exist
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* // Assuming 'mySymbol' is a symbol that has been added to the Symkeys
|
|
69
|
-
* // with associated data
|
|
70
|
-
* const isDeleted = Symbol.deleteData(mySymbol)
|
|
71
|
-
* console.log(isDeleted) // Output: true if data was deleted, false
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* // Deleting data and replacing it with a new value
|
|
75
|
-
* const mySymbol = Symbol.for('mySymbol')
|
|
76
|
-
* Symbol.setData(mySymbol, { foo: 'bar' })
|
|
77
|
-
* Symbol.deleteData(mySymbol, { newFoo: 'newBar' })
|
|
78
|
-
* console.log(Symbol.keys.data(mySymbol)) // Output: { newFoo: 'newBar' }
|
|
79
|
-
*/
|
|
80
|
-
deleteData(forSymbol, replaceWith = undefined) {
|
|
81
|
-
return this.keys.deleteData(forSymbol, replaceWith);
|
|
82
|
-
},
|
|
83
|
-
/**
|
|
84
|
-
* Evaluates a key or value and generates a shared symbol key based on
|
|
85
|
-
* the provided object name and owner name.
|
|
86
|
-
*
|
|
87
|
-
* This method takes a key or value, an object name, and an object owner
|
|
88
|
-
* name as parameters. It determines the type of each parameter and
|
|
89
|
-
* constructs a token string by concatenating the owner name, object
|
|
90
|
-
* name, and key/value (if they are valid object keys).
|
|
91
|
-
*
|
|
92
|
-
* The token string is then used to create a shared symbol key using the
|
|
93
|
-
* `sharedKey` method of the current instance. The shared symbol key is
|
|
94
|
-
* returned along with the token as associated data.
|
|
95
|
-
*
|
|
96
|
-
* @param {string|Symbol} keyOrValue - The key or value to evaluate.
|
|
97
|
-
* @param {string|Symbol} objectName - The name of the object associated
|
|
98
|
-
* with the key or value.
|
|
99
|
-
* @param {string|Function|Object} objectOwnerName - The name of the
|
|
100
|
-
* owner of the object.
|
|
101
|
-
* @returns {Symbol} The shared symbol key generated based on the
|
|
102
|
-
* provided parameters.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* const symbolKey = SymbolExtensions.evalKey('myKey', 'myObject', 'myOwner')
|
|
106
|
-
* console.log(symbolKey)
|
|
107
|
-
* // Output: Symbol(@nejs.internal.refkey:myOwner.myObject.myKey)
|
|
108
|
-
*
|
|
109
|
-
* @example
|
|
110
|
-
* const symbolKey = SymbolExtensions.evalKey(
|
|
111
|
-
* 'myValue', () => {}, { [Symbol.toStringTag]: 'myOwner' }
|
|
112
|
-
* )
|
|
113
|
-
* console.log(symbolKey)
|
|
114
|
-
* // Output: Symbol(@nejs.internal.refkey:myOwner.myValue)
|
|
115
|
-
*/
|
|
116
|
-
evalKey(keyOrValue, objectName, objectOwnerName) {
|
|
117
|
-
const is = {
|
|
118
|
-
string(v) { return typeof v === 'string'; },
|
|
119
|
-
func(v) { return typeof v === 'function'; },
|
|
120
|
-
object(v) { return typeof v === 'object'; },
|
|
121
|
-
objKey(v) { return ['symbol', 'string'].some(k => typeof v === k); },
|
|
122
|
-
};
|
|
123
|
-
is.key = is.objKey(keyOrValue);
|
|
124
|
-
const ownerName = ((is.string(objectOwnerName) && objectOwnerName) ||
|
|
125
|
-
(is.func(objectOwnerName) && objectOwnerName?.name) ||
|
|
126
|
-
(is.object(objectOwnerName) && objectOwnerName?.[Symbol.toStringTag]) ||
|
|
127
|
-
undefined);
|
|
128
|
-
const token = [
|
|
129
|
-
ownerName && `${ownerName}.` || '',
|
|
130
|
-
is.objKey(objectName) && `${objectName}.` || '',
|
|
131
|
-
is.objKey(keyOrValue) && `${keyOrValue}`,
|
|
132
|
-
].join('');
|
|
133
|
-
return this.sharedKey(`internal.refkey:${token}`, { token });
|
|
134
|
-
},
|
|
135
|
-
/**
|
|
136
|
-
* Checks if the Symkeys instance has data associated with a given
|
|
137
|
-
* symbol
|
|
138
|
-
*
|
|
139
|
-
* This method checks if the Symkeys instance has any data associated
|
|
140
|
-
* with the provided symbol. It is useful when you need to verify if
|
|
141
|
-
* data exists for a particular symbol before attempting to retrieve
|
|
142
|
-
* or manipulate it
|
|
143
|
-
*
|
|
144
|
-
* @param {Symbol} forSymbol - The symbol to check for associated data
|
|
145
|
-
* @returns {boolean} Returns true if data exists for the symbol,
|
|
146
|
-
* false otherwise
|
|
147
|
-
*
|
|
148
|
-
* @example
|
|
149
|
-
* // Assuming 'mySymbol' is a symbol that has been added to the
|
|
150
|
-
* // Symkeys with associated data
|
|
151
|
-
* const hasData = Symbol.hasData(mySymbol)
|
|
152
|
-
* console.log(hasData) // Output: true
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* // Assuming 'nonExistentSymbol' is a symbol that has not been added
|
|
156
|
-
* // to the Symkeys
|
|
157
|
-
* const hasData = Symbol.hasData(nonExistentSymbol)
|
|
158
|
-
* console.log(hasData) // Output: false
|
|
159
|
-
*/
|
|
160
|
-
hasData(forSymbol) {
|
|
161
|
-
return this.keys.hasData(forSymbol);
|
|
162
|
-
},
|
|
163
|
-
/**
|
|
164
|
-
* The `isSymbol` method does exactly what one would it expect. It returns
|
|
165
|
-
* true if the string matches typeof or instanceof as a symbol.
|
|
166
|
-
*
|
|
167
|
-
* @param {*} value checks to see if the `value` is a string
|
|
168
|
-
* @returns {boolean} `true` if it is a `Symbol`, `false` otherwise
|
|
169
|
-
*/
|
|
170
|
-
isSymbol(value) {
|
|
171
|
-
return value && (typeof value === 'symbol');
|
|
172
|
-
},
|
|
173
|
-
/**
|
|
174
|
-
* Returns true if the supplied value is a Symbol created using
|
|
175
|
-
* `Symbol.for()`.
|
|
176
|
-
*
|
|
177
|
-
* @param {any} value assumption is that the supplied value is of type
|
|
178
|
-
* 'symbol' however, unless `allowOnlySymbols` is set to `true`, `false`
|
|
179
|
-
* will be returned for any non-symbol values.
|
|
180
|
-
* @param {boolean} allowOnlySymbols true if an error should be thrown
|
|
181
|
-
* if the supplied value is not of type 'symbol'
|
|
182
|
-
* @returns true if the symbol is registered, meaning, none of the spec
|
|
183
|
-
* static symbols (`toStringTag`, `iterator`, etc...), and no symbols
|
|
184
|
-
* created by passing a value directly to the Symbol function, such as
|
|
185
|
-
* `Symbol('name')`
|
|
186
|
-
*/
|
|
187
|
-
isRegistered(value, allowOnlySymbols = false) {
|
|
188
|
-
if (!Symbol.isSymbol(value)) {
|
|
189
|
-
if (allowOnlySymbols) {
|
|
190
|
-
throw new TypeError('allowOnlySymbols specified; value is not a symbol');
|
|
191
|
-
}
|
|
192
|
-
return false;
|
|
193
|
-
}
|
|
194
|
-
return Symbol.keyFor(value) !== undefined;
|
|
195
|
-
},
|
|
196
|
-
/**
|
|
197
|
-
* A function that returns true if the symbol is not registered, meaning,
|
|
198
|
-
* any of the spec static symbols (`toStringTag`, `iterator`, etc...), and
|
|
199
|
-
* any symbols created by passing a value directly to the `Symbol` function,
|
|
200
|
-
* such as `Symbol('name')`.
|
|
201
|
-
*
|
|
202
|
-
* @param {any} value assumption is that the supplied value is of type
|
|
203
|
-
* 'symbol' however, unless allowOnlySymbols is set to true, false will
|
|
204
|
-
* be returned for any non-symbol values.
|
|
205
|
-
* @param {boolean} allowOnlySymbols true if an error should be thrown
|
|
206
|
-
* if the supplied value is not of type 'symbol'
|
|
207
|
-
* @returns true if the symbol is not registered, meaning, any of the
|
|
208
|
-
* spec static symbols (`toStringTag`, `iterator`, etc...), and any symbols
|
|
209
|
-
* created by passing a value directly to the `Symbol` function, such as
|
|
210
|
-
* `Symbol('name')`
|
|
211
|
-
* @returns true if the `value` in question is both a `symbol` and has
|
|
212
|
-
* returns `undefined` if passed to `Symbol.keyFor`
|
|
213
|
-
*/
|
|
214
|
-
isNonRegistered(value, allowOnlySymbols = false) {
|
|
215
|
-
return !Symbol.isRegistered(value, allowOnlySymbols);
|
|
216
|
-
},
|
|
217
|
-
/**
|
|
218
|
-
* `keys` is an instance of the `Symkeys` class, initialized with the
|
|
219
|
-
* domain 'nejs'. The `Symkeys` class provides a way to easily generate
|
|
220
|
-
* Symbol.for elements that follow particular pattern. Symkeys also
|
|
221
|
-
* allows associated data storage with each generated key.
|
|
222
|
-
*
|
|
223
|
-
* @type {Symkeys}
|
|
224
|
-
* @see {@link SymKeys}
|
|
225
|
-
* @example
|
|
226
|
-
* // Returns something like Symbol.for('@nejs.prototype #rwiy2o905d')
|
|
227
|
-
* const kOriginal = Symbol.keys.add('prototypes')
|
|
228
|
-
*
|
|
229
|
-
* // Which can be used to retrieve and fetch data associated with that key
|
|
230
|
-
* // The value stored is an array by default, but can be anything. It can
|
|
231
|
-
* // be accessed one property at a time
|
|
232
|
-
* Symbol.keys[kOriginal].original = Object.prototype
|
|
233
|
-
* Symbol.keys[kOriginal].modified = Object.create(Object.prototype, ...)
|
|
234
|
-
*
|
|
235
|
-
* // Or wholesale replaced
|
|
236
|
-
* Symbol.keys[kOriginal] = [Object.prototype, Array.prototype]
|
|
237
|
-
*
|
|
238
|
-
* // But if all Symbol Extensions are in place, including prototype add-ons
|
|
239
|
-
* kOriginal.data.original = Object.prototype // ...and...
|
|
240
|
-
* kOriginal.data = [Object.prototype, Array.prototype] // ...both work
|
|
241
|
-
*/
|
|
242
|
-
get keys() { return symkeys; },
|
|
243
|
-
/**
|
|
244
|
-
* Sets the data associated with a given symbol in the Symkeys
|
|
245
|
-
* instance.
|
|
246
|
-
*
|
|
247
|
-
* This method allows you to store data associated with a specific
|
|
248
|
-
* symbol in the Symkeys instance. It is useful when you want to
|
|
249
|
-
* attach additional information or metadata to a symbol for later
|
|
250
|
-
* retrieval.
|
|
251
|
-
*
|
|
252
|
-
* @param {Symbol} forSymbol - The symbol for which to set the
|
|
253
|
-
* associated data.
|
|
254
|
-
* @param {*} value - The data to be associated with the symbol.
|
|
255
|
-
*
|
|
256
|
-
* @example
|
|
257
|
-
* // Create a symbol
|
|
258
|
-
* const mySymbol = Symbol.for('mySymbol')
|
|
259
|
-
*
|
|
260
|
-
* // Set data for the symbol
|
|
261
|
-
* Symbol.setData(mySymbol, { foo: 'bar' })
|
|
262
|
-
*
|
|
263
|
-
* // Retrieve the data associated with the symbol
|
|
264
|
-
* const data = Symbol.keys.data(mySymbol)
|
|
265
|
-
* console.log(data) // Output: { foo: 'bar' }
|
|
266
|
-
*/
|
|
267
|
-
setData(forSymbol, value) {
|
|
268
|
-
this.keys.setData(forSymbol, value);
|
|
269
|
-
},
|
|
270
|
-
/**
|
|
271
|
-
* Creates or retrieves a shared symbol key with the given name and
|
|
272
|
-
* optional associated data.
|
|
273
|
-
*
|
|
274
|
-
* This method generates a shared symbol key using the provided name
|
|
275
|
-
* and optional parameters. If the symbol already exists in the
|
|
276
|
-
* Symkeys's internal map, it updates the associated data if provided.
|
|
277
|
-
* Otherwise, it creates a new symbol with the specified parameters.
|
|
278
|
-
*
|
|
279
|
-
* @param {string} named - The name to use for the shared symbol key.
|
|
280
|
-
* @param {Object} options - Optional parameters for the shared symbol key.
|
|
281
|
-
* @param {Object} [options.associate] - Data to associate with the symbol.
|
|
282
|
-
* @param {Object} [options.embed] - Data to embed in the symbol's name.
|
|
283
|
-
* @param {string} [options.useDomain] - Domain to include in the symbol's name.
|
|
284
|
-
* @param {string} [options.useSeparator] - Separator to use in the symbol's name.
|
|
285
|
-
* @returns {Symbol} The shared symbol key.
|
|
286
|
-
*
|
|
287
|
-
* @example
|
|
288
|
-
* // Create or retrieve a shared symbol key with associated data
|
|
289
|
-
* const sharedSymbol = Symbol.sharedKey('mySharedKey', {
|
|
290
|
-
* associate: { foo: 'bar' },
|
|
291
|
-
* embed: { baz: 'qux' },
|
|
292
|
-
* useDomain: 'exampleDomain',
|
|
293
|
-
* useSeparator: '-',
|
|
294
|
-
* })
|
|
295
|
-
* console.log(sharedSymbol)
|
|
296
|
-
* // Output: Symbol(@exampleDomain-mySharedKey {"baz":"qux"} #shared)
|
|
297
|
-
*/
|
|
298
|
-
sharedKey(named, options) {
|
|
299
|
-
return this.keys.sharedKey(named, options);
|
|
300
|
-
},
|
|
301
|
-
/**
|
|
302
|
-
* A symbol used as the storage key for the single instance of a
|
|
303
|
-
* singleton.
|
|
304
|
-
*
|
|
305
|
-
* This getter returns a unique symbol created using `Symbol.for()`
|
|
306
|
-
* with the string 'singleton'. The symbol is used to store and
|
|
307
|
-
* retrieve the single instance of a singleton object.
|
|
308
|
-
*
|
|
309
|
-
* @type {symbol}
|
|
310
|
-
* @readonly
|
|
311
|
-
*
|
|
312
|
-
* @example
|
|
313
|
-
* const singletonKey = Symbol.singleton
|
|
314
|
-
* const singletonInstance = {}
|
|
315
|
-
* global[singletonKey] = singletonInstance
|
|
316
|
-
*/
|
|
317
|
-
get singleton() {
|
|
318
|
-
return Symbol.for('singleton');
|
|
319
|
-
},
|
|
320
|
-
/**
|
|
321
|
-
* Creates a new Symbol with the given name and optional data. If data
|
|
322
|
-
* is provided, it will be stringified and appended to the symbol's
|
|
323
|
-
* name. This method is useful for creating unique symbols that carry
|
|
324
|
-
* additional metadata.
|
|
325
|
-
*
|
|
326
|
-
* @param {string} name The name of the symbol.
|
|
327
|
-
* @param {*} [data] Optional data to be associated with the symbol.
|
|
328
|
-
* @returns {symbol} A new symbol created with Symbol.for(), using the
|
|
329
|
-
* provided name and stringified data (if provided).
|
|
330
|
-
*
|
|
331
|
-
* @example
|
|
332
|
-
* const symbolWithData = Symbol.withData('mySymbol', { foo: 'bar' })
|
|
333
|
-
* console.log(symbolWithData.toString())
|
|
334
|
-
* // Output: "Symbol(mySymbol {"foo":"bar"})"
|
|
335
|
-
*
|
|
336
|
-
* @example
|
|
337
|
-
* const symbolWithoutData = Symbol.withData('mySymbol')
|
|
338
|
-
* console.log(symbolWithoutData.toString())
|
|
339
|
-
* // Output: "Symbol(mySymbol)"
|
|
340
|
-
*/
|
|
341
|
-
withData(name, data) {
|
|
342
|
-
return data !== undefined
|
|
343
|
-
? Symbol.for(`${name} ${JSON.stringify(data)}`)
|
|
344
|
-
: Symbol.for(name);
|
|
345
|
-
},
|
|
346
|
-
});
|
|
347
|
-
export const SymbolPrototypeExtensions = new Patch(Symbol.prototype, {
|
|
348
|
-
[Patch.kMutablyHidden]: {
|
|
349
|
-
/**
|
|
350
|
-
* Returns an object representation of the symbol instance.
|
|
351
|
-
*
|
|
352
|
-
* This getter method creates and returns an object that wraps the
|
|
353
|
-
* symbol instance, allowing it to be treated as an object. The
|
|
354
|
-
* returned object is created using the `Object()` constructor,
|
|
355
|
-
* which takes the symbol instance as its argument.
|
|
356
|
-
*
|
|
357
|
-
* @type {Object}
|
|
358
|
-
* @readonly
|
|
359
|
-
*
|
|
360
|
-
* @example
|
|
361
|
-
* const sym = Symbol('example')
|
|
362
|
-
* console.log(typeof sym) // 'symbol'
|
|
363
|
-
* console.log(typeof sym.instance) // 'object'
|
|
364
|
-
*/
|
|
365
|
-
get instance() {
|
|
366
|
-
return Object(this);
|
|
367
|
-
},
|
|
368
|
-
/**
|
|
369
|
-
* Getter method for retrieving the data associated with a symbol.
|
|
370
|
-
*
|
|
371
|
-
* This method first checks if the symbol is a Symkey created symbol
|
|
372
|
-
* by checking the existence of Symbol.keys and if the symbol's
|
|
373
|
-
* description matches the Symkey pattern. If it is a Symkey symbol,
|
|
374
|
-
* it attempts to fetch its associated data.
|
|
375
|
-
*
|
|
376
|
-
* NOTE: Symkey data is returned as its value directly, this is because
|
|
377
|
-
* it is stored in a {@link Map}. Embedded JSON data might be expensive
|
|
378
|
-
* to parse and as such a function is returned when data is accessed that
|
|
379
|
-
* needs to be invoked in order to decode its contents. See
|
|
380
|
-
* `{@link mightHaveEmbeddedJSON}` for more information.
|
|
381
|
-
*
|
|
382
|
-
* If the symbol is not a Symkey symbol or if no data is associated
|
|
383
|
-
* with it, the method attempts to parse the symbol's description as
|
|
384
|
-
* JSON and returns the first valid JSON object found.
|
|
385
|
-
*
|
|
386
|
-
* If no valid JSON object is found in the description, the method
|
|
387
|
-
* returns undefined.
|
|
388
|
-
*
|
|
389
|
-
* @type {Object|Function}
|
|
390
|
-
* @readonly
|
|
391
|
-
*
|
|
392
|
-
* @example
|
|
393
|
-
* const keys = new Symkeys
|
|
394
|
-
* const key = keys.add('example', {count: 0})
|
|
395
|
-
* const data = key.data // note this isn't function!!
|
|
396
|
-
* const count = data.count
|
|
397
|
-
*
|
|
398
|
-
* @example
|
|
399
|
-
* const sym = Symbol.for('fun {"name":"Brie"}')
|
|
400
|
-
* let json = sym.data() // {name: 'Brie'} JS object
|
|
401
|
-
*
|
|
402
|
-
* @example
|
|
403
|
-
* const sym = Symbol('mySymbol')
|
|
404
|
-
* let data = sym.data() // undefined
|
|
405
|
-
*/
|
|
406
|
-
get data() {
|
|
407
|
-
if (Symbol?.keys && Symkeys.isSymkey(this)) {
|
|
408
|
-
const possibleData = Symbol.keys[this];
|
|
409
|
-
if (possibleData) {
|
|
410
|
-
return possibleData;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
let result = undefined;
|
|
414
|
-
let revertToggle = false;
|
|
415
|
-
if (!JSONExtensions.applied) {
|
|
416
|
-
JSONToggle.start();
|
|
417
|
-
revertToggle = true;
|
|
418
|
-
}
|
|
419
|
-
if (JSON.mightContain(this.description)) {
|
|
420
|
-
try {
|
|
421
|
-
result = JSON.extractFrom(this.description);
|
|
422
|
-
}
|
|
423
|
-
catch (ignore) { }
|
|
424
|
-
}
|
|
425
|
-
if (revertToggle) {
|
|
426
|
-
JSONToggle.stop();
|
|
427
|
-
}
|
|
428
|
-
return result;
|
|
429
|
-
},
|
|
430
|
-
/**
|
|
431
|
-
* Sets the data associated with a symbol.
|
|
432
|
-
*
|
|
433
|
-
* This setter method checks if the symbol is a Symkey and if it has
|
|
434
|
-
* associated data. If both conditions are met, it sets the data of the
|
|
435
|
-
* symbol to the provided value and returns true. If the conditions are
|
|
436
|
-
* not met, it simply returns false.
|
|
437
|
-
*
|
|
438
|
-
* While Symbols have been upgraded to also support embedded JSON data
|
|
439
|
-
* with this extension, symbol descriptions are static. Non Symkey symbols
|
|
440
|
-
* do not associated their data outside of a symbol, and cannot be changed,
|
|
441
|
-
* there new data cannot be set on them.
|
|
442
|
-
*
|
|
443
|
-
* @param {any} value - The value to be set as the symbol's data.
|
|
444
|
-
* @returns {boolean} - Returns true if the data was successfully set,
|
|
445
|
-
* false otherwise.
|
|
446
|
-
*
|
|
447
|
-
* @example
|
|
448
|
-
* const sym = Symbol.for('fun {"name":"Brie"}')
|
|
449
|
-
* Symkeys.isSymkey(sym) // false; not in Symkey format
|
|
450
|
-
* let json = sym.data() // {name: 'Brie'} JS object
|
|
451
|
-
* sym.data = JSON.stringify({name: 'Jane'}) // fails silently
|
|
452
|
-
* json = sym.data() // {name: 'Brie'} is hard-coded in description
|
|
453
|
-
*
|
|
454
|
-
* @example
|
|
455
|
-
* const sym = Symbol('mySymbol')
|
|
456
|
-
* Symkeys.isSymkey(sym) // false; not in Symkey format
|
|
457
|
-
* Symkeys.hasData(sym) // false
|
|
458
|
-
* sym.data = { name: 'John', age: 30 } // will fail silently
|
|
459
|
-
* Symkeys.hasData(sym) // still false
|
|
460
|
-
*
|
|
461
|
-
* // Now let's create a Symkey with data
|
|
462
|
-
* const symWithData = Symkeys.create('mySymbolWithData',
|
|
463
|
-
* { name: 'Jane', age: 25 })
|
|
464
|
-
* Symkeys.isSymkey(symWithData) // true
|
|
465
|
-
* Symkeys.hasData(symWithData) // true
|
|
466
|
-
* symWithData.data = { name: 'Jane', age: 26 } // will succeed
|
|
467
|
-
* Symkeys.getData(symWithData) // returns { name: 'Jane', age: 26 }
|
|
468
|
-
*/
|
|
469
|
-
set data(value) {
|
|
470
|
-
if (Symkeys.isSymkey(this)) {
|
|
471
|
-
Symbol.keys.setData(this, value);
|
|
472
|
-
}
|
|
473
|
-
else {
|
|
474
|
-
console.error(`The symbol ${this.description} is not a symkey`);
|
|
475
|
-
}
|
|
476
|
-
},
|
|
477
|
-
/**
|
|
478
|
-
* Retrieves the embedded JSON data from the symbol's description.
|
|
479
|
-
*
|
|
480
|
-
* This getter method checks if the symbol's description might contain
|
|
481
|
-
* JSON data. If the description contains JSON, it parses and returns
|
|
482
|
-
* the first JSON object found. If no JSON is found, it returns
|
|
483
|
-
* `undefined`.
|
|
484
|
-
*
|
|
485
|
-
* @returns {Object|undefined} - The parsed JSON object if found,
|
|
486
|
-
* otherwise `undefined`.
|
|
487
|
-
*
|
|
488
|
-
* @example
|
|
489
|
-
* const sym = Symbol.for('example {"name":"Brie"}')
|
|
490
|
-
* console.log(sym.embeddedJSON) // Output: { name: 'Brie' }
|
|
491
|
-
*
|
|
492
|
-
* @example
|
|
493
|
-
* const sym = Symbol('noJSON')
|
|
494
|
-
* console.log(sym.embeddedJSON) // Output: undefined
|
|
495
|
-
*/
|
|
496
|
-
get embeddedJSON() {
|
|
497
|
-
return JSONToggle.perform((toggle, patch) => {
|
|
498
|
-
let [mightHave, index, parsed] = JSON.mightContain(this.description, true);
|
|
499
|
-
if (mightHave) {
|
|
500
|
-
return parsed?.[0];
|
|
501
|
-
}
|
|
502
|
-
return undefined;
|
|
503
|
-
});
|
|
504
|
-
},
|
|
505
|
-
/**
|
|
506
|
-
* Parses the embedded JSON data from the symbol's description.
|
|
507
|
-
*
|
|
508
|
-
* This getter method first retrieves the embedded JSON data using the
|
|
509
|
-
* `embeddedJSON` getter. If JSON data is found, it attempts to parse
|
|
510
|
-
* the JSON string and return the resulting object. If parsing fails,
|
|
511
|
-
* an error is logged to the console.
|
|
512
|
-
*
|
|
513
|
-
* @returns {Object|undefined} - The parsed JSON object if parsing is
|
|
514
|
-
* successful, otherwise `undefined`.
|
|
515
|
-
*
|
|
516
|
-
* @example
|
|
517
|
-
* const sym = Symbol.for('example {"name":"Brie"}')
|
|
518
|
-
* console.log(sym.embeddedJSONParsed) // Output: { name: 'Brie' }
|
|
519
|
-
*
|
|
520
|
-
* @example
|
|
521
|
-
* const sym = Symbol('invalidJSON')
|
|
522
|
-
* console.log(sym.embeddedJSONParsed) // Output: undefined
|
|
523
|
-
*/
|
|
524
|
-
get embeddedJSONParsed() {
|
|
525
|
-
const json = this.embeddedJSON;
|
|
526
|
-
if (json) {
|
|
527
|
-
try {
|
|
528
|
-
return JSON.parse(json);
|
|
529
|
-
}
|
|
530
|
-
catch (error) {
|
|
531
|
-
console.error(`Failed to parse json: "${json}"`);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
return undefined;
|
|
535
|
-
},
|
|
536
|
-
/**
|
|
537
|
-
* Checks if the current symbol is a Symkey.
|
|
538
|
-
*
|
|
539
|
-
* This getter method determines whether the current symbol instance
|
|
540
|
-
* conforms to the Symkey pattern. A Symkey is a symbol that matches
|
|
541
|
-
* a specific pattern defined in the Symkeys class.
|
|
542
|
-
*
|
|
543
|
-
* @type {boolean}
|
|
544
|
-
* @readonly
|
|
545
|
-
*
|
|
546
|
-
* @returns {boolean} - Returns true if the symbol is a Symkey,
|
|
547
|
-
* otherwise false.
|
|
548
|
-
*
|
|
549
|
-
* @example
|
|
550
|
-
* const sym = Symbol('@nejs.prototype #rwiy2o905d')
|
|
551
|
-
* console.log(sym.isSymkey) // Output: true
|
|
552
|
-
*
|
|
553
|
-
* @example
|
|
554
|
-
* const sym = Symbol('regularSymbol')
|
|
555
|
-
* console.log(sym.isSymkey) // Output: false
|
|
556
|
-
*/
|
|
557
|
-
get isSymkey() {
|
|
558
|
-
return Symkeys.isSymkey(this);
|
|
559
|
-
},
|
|
560
|
-
/**
|
|
561
|
-
* Checks if the symbol might have embedded JSON data.
|
|
562
|
-
*
|
|
563
|
-
* This getter method checks if the symbol's description might contain
|
|
564
|
-
* JSON data and if the data property of the symbol is a function. If both
|
|
565
|
-
* conditions are met, it returns true, otherwise it returns false.
|
|
566
|
-
*
|
|
567
|
-
* @returns {boolean} - Returns true if the symbol might have embedded
|
|
568
|
-
* JSON, false otherwise.
|
|
569
|
-
*
|
|
570
|
-
* @example
|
|
571
|
-
* const sym = Symbol.for('fun {"name":"Brie"}')
|
|
572
|
-
* console.log(sym.mightHaveEmbeddedJSON) // Output: true
|
|
573
|
-
*
|
|
574
|
-
* @example
|
|
575
|
-
* const sym = Symbol('mySymbol')
|
|
576
|
-
* console.log(sym.mightHaveEmbeddedJSON) // Output: false
|
|
577
|
-
*/
|
|
578
|
-
get mightHaveEmbeddedJSON() {
|
|
579
|
-
return JSONToggle.perform((toggle, patch) => {
|
|
580
|
-
return JSON.mightContain(this.description);
|
|
581
|
-
});
|
|
582
|
-
},
|
|
583
|
-
/**
|
|
584
|
-
* Retrieves the reference object associated with the symbol.
|
|
585
|
-
*
|
|
586
|
-
* This getter method checks if the symbol's description matches a
|
|
587
|
-
* specific pattern using a regular expression. If a match is found,
|
|
588
|
-
* it extracts the token from the description and constructs a shared
|
|
589
|
-
* key using the token. It then retrieves the symbol associated with
|
|
590
|
-
* the shared key using the `sharedKey` method of the
|
|
591
|
-
* `SymbolExtensions.patches` object. Finally, it returns the data
|
|
592
|
-
* associated with the retrieved symbol.
|
|
593
|
-
*
|
|
594
|
-
* If no match is found or the retrieved symbol has no associated
|
|
595
|
-
* data, `undefined` is returned.
|
|
596
|
-
*
|
|
597
|
-
* @type {any}
|
|
598
|
-
* @readonly
|
|
599
|
-
*
|
|
600
|
-
* @example
|
|
601
|
-
* const sym = Symbol.for('@nejs.internal.refkey:example #shared')
|
|
602
|
-
* console.log(sym.refObject) // Output: the data associated with the
|
|
603
|
-
* // 'internal.refkey:example' shared key
|
|
604
|
-
*
|
|
605
|
-
* @example
|
|
606
|
-
* const sym = Symbol('no_match')
|
|
607
|
-
* console.log(sym.refObject) // Output: undefined
|
|
608
|
-
*/
|
|
609
|
-
get refObject() {
|
|
610
|
-
const re = /@nejs.internal.refkey:(\S+) #shared/.exec(this.description);
|
|
611
|
-
if (re?.[1]) {
|
|
612
|
-
const [_match, token] = re;
|
|
613
|
-
const shareKey = `internal.refkey:${token}`;
|
|
614
|
-
const symbol = SymbolExtensions.patches.sharedKey(shareKey);
|
|
615
|
-
return symbol?.data;
|
|
616
|
-
}
|
|
617
|
-
return undefined;
|
|
618
|
-
},
|
|
619
|
-
/**
|
|
620
|
-
* Returns a formatted string representation of the symbol's
|
|
621
|
-
* description, highlighting any embedded JSON data.
|
|
622
|
-
*
|
|
623
|
-
* This getter method checks if the symbol's description contains
|
|
624
|
-
* JSON data and formats it for better readability. It uses the
|
|
625
|
-
* `sgr` function from the `String` object to apply color formatting
|
|
626
|
-
* to the output.
|
|
627
|
-
*
|
|
628
|
-
* If the symbol's description contains JSON data longer than 30
|
|
629
|
-
* characters, it truncates the JSON data and displays an ellipsis
|
|
630
|
-
* in the middle. The JSON data is highlighted using the 'di' color
|
|
631
|
-
* code.
|
|
632
|
-
*
|
|
633
|
-
* @type {string}
|
|
634
|
-
* @readonly
|
|
635
|
-
*
|
|
636
|
-
* @example
|
|
637
|
-
* const sym = Symbol.for('mySymbol {"name":"John Doe"}')
|
|
638
|
-
* console.log(sym.sgrString)
|
|
639
|
-
* // Output: Symbol.for(mySymbol {"name":"John ...hn Doe"})
|
|
640
|
-
*
|
|
641
|
-
* @example
|
|
642
|
-
* const sym = Symbol('mySymbol')
|
|
643
|
-
* console.log(sym.sgrString)
|
|
644
|
-
* // Output: mySymbol
|
|
645
|
-
*/
|
|
646
|
-
get sgrString() {
|
|
647
|
-
let { sgr } = String;
|
|
648
|
-
if (!sgr) {
|
|
649
|
-
sgr = (string, ...args) => string;
|
|
650
|
-
}
|
|
651
|
-
const response = JSONToggle.perform((toggle, patch) => {
|
|
652
|
-
let [mightContain, index, jsonResponse] = JSON.mightContain(this.description, true);
|
|
653
|
-
let jsonText = jsonResponse?.[0];
|
|
654
|
-
if (mightContain) {
|
|
655
|
-
if (~index && jsonText && jsonText.length > 30) {
|
|
656
|
-
let desc = this.description;
|
|
657
|
-
let newDescription = [
|
|
658
|
-
sgr(`Symbol.for(${desc.slice(0, index)}`, 'green'),
|
|
659
|
-
sgr(jsonText.slice(0, 10), 'di'),
|
|
660
|
-
'...',
|
|
661
|
-
sgr(jsonText.slice(-5), 'di'),
|
|
662
|
-
sgr(`${desc.slice(index + jsonText.length + 1)})`, 'green')
|
|
663
|
-
].join('');
|
|
664
|
-
return `${newDescription}`;
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
});
|
|
668
|
-
return response ?? this.description;
|
|
669
|
-
},
|
|
670
|
-
/**
|
|
671
|
-
* Custom inspect method for Node.js util.inspect.
|
|
672
|
-
*
|
|
673
|
-
* NOTE: this will only apply if looking at an instance of Symbol,
|
|
674
|
-
* sadly; {@see SymbolPrototypeExtensions.instance}
|
|
675
|
-
*
|
|
676
|
-
* This method is used by Node.js util.inspect to provide a custom
|
|
677
|
-
* representation of the symbol when inspected. It checks if the
|
|
678
|
-
* symbol's description might contain JSON data and if so, it
|
|
679
|
-
* truncates the JSON data in the description to a maximum of 30
|
|
680
|
-
* characters and formats the output with colors using the `sgr`
|
|
681
|
-
* function from the `String` object.
|
|
682
|
-
*
|
|
683
|
-
* @param {number} depth - The current depth of the recursive
|
|
684
|
-
* inspection.
|
|
685
|
-
* @param {Object} options - The options object passed to
|
|
686
|
-
* util.inspect.
|
|
687
|
-
* @param {Function} inspect - The original util.inspect function.
|
|
688
|
-
*
|
|
689
|
-
* @returns {string} - The formatted representation of the symbol.
|
|
690
|
-
*
|
|
691
|
-
* @example
|
|
692
|
-
* const sym = Symbol.for('fun {"name":"John Doe"}')
|
|
693
|
-
* console.log(util.inspect(sym))
|
|
694
|
-
* // Output: Symbol.for(fun {"name":"John ...hn Doe"})
|
|
695
|
-
*/
|
|
696
|
-
[Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
|
|
697
|
-
let revert = false;
|
|
698
|
-
let detail = undefined;
|
|
699
|
-
let { sgr } = String;
|
|
700
|
-
if (!sgr) {
|
|
701
|
-
sgr = (string, ...args) => string;
|
|
702
|
-
}
|
|
703
|
-
if (!JSONExtensions.applied) {
|
|
704
|
-
JSONToggle.start();
|
|
705
|
-
revert = true;
|
|
706
|
-
}
|
|
707
|
-
if ((detail = JSON.mightContain(this.description, true))) {
|
|
708
|
-
let jsonText = detail[2]?.[0];
|
|
709
|
-
let index = detail[1];
|
|
710
|
-
if (~index && jsonText && jsonText.length > 30) {
|
|
711
|
-
let desc = this.description;
|
|
712
|
-
let newDescription = [
|
|
713
|
-
sgr(`Symbol.for(${desc.slice(0, index)}`, 'green'),
|
|
714
|
-
sgr(jsonText.slice(0, 10), 'di'),
|
|
715
|
-
'...',
|
|
716
|
-
sgr(jsonText.slice(-5), 'di'),
|
|
717
|
-
sgr(`${desc.slice(index + jsonText.length + 1)})`, 'green'),
|
|
718
|
-
].join('');
|
|
719
|
-
if (revert) {
|
|
720
|
-
JSONToggle.stop();
|
|
721
|
-
}
|
|
722
|
-
return `${newDescription}`;
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
if (revert) {
|
|
726
|
-
JSONToggle.stop();
|
|
727
|
-
}
|
|
728
|
-
return inspect(this, { colors: true });
|
|
729
|
-
},
|
|
730
|
-
},
|
|
731
|
-
});
|
|
732
|
-
//# sourceMappingURL=symbol.extensions.js.map
|