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