@nejs/basic-extensions 2.21.0 → 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 (270) hide show
  1. package/.idea/markdown.xml +8 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/ne-basic-extensions.iml +8 -0
  4. package/.idea/vcs.xml +6 -0
  5. package/CODE_STYLE.md +393 -0
  6. package/CODING_PHILOSOPHY.md +36 -0
  7. package/DOCUMENTATION_GUIDELINES.md +221 -0
  8. package/README.md +78 -4
  9. package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
  10. package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
  11. package/dist/cjs/classes/index.cjs +11129 -0
  12. package/dist/cjs/classes/index.cjs.map +7 -0
  13. package/dist/cjs/index.cjs +15191 -0
  14. package/dist/cjs/index.cjs.map +7 -0
  15. package/dist/cjs/utils/index.cjs +3954 -0
  16. package/dist/cjs/utils/index.cjs.map +7 -0
  17. package/dist/esm/basic-extensions.mjs +25 -0
  18. package/dist/esm/basic-extensions.mjs.map +7 -0
  19. package/package.json +16 -22
  20. package/repl.bootstrap.js +4 -7
  21. package/repl.history +30 -30
  22. package/src/big.int.extension.js +171 -45
  23. package/src/classes/enumeration.js +466 -0
  24. package/src/classes/index.js +5 -1
  25. package/src/index.js +5 -1
  26. package/src/math.extension.js +73 -0
  27. package/src/number.extension.js +18 -0
  28. package/src/regular.expression.extensions.js +0 -35
  29. package/src/utils/toolkit.js +699 -516
  30. package/tests/arrayextensions.test.js +3 -3
  31. package/tests/index.test.js +3 -1
  32. package/tests/newClasses/asyncIterable.test.js +3 -3
  33. package/tests/newClasses/deferred.test.js +3 -3
  34. package/tests/newClasses/descriptor.test.js +3 -3
  35. package/tests/newClasses/iterable.test.js +3 -3
  36. package/tests/newClasses/refmap.test.js +3 -3
  37. package/tests/newClasses/refset.test.js +3 -3
  38. package/tests/objectextensions.test.js +3 -3
  39. package/tests/setextensions.test.js +3 -3
  40. package/tests/stringextensions.test.js +3 -2
  41. package/tests/utils/descriptor.utils.test.js +1 -1
  42. package/tests/utils/toolkit.test.js +429 -163
  43. package/.esdoc.json +0 -9
  44. package/.vscode/settings.json +0 -5
  45. package/bin/build +0 -27
  46. package/bin/clean +0 -14
  47. package/bin/esbuild +0 -91
  48. package/bin/fixup +0 -13
  49. package/bin/repl.basics.js +0 -584
  50. package/bin/repl.signature.js +0 -63
  51. package/bin/version +0 -100
  52. package/dist/@nejs/basic-extensions.bundle.2.21.0.js +0 -25
  53. package/dist/@nejs/basic-extensions.bundle.2.21.0.js.map +0 -7
  54. package/dist/cjs/array.extensions.d.ts +0 -39
  55. package/dist/cjs/array.extensions.js +0 -477
  56. package/dist/cjs/array.extensions.js.map +0 -1
  57. package/dist/cjs/big.int.extension.d.ts +0 -31
  58. package/dist/cjs/big.int.extension.js +0 -165
  59. package/dist/cjs/big.int.extension.js.map +0 -1
  60. package/dist/cjs/classes/asyncIterable.d.ts +0 -126
  61. package/dist/cjs/classes/asyncIterable.js +0 -209
  62. package/dist/cjs/classes/asyncIterable.js.map +0 -1
  63. package/dist/cjs/classes/deferred.d.ts +0 -146
  64. package/dist/cjs/classes/deferred.js +0 -291
  65. package/dist/cjs/classes/deferred.js.map +0 -1
  66. package/dist/cjs/classes/descriptor.d.ts +0 -334
  67. package/dist/cjs/classes/descriptor.js +0 -537
  68. package/dist/cjs/classes/descriptor.js.map +0 -1
  69. package/dist/cjs/classes/enum.d.ts +0 -50
  70. package/dist/cjs/classes/enum.js +0 -405
  71. package/dist/cjs/classes/enum.js.map +0 -1
  72. package/dist/cjs/classes/index.d.ts +0 -15
  73. package/dist/cjs/classes/index.js +0 -63
  74. package/dist/cjs/classes/index.js.map +0 -1
  75. package/dist/cjs/classes/introspector.d.ts +0 -20
  76. package/dist/cjs/classes/introspector.js +0 -130
  77. package/dist/cjs/classes/introspector.js.map +0 -1
  78. package/dist/cjs/classes/iterable.d.ts +0 -169
  79. package/dist/cjs/classes/iterable.js +0 -268
  80. package/dist/cjs/classes/iterable.js.map +0 -1
  81. package/dist/cjs/classes/param.parser.d.ts +0 -221
  82. package/dist/cjs/classes/param.parser.js +0 -242
  83. package/dist/cjs/classes/param.parser.js.map +0 -1
  84. package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
  85. package/dist/cjs/classes/pluggable.proxy.js +0 -444
  86. package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
  87. package/dist/cjs/classes/property.d.ts +0 -79
  88. package/dist/cjs/classes/property.js +0 -284
  89. package/dist/cjs/classes/property.js.map +0 -1
  90. package/dist/cjs/classes/refmap.d.ts +0 -238
  91. package/dist/cjs/classes/refmap.js +0 -421
  92. package/dist/cjs/classes/refmap.js.map +0 -1
  93. package/dist/cjs/classes/refset.d.ts +0 -186
  94. package/dist/cjs/classes/refset.js +0 -370
  95. package/dist/cjs/classes/refset.js.map +0 -1
  96. package/dist/cjs/classes/symkeys.d.ts +0 -349
  97. package/dist/cjs/classes/symkeys.js +0 -510
  98. package/dist/cjs/classes/symkeys.js.map +0 -1
  99. package/dist/cjs/classes/type.d.ts +0 -56
  100. package/dist/cjs/classes/type.js +0 -405
  101. package/dist/cjs/classes/type.js.map +0 -1
  102. package/dist/cjs/function.extensions.d.ts +0 -12
  103. package/dist/cjs/function.extensions.js +0 -758
  104. package/dist/cjs/function.extensions.js.map +0 -1
  105. package/dist/cjs/global.this.d.ts +0 -2
  106. package/dist/cjs/global.this.js +0 -300
  107. package/dist/cjs/global.this.js.map +0 -1
  108. package/dist/cjs/index.d.ts +0 -31
  109. package/dist/cjs/index.js +0 -226
  110. package/dist/cjs/index.js.map +0 -1
  111. package/dist/cjs/json.extensions.d.ts +0 -2
  112. package/dist/cjs/json.extensions.js +0 -109
  113. package/dist/cjs/json.extensions.js.map +0 -1
  114. package/dist/cjs/map.extensions.d.ts +0 -3
  115. package/dist/cjs/map.extensions.js +0 -143
  116. package/dist/cjs/map.extensions.js.map +0 -1
  117. package/dist/cjs/number.extension.d.ts +0 -44
  118. package/dist/cjs/number.extension.js +0 -261
  119. package/dist/cjs/number.extension.js.map +0 -1
  120. package/dist/cjs/object.extensions.d.ts +0 -33
  121. package/dist/cjs/object.extensions.js +0 -1091
  122. package/dist/cjs/object.extensions.js.map +0 -1
  123. package/dist/cjs/package.json +0 -3
  124. package/dist/cjs/proxy.extensions.d.ts +0 -2
  125. package/dist/cjs/proxy.extensions.js +0 -207
  126. package/dist/cjs/proxy.extensions.js.map +0 -1
  127. package/dist/cjs/reflect.extensions.d.ts +0 -14
  128. package/dist/cjs/reflect.extensions.js +0 -316
  129. package/dist/cjs/reflect.extensions.js.map +0 -1
  130. package/dist/cjs/regular.expression.extensions.d.ts +0 -2
  131. package/dist/cjs/regular.expression.extensions.js +0 -423
  132. package/dist/cjs/regular.expression.extensions.js.map +0 -1
  133. package/dist/cjs/set.extensions.d.ts +0 -40
  134. package/dist/cjs/set.extensions.js +0 -355
  135. package/dist/cjs/set.extensions.js.map +0 -1
  136. package/dist/cjs/string.extensions.d.ts +0 -23
  137. package/dist/cjs/string.extensions.js +0 -704
  138. package/dist/cjs/string.extensions.js.map +0 -1
  139. package/dist/cjs/symbol.extensions.d.ts +0 -11
  140. package/dist/cjs/symbol.extensions.js +0 -735
  141. package/dist/cjs/symbol.extensions.js.map +0 -1
  142. package/dist/cjs/utils/copy.object.d.ts +0 -408
  143. package/dist/cjs/utils/copy.object.js +0 -720
  144. package/dist/cjs/utils/copy.object.js.map +0 -1
  145. package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
  146. package/dist/cjs/utils/descriptor.utils.js +0 -889
  147. package/dist/cjs/utils/descriptor.utils.js.map +0 -1
  148. package/dist/cjs/utils/index.d.ts +0 -75
  149. package/dist/cjs/utils/index.js +0 -61
  150. package/dist/cjs/utils/index.js.map +0 -1
  151. package/dist/cjs/utils/stdout.d.ts +0 -742
  152. package/dist/cjs/utils/stdout.js +0 -1042
  153. package/dist/cjs/utils/stdout.js.map +0 -1
  154. package/dist/cjs/utils/toolkit.d.ts +0 -1898
  155. package/dist/cjs/utils/toolkit.js +0 -1378
  156. package/dist/cjs/utils/toolkit.js.map +0 -1
  157. package/dist/cjs/weakref.extensions.d.ts +0 -2
  158. package/dist/cjs/weakref.extensions.js +0 -19
  159. package/dist/cjs/weakref.extensions.js.map +0 -1
  160. package/dist/mjs/array.extensions.d.ts +0 -39
  161. package/dist/mjs/array.extensions.js +0 -474
  162. package/dist/mjs/array.extensions.js.map +0 -1
  163. package/dist/mjs/big.int.extension.d.ts +0 -31
  164. package/dist/mjs/big.int.extension.js +0 -162
  165. package/dist/mjs/big.int.extension.js.map +0 -1
  166. package/dist/mjs/classes/asyncIterable.d.ts +0 -126
  167. package/dist/mjs/classes/asyncIterable.js +0 -204
  168. package/dist/mjs/classes/asyncIterable.js.map +0 -1
  169. package/dist/mjs/classes/deferred.d.ts +0 -146
  170. package/dist/mjs/classes/deferred.js +0 -287
  171. package/dist/mjs/classes/deferred.js.map +0 -1
  172. package/dist/mjs/classes/descriptor.d.ts +0 -334
  173. package/dist/mjs/classes/descriptor.js +0 -533
  174. package/dist/mjs/classes/descriptor.js.map +0 -1
  175. package/dist/mjs/classes/enum.d.ts +0 -50
  176. package/dist/mjs/classes/enum.js +0 -400
  177. package/dist/mjs/classes/enum.js.map +0 -1
  178. package/dist/mjs/classes/index.d.ts +0 -15
  179. package/dist/mjs/classes/index.js +0 -46
  180. package/dist/mjs/classes/index.js.map +0 -1
  181. package/dist/mjs/classes/introspector.d.ts +0 -20
  182. package/dist/mjs/classes/introspector.js +0 -126
  183. package/dist/mjs/classes/introspector.js.map +0 -1
  184. package/dist/mjs/classes/iterable.d.ts +0 -169
  185. package/dist/mjs/classes/iterable.js +0 -263
  186. package/dist/mjs/classes/iterable.js.map +0 -1
  187. package/dist/mjs/classes/param.parser.d.ts +0 -221
  188. package/dist/mjs/classes/param.parser.js +0 -238
  189. package/dist/mjs/classes/param.parser.js.map +0 -1
  190. package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
  191. package/dist/mjs/classes/pluggable.proxy.js +0 -438
  192. package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
  193. package/dist/mjs/classes/property.d.ts +0 -79
  194. package/dist/mjs/classes/property.js +0 -280
  195. package/dist/mjs/classes/property.js.map +0 -1
  196. package/dist/mjs/classes/refmap.d.ts +0 -238
  197. package/dist/mjs/classes/refmap.js +0 -417
  198. package/dist/mjs/classes/refmap.js.map +0 -1
  199. package/dist/mjs/classes/refset.d.ts +0 -186
  200. package/dist/mjs/classes/refset.js +0 -366
  201. package/dist/mjs/classes/refset.js.map +0 -1
  202. package/dist/mjs/classes/symkeys.d.ts +0 -349
  203. package/dist/mjs/classes/symkeys.js +0 -506
  204. package/dist/mjs/classes/symkeys.js.map +0 -1
  205. package/dist/mjs/classes/type.d.ts +0 -56
  206. package/dist/mjs/classes/type.js +0 -401
  207. package/dist/mjs/classes/type.js.map +0 -1
  208. package/dist/mjs/function.extensions.d.ts +0 -12
  209. package/dist/mjs/function.extensions.js +0 -755
  210. package/dist/mjs/function.extensions.js.map +0 -1
  211. package/dist/mjs/global.this.d.ts +0 -2
  212. package/dist/mjs/global.this.js +0 -264
  213. package/dist/mjs/global.this.js.map +0 -1
  214. package/dist/mjs/index.d.ts +0 -31
  215. package/dist/mjs/index.js +0 -204
  216. package/dist/mjs/index.js.map +0 -1
  217. package/dist/mjs/json.extensions.d.ts +0 -2
  218. package/dist/mjs/json.extensions.js +0 -106
  219. package/dist/mjs/json.extensions.js.map +0 -1
  220. package/dist/mjs/map.extensions.d.ts +0 -3
  221. package/dist/mjs/map.extensions.js +0 -140
  222. package/dist/mjs/map.extensions.js.map +0 -1
  223. package/dist/mjs/number.extension.d.ts +0 -44
  224. package/dist/mjs/number.extension.js +0 -258
  225. package/dist/mjs/number.extension.js.map +0 -1
  226. package/dist/mjs/object.extensions.d.ts +0 -33
  227. package/dist/mjs/object.extensions.js +0 -1088
  228. package/dist/mjs/object.extensions.js.map +0 -1
  229. package/dist/mjs/package.json +0 -3
  230. package/dist/mjs/proxy.extensions.d.ts +0 -2
  231. package/dist/mjs/proxy.extensions.js +0 -204
  232. package/dist/mjs/proxy.extensions.js.map +0 -1
  233. package/dist/mjs/reflect.extensions.d.ts +0 -14
  234. package/dist/mjs/reflect.extensions.js +0 -313
  235. package/dist/mjs/reflect.extensions.js.map +0 -1
  236. package/dist/mjs/regular.expression.extensions.d.ts +0 -2
  237. package/dist/mjs/regular.expression.extensions.js +0 -420
  238. package/dist/mjs/regular.expression.extensions.js.map +0 -1
  239. package/dist/mjs/set.extensions.d.ts +0 -40
  240. package/dist/mjs/set.extensions.js +0 -352
  241. package/dist/mjs/set.extensions.js.map +0 -1
  242. package/dist/mjs/string.extensions.d.ts +0 -23
  243. package/dist/mjs/string.extensions.js +0 -701
  244. package/dist/mjs/string.extensions.js.map +0 -1
  245. package/dist/mjs/symbol.extensions.d.ts +0 -11
  246. package/dist/mjs/symbol.extensions.js +0 -732
  247. package/dist/mjs/symbol.extensions.js.map +0 -1
  248. package/dist/mjs/utils/copy.object.d.ts +0 -408
  249. package/dist/mjs/utils/copy.object.js +0 -702
  250. package/dist/mjs/utils/copy.object.js.map +0 -1
  251. package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
  252. package/dist/mjs/utils/descriptor.utils.js +0 -875
  253. package/dist/mjs/utils/descriptor.utils.js.map +0 -1
  254. package/dist/mjs/utils/index.d.ts +0 -75
  255. package/dist/mjs/utils/index.js +0 -45
  256. package/dist/mjs/utils/index.js.map +0 -1
  257. package/dist/mjs/utils/stdout.d.ts +0 -742
  258. package/dist/mjs/utils/stdout.js +0 -1037
  259. package/dist/mjs/utils/stdout.js.map +0 -1
  260. package/dist/mjs/utils/toolkit.d.ts +0 -1898
  261. package/dist/mjs/utils/toolkit.js +0 -1373
  262. package/dist/mjs/utils/toolkit.js.map +0 -1
  263. package/dist/mjs/weakref.extensions.d.ts +0 -2
  264. package/dist/mjs/weakref.extensions.js +0 -16
  265. package/dist/mjs/weakref.extensions.js.map +0 -1
  266. package/jsdoc-config.json +0 -31
  267. package/tsconfig.base.json +0 -28
  268. package/tsconfig.cjs.json +0 -8
  269. package/tsconfig.esm.json +0 -8
  270. package/vitest.config.js +0 -7
@@ -1,584 +0,0 @@
1
- const repl = await import('node:repl');
2
- const fs = await import('node:fs');
3
- const project = JSON.parse(String(fs.readFileSync('./package.json')));
4
-
5
- /**
6
- * Default prompt character used in the REPL.
7
- *
8
- * @constant {string} kDefaultPrompt
9
- * @default
10
- * @example
11
- * console.log(kDefaultPrompt) // Output: 'λ'
12
- */
13
- const kDefaultPrompt = 'λ'
14
-
15
- /**
16
- * Formats the given text in red color for terminal output.
17
- *
18
- * This function uses ANSI escape codes to colorize text. The default
19
- * additional character appended to the text is a space.
20
- *
21
- * @function
22
- * @param {string} text - The text to be colorized.
23
- * @param {string} [plus=' '] - An optional string to append after the text.
24
- * @returns {string} The colorized text with the appended string.
25
- * @example
26
- * console.log(red('Error')) // Output: '\x1b[31mError\x1b[1;39m\x1b[22m '
27
- */
28
- const red = (text, plus = ' ') =>
29
- `\x1b[31m${text}\x1b[1;39m\x1b[22m${plus}`
30
-
31
- /**
32
- * Formats the given text in green color for terminal output.
33
- *
34
- * This function uses ANSI escape codes to colorize text. The default
35
- * additional character appended to the text is a space.
36
- *
37
- * @function
38
- * @param {string} text - The text to be colorized.
39
- * @param {string} [plus=' '] - An optional string to append after the text.
40
- * @returns {string} The colorized text with the appended string.
41
- * @example
42
- * console.log(green('Success')) // Output: '\x1b[32mSuccess\x1b[1;39m\x1b[22m '
43
- */
44
- const green = (text, plus = ' ') =>
45
- `\x1b[32m${text}\x1b[1;39m\x1b[22m${plus}`
46
-
47
- /**
48
- * Default options for configuring the REPL (Read-Eval-Print Loop) environment.
49
- *
50
- * This object provides a set of default configurations that can be used to
51
- * initialize a REPL session. Each property in this object can be overridden
52
- * by providing a custom options object when creating a REPL instance.
53
- *
54
- * @constant {Object} replOpts
55
- * @property {Function|undefined} about - A function to display information
56
- * about the REPL or project. Defaults to undefined.
57
- * @property {boolean} allowInvocation - Determines if invocation of commands
58
- * is allowed. Defaults to true.
59
- * @property {boolean} allowDefaultCommands - Indicates if default commands
60
- * like 'clear' and 'about' should be available. Defaults to true.
61
- * @property {Array} commands - An array of custom command definitions to be
62
- * added to the REPL. Defaults to an empty array.
63
- * @property {Object} exports - An object containing variables or functions
64
- * to be exported to the REPL context. Defaults to an empty object.
65
- * @property {Function} onReady - A callback function that is executed when
66
- * the REPL is ready. Defaults to an empty function.
67
- * @property {string} prompt - The prompt string displayed in the REPL.
68
- * Defaults to the value of `kDefaultPrompt`.
69
- * @property {boolean} useGlobal - Specifies whether the REPL should use the
70
- * global context. Defaults to true.
71
- * @property {Object} replOpts - Additional options to be passed to the REPL
72
- * server. Defaults to an empty object.
73
- *
74
- * @example
75
- * // Creating a REPL with default options
76
- * const replInstance = createRepl()
77
- *
78
- * @example
79
- * // Overriding default options
80
- * const customRepl = createRepl({
81
- * prompt: '> ',
82
- * allowDefaultCommands: false
83
- * })
84
- */
85
- const replOpts = {
86
- about: undefined,
87
- allowDefaultCommands: true,
88
- commands: [],
89
- exports: {},
90
- onReady: () => { },
91
- prompt: kDefaultPrompt,
92
- useGlobal: true,
93
- replOpts: {},
94
- }
95
-
96
- /**
97
- * Creates a REPL (Read-Eval-Print Loop) instance with customizable options.
98
- *
99
- * This function initializes a REPL environment using the provided options,
100
- * allowing for the execution of commands and scripts in a dynamic context.
101
- * It supports custom commands, prompt customization, and context exports.
102
- *
103
- * @function createRepl
104
- * @param {Object} [options] - Configuration options for the REPL instance.
105
- * @param {Function} [options.about] - Function to display information about
106
- * the REPL or project.
107
- * @param {boolean} [options.allowDefaultCommands=true] - Indicates if default
108
- * commands like 'clear' and 'about' should be available.
109
- * @param {Array} [options.commands] - Custom command definitions to be added
110
- * to the REPL.
111
- * @param {Object} [options.exports] - Variables or functions to be exported
112
- * to the REPL context.
113
- * @param {Function} [options.onReady] - Callback executed when the REPL is
114
- * ready.
115
- * @param {string} [options.prompt=kDefaultPrompt] - The prompt string
116
- * displayed in the REPL.
117
- * @param {boolean} [options.useGlobal=true] - Specifies whether the REPL
118
- * should use the global context.
119
- * @param {Object} [options.replOpts] - Additional options for the REPL server.
120
- *
121
- * @example
122
- * // Creating a REPL with default options
123
- * const replInstance = createRepl()
124
- *
125
- * @example
126
- * // Overriding default options
127
- * const customRepl = createRepl({
128
- * prompt: '> ',
129
- * allowDefaultCommands: false
130
- * })
131
- */
132
- export function createRepl(options) {
133
- options = {
134
- ...replOpts,
135
- ...((options && typeof options === 'object' && options) || {})
136
- }
137
-
138
- const prompt = green(options?.prompt ?? kDefaultPrompt)
139
- const replServer = new repl.REPLServer({
140
- useGlobal: options?.useGlobal ?? false,
141
- prompt: options?.prompt ?? kDefaultPrompt,
142
- ...(options?.replOpts ?? {})
143
- })
144
-
145
- const aboutFn = options?.about ?? defaultAbout.bind(replServer)
146
- const state = {
147
- allowInvocation: true
148
- }
149
-
150
- const clearFn = (displayPrompt = true) => {
151
- clear(state)
152
-
153
- if (displayPrompt)
154
- replServer.displayPrompt()
155
- }
156
-
157
- let commands = [
158
- ...(options?.allowDefaultCommands === false ? [] : [
159
- ['cls', { action: () => clearFn(false), help: 'Clears the screen' }],
160
- ['clear', { action: () => clearFn(false), help: 'Clears the screen' }],
161
- ['about', { action: aboutFn, help: 'Shows info about this project' }],
162
- ['state', {
163
- action() {
164
- printStateString(options?.exports ?? replServer.context, state)
165
- },
166
- help: 'Generates state about this REPL context'
167
- }]
168
- ]),
169
- ...(Array.isArray(options?.commands) ? options.commands : [])
170
- ]
171
-
172
- for (const [command, options] of commands) {
173
- const { action, help, overridable } = options
174
-
175
- replServer.defineCommand(command, { action, help })
176
-
177
- if (overridable !== false) {
178
- overridableGlobal(replServer, command, action)
179
- }
180
- }
181
-
182
- Object.assign(replServer.context,
183
- options?.exports ?? {},
184
- {
185
- [Symbol.for('repl.prompt')]: prompt,
186
- replServer
187
- },
188
- )
189
-
190
- Object.defineProperty(replServer, '_initialPrompt', {
191
- get() {
192
- const _prompt = replServer.context[Symbol.for('repl.prompt')]
193
- const isRed = !globalThis?._
194
-
195
- return isRed ? red(_prompt) : green(_prompt)
196
- }
197
- })
198
-
199
- replServer.setupHistory('repl.history', function(err, repl) {
200
- clearFn(false)
201
- aboutFn(false)
202
- options?.onReady?.call(replServer)
203
- replServer.displayPrompt()
204
- })
205
-
206
- replServer.generateStateString = function() {
207
- return printStateString(
208
- options?.exports ?? replServer.context,
209
- state,
210
- true,
211
- )
212
- }
213
-
214
- return replServer
215
- }
216
-
217
- /**
218
- * Displays information about the current project in the REPL.
219
- *
220
- * This function outputs the project's name, version, description, and author
221
- * to the console using ANSI escape codes for color formatting. It attempts to
222
- * display the REPL prompt after printing the information.
223
- *
224
- * The function uses optional chaining to check if `this` context has a
225
- * `displayPrompt` method. If not, it defaults to using the `replServer`
226
- * instance to display the prompt.
227
- *
228
- * @function defaultAbout
229
- * @example
230
- * // Outputs project information in the REPL
231
- * defaultAbout()
232
- */
233
- function defaultAbout(displayPrompt = true) {
234
- console.log(`\x1b[32m${project.name}\x1b[39m v\x1b[1m${project.version}\x1b[22m`);
235
- console.log(`\x1b[3m${project.description}\x1b[23m`);
236
- console.log(`Written by \x1b[34m${project.author ?? 'Jane Doe'}\x1b[39m.`);
237
-
238
- if (displayPrompt) {
239
- console.log('')
240
- return this?.displayPrompt() ?? replServer.displayPrompt();
241
- }
242
- }
243
-
244
- /**
245
- * Clears the terminal screen if invocation is allowed.
246
- *
247
- * This function uses ANSI escape codes to reset the cursor position and
248
- * clear the terminal screen. It is typically used to refresh the display
249
- * in a REPL environment.
250
- *
251
- * @function clear
252
- * @param {boolean} [replState.allowInvocation] - A flag indicating whether the
253
- * screen clearing is permitted. Defaults to true.
254
- * @example
255
- * // Clears the screen if invocation is allowed
256
- * clear()
257
- */
258
- function clear(replState) {
259
- if (replState.allowInvocation) {
260
- process.stdout.write('\x1b[3;0f\x1b[2J')
261
- }
262
- }
263
-
264
- /**
265
- * Creates an overridable global property within a given context, allowing
266
- * dynamic reassignment and restoration of its default behavior.
267
- *
268
- * This function defines a property on the specified context that can be
269
- * overridden by an expression assignment. It also registers a REPL command
270
- * to restore the property to its default state. The property is initially
271
- * set to execute a provided action function, and upon reassignment, it
272
- * stores the new value and logs a message indicating the change.
273
- *
274
- * @function overridableGlobal
275
- * @param {Object} replServer - The REPL server instance to define commands on.
276
- * @param {string} property - The name of the property to be made overridable.
277
- * @param {Function} action - The default function to execute when the property
278
- * is accessed before being overridden.
279
- * @param {string} [changeText='Expression assignment to "@X", previous function now disabled.'] -
280
- * The message to log when the property is overridden. The placeholder "@X" is
281
- * replaced with the property name.
282
- * @param {Object} [context=globalThis] - The context in which to define the
283
- * property. Defaults to the global object.
284
- *
285
- * @example
286
- * // Define an overridable global property 'myProp' in the REPL
287
- * overridableGlobal(replServer, 'myProp', () => 'default value')
288
- */
289
- function overridableGlobal(
290
- replServer,
291
- property,
292
- action,
293
- changeText = 'Expression assignment to "@X", previous function now disabled.',
294
- context = globalThis,
295
- ) {
296
- const message = changeText.replaceAll(/\@X/g, property)
297
-
298
- let changed = false
299
- let storage = undefined
300
-
301
- const makeDescriptor = () => ({
302
- get() {
303
- if (changed === false) {
304
- return action()
305
- }
306
-
307
- return storage
308
- },
309
- set(value) {
310
- if (changed === false) {
311
- console.log(message)
312
- changed = true
313
- }
314
-
315
- storage = value
316
- },
317
- configurable: true,
318
- get enumerable() { return changed }
319
- })
320
-
321
- replServer.defineCommand(
322
- `restore${property.charAt(0).toUpperCase()}${property.substring(1,property.length)}`,
323
- {
324
- action() {
325
- changed = false
326
- storage = undefined
327
-
328
- Object.defineProperty(context, property, makeDescriptor())
329
- console.log(this.help)
330
- },
331
- help: `Restores ${property} to default REPL custom state.`
332
- }
333
- )
334
-
335
- Object.defineProperty(context, property, makeDescriptor())
336
- }
337
-
338
- /**
339
- * Generates a snapshot of the current REPL state, categorizing global objects
340
- * into classes, functions, properties, symbols, and descriptors. This function
341
- * is designed to capture and organize the current state for inspection or
342
- * modification purposes. It temporarily disables invocation to safely enumerate
343
- * global objects, capturing their descriptors and categorizing them accordingly.
344
- * If invocation is already disabled, it returns the current state without
345
- * modification. Skipped properties during enumeration are tracked but not
346
- * processed further.
347
- *
348
- * @param {object} forObject the object to generate state for. This object
349
- * defaults to `globalThis`
350
- * @param {object} _state a private system shared state variable used to
351
- * determine whether or not to allow invocation of getters that are aware
352
- * of the state variable.
353
- * @returns {Object} An object representing the current REPL state, with
354
- * properties for classes, functions, properties, symbols, and descriptors
355
- * (further divided into accessors and data descriptors). Each category is an
356
- * object with keys as the global identifiers and values containing the key,
357
- * value, and descriptor of the item.
358
- */
359
- function generateState(forObject = globalThis, _state) {
360
- const replState = {
361
- classes: {},
362
- functions: {},
363
- properties: {},
364
- symbols: {},
365
- descriptors: {
366
- accessors: {},
367
- data: {},
368
- },
369
- };
370
-
371
- if (!_state.allowInvocation) {
372
- return replState;
373
- }
374
-
375
- let skipped = [];
376
-
377
- _state.allowInvocation = false;
378
- Reflect.ownKeys(forObject).forEach(key => {
379
- try {
380
- const value = forObject[key];
381
- const descriptor = Object.getOwnPropertyDescriptor(forObject, key);
382
-
383
- if (String(value).startsWith('class')) {
384
- replState.classes[key] = {key, value, descriptor};
385
- }
386
- else if (typeof value === 'function') {
387
- replState.functions[key] = {key, value, descriptor};
388
- }
389
- else {
390
- replState.properties[key] = {key, value, descriptor};
391
- }
392
-
393
- if (typeof key === 'symbol') {
394
- replState.symbols[key] = { key, value, descriptor };
395
- }
396
-
397
- if (Reflect.has(descriptor, 'get') || Reflect.has(descriptor, 'set')) {
398
- replState.descriptors.accessors[key] = { key, descriptor };
399
- }
400
- else if (Reflect.has(descriptor, 'value')) {
401
- replState.descriptors.data[key] = { key, descriptor };
402
- }
403
- }
404
- catch (ignored) {
405
- skipped.push(String(key));
406
- }
407
- });
408
- _state.allowInvocation = true;
409
-
410
- return replState;
411
- }
412
-
413
- /**
414
- * Prints a formatted string representation of the state of an object.
415
- *
416
- * This function generates a state object for the given `forObject` and
417
- * prints its classes, functions, properties, and descriptors in a
418
- * human-readable format. The output is styled using ANSI escape codes
419
- * for terminal display.
420
- *
421
- * @param {Object} [forObject=globalThis] - The object to generate the
422
- * state from. Defaults to the global object.
423
- * @param {object} _state a private system shared state variable used to
424
- * determine whether or not to allow invocation of getters that are aware
425
- * of the state variable.
426
- * @param {boolean} [skipPrint=false] the function prints the state to
427
- * the console by default. If this value is `true`, the calls to console's
428
- * `.log()` will not be invoked.
429
- * @returns {string} the same string that is normally printed
430
- *
431
- * @example
432
- * // Prints the state of the global object
433
- * printStateString()
434
- *
435
- * @example
436
- * // Prints the state of a custom object without allowing invocation
437
- * const myObject = { a: 1, b: function() {}, c: class {} }
438
- * printStateString(myObject, false)
439
- */
440
- export function printStateString(
441
- forObject = globalThis,
442
- _state,
443
- skipPrint = false
444
- ) {
445
- const state = generateState(forObject, _state);
446
- const buffer = []
447
- const b = (s) => `\x1b[1m${s}\x1b[22m`;
448
- const i = (s) => `\x1b[3m${s}\x1b[23m`;
449
- const j = ', ';
450
-
451
- state.classes = [...Object.keys(state.classes)].map(k => String(k));
452
- state.functions = [...Object.keys(state.functions)].map(k => String(k));
453
- state.properties = [...Object.keys(state.properties)].map(k => String(k));
454
-
455
- state.descriptors.accessors = [...Object.keys(state.descriptors.accessors)]
456
- .map(k => String(k));
457
-
458
- state.descriptors.data = [...Object.keys(state.descriptors.data)]
459
- .map(k => String(k));
460
-
461
- if (state.classes.length)
462
- buffer.push(`${b('Classes')}\n${wrapContent(state.classes, i, j)}`);
463
-
464
- if (state.functions.length)
465
- buffer.push(`${b('Functions')}\n${wrapContent(state.functions, i, j)}`);
466
-
467
- if (state.properties.length)
468
- buffer.push(`${b('Properties')}\n${wrapContent(state.properties, i, j)}`);
469
-
470
- if (state.descriptors.accessors.length)
471
- buffer.push(`${b('Accessors')}\n${wrapContent(state.descriptors.accessors, i, j)}`);
472
-
473
- buffer.push('')
474
-
475
- if (!skipPrint)
476
- console.log(buffer.join('\n'))
477
-
478
- return buffer.join('\n')
479
- }
480
-
481
- /**
482
- * Formats a string or array of values into lines with specified indentation and line width.
483
- * @param {string|array} input - The input string or array of strings to be formatted.
484
- * @param {number} nCols - The maximum number of columns per line (default 80).
485
- * @param {number} nSpaceIndents - The number of spaces for indentation (default 2).
486
- * @returns {string} The formatted string.
487
- */
488
- function formatValues(input, transform, nCols = 80, nSpaceIndents = 2) {
489
- // Split the string into an array if input is a string
490
- const values = typeof input === 'string' ? input.split(', ') : input;
491
- let line = ''.padStart(nSpaceIndents, ' ');
492
- let result = [];
493
-
494
- values.forEach((value, index) => {
495
- // Transform value if a transform function is supplied.
496
- if (transform && typeof transform === 'function') {
497
- value = transform(value);
498
- }
499
-
500
- // Check if adding the next value exceeds the column limit
501
- if (line.length + value.length + 2 > nCols && line.trim().length > 0) {
502
- // If it does, push the line to the result and start a new line
503
- result.push(line);
504
- line = ''.padStart(nSpaceIndents, ' ');
505
- }
506
-
507
- // Add the value to the line, followed by ", " if it's not the last value
508
- line += value + (index < values.length - 1 ? ', ' : '');
509
- });
510
-
511
- // Add the last line if it's not empty
512
- if (line.trim().length > 0) {
513
- result.push(line);
514
- }
515
-
516
- return result.join('\n');
517
- }
518
-
519
- /**
520
- * Wraps a long string or array of strings into lines with specified
521
- * indentation and line width.
522
- *
523
- * This function processes the input by splitting it into lines, applying
524
- * optional transformations, and wrapping the content to fit within a
525
- * specified width. It handles ANSI escape codes to ensure accurate
526
- * length calculations for terminal output.
527
- *
528
- * @function wrapContent
529
- * @param {string|Array} longString - The input string or array of strings
530
- * to be wrapped.
531
- * @param {Function} [transform] - An optional function to transform each
532
- * element before wrapping.
533
- * @param {string} [joinOn=' '] - The string used to join elements in a line.
534
- * @param {number} [indent=2] - The number of spaces for indentation.
535
- * @param {number} [wrapAt=80] - The maximum line width for wrapping.
536
- * @returns {string} The wrapped content as a single string with lines
537
- * separated by newlines.
538
- * @example
539
- * // Wraps a long string with default settings
540
- * const wrapped = wrapContent('This is a very long string that needs to be wrapped.')
541
- * console.log(wrapped)
542
- */
543
- function wrapContent(
544
- longString,
545
- transform,
546
- joinOn = ' ',
547
- indent = 2,
548
- wrapAt = 80
549
- ) {
550
- let asArray = Array.isArray(longString)
551
- ? longString
552
- : String(longString).replaceAll(/\r\n/g, '\n').split('\n')
553
-
554
- asArray = asArray.map(element => String(element).trim())
555
-
556
- let lines = []
557
- let maxLen = wrapAt - indent
558
- let curLine = []
559
- let sgrLength = (s) => s.replaceAll(/\x1b\[?\d+(;\d+)*[a-zA-Z]/g, '').length
560
-
561
- for (let element of asArray) {
562
- if (typeof transform === 'function') {
563
- element = String(transform(element)).trim()
564
- }
565
-
566
- let curLength = sgrLength(curLine.join(joinOn))
567
- let elementLength = sgrLength(String(element) + joinOn)
568
-
569
- if (curLength + elementLength > maxLen) {
570
- let leading = indent > 0 ? ' '.repeat(indent) : ''
571
- lines.push(`${leading}${curLine.join(joinOn)}`)
572
- curLine = []
573
- }
574
-
575
- curLine.push(String(element))
576
- }
577
-
578
- if (curLine.length) {
579
- let leading = indent > 0 ? ' '.repeat(indent) : ''
580
- lines.push(`${leading}${curLine.join(joinOn)}`)
581
- }
582
-
583
- return lines.join('\n')
584
- }
@@ -1,63 +0,0 @@
1
- const ts = await import('typescript')
2
-
3
- export function findFunction(node) {
4
- const types = new Map([
5
- ['ArrowFunction', ts.SyntaxKind.ArrowFunction],
6
- ['FunctionDeclaration', ts.SyntaxKind.FunctionDeclaration],
7
- ])
8
-
9
- for (const child of node.getChildren()) {
10
- if ([...types.values()].includes(child.kind)) {
11
- return child
12
- }
13
- else if (child.getChildren().length) {
14
- let result = findFunction(child)
15
- if (result)
16
- return result
17
- }
18
- }
19
-
20
- return undefined
21
- }
22
-
23
- export function makeSourceFile(sourcecode) {
24
- return ts.createSourceFile('t.js', sourcecode, ts.ScriptTarget.Latest, true)
25
- }
26
-
27
- export function extractSignature(fn) {
28
- if (!fn || typeof fn !== 'function')
29
- return `Unable to extract signature from ${fn?.name}`
30
-
31
- const isAsync = !!(/object Async/.exec(Object.prototype.toString.call(fn)))
32
- const isBigArrow = Reflect.has(fn, 'prototype')
33
- let sourcecode = fn.toString()
34
-
35
- if (!isBigArrow) {
36
- const starters = ['function', 'async function']
37
- if (!starters.some(sw => fn.toString().startsWith(sw))) {
38
-
39
- }
40
- }
41
-
42
- const source = makeSourceFile(sourcecode)
43
- const declaration = findFunction(source)
44
-
45
- if (!declaration || !declaration.parameters)
46
- return `Unable to extract signature from ${fn?.name}`
47
-
48
-
49
- const signature = [
50
- isAsync ? 'async ' : '',
51
- isBigArrow ? '(' : 'function (',
52
- declaration.parameters.map(parameter => parameter.getText()).join(', '),
53
- ')',
54
- ].join('')
55
-
56
- return signature
57
- }
58
-
59
- Object.defineProperty(Function.prototype, 'signature', {
60
- get() { return extractSignature(this) },
61
- configurable: true,
62
- enumerable: false,
63
- })