@nejs/basic-extensions 2.21.5 → 2.22.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/README.md +78 -4
  2. package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
  3. package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
  4. package/dist/cjs/classes/index.cjs +11129 -0
  5. package/dist/cjs/classes/index.cjs.map +7 -0
  6. package/dist/cjs/index.cjs +15191 -0
  7. package/dist/cjs/index.cjs.map +7 -0
  8. package/dist/cjs/utils/index.cjs +3954 -0
  9. package/dist/cjs/utils/index.cjs.map +7 -0
  10. package/dist/esm/basic-extensions.mjs +25 -0
  11. package/dist/esm/basic-extensions.mjs.map +7 -0
  12. package/package.json +16 -22
  13. package/repl.bootstrap.js +4 -7
  14. package/repl.history +26 -26
  15. package/src/classes/enumeration.js +466 -0
  16. package/src/classes/index.js +5 -1
  17. package/src/index.js +3 -1
  18. package/src/regular.expression.extensions.js +0 -35
  19. package/src/utils/toolkit.js +699 -516
  20. package/tests/arrayextensions.test.js +3 -3
  21. package/tests/index.test.js +3 -1
  22. package/tests/newClasses/asyncIterable.test.js +3 -3
  23. package/tests/newClasses/deferred.test.js +3 -3
  24. package/tests/newClasses/descriptor.test.js +3 -3
  25. package/tests/newClasses/iterable.test.js +3 -3
  26. package/tests/newClasses/refmap.test.js +3 -3
  27. package/tests/newClasses/refset.test.js +3 -3
  28. package/tests/objectextensions.test.js +3 -3
  29. package/tests/setextensions.test.js +3 -3
  30. package/tests/stringextensions.test.js +3 -2
  31. package/tests/utils/descriptor.utils.test.js +1 -1
  32. package/tests/utils/toolkit.test.js +429 -163
  33. package/.esdoc.json +0 -9
  34. package/.vscode/settings.json +0 -5
  35. package/bin/build +0 -27
  36. package/bin/clean +0 -14
  37. package/bin/esbuild +0 -91
  38. package/bin/fixup +0 -13
  39. package/bin/repl.basics.js +0 -584
  40. package/bin/repl.signature.js +0 -63
  41. package/bin/version +0 -100
  42. package/dist/@nejs/basic-extensions.bundle.2.21.5.js +0 -25
  43. package/dist/@nejs/basic-extensions.bundle.2.21.5.js.map +0 -7
  44. package/dist/cjs/array.extensions.d.ts +0 -39
  45. package/dist/cjs/array.extensions.js +0 -477
  46. package/dist/cjs/array.extensions.js.map +0 -1
  47. package/dist/cjs/big.int.extension.d.ts +0 -31
  48. package/dist/cjs/big.int.extension.js +0 -273
  49. package/dist/cjs/big.int.extension.js.map +0 -1
  50. package/dist/cjs/classes/asyncIterable.d.ts +0 -126
  51. package/dist/cjs/classes/asyncIterable.js +0 -209
  52. package/dist/cjs/classes/asyncIterable.js.map +0 -1
  53. package/dist/cjs/classes/deferred.d.ts +0 -146
  54. package/dist/cjs/classes/deferred.js +0 -291
  55. package/dist/cjs/classes/deferred.js.map +0 -1
  56. package/dist/cjs/classes/descriptor.d.ts +0 -334
  57. package/dist/cjs/classes/descriptor.js +0 -537
  58. package/dist/cjs/classes/descriptor.js.map +0 -1
  59. package/dist/cjs/classes/enum.d.ts +0 -50
  60. package/dist/cjs/classes/enum.js +0 -405
  61. package/dist/cjs/classes/enum.js.map +0 -1
  62. package/dist/cjs/classes/index.d.ts +0 -15
  63. package/dist/cjs/classes/index.js +0 -63
  64. package/dist/cjs/classes/index.js.map +0 -1
  65. package/dist/cjs/classes/introspector.d.ts +0 -20
  66. package/dist/cjs/classes/introspector.js +0 -130
  67. package/dist/cjs/classes/introspector.js.map +0 -1
  68. package/dist/cjs/classes/iterable.d.ts +0 -169
  69. package/dist/cjs/classes/iterable.js +0 -268
  70. package/dist/cjs/classes/iterable.js.map +0 -1
  71. package/dist/cjs/classes/param.parser.d.ts +0 -221
  72. package/dist/cjs/classes/param.parser.js +0 -242
  73. package/dist/cjs/classes/param.parser.js.map +0 -1
  74. package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
  75. package/dist/cjs/classes/pluggable.proxy.js +0 -444
  76. package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
  77. package/dist/cjs/classes/property.d.ts +0 -79
  78. package/dist/cjs/classes/property.js +0 -284
  79. package/dist/cjs/classes/property.js.map +0 -1
  80. package/dist/cjs/classes/refmap.d.ts +0 -238
  81. package/dist/cjs/classes/refmap.js +0 -421
  82. package/dist/cjs/classes/refmap.js.map +0 -1
  83. package/dist/cjs/classes/refset.d.ts +0 -186
  84. package/dist/cjs/classes/refset.js +0 -370
  85. package/dist/cjs/classes/refset.js.map +0 -1
  86. package/dist/cjs/classes/symkeys.d.ts +0 -349
  87. package/dist/cjs/classes/symkeys.js +0 -510
  88. package/dist/cjs/classes/symkeys.js.map +0 -1
  89. package/dist/cjs/classes/type.d.ts +0 -56
  90. package/dist/cjs/classes/type.js +0 -405
  91. package/dist/cjs/classes/type.js.map +0 -1
  92. package/dist/cjs/function.extensions.d.ts +0 -12
  93. package/dist/cjs/function.extensions.js +0 -758
  94. package/dist/cjs/function.extensions.js.map +0 -1
  95. package/dist/cjs/global.this.d.ts +0 -2
  96. package/dist/cjs/global.this.js +0 -300
  97. package/dist/cjs/global.this.js.map +0 -1
  98. package/dist/cjs/index.d.ts +0 -31
  99. package/dist/cjs/index.js +0 -228
  100. package/dist/cjs/index.js.map +0 -1
  101. package/dist/cjs/json.extensions.d.ts +0 -2
  102. package/dist/cjs/json.extensions.js +0 -109
  103. package/dist/cjs/json.extensions.js.map +0 -1
  104. package/dist/cjs/map.extensions.d.ts +0 -3
  105. package/dist/cjs/map.extensions.js +0 -143
  106. package/dist/cjs/map.extensions.js.map +0 -1
  107. package/dist/cjs/math.extension.d.ts +0 -14
  108. package/dist/cjs/math.extension.js +0 -71
  109. package/dist/cjs/math.extension.js.map +0 -1
  110. package/dist/cjs/number.extension.d.ts +0 -44
  111. package/dist/cjs/number.extension.js +0 -278
  112. package/dist/cjs/number.extension.js.map +0 -1
  113. package/dist/cjs/object.extensions.d.ts +0 -33
  114. package/dist/cjs/object.extensions.js +0 -1091
  115. package/dist/cjs/object.extensions.js.map +0 -1
  116. package/dist/cjs/package.json +0 -3
  117. package/dist/cjs/proxy.extensions.d.ts +0 -2
  118. package/dist/cjs/proxy.extensions.js +0 -207
  119. package/dist/cjs/proxy.extensions.js.map +0 -1
  120. package/dist/cjs/reflect.extensions.d.ts +0 -14
  121. package/dist/cjs/reflect.extensions.js +0 -316
  122. package/dist/cjs/reflect.extensions.js.map +0 -1
  123. package/dist/cjs/regular.expression.extensions.d.ts +0 -2
  124. package/dist/cjs/regular.expression.extensions.js +0 -423
  125. package/dist/cjs/regular.expression.extensions.js.map +0 -1
  126. package/dist/cjs/set.extensions.d.ts +0 -40
  127. package/dist/cjs/set.extensions.js +0 -355
  128. package/dist/cjs/set.extensions.js.map +0 -1
  129. package/dist/cjs/string.extensions.d.ts +0 -23
  130. package/dist/cjs/string.extensions.js +0 -704
  131. package/dist/cjs/string.extensions.js.map +0 -1
  132. package/dist/cjs/symbol.extensions.d.ts +0 -11
  133. package/dist/cjs/symbol.extensions.js +0 -735
  134. package/dist/cjs/symbol.extensions.js.map +0 -1
  135. package/dist/cjs/utils/copy.object.d.ts +0 -408
  136. package/dist/cjs/utils/copy.object.js +0 -720
  137. package/dist/cjs/utils/copy.object.js.map +0 -1
  138. package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
  139. package/dist/cjs/utils/descriptor.utils.js +0 -889
  140. package/dist/cjs/utils/descriptor.utils.js.map +0 -1
  141. package/dist/cjs/utils/index.d.ts +0 -75
  142. package/dist/cjs/utils/index.js +0 -61
  143. package/dist/cjs/utils/index.js.map +0 -1
  144. package/dist/cjs/utils/stdout.d.ts +0 -742
  145. package/dist/cjs/utils/stdout.js +0 -1042
  146. package/dist/cjs/utils/stdout.js.map +0 -1
  147. package/dist/cjs/utils/toolkit.d.ts +0 -1898
  148. package/dist/cjs/utils/toolkit.js +0 -1378
  149. package/dist/cjs/utils/toolkit.js.map +0 -1
  150. package/dist/cjs/weakref.extensions.d.ts +0 -2
  151. package/dist/cjs/weakref.extensions.js +0 -19
  152. package/dist/cjs/weakref.extensions.js.map +0 -1
  153. package/dist/mjs/array.extensions.d.ts +0 -39
  154. package/dist/mjs/array.extensions.js +0 -474
  155. package/dist/mjs/array.extensions.js.map +0 -1
  156. package/dist/mjs/big.int.extension.d.ts +0 -31
  157. package/dist/mjs/big.int.extension.js +0 -270
  158. package/dist/mjs/big.int.extension.js.map +0 -1
  159. package/dist/mjs/classes/asyncIterable.d.ts +0 -126
  160. package/dist/mjs/classes/asyncIterable.js +0 -204
  161. package/dist/mjs/classes/asyncIterable.js.map +0 -1
  162. package/dist/mjs/classes/deferred.d.ts +0 -146
  163. package/dist/mjs/classes/deferred.js +0 -287
  164. package/dist/mjs/classes/deferred.js.map +0 -1
  165. package/dist/mjs/classes/descriptor.d.ts +0 -334
  166. package/dist/mjs/classes/descriptor.js +0 -533
  167. package/dist/mjs/classes/descriptor.js.map +0 -1
  168. package/dist/mjs/classes/enum.d.ts +0 -50
  169. package/dist/mjs/classes/enum.js +0 -400
  170. package/dist/mjs/classes/enum.js.map +0 -1
  171. package/dist/mjs/classes/index.d.ts +0 -15
  172. package/dist/mjs/classes/index.js +0 -46
  173. package/dist/mjs/classes/index.js.map +0 -1
  174. package/dist/mjs/classes/introspector.d.ts +0 -20
  175. package/dist/mjs/classes/introspector.js +0 -126
  176. package/dist/mjs/classes/introspector.js.map +0 -1
  177. package/dist/mjs/classes/iterable.d.ts +0 -169
  178. package/dist/mjs/classes/iterable.js +0 -263
  179. package/dist/mjs/classes/iterable.js.map +0 -1
  180. package/dist/mjs/classes/param.parser.d.ts +0 -221
  181. package/dist/mjs/classes/param.parser.js +0 -238
  182. package/dist/mjs/classes/param.parser.js.map +0 -1
  183. package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
  184. package/dist/mjs/classes/pluggable.proxy.js +0 -438
  185. package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
  186. package/dist/mjs/classes/property.d.ts +0 -79
  187. package/dist/mjs/classes/property.js +0 -280
  188. package/dist/mjs/classes/property.js.map +0 -1
  189. package/dist/mjs/classes/refmap.d.ts +0 -238
  190. package/dist/mjs/classes/refmap.js +0 -417
  191. package/dist/mjs/classes/refmap.js.map +0 -1
  192. package/dist/mjs/classes/refset.d.ts +0 -186
  193. package/dist/mjs/classes/refset.js +0 -366
  194. package/dist/mjs/classes/refset.js.map +0 -1
  195. package/dist/mjs/classes/symkeys.d.ts +0 -349
  196. package/dist/mjs/classes/symkeys.js +0 -506
  197. package/dist/mjs/classes/symkeys.js.map +0 -1
  198. package/dist/mjs/classes/type.d.ts +0 -56
  199. package/dist/mjs/classes/type.js +0 -401
  200. package/dist/mjs/classes/type.js.map +0 -1
  201. package/dist/mjs/function.extensions.d.ts +0 -12
  202. package/dist/mjs/function.extensions.js +0 -755
  203. package/dist/mjs/function.extensions.js.map +0 -1
  204. package/dist/mjs/global.this.d.ts +0 -2
  205. package/dist/mjs/global.this.js +0 -264
  206. package/dist/mjs/global.this.js.map +0 -1
  207. package/dist/mjs/index.d.ts +0 -31
  208. package/dist/mjs/index.js +0 -206
  209. package/dist/mjs/index.js.map +0 -1
  210. package/dist/mjs/json.extensions.d.ts +0 -2
  211. package/dist/mjs/json.extensions.js +0 -106
  212. package/dist/mjs/json.extensions.js.map +0 -1
  213. package/dist/mjs/map.extensions.d.ts +0 -3
  214. package/dist/mjs/map.extensions.js +0 -140
  215. package/dist/mjs/map.extensions.js.map +0 -1
  216. package/dist/mjs/math.extension.d.ts +0 -14
  217. package/dist/mjs/math.extension.js +0 -68
  218. package/dist/mjs/math.extension.js.map +0 -1
  219. package/dist/mjs/number.extension.d.ts +0 -44
  220. package/dist/mjs/number.extension.js +0 -275
  221. package/dist/mjs/number.extension.js.map +0 -1
  222. package/dist/mjs/object.extensions.d.ts +0 -33
  223. package/dist/mjs/object.extensions.js +0 -1088
  224. package/dist/mjs/object.extensions.js.map +0 -1
  225. package/dist/mjs/package.json +0 -3
  226. package/dist/mjs/proxy.extensions.d.ts +0 -2
  227. package/dist/mjs/proxy.extensions.js +0 -204
  228. package/dist/mjs/proxy.extensions.js.map +0 -1
  229. package/dist/mjs/reflect.extensions.d.ts +0 -14
  230. package/dist/mjs/reflect.extensions.js +0 -313
  231. package/dist/mjs/reflect.extensions.js.map +0 -1
  232. package/dist/mjs/regular.expression.extensions.d.ts +0 -2
  233. package/dist/mjs/regular.expression.extensions.js +0 -420
  234. package/dist/mjs/regular.expression.extensions.js.map +0 -1
  235. package/dist/mjs/set.extensions.d.ts +0 -40
  236. package/dist/mjs/set.extensions.js +0 -352
  237. package/dist/mjs/set.extensions.js.map +0 -1
  238. package/dist/mjs/string.extensions.d.ts +0 -23
  239. package/dist/mjs/string.extensions.js +0 -701
  240. package/dist/mjs/string.extensions.js.map +0 -1
  241. package/dist/mjs/symbol.extensions.d.ts +0 -11
  242. package/dist/mjs/symbol.extensions.js +0 -732
  243. package/dist/mjs/symbol.extensions.js.map +0 -1
  244. package/dist/mjs/utils/copy.object.d.ts +0 -408
  245. package/dist/mjs/utils/copy.object.js +0 -702
  246. package/dist/mjs/utils/copy.object.js.map +0 -1
  247. package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
  248. package/dist/mjs/utils/descriptor.utils.js +0 -875
  249. package/dist/mjs/utils/descriptor.utils.js.map +0 -1
  250. package/dist/mjs/utils/index.d.ts +0 -75
  251. package/dist/mjs/utils/index.js +0 -45
  252. package/dist/mjs/utils/index.js.map +0 -1
  253. package/dist/mjs/utils/stdout.d.ts +0 -742
  254. package/dist/mjs/utils/stdout.js +0 -1037
  255. package/dist/mjs/utils/stdout.js.map +0 -1
  256. package/dist/mjs/utils/toolkit.d.ts +0 -1898
  257. package/dist/mjs/utils/toolkit.js +0 -1373
  258. package/dist/mjs/utils/toolkit.js.map +0 -1
  259. package/dist/mjs/weakref.extensions.d.ts +0 -2
  260. package/dist/mjs/weakref.extensions.js +0 -16
  261. package/dist/mjs/weakref.extensions.js.map +0 -1
  262. package/jsdoc-config.json +0 -31
  263. package/tsconfig.base.json +0 -28
  264. package/tsconfig.cjs.json +0 -8
  265. package/tsconfig.esm.json +0 -8
  266. package/vitest.config.js +0 -7
@@ -1,1037 +0,0 @@
1
- import { Extension, Patch } from '@nejs/extension';
2
- /**
3
- * Captures the output written to `process.stdout` during the execution of
4
- * a callback function. This function temporarily overrides the standard
5
- * output stream to capture any data written to it, allowing for inspection
6
- * or testing of console output.
7
- *
8
- * @param {Function|*} callback - The function to execute, during which
9
- * `process.stdout` is captured. If not a function, it will be treated
10
- * as the first argument to a console log.
11
- * @param {Array} [args=[]] - Arguments to pass to the callback function.
12
- * @param {Object} [thisArg=console] - The value of `this` provided for
13
- * the call to `callback`.
14
- * @returns {string} The captured output from `process.stdout`.
15
- *
16
- * @example
17
- * const output = captureStdout(() => {
18
- * console.log('Hello, World!')
19
- * })
20
- * console.log(output) // Outputs: 'Hello, World!'
21
- *
22
- * @description
23
- * This function is useful for testing or capturing console output without
24
- * displaying it in the terminal. It works by temporarily replacing
25
- * `process.stdout.write` with a custom function that appends output to a
26
- * string. After the callback is executed, the original `process.stdout.write`
27
- * is restored.
28
- */
29
- export function captureStdout(callback, args = [], thisArg = console) {
30
- let captured = '';
31
- const originalWrite = process.stdout.write;
32
- if (typeof callback !== 'function') {
33
- let newArgs = [callback];
34
- if (thisArg) {
35
- newArgs.push(thisArg);
36
- }
37
- newArgs = newArgs.concat(args);
38
- callback = function () {
39
- console.log(...newArgs);
40
- };
41
- thisArg = console;
42
- args = [];
43
- }
44
- process.stdout.write = (chunk, encoding, callback) => {
45
- captured += chunk;
46
- };
47
- try {
48
- callback.apply(thisArg, args);
49
- }
50
- finally {
51
- process.stdout.write = originalWrite;
52
- }
53
- return captured.substring(0, captured.length - 1);
54
- }
55
- /**
56
- * A class that simulates a console for capturing and manipulating console
57
- * output as strings. This class provides methods to log messages, format
58
- * them with colors, and store them in a buffer for later inspection or
59
- * manipulation.
60
- *
61
- * @example
62
- * const stringConsole = new StringConsole()
63
- * stringConsole.log('Hello, World!')
64
- * stringConsole.buffer // ['Hello, World!']
65
- *
66
- * @description
67
- * The StringConsole class is designed to capture console output without
68
- * displaying it in the terminal. It stores the output in a buffer, allowing
69
- * for easy retrieval and manipulation. This is particularly useful for
70
- * testing or when console output needs to be processed programmatically.
71
- */
72
- export class StringConsole {
73
- /**
74
- * @type {Array}
75
- * @description
76
- * The buffer array is used to store captured console output. It is
77
- * initialized as an empty array and can be populated with strings
78
- * representing console messages. This buffer serves as a temporary
79
- * storage for output that can be manipulated or inspected later.
80
- *
81
- * @example
82
- * const console = new StringConsole()
83
- * console.buffer.push('Hello, World!')
84
- * console.buffer // ['Hello, World!']
85
- */
86
- buffer = [];
87
- /**
88
- * The last index of the buffer when capture began. This number should be
89
- * set to `NaN` when not in use.
90
- *
91
- * @type {number|NaN}
92
- */
93
- capturedAt = NaN;
94
- /**
95
- * If this is `true`, all "logged" output will be captured in an ever
96
- * growing buffer.
97
- *
98
- * @type {boolean}
99
- * @see {@link StringConsole.buffer}
100
- */
101
- captureOutput = true;
102
- /**
103
- * @typedef {
104
- * Int8Array|Int16Array|Int32Array|Float32Array|Float64Array
105
- * } TypedArray
106
- */
107
- /**
108
- * @typedef {(
109
- * chunk: string|Buffer|TypedArray|DataView,
110
- * encoding: string|null,
111
- * callback: ()=>{}
112
- * )=>boolean} StringConsoleRecorder
113
- * @property {boolean} [Symbol.for('StringConsole.recorder')]
114
- */
115
- /**
116
- * The recorder function is what is subsituted for the `process.stdout.write`
117
- * function whenever we need to temporarily capture the output of data bound
118
- * for the bidirectional read-write stream, `stdout`.
119
- *
120
- * @type {StringConsoleRecorder}
121
- * @param {string|Buffer|TypedArray|DataView|any} chunk Optional data to
122
- * write. For streams not operating in object mode, chunk must be a
123
- * {@link String}, {@link Buffer}, {@link Int8Array}, {@link Int16Array},
124
- * {@link Int32Array}, {@link Float32Array}, {@link Float64Array} or
125
- * {@link DataView}. For object mode streams, chunk may be any JavaScript
126
- * value other than `null`.
127
- * @param {string|null} encoding the encoding, if chunk is a string.
128
- * Default: `'utf8'`
129
- * @param {Function} callback callback for when this chunk of data is
130
- * flushed.
131
- *
132
- * @returns {boolean} false if the stream wishes for the calling code to
133
- * wait for the 'drain' event to be emitted before continuing to write
134
- * additional data; otherwise true.
135
- */
136
- recorder = Object.defineProperty(function recorder(chunk, encoding, callback) { this.buffer.push(chunk); }, Symbol.for(`StringConsole.recorder`), { value: true, configurable: true });
137
- /**
138
- * Initializes a new instance of the StringConsole class.
139
- *
140
- * @param {string|string[]} [initialContents] - The initial contents to
141
- * populate the buffer. If an array is provided, it will be used directly
142
- * as the buffer. If a single string is provided, it will be converted
143
- * to a string and added to the buffer.
144
- *
145
- * @example
146
- * const console1 = new StringConsole('Hello')
147
- * console1.buffer // ['Hello']
148
- *
149
- * const console2 = new StringConsole(['Hello', 'World'])
150
- * console2.buffer // ['Hello', 'World']
151
- */
152
- constructor(captureOutput = true, initialContents = undefined) {
153
- this.recorder = this.recorder.bind(this);
154
- if (Array.isArray(initialContents))
155
- this.buffer = initialContents;
156
- else if (initialContents)
157
- this.buffer.push(String(initialContents));
158
- }
159
- /**
160
- * Clears the buffer by removing all elements.
161
- *
162
- * This method utilizes the `splice` function to remove all elements
163
- * from the buffer array, effectively resetting it to an empty state.
164
- * This is useful when you want to discard all previously captured
165
- * console output and start fresh.
166
- *
167
- * @returns {StringConsole} `this` to allow for calling `clear()`
168
- * before immediately invoking a console method.
169
- *
170
- * @example
171
- * const console = new StringConsole(['Hello', 'World'])
172
- * console.clear()
173
- * console.buffer // []
174
- */
175
- clear() {
176
- this.buffer.splice(0, this.buffer.length);
177
- return this;
178
- }
179
- /**
180
- * Checks if the console output is currently being captured.
181
- *
182
- * This method determines if the `process.stdout.write` function has been
183
- * overridden to capture console output by checking for the presence of
184
- * a specific symbol.
185
- *
186
- * @returns {boolean} True if capturing is active, false otherwise.
187
- *
188
- * @example
189
- * const stringConsole = new StringConsole()
190
- * stringConsole.startCapture()
191
- * console.log(stringConsole.isCapturing()) // Stores 'true' in the buffer
192
- */
193
- isCapturing() {
194
- return Reflect.has(process.stdout.write, Symbol.for('StringConsole.recorder'));
195
- }
196
- /**
197
- * Starts capturing console output.
198
- *
199
- * This method overrides the `process.stdout.write` function with a custom
200
- * recorder function to capture all console output.
201
- *
202
- * @returns {number} the last index of the buffer in its current state or
203
- * 0 if it is empty
204
- *
205
- * @example
206
- * const stringConsole = new StringConsole()
207
- * stringConsole.startCapture()
208
- * console.log('This will be stored in stringConsole.buffer')
209
- */
210
- startCapture() {
211
- if (this.captureOutput === false)
212
- this.buffer = [];
213
- process.stdout.write = this.recorder;
214
- process.stderr.write = this.recorder;
215
- this.capturedAt = this.buffer.length ? this.buffer.length : 0;
216
- return this.capturedAt;
217
- }
218
- /**
219
- * An object containing two properties covering the captured content
220
- * while `process.stdout.write` was swapped. It should contain the
221
- * range of line indicies as well as the content as an array of strings
222
- *
223
- * @typedef {object} StringConsoleCapturedOutput
224
- * @property {number[]} range an array of two numbers, a starting index
225
- * and an ending index. This value will be [NaN,NaN] if this instance
226
- * has indicated that storing captured output is disabled.
227
- * @property {string[]} lines an array of strings of captured output
228
- * that occurred in between calls to {@link ~startCapture} and then
229
- * ending call to {@link ~stopCapture}
230
- */
231
- /**
232
- * Stops capturing console output.
233
- *
234
- * This method restores the original `process.stdout.write` function,
235
- * ceasing the capture of console output.
236
- *
237
- * @returns {StringConsoleCapturedOutput} the range of indices capturing
238
- * the lines of the buffer that have been added since capturing was
239
- * started.
240
- *
241
- * @example
242
- * const stringConsole = new StringConsole()
243
- * stringConsole.startCapture()
244
- * console.log('This will be stored in stringConsole.buffer')
245
- * stringConsole.stopCapture()
246
- * console.log('This will not be captured')
247
- */
248
- stopCapture() {
249
- const range = [this.capturedAt || 0, this.buffer.length - 1];
250
- const lines = this.buffer.slice(range[0], range[1] + 1);
251
- if (this.captureOutput === false)
252
- this.buffer = [];
253
- process.stdout.write = StringConsole[Symbol.for('process.stdout.write')];
254
- process.stderr.write = StringConsole[Symbol.for('process.stderr.write')];
255
- this.capturedAt = NaN;
256
- return { range, lines };
257
- }
258
- /**
259
- * Joins the StringConsole output as a single string. By default, each entry
260
- * captured so far is joined on a new line. Pass a different joiner such as
261
- * an empty string or a whitespace character, as examples, to change the
262
- * output string.
263
- *
264
- * @param {string} joinOn the string to join the output buffer on, defaults
265
- * to a new line character
266
- * @returns a single string of contatenated entries so far to this buffer.
267
- */
268
- toString(joinOn = '') {
269
- return this.buffer.join(joinOn);
270
- }
271
- /**
272
- * Captures formatted debug messages as though they'd been printed. The
273
- * resulting output that would have been printed is stored in the buffer
274
- * as well as being returned.
275
- *
276
- * This method formats the provided arguments with color coding specific
277
- * to the 'debug' level as though `console.debug` were used. The output
278
- * is captured and stored in the buffer for later inspection, but not
279
- * actually printed to the standard output.
280
- *
281
- * @param {any[]} args - The arguments to be log captured. These can be
282
- * of any type and will be formatted with color coding without being logged.
283
- *
284
- * @returns {string} The captured console output as a string.
285
- *
286
- * @example
287
- * const stringConsole = new StringConsole()
288
- * stringConsole.debug('[debug]', 'message')
289
- * stringConsole.buffer // Contains the captured messages so far as an array
290
- */
291
- debug(...args) {
292
- args = this.constructor.colorArgs('debug', args);
293
- this.startCapture();
294
- console.debug(...args);
295
- return this.stopCapture().lines.join('\n');
296
- }
297
- /**
298
- * Captures formatted error messages as though they'd been printed. The
299
- * resulting output that would have been printed is stored in the buffer
300
- * as well as being returned.
301
- *
302
- * This method formats the provided arguments with color coding specific
303
- * to the 'error' level as though `console.error` were used. The output
304
- * is captured and stored in the buffer for later inspection, but not
305
- * actually printed to the standard output.
306
- *
307
- * @param {any[]} args - The arguments to be log captured. These can be
308
- * of any type and will be formatted with color coding without being logged.
309
- *
310
- * @returns {string} The captured console output as a string.
311
- *
312
- * @example
313
- * const stringConsole = new StringConsole()
314
- * stringConsole.error('[error]', 'message')
315
- * stringConsole.buffer // Contains the captured messages so far as an array
316
- */
317
- error(...args) {
318
- args = this.constructor.colorArgs('error', args);
319
- this.startCapture();
320
- console.error(...args);
321
- return this.stopCapture().lines.join('\n');
322
- }
323
- /**
324
- * Groups console output under a specified group name and captures the
325
- * output. No content will actually be logged to the console, just
326
- * the output that normally would be is formatted in a string and returned
327
- * instead.
328
- *
329
- * This method allows you to format multiple messages under a single
330
- * group name. It captures the output of each invocation and stores it in
331
- * a buffer. The captured output is returned as a single string.
332
- *
333
- * @param {string} groupName - The name of the group under which the
334
- * messages will be logged.
335
- * @param {...Array} invocations - An array of invocations where each
336
- * invocation is an array. The first element is the log level (e.g.,
337
- * 'log', 'info'), and the remaining elements are the arguments to be
338
- * logged.
339
- *
340
- * @returns {string} The captured console output as a string.
341
- *
342
- * @example
343
- * const console = new StringConsole()
344
- * const output = console.group('MyGroup',
345
- * ['log', 'Hello'],
346
- * ['warn', 'Warning!']
347
- * )
348
- *
349
- * console.buffer // Contains the captured group output
350
- */
351
- group(groupName, ...invocations) {
352
- const commands = ['log', 'info', 'warn', 'error', 'debug', 'trace'];
353
- const buffer = [];
354
- invocations = invocations.filter(i => commands.includes(i?.[0]));
355
- if (groupName)
356
- groupName = this.constructor.style(groupName, ['underline', 'bold']);
357
- else
358
- groupName = this.constructor.style('grouped', ['underline', 'bold']);
359
- this.startCapture();
360
- console.group(groupName);
361
- for (const invocation of invocations) {
362
- if (!Array.isArray(invocation) || invocation.length < 2)
363
- continue;
364
- const [level, ...args] = invocation;
365
- console[level](...this.constructor.colorArgs(level, args));
366
- }
367
- console.groupEnd(groupName);
368
- return this.stopCapture().lines.join('');
369
- }
370
- /**
371
- * Captures formatted info messages as though they'd been printed. The
372
- * resulting output that would have been printed is stored in the buffer
373
- * as well as being returned.
374
- *
375
- * This method formats the provided arguments with color coding specific
376
- * to the 'info' level as though `console.info` were used. The output
377
- * is captured and stored in the buffer for later inspection, but not
378
- * actually printed to the standard output.
379
- *
380
- * @param {any[]} args - The arguments to be log captured. These can be
381
- * of any type and will be formatted with color coding without being logged.
382
- *
383
- * @returns {string} The captured console output as a string.
384
- *
385
- * @example
386
- * const stringConsole = new StringConsole()
387
- * stringConsole.info('[info]', 'message')
388
- * stringConsole.buffer // Contains the captured messages so far as an array
389
- */
390
- info(...args) {
391
- args = this.constructor.colorArgs('info', args);
392
- this.startCapture();
393
- console.info(...args);
394
- return this.stopCapture().lines.join('\n');
395
- }
396
- /**
397
- * Captures formatted log messages as though they'd been printed. The
398
- * resulting output that would have been printed is stored in the buffer
399
- * as well as being returned.
400
- *
401
- * This method formats the provided arguments with color coding specific
402
- * to the 'log' level as though `console.log` were used. The output
403
- * is captured and stored in the buffer for later inspection, but not
404
- * actually printed to the standard output.
405
- *
406
- * @param {any[]} args - The arguments to be log captured. These can be
407
- * of any type and will be formatted with color coding without being logged.
408
- *
409
- * @returns {string} The captured console output as a string.
410
- *
411
- * @example
412
- * const stringConsole = new StringConsole()
413
- * stringConsole.log('[log]', 'message')
414
- * stringConsole.buffer // Contains the captured messages so far as an array
415
- */
416
- log(...args) {
417
- args = this.constructor.colorArgs('log', args);
418
- this.startCapture();
419
- console.log(...args);
420
- return this.stopCapture().lines.join('\n');
421
- }
422
- /**
423
- * Captures formatted trace messages as though they'd been printed. The
424
- * resulting output that would have been printed is stored in the buffer
425
- * as well as being returned.
426
- *
427
- * This method formats the provided arguments with color coding specific
428
- * to the 'trace' level as though `console.trace` were used. The output
429
- * is captured and stored in the buffer for later inspection, but not
430
- * actually printed to the standard output.
431
- *
432
- * @param {any[]} args - The arguments to be log captured. These can be
433
- * of any type and will be formatted with color coding without being logged.
434
- *
435
- * @returns {string} The captured console output as a string.
436
- *
437
- * @example
438
- * const stringConsole = new StringConsole()
439
- * stringConsole.trace('[trace]', 'message')
440
- * stringConsole.buffer // Contains the captured messages so far as an array
441
- */
442
- trace(...args) {
443
- args = this.constructor.colorArgs('trace', args);
444
- this.startCapture();
445
- console.trace(...args);
446
- return this.stopCapture().lines.join('\n');
447
- }
448
- /**
449
- * Captures formatted warn messages as though they'd been printed. The
450
- * resulting output that would have been printed is stored in the buffer
451
- * as well as being returned.
452
- *
453
- * This method formats the provided arguments with color coding specific
454
- * to the 'warn' level as though `console.warn` were used. The output
455
- * is captured and stored in the buffer for later inspection, but not
456
- * actually printed to the standard output.
457
- *
458
- * @param {any[]} args - The arguments to be log captured. These can be
459
- * of any type and will be formatted with color coding without being logged.
460
- *
461
- * @returns {string} The captured console output as a string.
462
- *
463
- * @example
464
- * const stringConsole = new StringConsole()
465
- * stringConsole.warn('[warn]', 'message')
466
- * stringConsole.buffer // Contains the captured messages so far as an array
467
- */
468
- warn(...args) {
469
- args = this.constructor.colorArgs('warn', args);
470
- this.startCapture();
471
- console.warn(...args);
472
- return this.stopCapture().lines.join('\n');
473
- }
474
- /**
475
- * Captures a single line of text that would be logged to the console if
476
- * the console function of the same name were to be invoked. The string
477
- * is formatted according to the log colors, or any pre-existing colors as
478
- * those are untouched. After formatting, the string is returned.
479
- *
480
- * @param {...*} args the arguments to be logged. These can be of any
481
- * type and will be passed to the underlying console's method of the same
482
- * name.
483
- *
484
- * @returns {string}
485
- *
486
- * @example
487
- * const string = StringConsole.debug('[debug]: %o', someVariable)
488
- */
489
- static debug(...args) {
490
- return this.#console.clear().debug(...args);
491
- }
492
- /**
493
- * Captures a single line of text that would be logged to the console if
494
- * the console function of the same name were to be invoked. The string
495
- * is formatted according to the log colors, or any pre-existing colors as
496
- * those are untouched. After formatting, the string is returned.
497
- *
498
- * @param {...*} args the arguments to be logged. These can be of any
499
- * type and will be passed to the underlying console's method of the same
500
- * name.
501
- *
502
- * @returns {string}
503
- *
504
- * @example
505
- * const string = StringConsole.error('[error]: %o', someVariable)
506
- */
507
- static error(...args) {
508
- return this.#console.clear().error(...args);
509
- }
510
- /**
511
- * Groups console output under a specified group name and captures the
512
- * output. No content will actually be logged to the console, just
513
- * the output that normally would be is formatted in a string and returned
514
- * instead.
515
- *
516
- * This method allows you to format multiple messages under a single
517
- * group name. It captures the output of each invocation and stores it in
518
- * a buffer. The captured output is returned as a single string.
519
- *
520
- * @param {string} groupName - The name of the group under which the
521
- * messages will be logged.
522
- * @param {...Array} invocations - An array of invocations where each
523
- * invocation is an array. The first element is the log level (e.g.,
524
- * 'log', 'info'), and the remaining elements are the arguments to be
525
- * logged.
526
- *
527
- * @returns {string} The captured console output as a string.
528
- *
529
- * @example
530
- * const console = new StringConsole()
531
- * const output = console.group('MyGroup',
532
- * ['log', 'Hello'],
533
- * ['warn', 'Warning!']
534
- * )
535
- *
536
- * console.buffer // Contains the captured group output
537
- */
538
- static group(groupName, ...invocations) {
539
- return this.#console.clear().group(groupName, ...invocations);
540
- }
541
- /**
542
- * Captures a single line of text that would be logged to the console if
543
- * the console function of the same name were to be invoked. The string
544
- * is formatted according to the log colors, or any pre-existing colors as
545
- * those are untouched. After formatting, the string is returned.
546
- *
547
- * @param {...*} args the arguments to be logged. These can be of any
548
- * type and will be passed to the underlying console's method of the same
549
- * name.
550
- *
551
- * @returns {string}
552
- *
553
- * @example
554
- * const string = StringConsole.info('[info]: %o', someVariable)
555
- */
556
- static info(...args) {
557
- return this.#console.clear().info(...args);
558
- }
559
- /**
560
- * Captures a single line of text that would be logged to the console if
561
- * the console function of the same name were to be invoked. The string
562
- * is formatted according to the log colors, or any pre-existing colors as
563
- * those are untouched. After formatting, the string is returned.
564
- *
565
- * @param {...*} args the arguments to be logged. These can be of any
566
- * type and will be passed to the underlying console's method of the same
567
- * name.
568
- *
569
- * @returns {string}
570
- *
571
- * @example
572
- * const string = StringConsole.log('[log]: %o', someVariable)
573
- */
574
- static log(...args) {
575
- return this.#console.clear().log(...args);
576
- }
577
- /**
578
- * Captures a single line of text that would be logged to the console if
579
- * the console function of the same name were to be invoked. The string
580
- * is formatted according to the log colors, or any pre-existing colors as
581
- * those are untouched. After formatting, the string is returned.
582
- *
583
- * @param {...*} args the arguments to be logged. These can be of any
584
- * type and will be passed to the underlying console's method of the same
585
- * name.
586
- *
587
- * @returns {string}
588
- *
589
- * @example
590
- * const string = StringConsole.trace('[trace]: %o', someVariable)
591
- */
592
- static trace(...args) {
593
- return this.#console.clear().trace(...args);
594
- }
595
- /**
596
- * Captures a single line of text that would be logged to the console if
597
- * the console function of the same name were to be invoked. The string
598
- * is formatted according to the log colors, or any pre-existing colors as
599
- * those are untouched. After formatting, the string is returned.
600
- *
601
- * @param {...*} args the arguments to be logged. These can be of any
602
- * type and will be passed to the underlying console's method of the same
603
- * name.
604
- *
605
- * @returns {string}
606
- *
607
- * @example
608
- * const string = StringConsole.warn('[warn]: %o', someVariable)
609
- */
610
- static warn(...args) {
611
- return this.#console.clear().warn(...args);
612
- }
613
- /**
614
- * Internal instance of {@link StringConsole} used for static logging
615
- * methods.
616
- *
617
- * @type {StringConsole}
618
- */
619
- static #console = new StringConsole(false);
620
- /**
621
- * A static map defining color codes for console output. Each color is
622
- * associated with an array containing two numbers, which represent
623
- * the ANSI escape codes for styling text in the terminal.
624
- *
625
- * The first number in the array is the suffix code for the standard
626
- * color, while the second number suffix code to undo the color. These
627
- * codes are useless without the pen prefix code.
628
- *
629
- * @type {Map<string, number[]>}
630
- * @see {@link StringConsole.pens}
631
- *
632
- * @example
633
- * // Accessing the color codes for 'red'
634
- * const redCodes = StringConsole.colors.get('red')
635
- * const fgCodes = StringConsole.pens.get('foreground')
636
- * const prefix = `\x1b[${fgCodes[0]}${redCodes[0]}m`
637
- * const suffix = `\x1b[${fgCodes[1]}${redCodes[1]}m`
638
- * // Outputs: "Text" in red but "!!" in the default color
639
- * console.log(`${prefix}Text!!${suffix}`)
640
- *
641
- * @description
642
- * This map is used to apply color coding to console messages, enhancing
643
- * readability and providing visual cues for different log levels.
644
- */
645
- static colors = new Map([
646
- ['black', [0, 9]],
647
- ['red', [1, 9]],
648
- ['green', [2, 9]],
649
- ['yellow', [3, 9]],
650
- ['blue', [4, 9]],
651
- ['magenta', [5, 9]],
652
- ['cyan', [6, 9]],
653
- ['white', [7, 9]]
654
- ]);
655
- /**
656
- * A static map defining the color schemes for different logging levels.
657
- * Each log level is associated with an array of color styles that are
658
- * applied to the console output for that level.
659
- *
660
- * The available log levels and their corresponding color styles are:
661
- * - 'log': White
662
- * - 'info': Cyan
663
- * - 'warn': Yellow
664
- * - 'error': Red
665
- * - 'trace': Magenta
666
- * - 'debug': Bold Yellow
667
- *
668
- * @type {Map<string, string[]>}
669
- *
670
- * @example
671
- * const logColor = StringConsole.levels.get('log') // ['white']
672
- * const errorColor = StringConsole.levels.get('error') // ['red']
673
- */
674
- static levels = Object.defineProperties(new Map([
675
- ['log', ['white']],
676
- ['info', ['cyan']],
677
- ['warn', ['yellow']],
678
- ['error', ['red']],
679
- ['trace', ['magenta']],
680
- ['debug', ['bold', 'yellow']]
681
- ]), {
682
- color: {
683
- value: function color(key) {
684
- for (const value of this.get(key)) {
685
- if (StringConsole.colors.has(value)) {
686
- return StringConsole.colors.get(value);
687
- }
688
- }
689
- return StringConsole.colors.get('white');
690
- },
691
- configurable: true,
692
- },
693
- styles: {
694
- value: function styles(key) {
695
- const styles = [];
696
- for (const value of this.get(key)) {
697
- if (StringConsole.styles.has(value)) {
698
- styles.push(StringConsole.styles.get(value));
699
- }
700
- }
701
- return styles;
702
- },
703
- configurable: true,
704
- },
705
- });
706
- /**
707
- * A static map defining the ANSI escape codes for different pen styles
708
- * used in console output. Each pen style is associated with an array
709
- * containing two numbers: the first for setting the style and the second
710
- * for resetting it.
711
- *
712
- * The available pen styles and their corresponding ANSI codes are:
713
- * - 'foreground': [3, 3] - Standard foreground color
714
- * - 'background': [4, 4] - Standard background color
715
- * - 'bright.foreground': [9, 3] - Bright foreground color
716
- * - 'bright.background': [10, 4] - Bright background color
717
- *
718
- * These are prefixes for both enabling and disabling. Normally a red color
719
- * is represented using SGR (Select Graphic Rendition) codes like \x1b[31m
720
- * for the foreground and \x1b[39m to return to normal color. So the 3
721
- * determines a foreground prefix for starting and stopping (the 3's in 31
722
- * and 39). Background prefixes are usually 4. These change for bright
723
- * colors which use 9 and 3, and 10 and 4, respectively.
724
- *
725
- * @type {Map<string, number[]>}
726
- *
727
- * @example
728
- * // [3, 3]
729
- * const foregroundPen = StringConsole.pens.get('foreground')
730
- *
731
- * // [10, 4]
732
- * const brightBackgroundPen = StringConsole.pens.get('bright.background')
733
- */
734
- static pens = new Map([
735
- ['foreground', [3, 3]],
736
- ['background', [4, 4]],
737
- ['bright.foreground', [9, 3]],
738
- ['bright.background', [10, 4]]
739
- ]);
740
- /**
741
- * A static map defining ANSI escape codes for various text styles used
742
- * in console output. Each style is associated with an array containing
743
- * two escape codes: one for enabling the style and one for disabling it.
744
- *
745
- * The available styles and their corresponding ANSI codes are:
746
- * - 'reset': Resets all styles to default.
747
- * - 'blink': Enables blinking text.
748
- * - 'bold': Makes text bold.
749
- * - 'conceal': Conceals text.
750
- * - 'dim': Dims the text.
751
- * - 'italics': Italicizes the text.
752
- * - 'negative': Inverts the foreground and background colors.
753
- * - 'strike': Strikes through the text.
754
- * - 'underline': Underlines the text.
755
- *
756
- * @type {Map<string, string[]>}
757
- *
758
- * @example
759
- * const boldStyle = StringConsole.styles.get('bold')
760
- * // ['\x1b[1m', '\x1b[22m']
761
- */
762
- static styles = new Map([
763
- ['reset', ['\x1b[0m']],
764
- ['blink', ['\x1b[5m', '\x1b[25m']],
765
- ['bold', ['\x1b[1m', '\x1b[22m']],
766
- ['conceal', ['\x1b[8m', '\x1b[28m']],
767
- ['dim', ['\x1b[2m', '\x1b[22m']],
768
- ['italics', ['\x1b[3m', '\x1b[23m']],
769
- ['negative', ['\x1b[7m', '\x1b[27m']],
770
- ['strike', ['\x1b[9m', '\x1b[29m']],
771
- ['underline', ['\x1b[4m', '\x1b[24m']]
772
- ]);
773
- /**
774
- * Applies ANSI color codes to a given string based on specified options.
775
- * This method checks if the string already contains color codes or if
776
- * the input is not a string, in which case it returns the original input.
777
- * Otherwise, it formats the string with the specified color and pen
778
- * options.
779
- *
780
- * @param {string} string - The string to be colorized.
781
- * @param {Object} [options] - Configuration options for colorization.
782
- * @param {string} [options.level] - The log level determining
783
- * which colors to apply.
784
- * @param {number[]} [options.rgb8] a single color code where 0 - 7, for
785
- * the 'standard' colors specified by the SGR sequences 30 to 37; 8-15 are
786
- * high intensity or bright colors,
787
- * @param {number[]} [options.rgb24] An array of three values, ordered, red,
788
- * green and then blue. The values should range from 0 to 255.
789
- * @param {string|string[]} [options.styles] defaulting to an empty array, if
790
- * supplied with a single known style {@link ~styles}, or an array of them.
791
- * @param {string} [options.pen='foreground'] - The pen type for color
792
- * application, either 'foreground' or 'background'.
793
- * @param {Array} [options.buffer=[]] - An array to prepend to the
794
- * formatted string.
795
- * @param {Array} [options.before=[]] - An array of strings to prepend
796
- * before the main string.
797
- * @param {Array} [options.after=[]] - An array of strings to append
798
- * after the main string. 16 - 231, for the colors in the 6 × 6 × 6 cube
799
- * defined by 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5); 232-255:
800
- * grayscale from dark to light in 24 steps.
801
- *
802
- * @returns {string} The colorized string with ANSI codes applied.
803
- *
804
- * @example
805
- * const coloredString = StringConsole.color('Hello', {
806
- * level: 'info',
807
- * pen: 'bright.foreground'
808
- * })
809
- * console.log(coloredString)
810
- */
811
- static color(string, options = {
812
- level: undefined,
813
- color: undefined,
814
- pen: 'foreground',
815
- rgb8: undefined,
816
- rgb24: [186, 186, 186],
817
- styles: [],
818
- buffer: [],
819
- before: [],
820
- after: [],
821
- }) {
822
- const { colors: Colors, styles: Styles, pens: Pens, levels: Levels } = this;
823
- let useColors = undefined;
824
- let useRGB = false;
825
- let styles = [];
826
- const pens = this.pens.get(options?.pen ?? 'foreground');
827
- const [p0, p1] = pens;
828
- if (options?.styles) {
829
- if (Array.isArray(options.styles))
830
- styles = options.styles
831
- .filter(s => Styles.has(s))
832
- .map(s => Styles.get(s));
833
- else if (typeof options.styles === 'string' && Styles.has(options.styles))
834
- styles = Styles.get(options.styles);
835
- }
836
- if (options?.level && Levels.has(options.level)) {
837
- useColors = Levels.color(options.level);
838
- const addlStyles = Levels.styles(options.level);
839
- if (addlStyles.length)
840
- styles = styles.concat(addlStyles);
841
- }
842
- else if (options?.color && Colors.has(options.color))
843
- useColors = Colors.get(options.color);
844
- else if (options?.rgb24 && Array.isArray(options.rgb24)) {
845
- useColors = [`\x1b[${p0}8;2;${options.rgb24.join(';')};m`, `\x1b[${p1}9m`];
846
- useRGB = true;
847
- }
848
- else if (options?.rgb8 && typeof options.rgb8 === 'number') {
849
- useColors = [`\x1b[${p0}8;5;${options.rgb8}m`, `\x1b[${p1}9m`];
850
- useRGB = true;
851
- }
852
- else
853
- useColors = [9, 9];
854
- const [c0, c1] = (useRGB
855
- ? useColors
856
- : useColors?.map((c, i) => `\x1b[${pens[i]}${c}m`) ?? [`\x1b[39;49m`]);
857
- if (this.hasColor(string))
858
- return string;
859
- if (typeof string !== 'string')
860
- return string;
861
- if (string instanceof String)
862
- (string = String(string));
863
- if (!useColors)
864
- return string;
865
- if (options?.buffer && !Array.isArray(options.buffer))
866
- options.buffer = [String(options.buffer)];
867
- if (options?.before && !Array.isArray(options.before))
868
- options.before = [String(options.before)];
869
- if (options?.after && !Array.isArray(options.after))
870
- options.after = [String(options.after)];
871
- const buffer = [].concat(options?.buffer ?? []);
872
- const before = [].concat(options?.before ?? []);
873
- const after = [].concat(options?.after ?? []);
874
- if (c0)
875
- before.push(c0);
876
- if (c1)
877
- after.push(c1);
878
- for (const style of styles) {
879
- if (style?.[0])
880
- before.push(style[0]);
881
- if (style?.[1])
882
- after.push(style[1]);
883
- }
884
- return [...buffer, before.join(''), string, after.join('')].join('');
885
- }
886
- /**
887
- * Applies color formatting to each argument based on the specified log level.
888
- *
889
- * This method processes an array of arguments, applying color formatting
890
- * to each one according to the provided log level. The color formatting
891
- * is determined by the `color` method, which uses the log level to
892
- * select the appropriate color scheme.
893
- *
894
- * @param {string} level - The log level that determines the color scheme
895
- * to be applied. Common levels include 'log', 'info', 'warn', 'error',
896
- * etc.
897
- * @param {Array} args - An array of arguments to be formatted. Each
898
- * argument will be processed individually to apply the color formatting.
899
- *
900
- * @returns {Array} A new array containing the formatted arguments with
901
- * color applied.
902
- *
903
- * @example
904
- * const formattedArgs = StringConsole.colorArgs(
905
- * 'info',
906
- * ['Message 1', 'Message 2']
907
- * )
908
- * // formattedArgs will contain the messages with 'info' level
909
- * // color formatting
910
- */
911
- static colorArgs(level, args) {
912
- const newArgs = [];
913
- if (args === null || args === undefined || !Array.isArray(args))
914
- return args;
915
- for (const arg of args) {
916
- newArgs.push(this.color(arg, { level }));
917
- }
918
- return newArgs;
919
- }
920
- /**
921
- * Determines if a given string contains ANSI color codes.
922
- *
923
- * This method checks for the presence of ANSI escape codes in the
924
- * provided string, which are used for color formatting in terminal
925
- * outputs. The presence of these codes indicates that the string
926
- * has color formatting applied.
927
- *
928
- * @param {string} string - The string to be checked for ANSI color codes.
929
- *
930
- * @returns {boolean} Returns true if the string contains ANSI color codes,
931
- * otherwise false.
932
- *
933
- * @example
934
- * const hasColor = StringConsole.hasColor('\x1b[31mRed Text\x1b[0m')
935
- * // hasColor will be true
936
- *
937
- * const noColor = StringConsole.hasColor('Plain Text')
938
- * // noColor will be false
939
- */
940
- static hasColor(string) {
941
- return string.includes('\x1b[');
942
- }
943
- /**
944
- * Applies a series of styles to a given string using ANSI escape codes.
945
- *
946
- * This method takes a string and an array of style names or style arrays,
947
- * and applies the corresponding ANSI escape codes to the string. The
948
- * styles are defined in the `styles` map, which associates style names
949
- * with their respective ANSI codes.
950
- *
951
- * @param {string} string - The string to which styles will be applied.
952
- * @param {string|string[]} styles - A style name or an array of style
953
- * names/arrays to be applied. Each style can be a string that matches
954
- * a key in the `styles` map or an array containing ANSI codes.
955
- *
956
- * @returns {string} The styled string with ANSI escape codes applied.
957
- *
958
- * @example
959
- * const styledText = StringConsole.style('Hello', ['bold', 'underline'])
960
- * // styledText will have 'Hello' with bold and underline styles
961
- */
962
- static style(string, styles) {
963
- const before = [];
964
- const after = [];
965
- const buffer = [];
966
- if (typeof styles === 'string' && this.styles.has(styles)) {
967
- styles = [styles];
968
- }
969
- for (const style of styles) {
970
- let group = [];
971
- if (this.styles.has(style))
972
- group = this.styles.get(style);
973
- else if (Array.isArray(style) && style.length >= 1)
974
- group = style;
975
- if (group?.[0])
976
- before.push(group?.[0]);
977
- if (group?.[1])
978
- after.push(group?.[1]);
979
- }
980
- return [before.join(''), string, after.join('')].join('');
981
- }
982
- /* Since this class captures the swaps the process.stdout.write function,
983
- * in to and out of place repeatedly, we want to avoid any possible issues
984
- * where this conflict could be problematic. To this end, we capture the
985
- * global process.stdout.write function as a copy when this class is defined
986
- * but before it is used.
987
- *
988
- * If no other code modifies the `writer` property, this is created as a
989
- * non-enumerable alias to the [Symbol.for('process.stdout.write')] symbol
990
- * the actual function is stored in.
991
- */
992
- static {
993
- Object.defineProperties(StringConsole, {
994
- [Symbol.for('process.stdout.write')]: {
995
- value: Object.defineProperties(process.stdout.write, {
996
- [Symbol.for('original')]: { value: true, configurable: true },
997
- isOriginal: { get() { return true; }, configurable: true },
998
- }),
999
- configurable: true,
1000
- },
1001
- [Symbol.for('process.stderr.write')]: {
1002
- value: Object.defineProperties(process.stderr.write, {
1003
- [Symbol.for('original')]: { value: true, configurable: true },
1004
- isOriginal: { get() { return true; }, configurable: true },
1005
- }),
1006
- configurable: true,
1007
- },
1008
- });
1009
- if (!Reflect.has(StringConsole, 'writer')) {
1010
- Object.defineProperties(StringConsole, {
1011
- writer: {
1012
- value: StringConsole[Symbol.for('process.stdout.write')],
1013
- configurable: true,
1014
- },
1015
- errorWriter: {
1016
- value: StringConsole[Symbol.for('process.stderr.write')],
1017
- configurable: true,
1018
- },
1019
- });
1020
- }
1021
- }
1022
- }
1023
- export const SC = StringConsole;
1024
- export const StringConsoleExtension = new Extension(StringConsole);
1025
- export const StdoutGlobalPatches = new Patch(globalThis, {
1026
- [Patch.kMutablyHidden]: {
1027
- captureStdout,
1028
- }
1029
- });
1030
- export default {
1031
- SC: StringConsole,
1032
- StringConsole,
1033
- StringConsoleExtension,
1034
- StdoutGlobalPatches,
1035
- captureStdout,
1036
- };
1037
- //# sourceMappingURL=stdout.js.map