@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
package/package.json CHANGED
@@ -49,7 +49,8 @@
49
49
  "name": "@nejs/basic-extensions",
50
50
  "scripts": {
51
51
  "preinstall": "npx only-allow pnpm",
52
- "build": "bin/clean && bin/esbuild && bin/build && npm run documentation",
52
+ "build": "bin/clean && bin/esbuild && bin/build",
53
+ "distribute": "bin/clean && bin/esbuild && bin/build && npm run documentation",
53
54
  "browser": "bin/esbuild",
54
55
  "clean": "bin/clean",
55
56
  "jsdoc": "jsdoc -c jsdoc-config.json -p -a all -R README.md",
@@ -59,9 +60,10 @@
59
60
  "repl": "npm run build && node --no-warnings repl.bootstrap.js"
60
61
  },
61
62
  "type": "module",
62
- "version": "2.6.0",
63
+ "version": "2.8.0",
63
64
  "dependencies": {
64
- "@nejs/extension": "^2.8.0"
65
+ "@nejs/extension": "^2.15.0",
66
+ "zod": "^3.22.5"
65
67
  },
66
- "browser": "dist/@nejs/basic-extensions.bundle.2.5.0.js"
68
+ "browser": "dist/@nejs/basic-extensions.bundle.2.7.0.js"
67
69
  }
package/repl.bootstrap.js CHANGED
@@ -128,11 +128,28 @@ function overridableGlobal(
128
128
  Object.defineProperty(globalThis, property, makeDescriptor());
129
129
  }
130
130
 
131
+ /**
132
+ * Generates a snapshot of the current REPL state, categorizing global objects
133
+ * into classes, functions, properties, symbols, and descriptors. This function
134
+ * is designed to capture and organize the current state for inspection or
135
+ * modification purposes. It temporarily disables invocation to safely enumerate
136
+ * global objects, capturing their descriptors and categorizing them accordingly.
137
+ * If invocation is already disabled, it returns the current state without
138
+ * modification. Skipped properties during enumeration are tracked but not
139
+ * processed further.
140
+ *
141
+ * @returns {Object} An object representing the current REPL state, with
142
+ * properties for classes, functions, properties, symbols, and descriptors
143
+ * (further divided into accessors and data descriptors). Each category is an
144
+ * object with keys as the global identifiers and values containing the key,
145
+ * value, and descriptor of the item.
146
+ */
131
147
  function generateState() {
132
148
  const replState = {
133
149
  classes: {},
134
150
  functions: {},
135
151
  properties: {},
152
+ symbols: {},
136
153
  descriptors: {
137
154
  accessors: {},
138
155
  data: {},
@@ -161,6 +178,10 @@ function generateState() {
161
178
  replState.properties[key] = {key, value, descriptor};
162
179
  }
163
180
 
181
+ if (typeof key === 'symbol') {
182
+ replState.symbols[key] = { key, value, descriptor };
183
+ }
184
+
164
185
  if (Reflect.has(descriptor, 'get') || Reflect.has(descriptor, 'set')) {
165
186
  replState.descriptors.accessors[key] = { key, descriptor };
166
187
  }
@@ -0,0 +1,322 @@
1
+ import { Patch } from '@nejs/extension'
2
+
3
+ /**
4
+ * `ArrayExtensions` is a constant that applies a patch to the global
5
+ * `Array` constructor. This patch extends the `Array` with additional
6
+ * methods and properties, enhancing its functionality.
7
+ *
8
+ * The `Patch` function takes two arguments: the target object to be patched
9
+ * (in this case, `Array`), and an object containing the methods and
10
+ * properties to be added to the target object.
11
+ *
12
+ * @example
13
+ * // Using a method added by ArrayExtensions
14
+ * const arr = [1, 2, 3];
15
+ * console.log(Array.ifArray(arr, 'Array', 'Not Array')); // Output: 'Array'
16
+ *
17
+ * @const
18
+ * @type {Patch}
19
+ * @memberof module:array.extensions
20
+ */
21
+ export const ArrayExtensions = new Patch(Array, {
22
+ /**
23
+ * Checks if the provided value is an array and returns one of two
24
+ * provided values based on the result. This function is a convenience
25
+ * method for performing conditional operations based on the type of
26
+ * the provided value.
27
+ *
28
+ * @name ifArray
29
+ * @type {function}
30
+ * @memberof ArrayExtensions
31
+ * @param {any} value - The value to be checked.
32
+ * @param {function | any} thenValue - The value to be returned if the
33
+ * provided value is an array.
34
+ * @param {function | any} elseValue - The value to be returned if the
35
+ * provided value is not an array.
36
+ * @returns {any} Returns `thenValue` if the provided value is an array,
37
+ * otherwise returns `elseValue`.
38
+ *
39
+ * @example
40
+ * const arr = [1, 2, 3];
41
+ * console.log(ArrayExtensions.ifArray(arr, 'Array', 'Not Array'));
42
+ * // Output: 'Array'
43
+ *
44
+ * const notArr = "I'm not an array";
45
+ * console.log(ArrayExtensions.ifArray(notArr, 'Array', 'Not Array'));
46
+ * // Output: 'Not Array'
47
+ */
48
+ ifArray(value, thenValue, elseValue) {
49
+ return isThenElse(Array.isArray(value), thenValue, elseValue)
50
+ },
51
+ })
52
+
53
+ const { ifArray: pIfArray } = ArrayExtensions.patches
54
+
55
+ /**
56
+ * `ArrayPrototypeExtensions` is a constant that applies a patch to the
57
+ * Array prototype. This patch extends the Array prototype with additional
58
+ * methods and properties, enhancing its functionality.
59
+ *
60
+ * The `Patch` function takes two arguments: the target object to be patched
61
+ * (in this case, `Array.prototype`), and an object containing the methods
62
+ * and properties to be added to the target object.
63
+ *
64
+ * @example
65
+ * // Using a method added by ArrayPrototypeExtensions
66
+ * const arr = [1, 2, 3];
67
+ * console.log(arr.ifArray('Array', 'Not Array')); // Output: 'Array'
68
+ *
69
+ * @const
70
+ * @type {Patch}
71
+ * @memberof module:array.extensions
72
+ */
73
+ export const ArrayPrototypeExtensions = new Patch(Array.prototype, {
74
+ [Patch.kMutablyHidden]: {
75
+ /**
76
+ * Sometimes defining even a short function for the invocation of `find`
77
+ * can be troublesome. This helper function performs that job for you. If
78
+ * the specified element is in the array, `true` will be returned.
79
+ *
80
+ * @param {*} value the value to search for. This value must triple equals
81
+ * the array element in order to return true.
82
+ * @returns true if the exact element exists in the array, false otherwise
83
+ */
84
+ contains(value) {
85
+ return !!this.find(entry => entry === value)
86
+ },
87
+
88
+ /**
89
+ * The `findEntry` function searches the entries of the object and returns
90
+ * the `[index, value]` entry array for the first matching value found.
91
+ *
92
+ * @param {function} findFn a function that takes the element to be checked
93
+ * and returns a boolean value
94
+ * @returns if `findFn` returns `true`, an array with two elements, the first
95
+ * being the index, the second being the value, is returned.
96
+ */
97
+ findEntry(findFn) {
98
+ const entries = this.entries()
99
+ const VALUE = 1
100
+
101
+ for (let entry of entries) {
102
+ if (findFn(entry[VALUE])) {
103
+ return entry
104
+ }
105
+ }
106
+
107
+ return undefined
108
+ },
109
+
110
+ /**
111
+ * A getter property that returns the first element of the array. If the
112
+ * array is empty, it returns `undefined`. This property is useful for
113
+ * scenarios where you need to quickly access the first item of an array
114
+ * without the need for additional checks or method calls.
115
+ *
116
+ * @returns {*} The first element of the array or `undefined` if the array
117
+ * is empty.
118
+ */
119
+ get first() {
120
+ return this[0];
121
+ },
122
+
123
+ /**
124
+ * A getter property that checks if the current context (`this`) is an
125
+ * array. This is a convenience method that wraps the native
126
+ * `Array.isArray` function.
127
+ *
128
+ * @name isArray
129
+ * @type {function}
130
+ * @memberof Array.prototype
131
+ * @returns {boolean} `true` if the current context is an array,
132
+ * `false` otherwise.
133
+ *
134
+ * @example
135
+ * const arr = [1, 2, 3];
136
+ * console.log(arr.isArray); // Output: true
137
+ *
138
+ * const notArr = "I'm not an array";
139
+ * console.log(notArr.isArray); // Output: false
140
+ */
141
+ get isArray() {
142
+ return Array.isArray(this)
143
+ },
144
+
145
+ /**
146
+ * Checks if the current context (`this`) is an array and returns one of
147
+ * two provided values based on the result. This function is a convenience
148
+ * method for performing conditional operations based on the type of
149
+ * the current context.
150
+ *
151
+ * @name ifArray
152
+ * @type {function}
153
+ * @memberof Array.prototype
154
+ * @param {function | any} thenValue - The value to be returned if the
155
+ * current context is an array.
156
+ * @param {function | any} elseValue - The value to be returned if the
157
+ * current context is not an array.
158
+ * @returns {*} Returns `thenValue` if the current context is an array,
159
+ * otherwise returns `elseValue`.
160
+ *
161
+ * @example
162
+ * const arr = [1, 2, 3];
163
+ * console.log(arr.ifArray('Array', 'Not Array')); // Output: 'Array'
164
+ *
165
+ * const notArr = "I'm not an array";
166
+ * console.log(notArr.ifArray('Array', 'Not Array')); // Output: 'Not Array'
167
+ */
168
+ ifArray(thenValue, elseValue) {
169
+ return pIfArray(this, thenValue, elseValue)
170
+ },
171
+
172
+ /**
173
+ * Checks if at least one element in the array is equal to the provided
174
+ * value. This method uses the `Array.prototype.some` function to iterate
175
+ * over the array and compare each element with the provided value.
176
+ *
177
+ * @name oneIs
178
+ * @type {function}
179
+ * @memberof Array.prototype
180
+ * @param {*} value - The value to be compared with the array elements.
181
+ * @param {boolean} [doubleEqualsOkay=true] - A flag indicating whether to
182
+ * use loose equality (`==`) or strict equality (`===`) for the comparison.
183
+ * If `true`, loose equality is used. If `false`, strict equality is used.
184
+ * @returns {boolean} Returns `true` if at least one element in the array
185
+ * is equal to the provided value, otherwise `false`.
186
+ *
187
+ * @example
188
+ * const arr = [1, 2, 3];
189
+ * console.log(arr.oneIs(2)); // Output: true
190
+ *
191
+ * const arr2 = ['1', '2', '3'];
192
+ * console.log(arr2.oneIs(2, false)); // Output: false
193
+ */
194
+ oneIs(value, doubleEqualsOkay = true) {
195
+ return this.some(element => (
196
+ doubleEqualsOkay ? element == value : element === value
197
+ ))
198
+ },
199
+
200
+ /**
201
+ * Checks if some elements in the array are included in the provided values.
202
+ * This method uses the `Array.prototype.some` function to iterate over the
203
+ * array and checks if any of the elements are included in the provided values.
204
+ *
205
+ * @name someAre
206
+ * @type {function}
207
+ * @memberof Array.prototype
208
+ * @param {...*} values - The values to be checked against the array elements.
209
+ * @returns {boolean} Returns `true` if at least one element in the array
210
+ * is included in the provided values, otherwise `false`.
211
+ *
212
+ * @example
213
+ * const arr = [1, 2, 3];
214
+ * console.log(arr.someAre(2, 4)); // Output: true
215
+ *
216
+ * const arr2 = ['1', '2', '3'];
217
+ * console.log(arr2.someAre(4, 5)); // Output: false
218
+ */
219
+ someAre(...values) {
220
+ return this.some(element => !!~values.indexOf(element))
221
+ },
222
+
223
+ /**
224
+ * Checks if all elements in the array are equal to the provided value.
225
+ * This method uses the `Array.prototype.every` function to iterate over
226
+ * the array and compare each element with the provided value.
227
+ *
228
+ * @name allAre
229
+ * @type {function}
230
+ * @memberof Array.prototype
231
+ * @param {*} value - The value to be compared with the array elements.
232
+ * @param {boolean} [doubleEqualsOkay=true] - A flag indicating whether to
233
+ * use loose equality (`==`) or strict equality (`===`) for the comparison.
234
+ * If `true`, loose equality is used. If `false`, strict equality is used.
235
+ * @returns {boolean} Returns `true` if all elements in the array are equal
236
+ * to the provided value, otherwise `false`.
237
+ *
238
+ * @example
239
+ * const arr = [2, 2, 2];
240
+ * console.log(arr.allAre(2)); // Output: true
241
+ *
242
+ * const arr2 = ['2', '2', '2'];
243
+ * console.log(arr2.allAre(2, false)); // Output: false
244
+ */
245
+ allAre(value, doubleEqualsOkay = true) {
246
+ return this.every(element => (
247
+ doubleEqualsOkay ? element == value : element === value
248
+ ))
249
+ },
250
+
251
+ /**
252
+ * A getter property that returns the last element of the array. It
253
+ * calculates the last index based on the array's length. If the array is
254
+ * empty, it returns `undefined`. This property is beneficial when you need
255
+ * to access the last item in an array, improving code readability and
256
+ * avoiding manual index calculation.
257
+ *
258
+ * @returns {*} The last element of the array or `undefined` if the
259
+ * array is empty.
260
+ */
261
+ get last() {
262
+ return this[this.length - 1];
263
+ },
264
+
265
+ // expected usage:
266
+ // function example(name, age) {
267
+ // const variants = [{name}, {age}].variants()
268
+ // if (typeof name === 'object')
269
+ // }
270
+ variants() {
271
+ const keys = this.map(o => Object.keys(o)?.[0])
272
+ const entries = this.map(o => Object.entries(o)?.[0])
273
+ const object = entries.reduce((acc,[key, value]) => {
274
+ acc[key] = value;
275
+ return acc;
276
+ }, {})
277
+
278
+ const result = {
279
+ order: keys,
280
+ entries: entries,
281
+ object: object,
282
+ }
283
+
284
+ Object.defineProperty(result, 'check', {
285
+ value(position) {
286
+ if (
287
+ typeof position !== 'number' &&
288
+ position >= 0 &&
289
+ position < this.order.length
290
+ ) {
291
+ return false
292
+ }
293
+
294
+ const value = this.entries[position][1]
295
+
296
+ if (typeof value === 'object' && value) {
297
+ if (Object.keys(value).every(key => ~this.order.indexOf(key))) {
298
+ return true
299
+ }
300
+ }
301
+
302
+ return false
303
+ },
304
+ enumerable: false,
305
+ configurable: true
306
+ })
307
+
308
+ return result
309
+ },
310
+ },
311
+ })
312
+
313
+ // NOTE to self; this is repeated here otherwise a circular reference from
314
+ // Object<->Function<->Global occurs. See original source in global.this.js
315
+ // {@see globalThis.isThenElse}
316
+ function isThenElse(bv, tv, ev) {
317
+ if (arguments.length > 1) {
318
+ var _then = isFunction(tv) ? tv(bv) : tv; if (arguments.length > 2) {
319
+ var _else = isFunction(ev) ? tv(bv) : ev; return bv ? _then : _else
320
+ } return bv || _then;
321
+ } return bv
322
+ }
@@ -0,0 +1,163 @@
1
+ import { Patch } from '@nejs/extension'
2
+
3
+ /**
4
+ * `BigIntExtensions` is a patch for the JavaScript built-in `BigInt` class.
5
+ * It adds utility methods to the `BigInt` class without modifying the global
6
+ * namespace directly. This patch includes methods for checking if a value is
7
+ * a `BigInt` and conditionally returning a value based on whether the supplied
8
+ * value is a `BigInt` or not.
9
+ *
10
+ * @type {Patch}
11
+ * @example
12
+ * import { BigIntExtensions } from 'big.int.extension.js'
13
+ *
14
+ * BigIntExtensions.apply()
15
+ * // Now the `BigInt` class has additional methods available
16
+ */
17
+ export const BigIntExtensions = new Patch(BigInt, {
18
+ /**
19
+ * Determines if the supplied `value` is a `BigInt`. This check is
20
+ * performed by first checking the `typeof` the `value` and then
21
+ * checking to see if the `value` is an `instanceof` `BigInt`
22
+ *
23
+ * @param {*} value The value that needs to be checked to determine
24
+ * if it is a `BigInt` or not
25
+ * @returns {boolean} `true` if the supplied `value` is a `BigInt`,
26
+ * `false` otherwise
27
+ *
28
+ * @example
29
+ * const bigInt = 1234567890123456789012345678901234567890n
30
+ * isBigInt(bigInt) // true
31
+ * isBigInt(1234567890123456789012345678901234567890) // false
32
+ * isBigInt('1234567890123456789012345678901234567890') // false
33
+ * isBigInt(BigInt('1234567890123456789012345678901234567890')) // true
34
+ */
35
+ isBigInt(value) {
36
+ return typeof value === 'bigint' || value instanceof BigInt
37
+ },
38
+
39
+ /**
40
+ * Conditionally returns a value based on whether the supplied
41
+ * `value` is a `BigInt` or not. If the `value` is a `BigInt`,
42
+ * the `thenValue` will be returned. If it is not a `BigInt`,
43
+ * the `elseValue` will be returned instead.
44
+ *
45
+ * @param {any} value The value to check to determine if it is a
46
+ * `BigInt`
47
+ * @param {any} thenValue The value to return if the supplied
48
+ * `value` is a `BigInt`
49
+ * @param {any} elseValue The value to return if the supplied
50
+ * `value` is not a `BigInt`
51
+ * @returns {any} Either the `thenValue` or `elseValue` depending
52
+ * on if the supplied `value` is a `BigInt`
53
+ *
54
+ * @example
55
+ * const bigInt = 1234567890123456789012345678901234567890n
56
+ * const num = 42
57
+ * ifBigInt(bigInt, 'is a BigInt', 'not a BigInt')
58
+ * // 'is a BigInt'
59
+ * ifBigInt(num, 'is a BigInt', 'not a BigInt')
60
+ * // 'not a BigInt'
61
+ */
62
+ ifBigInt(value, thenValue, elseValue) {
63
+ return isThenElse(this.isBigInt(value), thenValue, elseValue)
64
+ },
65
+ })
66
+
67
+ const { isBigInt: pIsBigInt, ifBigInt: pIfBigInt } = BigIntExtensions.patches
68
+
69
+ /**
70
+ * `BigIntPrototypeExtensions` is a patch for the JavaScript built-in
71
+ * `BigInt.prototype`. It adds utility methods to the `BigInt` prototype
72
+ * without modifying the global namespace directly. This patch includes
73
+ * methods for checking if a value is a BigInt and conditionally returning
74
+ * a value based on whether the supplied value is a BigInt or not.
75
+ *
76
+ * @type {Patch}
77
+ * @example
78
+ * import { BigIntPrototypeExtensions } from 'big.int.extension.js'
79
+ *
80
+ * BigIntPrototypeExtensions.apply()
81
+ * // Now the `BigInt` prototype has additional methods available
82
+ */
83
+ export const BigIntPrototypeExtensions = new Patch(BigInt.prototype, {
84
+ /**
85
+ * A getter method that returns an object representation of the BigInt
86
+ * instance.
87
+ *
88
+ * This method wraps the BigInt instance in an object, allowing it to be
89
+ * treated as an object. The returned object is created using the `Object()`
90
+ * constructor, which takes the BigInt instance as its argument.
91
+ *
92
+ * @type {Object}
93
+ * @readonly
94
+ *
95
+ * @example
96
+ * const bigInt = 1234567890123456789012345678901234567890n
97
+ * console.log(typeof bigInt) // 'bigint'
98
+ * console.log(typeof bigInt.instance) // 'object'
99
+ */
100
+ get instance() {
101
+ return Object(this)
102
+ },
103
+
104
+ /**
105
+ * A getter method that checks if the current instance is a BigInt.
106
+ *
107
+ * This method uses the `pIsBigInt` function from the `BigIntExtensions`
108
+ * patch to determine if the current instance (`this`) is a BigInt.
109
+ *
110
+ * @type {boolean}
111
+ * @readonly
112
+ *
113
+ * @example
114
+ * const bigInt = 1234567890123456789012345678901234567890n
115
+ * console.log(bigInt.isBigInt) // Output: true
116
+ *
117
+ * const notBigInt = 42
118
+ * console.log(notBigInt.isBigInt) // Output: false
119
+ */
120
+ get isBigInt() {
121
+ return pIsBigInt(this)
122
+ },
123
+
124
+ /**
125
+ * Checks if the current object is a BigInt and returns the corresponding
126
+ * value based on the result.
127
+ *
128
+ * This method uses the `pIfBigInt` function from the `BigIntExtensions`
129
+ * patch to determine if the current object (`this`) is a BigInt. If it is
130
+ * a BigInt, the `thenValue` is returned. Otherwise, the `elseValue` is
131
+ * returned.
132
+ *
133
+ * @param {any} thenValue - The value to return if the current object
134
+ * is a BigInt.
135
+ * @param {any} elseValue - The value to return if the current object
136
+ * is not a BigInt.
137
+ * @returns {any} The `thenValue` if the current object is a BigInt, or
138
+ * the `elseValue` if it is not a BigInt.
139
+ *
140
+ * @example
141
+ * const bigInt = 1234567890123456789012345678901234567890n
142
+ * // 'Is a BigInt'
143
+ * console.log(bigInt.ifBigInt('Is a BigInt', 'Not a BigInt'))
144
+ *
145
+ * const notBigInt = 42
146
+ * // 'Not a BigInt'
147
+ * console.log(notBigInt.ifBigInt('Is a BigInt', 'Not a BigInt'))
148
+ */
149
+ ifBigInt(thenValue, elseValue) {
150
+ return pIfBigInt(this, thenValue, elseValue)
151
+ },
152
+ })
153
+
154
+ // NOTE to self; this is repeated here otherwise a circular reference from
155
+ // Object<->Function<->Global occurs. See original source in global.this.js
156
+ // {@see globalThis.isThenElse}
157
+ function isThenElse(bv, tv, ev) {
158
+ if (arguments.length > 1) {
159
+ var _then = isFunction(tv) ? tv(bv) : tv; if (arguments.length > 2) {
160
+ var _else = isFunction(ev) ? tv(bv) : ev; return bv ? _then : _else
161
+ } return bv || _then;
162
+ } return bv
163
+ }
@@ -1,9 +1,4 @@
1
1
  import { Extension } from '@nejs/extension'
2
- import { ObjectExtensions } from '../objectextensions.js'
3
- import { ReflectExtensions } from '../reflectextensions.js'
4
-
5
- const { isObject, isValidKey } = ObjectExtensions.patches
6
- const { hasSome } = ReflectExtensions.patches
7
2
 
8
3
  export class Descriptor {
9
4
  /**
@@ -36,7 +31,7 @@ export class Descriptor {
36
31
  * is to be created. This parameter is ignored if `object` is a Descriptor
37
32
  * instance. If `key` is an object and `object` is a valid descriptor, `key`
38
33
  * is treated as the associated object.
39
- * @throws {Error} Throws an error if the constructed descriptor is not
34
+ * @throws {Error} Throws an error if the constructed descriptor is not
40
35
  * valid.
41
36
  */
42
37
  constructor(object, key) {
@@ -46,7 +41,7 @@ export class Descriptor {
46
41
  else if (Descriptor.isDescriptor(object)) {
47
42
  this.#desc = object
48
43
  this.#object = isObject(key) ? key : undefined
49
- }
44
+ }
50
45
  else if (isObject(object) && isValidKey(key)) {
51
46
  this.#desc = Object.getOwnPropertyDescriptor(object, key)
52
47
  this.#object = object
@@ -327,7 +322,7 @@ export class Descriptor {
327
322
  */
328
323
  applyTo(object, forKey, bindAccessors = false) {
329
324
  if (!isObject(object) || !isValidKey(forKey)) {
330
- throw new Error(`Cannot apply descriptor to non-object or invalid key`)
325
+ throw new Error( `Cannot apply descriptor to non-object or invalid key`)
331
326
  }
332
327
 
333
328
  return Object.defineProperty(object, forKey, this.toObject(bindAccessors))
@@ -337,7 +332,7 @@ export class Descriptor {
337
332
  * Converts this Descriptor class instance into a basic object descriptor
338
333
  * that is accepted by all the standard JavaScript runtime methods that
339
334
  * deal with object descriptors.
340
- *
335
+ *
341
336
  * @param {boolean|object} bindAccessors if `true`, a non-fatal attempt to
342
337
  * bind accessor getter and setter methods is made before returning the
343
338
  * object. If `bindAccessors` is truthy and is also an object, this is the
@@ -351,8 +346,8 @@ export class Descriptor {
351
346
 
352
347
  if (bindAccessors && this.isAccessor) {
353
348
  if (this.hasObject) {
354
- descriptor = {
355
- ...descriptor,
349
+ descriptor = {
350
+ ...descriptor,
356
351
  get: this.boundGet,
357
352
  set: this.boundSet
358
353
  }
@@ -702,4 +697,13 @@ export class Descriptor {
702
697
  }
703
698
  }
704
699
 
705
- export const DescriptorExtensions = new Extension(Descriptor)
700
+ export const DescriptorExtensions = new Extension(Descriptor)
701
+
702
+ function isObject(o) { return o && typeof o === 'object' }
703
+ function isValidKey(o) { return ['string','symbol'].some(t => typeof o === t) }
704
+ function hasSome(object, ...keys) {
705
+ return isObject(object) && (keys.flat(Infinity)
706
+ .map(key => Reflect.has(object, key))
707
+ .some(has => has)
708
+ )
709
+ }
@@ -0,0 +1,51 @@
1
+ import { Patch } from '@nejs/extension'
2
+
3
+ import { AsyncIterable, AsyncIterator } from './asyncIterable.js'
4
+ export * from './asyncIterable.js'
5
+
6
+ import { Deferred } from './deferred.js'
7
+ export * from './deferred.js'
8
+
9
+ import { Descriptor } from './descriptor.js'
10
+ export * from './descriptor.js'
11
+
12
+ import { Introspector } from './introspector.js'
13
+ export * from './introspector.js'
14
+
15
+ import {Iterable, Iterator } from './iterable.js'
16
+ export * from './iterable.js'
17
+
18
+ import { ParamParser } from './param.parser.js'
19
+ export * from './param.parser.js'
20
+
21
+ import { PluggableProxy, ProxyHandler } from './pluggable.proxy.js'
22
+ export * from './pluggable.proxy.js'
23
+
24
+ import { RefMap } from './refmap.js'
25
+ export * from './refmap.js'
26
+
27
+ import { RefSet } from './refset.js'
28
+ export * from './refset.js'
29
+
30
+ import { Symkeys } from './symkeys.js'
31
+ export * from './symkeys.js'
32
+
33
+ import { Type } from './type.js'
34
+ export * from './type.js'
35
+
36
+ export const NewClassesExtension = new Patch(globalThis, {
37
+ AsyncIterable,
38
+ AsyncIterator,
39
+ Deferred,
40
+ Descriptor,
41
+ Introspector,
42
+ Iterable,
43
+ Iterator,
44
+ ParamParser,
45
+ PluggableProxy,
46
+ ProxyHandler,
47
+ RefMap,
48
+ RefSet,
49
+ Symkeys,
50
+ Type,
51
+ })