@nejs/basic-extensions 2.6.0 → 2.8.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.
Files changed (271) hide show
  1. package/.vscode/settings.json +5 -0
  2. package/README.md +6129 -1574
  3. package/dist/@nejs/basic-extensions.bundle.2.7.0.js +19 -0
  4. package/dist/@nejs/basic-extensions.bundle.2.7.0.js.map +7 -0
  5. package/dist/cjs/array.extensions.d.ts +39 -0
  6. package/dist/cjs/array.extensions.js +303 -0
  7. package/dist/cjs/array.extensions.js.map +1 -0
  8. package/dist/cjs/big.int.extension.d.ts +31 -0
  9. package/dist/cjs/big.int.extension.js +164 -0
  10. package/dist/cjs/big.int.extension.js.map +1 -0
  11. package/dist/cjs/{newClasses → classes}/asyncIterable.js +32 -44
  12. package/dist/cjs/classes/asyncIterable.js.map +1 -0
  13. package/dist/cjs/{newClasses → classes}/deferred.js +66 -138
  14. package/dist/cjs/classes/deferred.js.map +1 -0
  15. package/dist/cjs/{newClasses → classes}/descriptor.js +56 -90
  16. package/dist/cjs/classes/descriptor.js.map +1 -0
  17. package/dist/cjs/classes/index.d.ts +13 -0
  18. package/dist/cjs/classes/index.js +57 -0
  19. package/dist/cjs/classes/index.js.map +1 -0
  20. package/dist/cjs/classes/introspector.d.ts +20 -0
  21. package/dist/cjs/classes/introspector.js +130 -0
  22. package/dist/cjs/classes/introspector.js.map +1 -0
  23. package/dist/cjs/{newClasses → classes}/iterable.js +42 -63
  24. package/dist/cjs/classes/iterable.js.map +1 -0
  25. package/dist/cjs/classes/param.parser.d.ts +227 -0
  26. package/dist/cjs/classes/param.parser.js +242 -0
  27. package/dist/cjs/classes/param.parser.js.map +1 -0
  28. package/dist/cjs/classes/pluggable.proxy.d.ts +152 -0
  29. package/dist/cjs/classes/pluggable.proxy.js +444 -0
  30. package/dist/cjs/classes/pluggable.proxy.js.map +1 -0
  31. package/dist/cjs/{newClasses → classes}/refmap.js +18 -30
  32. package/dist/cjs/classes/refmap.js.map +1 -0
  33. package/dist/cjs/{newClasses → classes}/refset.js +28 -47
  34. package/dist/cjs/classes/refset.js.map +1 -0
  35. package/dist/cjs/classes/symkeys.d.ts +292 -0
  36. package/dist/cjs/classes/symkeys.js +424 -0
  37. package/dist/cjs/classes/symkeys.js.map +1 -0
  38. package/dist/cjs/classes/type.d.ts +56 -0
  39. package/dist/cjs/classes/type.js +405 -0
  40. package/dist/cjs/classes/type.js.map +1 -0
  41. package/dist/cjs/function.extensions.js +757 -0
  42. package/dist/cjs/function.extensions.js.map +1 -0
  43. package/dist/cjs/global.this.js +261 -0
  44. package/dist/cjs/global.this.js.map +1 -0
  45. package/dist/cjs/index.d.ts +4 -3
  46. package/dist/cjs/index.js +62 -32
  47. package/dist/cjs/index.js.map +1 -1
  48. package/dist/cjs/json.extensions.d.ts +2 -0
  49. package/dist/cjs/json.extensions.js +107 -0
  50. package/dist/cjs/json.extensions.js.map +1 -0
  51. package/dist/{mjs/mapextensions.d.ts → cjs/map.extensions.d.ts} +1 -0
  52. package/dist/cjs/map.extensions.js +142 -0
  53. package/dist/cjs/map.extensions.js.map +1 -0
  54. package/dist/cjs/number.extension.d.ts +44 -0
  55. package/dist/cjs/number.extension.js +260 -0
  56. package/dist/cjs/number.extension.js.map +1 -0
  57. package/dist/cjs/object.extensions.d.ts +62 -0
  58. package/dist/cjs/object.extensions.js +1116 -0
  59. package/dist/cjs/object.extensions.js.map +1 -0
  60. package/dist/cjs/proxy.extensions.d.ts +2 -0
  61. package/dist/cjs/proxy.extensions.js +207 -0
  62. package/dist/cjs/proxy.extensions.js.map +1 -0
  63. package/dist/cjs/reflect.extensions.js +316 -0
  64. package/dist/cjs/reflect.extensions.js.map +1 -0
  65. package/dist/cjs/regular.expression.extensions.d.ts +2 -0
  66. package/dist/cjs/regular.expression.extensions.js +423 -0
  67. package/dist/cjs/regular.expression.extensions.js.map +1 -0
  68. package/dist/cjs/set.extensions.d.ts +40 -0
  69. package/dist/cjs/{setextensions.js → set.extensions.js} +150 -2
  70. package/dist/cjs/set.extensions.js.map +1 -0
  71. package/dist/cjs/string.extensions.js +471 -0
  72. package/dist/cjs/string.extensions.js.map +1 -0
  73. package/dist/{mjs/symbolextensions.d.ts → cjs/symbol.extensions.d.ts} +1 -0
  74. package/dist/cjs/symbol.extensions.js +259 -0
  75. package/dist/cjs/symbol.extensions.js.map +1 -0
  76. package/dist/cjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
  77. package/dist/cjs/weakref.extensions.js.map +1 -0
  78. package/dist/mjs/array.extensions.d.ts +39 -0
  79. package/dist/mjs/array.extensions.js +300 -0
  80. package/dist/mjs/array.extensions.js.map +1 -0
  81. package/dist/mjs/big.int.extension.d.ts +31 -0
  82. package/dist/mjs/big.int.extension.js +161 -0
  83. package/dist/mjs/big.int.extension.js.map +1 -0
  84. package/dist/mjs/classes/asyncIterable.js.map +1 -0
  85. package/dist/mjs/classes/deferred.js.map +1 -0
  86. package/dist/mjs/{newClasses → classes}/descriptor.js +7 -4
  87. package/dist/mjs/classes/descriptor.js.map +1 -0
  88. package/dist/mjs/classes/index.d.ts +13 -0
  89. package/dist/mjs/classes/index.js +40 -0
  90. package/dist/mjs/classes/index.js.map +1 -0
  91. package/dist/mjs/classes/introspector.d.ts +20 -0
  92. package/dist/mjs/classes/introspector.js +126 -0
  93. package/dist/mjs/classes/introspector.js.map +1 -0
  94. package/dist/mjs/classes/iterable.js.map +1 -0
  95. package/dist/mjs/classes/param.parser.d.ts +227 -0
  96. package/dist/mjs/classes/param.parser.js +238 -0
  97. package/dist/mjs/classes/param.parser.js.map +1 -0
  98. package/dist/mjs/classes/pluggable.proxy.d.ts +152 -0
  99. package/dist/mjs/classes/pluggable.proxy.js +438 -0
  100. package/dist/mjs/classes/pluggable.proxy.js.map +1 -0
  101. package/dist/mjs/{newClasses → classes}/refmap.js +3 -3
  102. package/dist/mjs/classes/refmap.js.map +1 -0
  103. package/dist/mjs/classes/refset.js.map +1 -0
  104. package/dist/mjs/classes/symkeys.d.ts +292 -0
  105. package/dist/mjs/classes/symkeys.js +420 -0
  106. package/dist/mjs/classes/symkeys.js.map +1 -0
  107. package/dist/mjs/classes/type.d.ts +56 -0
  108. package/dist/mjs/classes/type.js +401 -0
  109. package/dist/mjs/classes/type.js.map +1 -0
  110. package/dist/mjs/function.extensions.js +754 -0
  111. package/dist/mjs/function.extensions.js.map +1 -0
  112. package/dist/mjs/global.this.js +258 -0
  113. package/dist/mjs/global.this.js.map +1 -0
  114. package/dist/mjs/index.d.ts +4 -3
  115. package/dist/mjs/index.js +49 -19
  116. package/dist/mjs/index.js.map +1 -1
  117. package/dist/mjs/json.extensions.d.ts +2 -0
  118. package/dist/mjs/json.extensions.js +104 -0
  119. package/dist/mjs/json.extensions.js.map +1 -0
  120. package/dist/{cjs/mapextensions.d.ts → mjs/map.extensions.d.ts} +1 -0
  121. package/dist/mjs/map.extensions.js +139 -0
  122. package/dist/mjs/map.extensions.js.map +1 -0
  123. package/dist/mjs/number.extension.d.ts +44 -0
  124. package/dist/mjs/number.extension.js +257 -0
  125. package/dist/mjs/number.extension.js.map +1 -0
  126. package/dist/mjs/object.extensions.d.ts +62 -0
  127. package/dist/mjs/object.extensions.js +1112 -0
  128. package/dist/mjs/object.extensions.js.map +1 -0
  129. package/dist/mjs/proxy.extensions.d.ts +2 -0
  130. package/dist/mjs/proxy.extensions.js +204 -0
  131. package/dist/mjs/proxy.extensions.js.map +1 -0
  132. package/dist/mjs/reflect.extensions.js +313 -0
  133. package/dist/mjs/reflect.extensions.js.map +1 -0
  134. package/dist/mjs/regular.expression.extensions.d.ts +2 -0
  135. package/dist/mjs/regular.expression.extensions.js +420 -0
  136. package/dist/mjs/regular.expression.extensions.js.map +1 -0
  137. package/dist/mjs/set.extensions.d.ts +40 -0
  138. package/dist/mjs/{setextensions.js → set.extensions.js} +149 -1
  139. package/dist/mjs/set.extensions.js.map +1 -0
  140. package/dist/mjs/string.extensions.js +468 -0
  141. package/dist/mjs/string.extensions.js.map +1 -0
  142. package/dist/{cjs/symbolextensions.d.ts → mjs/symbol.extensions.d.ts} +1 -0
  143. package/dist/mjs/symbol.extensions.js +256 -0
  144. package/dist/mjs/symbol.extensions.js.map +1 -0
  145. package/dist/mjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
  146. package/dist/mjs/weakref.extensions.js.map +1 -0
  147. package/docs/index.html +24045 -5805
  148. package/package.json +6 -4
  149. package/repl.bootstrap.js +21 -0
  150. package/src/array.extensions.js +322 -0
  151. package/src/big.int.extension.js +163 -0
  152. package/src/{newClasses → classes}/descriptor.js +16 -12
  153. package/src/classes/index.js +51 -0
  154. package/src/classes/introspector.js +167 -0
  155. package/src/classes/param.parser.js +253 -0
  156. package/src/classes/pluggable.proxy.js +485 -0
  157. package/src/{newClasses → classes}/refmap.js +5 -3
  158. package/src/classes/symkeys.js +464 -0
  159. package/src/classes/type.js +427 -0
  160. package/src/function.extensions.js +818 -0
  161. package/src/global.this.js +304 -0
  162. package/src/index.js +56 -23
  163. package/src/json.extensions.js +108 -0
  164. package/src/map.extensions.js +144 -0
  165. package/src/number.extension.js +273 -0
  166. package/src/object.extensions.js +1222 -0
  167. package/src/proxy.extensions.js +229 -0
  168. package/src/reflect.extensions.js +346 -0
  169. package/src/regular.expression.extensions.js +451 -0
  170. package/src/{setextensions.js → set.extensions.js} +151 -2
  171. package/src/string.extensions.js +515 -0
  172. package/src/symbol.extensions.js +268 -0
  173. package/tests/newClasses/refmap.test.js +3 -2
  174. package/tsconfig.base.json +5 -3
  175. package/tsconfig.cjs.json +2 -2
  176. package/tsconfig.esm.json +2 -2
  177. package/dist/@nejs/basic-extensions.bundle.2.5.0.js +0 -8
  178. package/dist/@nejs/basic-extensions.bundle.2.5.0.js.map +0 -7
  179. package/dist/cjs/arrayextensions.d.ts +0 -10
  180. package/dist/cjs/arrayextensions.js +0 -73
  181. package/dist/cjs/arrayextensions.js.map +0 -1
  182. package/dist/cjs/functionextensions.js +0 -202
  183. package/dist/cjs/functionextensions.js.map +0 -1
  184. package/dist/cjs/globals.js +0 -166
  185. package/dist/cjs/globals.js.map +0 -1
  186. package/dist/cjs/mapextensions.js +0 -32
  187. package/dist/cjs/mapextensions.js.map +0 -1
  188. package/dist/cjs/newClasses/asyncIterable.js.map +0 -1
  189. package/dist/cjs/newClasses/deferred.js.map +0 -1
  190. package/dist/cjs/newClasses/descriptor.js.map +0 -1
  191. package/dist/cjs/newClasses/iterable.js.map +0 -1
  192. package/dist/cjs/newClasses/refmap.js.map +0 -1
  193. package/dist/cjs/newClasses/refset.js.map +0 -1
  194. package/dist/cjs/objectextensions.d.ts +0 -11
  195. package/dist/cjs/objectextensions.js +0 -232
  196. package/dist/cjs/objectextensions.js.map +0 -1
  197. package/dist/cjs/reflectextensions.js +0 -111
  198. package/dist/cjs/reflectextensions.js.map +0 -1
  199. package/dist/cjs/setextensions.d.ts +0 -2
  200. package/dist/cjs/setextensions.js.map +0 -1
  201. package/dist/cjs/stringextensions.js +0 -158
  202. package/dist/cjs/stringextensions.js.map +0 -1
  203. package/dist/cjs/symbolextensions.js +0 -69
  204. package/dist/cjs/symbolextensions.js.map +0 -1
  205. package/dist/cjs/weakrefextensions.js.map +0 -1
  206. package/dist/mjs/arrayextensions.d.ts +0 -10
  207. package/dist/mjs/arrayextensions.js +0 -70
  208. package/dist/mjs/arrayextensions.js.map +0 -1
  209. package/dist/mjs/functionextensions.js +0 -199
  210. package/dist/mjs/functionextensions.js.map +0 -1
  211. package/dist/mjs/globals.js +0 -163
  212. package/dist/mjs/globals.js.map +0 -1
  213. package/dist/mjs/mapextensions.js +0 -29
  214. package/dist/mjs/mapextensions.js.map +0 -1
  215. package/dist/mjs/newClasses/asyncIterable.js.map +0 -1
  216. package/dist/mjs/newClasses/deferred.js.map +0 -1
  217. package/dist/mjs/newClasses/descriptor.js.map +0 -1
  218. package/dist/mjs/newClasses/iterable.js.map +0 -1
  219. package/dist/mjs/newClasses/refmap.js.map +0 -1
  220. package/dist/mjs/newClasses/refset.js.map +0 -1
  221. package/dist/mjs/objectextensions.d.ts +0 -11
  222. package/dist/mjs/objectextensions.js +0 -229
  223. package/dist/mjs/objectextensions.js.map +0 -1
  224. package/dist/mjs/reflectextensions.js +0 -108
  225. package/dist/mjs/reflectextensions.js.map +0 -1
  226. package/dist/mjs/setextensions.d.ts +0 -2
  227. package/dist/mjs/setextensions.js.map +0 -1
  228. package/dist/mjs/stringextensions.js +0 -155
  229. package/dist/mjs/stringextensions.js.map +0 -1
  230. package/dist/mjs/symbolextensions.js +0 -66
  231. package/dist/mjs/symbolextensions.js.map +0 -1
  232. package/dist/mjs/weakrefextensions.js.map +0 -1
  233. package/src/arrayextensions.js +0 -75
  234. package/src/functionextensions.js +0 -225
  235. package/src/globals.js +0 -196
  236. package/src/mapextensions.js +0 -32
  237. package/src/objectextensions.js +0 -256
  238. package/src/reflectextensions.js +0 -118
  239. package/src/stringextensions.js +0 -166
  240. package/src/symbolextensions.js +0 -69
  241. /package/dist/cjs/{newClasses → classes}/asyncIterable.d.ts +0 -0
  242. /package/dist/cjs/{newClasses → classes}/deferred.d.ts +0 -0
  243. /package/dist/cjs/{newClasses → classes}/descriptor.d.ts +0 -0
  244. /package/dist/cjs/{newClasses → classes}/iterable.d.ts +0 -0
  245. /package/dist/cjs/{newClasses → classes}/refmap.d.ts +0 -0
  246. /package/dist/cjs/{newClasses → classes}/refset.d.ts +0 -0
  247. /package/dist/cjs/{functionextensions.d.ts → function.extensions.d.ts} +0 -0
  248. /package/dist/cjs/{globals.d.ts → global.this.d.ts} +0 -0
  249. /package/dist/cjs/{reflectextensions.d.ts → reflect.extensions.d.ts} +0 -0
  250. /package/dist/cjs/{stringextensions.d.ts → string.extensions.d.ts} +0 -0
  251. /package/dist/cjs/{weakrefextensions.d.ts → weakref.extensions.d.ts} +0 -0
  252. /package/dist/mjs/{newClasses → classes}/asyncIterable.d.ts +0 -0
  253. /package/dist/mjs/{newClasses → classes}/asyncIterable.js +0 -0
  254. /package/dist/mjs/{newClasses → classes}/deferred.d.ts +0 -0
  255. /package/dist/mjs/{newClasses → classes}/deferred.js +0 -0
  256. /package/dist/mjs/{newClasses → classes}/descriptor.d.ts +0 -0
  257. /package/dist/mjs/{newClasses → classes}/iterable.d.ts +0 -0
  258. /package/dist/mjs/{newClasses → classes}/iterable.js +0 -0
  259. /package/dist/mjs/{newClasses → classes}/refmap.d.ts +0 -0
  260. /package/dist/mjs/{newClasses → classes}/refset.d.ts +0 -0
  261. /package/dist/mjs/{newClasses → classes}/refset.js +0 -0
  262. /package/dist/mjs/{functionextensions.d.ts → function.extensions.d.ts} +0 -0
  263. /package/dist/mjs/{globals.d.ts → global.this.d.ts} +0 -0
  264. /package/dist/mjs/{reflectextensions.d.ts → reflect.extensions.d.ts} +0 -0
  265. /package/dist/mjs/{stringextensions.d.ts → string.extensions.d.ts} +0 -0
  266. /package/dist/mjs/{weakrefextensions.d.ts → weakref.extensions.d.ts} +0 -0
  267. /package/src/{newClasses → classes}/asyncIterable.js +0 -0
  268. /package/src/{newClasses → classes}/deferred.js +0 -0
  269. /package/src/{newClasses → classes}/iterable.js +0 -0
  270. /package/src/{newClasses → classes}/refset.js +0 -0
  271. /package/src/{weakrefextensions.js → weakref.extensions.js} +0 -0
@@ -0,0 +1,464 @@
1
+ import { Extension } from '@nejs/extension'
2
+
3
+ /**
4
+ * Represents a secure container for storing and retrieving unique symbols
5
+ * associated with data. This class provides methods to add new symbols to
6
+ * the Symkeys and to retrieve data associated with a particular symbol.
7
+ *
8
+ * @example
9
+ * // Create a new Symkeys instance
10
+ * const symkeys = new Symkeys();
11
+ *
12
+ * // Add a symbol with associated data to the Symkeys
13
+ * const mySymbol = Symkeys.add('myIdentifier', { foo: 'bar' });
14
+ *
15
+ * // Retrieve the data using the symbol
16
+ * const myData = Symkeys.dataFor(mySymbol);
17
+ * console.log(myData); // Output: { foo: 'bar' }
18
+ */
19
+ export class Symkeys {
20
+ /**
21
+ * Adds a new entry to the Symkeys with a unique symbol based on the provided
22
+ * name and associates it with the given data.
23
+ *
24
+ * @param named - The base name for the symbol to be created.
25
+ * @param [associatedData={}] - The data to associate with the symbol.
26
+ * @returns The unique symbol created for the entry.
27
+ *
28
+ * @example
29
+ * // Add an entry with associated data
30
+ * const symbol = Symkeys.add('myEntry', { foo: 'bar' });
31
+ * // Retrieve the associated data using the symbol
32
+ * const data = Symkeys.dataFor(symbol);
33
+ * console.log(data); // Output: { foo: 'bar' }
34
+ */
35
+ add(named, associatedData = {}) {
36
+ // Generate a unique token for the symbol
37
+ const token = Symkeys.token;
38
+
39
+ // Calculate a name (optionally with domain and separator)
40
+ const symName = this.calculateName(named)
41
+
42
+ // Create a symbol using the provided name and the unique token
43
+ const symbol = Symbol.for(`@${symName} #${token}`);
44
+
45
+ // Store the symbol and associated data in the Symkeys's internal map
46
+ this[Symkeys.kDataKey].set(symbol, associatedData);
47
+
48
+ // Return the unique symbol for external use
49
+ return symbol;
50
+ }
51
+
52
+ /**
53
+ * Retrieves the data associated with a given symbol from the Symkeys.
54
+ *
55
+ * This method allows access to the data that has been associated with a
56
+ * particular symbol in the Symkeys. It is useful for retrieving stored
57
+ * information when only the symbol is known.
58
+ *
59
+ * @param symbol - The symbol whose associated data is to be
60
+ * retrieved.
61
+ * @returns The data associated with the symbol, or undefined if
62
+ * the symbol is not found in the Symkeys.
63
+ *
64
+ * @example
65
+ * // Assuming 'mySymbol' is a symbol that has been added to the Symkeys
66
+ * // with associated data
67
+ * const data = Symkeys.dataFor(mySymbol);
68
+ * console.log(data); // Output: The data associated with 'mySymbol'
69
+ */
70
+ data(forSymbol) {
71
+ return this[Symkeys.kDataKey].get(forSymbol)
72
+ }
73
+
74
+ /**
75
+ * Deletes the data associated with a given symbol from the Symkeys.
76
+ *
77
+ * This method allows removal of the data that has been associated with a
78
+ * particular symbol in the Symkeys. It is useful when you want to clean up
79
+ * or remove stored information associated with a symbol.
80
+ *
81
+ * @param {Symbol} forSymbol - The symbol whose associated data is to be
82
+ * deleted.
83
+ * @param {*} replaceWith - Optionally, if `replaceWith` is not `undefined`,
84
+ * a new value can be set after the original is deleted
85
+ * @returns {boolean} - Returns true if an element in the Symkeys existed and
86
+ * has been removed, or false if the element does not exist
87
+ *
88
+ * @example
89
+ * // Assuming 'mySymbol' is a symbol that has been added to the Symkeys
90
+ * // with associated data
91
+ * const isDeleted = Symkeys.deleteData(mySymbol);
92
+ * console.log(isDeleted); // Output: true if data was deleted, false otherwise
93
+ */
94
+ deleteData(forSymbol, replaceWith = undefined) {
95
+ if (this.hasData(forSymbol)) {
96
+ const result = this[Symkeys.kDataKey].delete(forSymbol)
97
+
98
+ if (replaceWith !== undefined) {
99
+ this[Symkeys.kDataKey].set(forSymbol, replaceWith)
100
+ }
101
+
102
+ return result
103
+ }
104
+
105
+ return false
106
+ }
107
+
108
+ /**
109
+ * Checks if the Symkeys instance has data associated with a given symbol.
110
+ *
111
+ * This method checks if the Symkeys instance has any data associated with
112
+ * the provided symbol. It is useful when you need to verify if data exists
113
+ * for a particular symbol before attempting to retrieve or manipulate it.
114
+ *
115
+ * @param {Symbol} forSymbol - The symbol to check for associated data.
116
+ * @returns {boolean} Returns true if data exists for the symbol, false otherwise.
117
+ *
118
+ * @example
119
+ * // Assuming 'mySymbol' is a symbol that has been added to the Symkeys
120
+ * // with associated data
121
+ * const hasData = Symkeys.hasData(mySymbol);
122
+ * console.log(hasData); // Output: true if data exists, false otherwise
123
+ */
124
+ hasData(forSymbol) {
125
+ return this[Symkeys.kDataKey].has(forSymbol)
126
+ }
127
+
128
+ /**
129
+ * Sets the data associated with a given symbol in the Symkeys.
130
+ *
131
+ * This method allows you to associate data with a particular symbol in the
132
+ * Symkeys. It is useful when you want to store information that can be
133
+ * retrieved later using the symbol.
134
+ *
135
+ * Note that setting only succeeds if the Symkey symbol has already been
136
+ * added via {@link Symkeys.add}
137
+ *
138
+ * @param {Symbol} forSymbol - The symbol with which the data is to be
139
+ * associated.
140
+ * @param {*} value - The data to be associated with the symbol.
141
+ * @returns {boolean} true if the value has been set, false if the key
142
+ * has not yet been added via {@link Symkeys.add}
143
+ *
144
+ * @example
145
+ * // Assuming 'mySymbol' is a symbol that has been added to the Symkeys
146
+ * // and 'myData' is the data to be associated with 'mySymbol'
147
+ * Symkeys.setData(mySymbol, myData);
148
+ */
149
+ setData(forSymbol, value) {
150
+ if (this.hasData(forSymbol)) {
151
+ this[Symkeys.kDataKey].set(forSymbol, value)
152
+ return true
153
+ }
154
+ return false
155
+ }
156
+
157
+ /**
158
+ * Extracts the token part from a symbol created by the `add` method.
159
+ *
160
+ * This method parses the string representation of a symbol to retrieve
161
+ * the unique token that was appended to the symbol's name upon creation.
162
+ * It is useful for debugging or for operations that require knowledge of
163
+ * the token associated with a symbol.
164
+ *
165
+ * @param symbol - The symbol whose token is to be extracted.
166
+ * @returns The extracted token or undefined if the
167
+ * token cannot be extracted.
168
+ *
169
+ * @example
170
+ * // Assuming 'mySymbol' is a symbol created with the name 'myEntry'
171
+ * // and a token 'agftofxob6f'
172
+ * const token = Symkeys.tokenFor(mySymbol);
173
+ * console.log(token); // Output: 'agftofxob6f'
174
+ */
175
+ token(forSymbol) {
176
+ // Use a regular expression to match the token pattern in the symbol
177
+ // description exists on symbol but our JS output target is too old
178
+ return /^.* \#(.*?)$/.exec(forSymbol).description?.[1];
179
+ }
180
+
181
+ /**
182
+ * Retrieves the separator used in the Symkeys instance.
183
+ *
184
+ * This getter method allows access to the separator that is used to
185
+ * distinguish between different parts of a symbol in the Symkeys instance.
186
+ * It is useful when you need to know the separator for parsing symbols or
187
+ * constructing new ones.
188
+ *
189
+ * @returns {string} The separator used in the Symkeys instance.
190
+ *
191
+ * @example
192
+ * // Assuming the Symkeys instance has a separator '.'
193
+ * const separator = Symkeys.separator;
194
+ * console.log(separator); // Output: '.'
195
+ */
196
+ get separator() {
197
+ return this[kSeparator]
198
+ }
199
+
200
+ /**
201
+ * Retrieves an iterator for the symbols stored in the Symkeys.
202
+ *
203
+ * This method provides access to the symbols that have been stored in
204
+ * the Symkeys. It returns an iterator which can be used to loop over
205
+ * all the symbols. This is particularly useful for iterating through
206
+ * all stored data without knowing the individual symbols in advance.
207
+ *
208
+ * @returns An iterator that yields all the symbols
209
+ * stored in the Symkeys.
210
+ *
211
+ * @example
212
+ * // Assuming the Symkeys has symbols stored
213
+ * for (const symbol of Symkeys.symbols()) {
214
+ * console.log(symbol);
215
+ * }
216
+ */
217
+ symbols() {
218
+ // Retrieve the keys (symbols) from the Symkeys data map and return
219
+ // the iterator.
220
+ return this[Symkeys.kDataKey].keys();
221
+ }
222
+
223
+ /**
224
+ * Calculates a name by combining a provided name, domain, and separator.
225
+ *
226
+ * This method takes a provided name, domain, and separator as input and
227
+ * constructs a new name by combining these elements. If the domain or
228
+ * separator are not provided, it uses the default domain and separator
229
+ * stored in the Symkeys instance. If the provided name starts with the
230
+ * separator, it removes the leading separator from the name.
231
+ *
232
+ * If the domain ends with the separator, it removes the trailing separator
233
+ * from the domain. If the domain is empty, it sets the separator to an
234
+ * empty string.
235
+ *
236
+ * @param {string} providedName - The name to be used in the calculation.
237
+ * @param {string} [useDomain] - The domain to be used in the calculation.
238
+ * If not provided, the default domain of the Symkeys instance is used.
239
+ * @param {string} [useSeparator] - The separator to be used in the
240
+ * calculation. If not provided, the default separator of the Symkeys
241
+ * instance is used.
242
+ * @returns {string} The calculated name.
243
+ *
244
+ * @example
245
+ * // Assuming the Symkeys instance has a domain 'symkeys.internal'
246
+ * // and a separator '.'
247
+ * const name = Symkeys.calculateName('feature', 'symkeys.public', '/');
248
+ * console.log(name); // Output: 'symkeys.public/feature'
249
+ */
250
+ calculateName(providedName, useDomain, useSeparator) {
251
+ let domain = String(useDomain || this[Symkeys.kDomain])
252
+ let separator = String(useSeparator || this[Symkeys.kSeparator])
253
+ let postfix = (String(providedName).startsWith(separator)
254
+ ? providedName.substring(1)
255
+ : providedName
256
+ )
257
+
258
+ if (domain.length) {
259
+ if (domain.endsWith(separator)) {
260
+ domain = domain.substring(0, domain.length - 1)
261
+ }
262
+ }
263
+ else {
264
+ separator = ''
265
+ }
266
+
267
+ return `${domain}${separator}${postfix}`
268
+ }
269
+
270
+ /**
271
+ * Constructs a new instance of the Symkeys, setting up a proxy to
272
+ * intercept and manage access to properties.
273
+ *
274
+ * This constructor initializes the Symkeys with a proxy that
275
+ * overrides the default behavior for property access, checking, and
276
+ * enumeration. It allows the Symkeys to behave like a map for its
277
+ * own properties, while also maintaining the prototype chain.
278
+ *
279
+ * @param {string} domain an optional prefix string, to which the
280
+ * `separator` parameter value will be guaranteed to have in between
281
+ * the domain (if truthy) and the name of the added key.
282
+ * @param {string} separator defaults to a period. So if your domain
283
+ * is 'symkeys.internal' then calling {@link add()} with a name of
284
+ * `"feature"` will result in the full name being
285
+ * `"symkeys.internal.feature"`
286
+ *
287
+ * @example
288
+ * const Symkeys = new Symkeys();
289
+ * Symkeys[Symbol.for('myProperty')] = 'myValue';
290
+ * console.log(Symkeys[Symbol.for('myProperty')]); // 'myValue'
291
+ */
292
+ constructor(domain = '', separator = '.') {
293
+ // Create a prototype from the parent class to maintain the chain.
294
+ const prototype = Object.create(Object.getPrototypeOf(this))
295
+
296
+ // Store the original prototype for potential future use.
297
+ this[Symkeys.kPrototype] = prototype
298
+
299
+ // Create map for this instance
300
+ this[Symkeys.kDataKey] = new Map()
301
+
302
+ // Store the domain
303
+ this[Symkeys.kDomain] = (typeof domain === 'string' && domain)
304
+
305
+ // Store the separator
306
+ this[Symkeys.kSeparator] = separator
307
+
308
+ // Access the internal map that stores Symkeys data.
309
+ const map = this[Symkeys.kDataKey];
310
+
311
+ // Replace the instance's prototype with a proxy that manages
312
+ // property access and manipulation.
313
+ Object.setPrototypeOf(
314
+ this,
315
+ new Proxy(Object.create(prototype), {
316
+ // Return the stored prototype for the target.
317
+ getPrototypeOf(_) {
318
+ return prototype;
319
+ },
320
+
321
+ // Intercept property access.
322
+ get(target, property, receiver) {
323
+ // If the property exists in the Symkeys map, return its value.
324
+ if (map.has(property)) {
325
+ return map.get(property);
326
+ }
327
+ // Otherwise, perform the default behavior.
328
+ return Reflect.get(target, property, receiver);
329
+ },
330
+
331
+ // Check for property existence. Check both the Symkeys map and the target for
332
+ // the property.
333
+ has(target, property) {
334
+ return map.has(property) || Reflect.has(target, property);
335
+ },
336
+
337
+ // Retrieve all property keys. Combine keys from the Symkeys map and the target.
338
+ ownKeys(target) {
339
+ return [...Array.from(map.keys()), ...Reflect.ownKeys(target)];
340
+ },
341
+
342
+ // Intercept property assignment.
343
+ set(_, property, value, __) {
344
+ // If the property exists in the Symkeys map, set its value.
345
+ if (map.has(property)) {
346
+ map.set(property, value);
347
+ return true;
348
+ }
349
+ // If not, the operation is not allowed.
350
+ return false;
351
+ },
352
+
353
+ // Retrieve property descriptors.
354
+ getOwnPropertyDescriptor(_, property) {
355
+ // Convert the Symkeys map to an object to use with
356
+ // Object.getOwnPropertyDescriptor.
357
+ const object = [...map.entries()].reduce(
358
+ (a, e) => Object.assign(a, { [e[0]]: e[1] }),
359
+ {},
360
+ );
361
+ // Retrieve the descriptor from the object.
362
+ return Object.getOwnPropertyDescriptor(object, property);
363
+ },
364
+ }),
365
+ );
366
+ }
367
+
368
+ /**
369
+ * Checks if a given value is a Symkey.
370
+ *
371
+ * This method checks if the provided value is a Symkey. A Symkey is a
372
+ * symbol that matches a specific pattern. The pattern is defined as a
373
+ * symbol that starts with '@', followed by any characters, a space, a '#',
374
+ * and ends with one or more word characters.
375
+ *
376
+ * @param {Symbol} value - The value to check.
377
+ * @returns {boolean} Returns true if the value is a Symkey, false otherwise.
378
+ *
379
+ * @example
380
+ * // Check if a symbol is a Symkey:
381
+ * const sym = Symbol('@nejs.prototype #rwiy2o905d');
382
+ * console.log(Symkeys.isSymkey(sym)); // Outputs: true
383
+ */
384
+ static isSymkey(value) {
385
+ if (!(typeof value === 'symbol' || value instanceof Symbol)) {
386
+ return false
387
+ }
388
+
389
+ return !!/^@.*? #\w+$/.exec(value.description)
390
+ }
391
+
392
+ /**
393
+ * Generates a random token string.
394
+ *
395
+ * This method creates a pseudo-random token that can be used for various
396
+ * purposes within the library, such as generating unique identifiers or
397
+ * keys. The token is generated using a base 36 encoding, which includes
398
+ * numbers and lowercase letters.
399
+ *
400
+ * @returns A random token string.
401
+ *
402
+ * @example
403
+ * // Example of getting a random token:
404
+ * const token = MyClass.token;
405
+ * console.log(token); // Outputs a string like 'qg6k1zr0is'
406
+ */
407
+ static get token() {
408
+ return Math.random().toString(36).slice(2);
409
+ }
410
+
411
+ /**
412
+ * Reusable publicly static key for identifying where data is stored.
413
+ */
414
+ static get kDataKey() {
415
+ return Symbol.for('symkeys.data');
416
+ }
417
+
418
+ /**
419
+ * Reusable publicly static key for identifying where data is stored.
420
+ */
421
+ static get kPrototype() {
422
+ return Symbol.for('symkeys.prototype')
423
+ }
424
+
425
+ /**
426
+ * A static getter that returns a unique, reusable symbol for 'symkeys.domain'.
427
+ *
428
+ * This getter is used to create a unique symbol that can be used as a key
429
+ * for storing and retrieving domain-specific data in the Symkeys. The symbol
430
+ * is created using the `Symbol.for` method, which ensures that the same
431
+ * symbol is returned for a given key, in this case 'symkeys.domain'.
432
+ *
433
+ * @returns {Symbol} A unique symbol for 'symkeys.domain'.
434
+ *
435
+ * @example
436
+ * // Retrieve the 'symkeys.domain' symbol
437
+ * const domainSymbol = Symkeys.kDomain;
438
+ * console.log(domainSymbol); // Outputs: Symbol(symkeys.domain)
439
+ */
440
+ static get kDomain() {
441
+ return Symbol.for('symkeys.domain')
442
+ }
443
+
444
+ /**
445
+ * A static getter that returns a unique, reusable symbol for 'symkeys.separator'.
446
+ *
447
+ * This getter is used to create a unique symbol that can be used as a key
448
+ * for storing and retrieving separator-specific data in the Symkeys. The symbol
449
+ * is created using the `Symbol.for` method, which ensures that the same
450
+ * symbol is returned for a given key, in this case 'symkeys.separator'.
451
+ *
452
+ * @returns {Symbol} A unique symbol for 'symkeys.separator'.
453
+ *
454
+ * @example
455
+ * // Retrieve the 'symkeys.separator' symbol
456
+ * const separatorSymbol = Symkeys.kSeparator;
457
+ * console.log(separatorSymbol); // Outputs: Symbol(symkeys.separator)
458
+ */
459
+ static get kSeparator() {
460
+ return Symbol.for('symkeys.separator')
461
+ }
462
+ }
463
+
464
+ export const SymkeysExtension = new Extension(Symkeys)