@nejs/basic-extensions 2.8.0 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/README.md +268 -147
  2. package/bin/version +100 -0
  3. package/dist/@nejs/basic-extensions.bundle.2.10.0.js +19 -0
  4. package/dist/@nejs/basic-extensions.bundle.2.10.0.js.map +7 -0
  5. package/dist/cjs/array.extensions.js +174 -0
  6. package/dist/cjs/array.extensions.js.map +1 -1
  7. package/dist/cjs/big.int.extension.js +1 -0
  8. package/dist/cjs/big.int.extension.js.map +1 -1
  9. package/dist/cjs/classes/descriptor.js +1 -1
  10. package/dist/cjs/classes/descriptor.js.map +1 -1
  11. package/dist/cjs/classes/index.d.ts +1 -0
  12. package/dist/cjs/classes/index.js +3 -0
  13. package/dist/cjs/classes/index.js.map +1 -1
  14. package/dist/cjs/classes/iterable.d.ts +44 -0
  15. package/dist/cjs/classes/iterable.js +64 -0
  16. package/dist/cjs/classes/iterable.js.map +1 -1
  17. package/dist/cjs/classes/param.parser.d.ts +10 -10
  18. package/dist/cjs/classes/property.d.ts +86 -0
  19. package/dist/cjs/classes/property.js +284 -0
  20. package/dist/cjs/classes/property.js.map +1 -0
  21. package/dist/cjs/classes/symkeys.d.ts +68 -11
  22. package/dist/cjs/classes/symkeys.js +103 -17
  23. package/dist/cjs/classes/symkeys.js.map +1 -1
  24. package/dist/cjs/classes/type.d.ts +4 -4
  25. package/dist/cjs/function.extensions.js +1 -0
  26. package/dist/cjs/function.extensions.js.map +1 -1
  27. package/dist/cjs/global.this.js +29 -0
  28. package/dist/cjs/global.this.js.map +1 -1
  29. package/dist/cjs/index.d.ts +2 -0
  30. package/dist/cjs/index.js +18 -0
  31. package/dist/cjs/index.js.map +1 -1
  32. package/dist/cjs/json.extensions.js +22 -20
  33. package/dist/cjs/json.extensions.js.map +1 -1
  34. package/dist/cjs/map.extensions.js +1 -0
  35. package/dist/cjs/map.extensions.js.map +1 -1
  36. package/dist/cjs/number.extension.js +1 -0
  37. package/dist/cjs/number.extension.js.map +1 -1
  38. package/dist/cjs/object.extensions.d.ts +0 -29
  39. package/dist/cjs/object.extensions.js +218 -243
  40. package/dist/cjs/object.extensions.js.map +1 -1
  41. package/dist/cjs/set.extensions.js +1 -0
  42. package/dist/cjs/set.extensions.js.map +1 -1
  43. package/dist/cjs/string.extensions.js +478 -283
  44. package/dist/cjs/string.extensions.js.map +1 -1
  45. package/dist/cjs/symbol.extensions.js +500 -24
  46. package/dist/cjs/symbol.extensions.js.map +1 -1
  47. package/dist/cjs/utils/copy.object.d.ts +408 -0
  48. package/dist/cjs/utils/copy.object.js +720 -0
  49. package/dist/cjs/utils/copy.object.js.map +1 -0
  50. package/dist/cjs/utils/index.d.ts +1 -0
  51. package/dist/cjs/utils/index.js +19 -0
  52. package/dist/cjs/utils/index.js.map +1 -0
  53. package/dist/cjs/utils/toolkit.d.ts +1897 -0
  54. package/dist/cjs/utils/toolkit.js +1377 -0
  55. package/dist/cjs/utils/toolkit.js.map +1 -0
  56. package/dist/mjs/array.extensions.js +174 -0
  57. package/dist/mjs/array.extensions.js.map +1 -1
  58. package/dist/mjs/big.int.extension.js +1 -0
  59. package/dist/mjs/big.int.extension.js.map +1 -1
  60. package/dist/mjs/classes/descriptor.js +1 -1
  61. package/dist/mjs/classes/descriptor.js.map +1 -1
  62. package/dist/mjs/classes/index.d.ts +1 -0
  63. package/dist/mjs/classes/index.js +3 -0
  64. package/dist/mjs/classes/index.js.map +1 -1
  65. package/dist/mjs/classes/iterable.d.ts +44 -0
  66. package/dist/mjs/classes/iterable.js +64 -0
  67. package/dist/mjs/classes/iterable.js.map +1 -1
  68. package/dist/mjs/classes/param.parser.d.ts +10 -10
  69. package/dist/mjs/classes/property.d.ts +86 -0
  70. package/dist/mjs/classes/property.js +280 -0
  71. package/dist/mjs/classes/property.js.map +1 -0
  72. package/dist/mjs/classes/symkeys.d.ts +68 -11
  73. package/dist/mjs/classes/symkeys.js +103 -17
  74. package/dist/mjs/classes/symkeys.js.map +1 -1
  75. package/dist/mjs/classes/type.d.ts +4 -4
  76. package/dist/mjs/function.extensions.js +1 -0
  77. package/dist/mjs/function.extensions.js.map +1 -1
  78. package/dist/mjs/global.this.js +6 -0
  79. package/dist/mjs/global.this.js.map +1 -1
  80. package/dist/mjs/index.d.ts +2 -0
  81. package/dist/mjs/index.js +4 -0
  82. package/dist/mjs/index.js.map +1 -1
  83. package/dist/mjs/json.extensions.js +22 -20
  84. package/dist/mjs/json.extensions.js.map +1 -1
  85. package/dist/mjs/map.extensions.js +1 -0
  86. package/dist/mjs/map.extensions.js.map +1 -1
  87. package/dist/mjs/number.extension.js +1 -0
  88. package/dist/mjs/number.extension.js.map +1 -1
  89. package/dist/mjs/object.extensions.d.ts +0 -29
  90. package/dist/mjs/object.extensions.js +215 -239
  91. package/dist/mjs/object.extensions.js.map +1 -1
  92. package/dist/mjs/set.extensions.js +1 -0
  93. package/dist/mjs/set.extensions.js.map +1 -1
  94. package/dist/mjs/string.extensions.js +478 -283
  95. package/dist/mjs/string.extensions.js.map +1 -1
  96. package/dist/mjs/symbol.extensions.js +501 -25
  97. package/dist/mjs/symbol.extensions.js.map +1 -1
  98. package/dist/mjs/utils/copy.object.d.ts +408 -0
  99. package/dist/mjs/utils/copy.object.js +702 -0
  100. package/dist/mjs/utils/copy.object.js.map +1 -0
  101. package/dist/mjs/utils/index.d.ts +1 -0
  102. package/dist/mjs/utils/index.js +3 -0
  103. package/dist/mjs/utils/index.js.map +1 -0
  104. package/dist/mjs/utils/toolkit.d.ts +1897 -0
  105. package/dist/mjs/utils/toolkit.js +1372 -0
  106. package/dist/mjs/utils/toolkit.js.map +1 -0
  107. package/package.json +30 -38
  108. package/repl.bootstrap.js +12 -1
  109. package/src/array.extensions.js +191 -1
  110. package/src/big.int.extension.js +3 -1
  111. package/src/classes/descriptor.js +1 -1
  112. package/src/classes/index.js +4 -0
  113. package/src/classes/iterable.js +74 -0
  114. package/src/classes/property.js +333 -0
  115. package/src/classes/symkeys.js +120 -19
  116. package/src/function.extensions.js +2 -0
  117. package/src/global.this.js +8 -0
  118. package/src/index.js +5 -0
  119. package/src/json.extensions.js +21 -21
  120. package/src/map.extensions.js +3 -1
  121. package/src/number.extension.js +3 -1
  122. package/src/object.extensions.js +240 -262
  123. package/src/set.extensions.js +3 -1
  124. package/src/string.extensions.js +531 -306
  125. package/src/symbol.extensions.js +529 -25
  126. package/src/utils/copy.object.js +780 -0
  127. package/src/utils/index.js +2 -0
  128. package/src/utils/toolkit.js +1471 -0
  129. package/tests/arrayextensions.test.js +2 -0
  130. package/tests/index.test.js +1 -0
  131. package/tests/newClasses/asyncIterable.test.js +2 -0
  132. package/tests/newClasses/deferred.test.js +5 -3
  133. package/tests/newClasses/descriptor.test.js +2 -0
  134. package/tests/newClasses/iterable.test.js +2 -0
  135. package/tests/newClasses/refmap.test.js +2 -1
  136. package/tests/newClasses/refset.test.js +2 -0
  137. package/tests/objectextensions.test.js +2 -0
  138. package/tests/setextensions.test.js +2 -0
  139. package/tests/stringextensions.test.js +1 -0
  140. package/tests/utils/toolkit.test.js +223 -0
  141. package/tsconfig.base.json +1 -1
  142. package/vitest.config.js +7 -0
  143. package/dist/@nejs/basic-extensions.bundle.2.7.0.js +0 -19
  144. package/dist/@nejs/basic-extensions.bundle.2.7.0.js.map +0 -7
  145. package/docs/assets/anchor.js +0 -350
  146. package/docs/assets/bass-addons.css +0 -12
  147. package/docs/assets/bass.css +0 -544
  148. package/docs/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  149. package/docs/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  150. package/docs/assets/fonts/LICENSE.txt +0 -93
  151. package/docs/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  152. package/docs/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  153. package/docs/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  154. package/docs/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  155. package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  156. package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  157. package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  158. package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  159. package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  160. package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  161. package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  162. package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  163. package/docs/assets/fonts/source-code-pro.css +0 -23
  164. package/docs/assets/github.css +0 -123
  165. package/docs/assets/site.js +0 -168
  166. package/docs/assets/split.css +0 -15
  167. package/docs/assets/split.js +0 -782
  168. package/docs/assets/style.css +0 -147
  169. package/docs/index.html +0 -34965
@@ -12,292 +12,486 @@ const parenthesisPair = ['(', ')'];
12
12
  * utility functions.
13
13
  */
14
14
  exports.StringExtensions = new extension_1.Patch(String, {
15
- /**
16
- * The `isString` method does exactly what one would it expect. It returns
17
- * true if the string matches typeof or instanceof as a string.
18
- *
19
- * @param {*} value checks to see if the `value` is a string
20
- * @returns {boolean} `true` if it is a `String`, `false` otherwise
21
- */
22
- isString(value) {
23
- return (value !== null && value !== undefined &&
24
- (typeof value === 'string' || value instanceof String));
25
- },
26
- /**
27
- * Conditionally returns a value based on whether the supplied
28
- * `value` is a `String` or not. If the `value` is a `String`,
29
- * the `thenValue` will be returned. If it is not a `String`,
30
- * the `elseValue` will be returned instead.
31
- *
32
- * @param {any} value the value to check to determine if it is a
33
- * `String`
34
- * @param {any} thenValue the value to return if the supplied
35
- * `value` is a `String`
36
- * @param {any} elseValue the value to return if the supplied
37
- * `value` is not a `String`
38
- * @returns {any} either the `thenValue` or `elseValue` depending
39
- * on if the supplied `value` is a `String`
40
- *
41
- * @example
42
- * const str = 'hello'
43
- * const num = 42
44
- * ifString(str, 'is a string', 'not a string') // 'is a string'
45
- * ifString(num, 'is a string', 'not a string') // 'not a string'
46
- */
47
- ifString(value, thenValue, elseValue) {
48
- return isThenElse(this.isString(value), thenValue, elseValue);
49
- },
50
- /**
51
- * A getter property that returns a pair of parentheses as an array.
52
- * This property can be used when operations require a clear distinction
53
- * between the opening and closing parentheses, such as parsing or
54
- * matching balanced expressions in strings.
55
- *
56
- * @returns {[string, string]} An array containing a pair of strings: the
57
- * opening parenthesis '(' as the first element, and the closing parenthesis
58
- * ')' as the second element.
59
- */
60
- get parenthesisPair() {
61
- return ['(', ')'];
62
- },
63
- /**
64
- * A getter property that returns a pair of square brackets as an array.
65
- * This property is particularly useful for operations that require a clear
66
- * distinction between the opening and closing square brackets, such as
67
- * parsing arrays in strings or matching balanced expressions within
68
- * square brackets.
69
- *
70
- * @returns {[string, string]} An array containing a pair of strings: the
71
- * opening square bracket '[' as the first element, and the closing square
72
- * bracket ']' as the second element.
73
- */
74
- get squareBracketsPair() {
75
- return ['[', ']'];
76
- },
77
- /**
78
- * A getter property that returns a pair of curly brackets as an array.
79
- * This property is particularly useful for operations that require a clear
80
- * distinction between the opening and closing curly brackets, such as
81
- * parsing objects in strings or matching balanced expressions within
82
- * curly brackets. The returned array consists of the opening curly bracket
83
- * '{' as the first element, and the closing curly bracket '}' as the
84
- * second element.
85
- *
86
- * @returns {[string, string]} An array containing a pair of strings: the
87
- * opening curly bracket '{' as the first element, and the closing curly
88
- * bracket '}' as the second element.
89
- */
90
- get curlyBracketsPair() {
91
- return ['{', '}'];
92
- },
93
- /**
94
- * Generates a random string using base 36 (numbers and lowercase letters).
95
- * This method is useful when you need a random string that includes both
96
- * numbers and letters. The generated string does not include the leading
97
- * '0.' that is part of the string representation of a random number in
98
- * base 36.
99
- *
100
- * @returns {string} A random string of characters in base 36.
101
- *
102
- * @example
103
- * const randomStr = StringExtensions.random36();
104
- * console.log(randomStr); // Output: "3n5yzxjkf2o"
105
- */
106
- random36() {
107
- return Math.random().toString(36).slice(2);
108
- },
109
- /**
110
- * Generates a random string using base 16 (hexadecimal numbers).
111
- * This method is useful when you need a random string that includes both
112
- * numbers and letters in hexadecimal format. The generated string does not
113
- * include the leading '0.' that is part of the string representation of a
114
- * random number in base 16.
115
- *
116
- * @returns {string} A random string of characters in base 16.
117
- *
118
- * @example
119
- * const randomStr = StringExtensions.random16();
120
- * console.log(randomStr); // Output: "3a5f4c"
121
- */
122
- random16() {
123
- return Math.random().toString(16).slice(2);
124
- },
125
- /**
126
- * Generates a random RGB color code.
127
- *
128
- * This method generates a random hexadecimal number, slices off the
129
- * leading '0.' and takes the first 6 characters. It then pads the
130
- * end of the string with '0' until it is 6 characters long. The
131
- * result is a string that can be used as a color code in CSS.
132
- *
133
- * @param {string} [prefix='#'] - The prefix to prepend to the color
134
- * code. Defaults to '#'.
135
- *
136
- * @returns {string} A random RGB color code.
137
- *
138
- * @example
139
- * const randomColor = StringExtensions.randomRGB();
140
- * console.log(randomColor); // Output: "#3a5f4c"
141
- */
142
- randomRGBHex(prefix = '#') {
143
- const hex = Math.random().toString(16).slice(2).substring(0, 6);
144
- return `${prefix}${hex.padEnd(6, '0')}`;
145
- },
146
- /**
147
- * Generates a random ARGB color code.
148
- *
149
- * This method generates a random hexadecimal number, slices off the
150
- * leading '0.' and takes the first 8 characters. It then pads the
151
- * start of the string with '0' until it is 6 characters long and the
152
- * end of the string with '0' until it is 8 characters long. The
153
- * result is a string that can be used as a color code in CSS.
154
- *
155
- * @param {string} [prefix='#'] - The prefix to prepend to the color
156
- * code. Defaults to '#'.
157
- *
158
- * @returns {string} A random ARGB color code.
159
- *
160
- * @example
161
- * const randomColor = StringExtensions.randomARGB();
162
- * console.log(randomColor); // Output: "#3a5f4c00"
163
- */
164
- randomARGBHex(prefix = '#') {
165
- const hex = Math.random().toString(16).slice(2).substring(0, 8);
166
- return `${prefix}${hex.padStart(6, '0').padEnd(8, '0')}`;
167
- },
168
- /**
169
- * Generates a random RGBA color code.
170
- *
171
- * This method generates a random hexadecimal number, slices off the
172
- * leading '0.' and takes the first 8 characters. It then pads the
173
- * start of the string with '0' until it is 6 characters long and the
174
- * end of the string with '0' until it is 8 characters long. The
175
- * result is a string that can be used as a color code in CSS.
176
- *
177
- * @param {string} [prefix='#'] - The prefix to prepend to the color
178
- * code. Defaults to '#'.
179
- *
180
- * @returns {string} A random RGBA color code.
181
- *
182
- * @example
183
- * const randomColor = StringExtensions.randomRGBA();
184
- * console.log(randomColor); // Output: "#3a5f4c00"
185
- */
186
- randomRGBAHex(prefix = '#') {
187
- const hex = Math.random().toString(16).slice(2).substring(0, 8);
188
- return `${prefix}${hex.padStart(6, '0').padStart(8, '0')}`;
189
- },
190
- /**
191
- * Generates a random RGB color code.
192
- *
193
- * This method generates a random hexadecimal number, slices off the
194
- * leading '0.' and pads the end of the string with '0' until it is
195
- * 8 characters long. It then parses the first 6 characters into
196
- * three separate 2-character strings, each representing a color
197
- * component (red, green, blue) in hexadecimal format. These strings
198
- * are then converted into decimal format and used to construct an
199
- * RGB color code.
200
- *
201
- * @returns {string} A random RGB color code.
202
- *
203
- * @example
204
- * const randomColor = StringExtensions.randomRGB();
205
- * console.log(randomColor); // Output: "rgb(58,95,76)"
206
- */
207
- randomRGB() {
208
- const hex = Math.random().toString(16).slice(2).padEnd(8, '0');
209
- const red = parseInt(hex.substring(0, 2), 16);
210
- const green = parseInt(hex.substring(2, 4), 16);
211
- const blue = parseInt(hex.substring(4, 6), 16);
212
- return `rgb(${red}, ${green}, ${blue})`;
213
- },
214
- /**
215
- * Generates a random RGBA color code with optional forced color values.
216
- *
217
- * This method generates a random hexadecimal number, slices off the
218
- * leading '0.' and pads the end of the string with '0' until it is
219
- * 8 characters long. It then parses the first 8 characters into
220
- * four separate 2-character strings, each representing a color
221
- * component (red, green, blue, alpha) in hexadecimal format. These strings
222
- * are then converted into decimal format and used to construct an
223
- * RGBA color code.
224
- *
225
- * If a color component is provided in the `force` parameter, it will
226
- * be used instead of a random value for that component.
227
- *
228
- * @param {Object} force - An object with properties for each color
229
- * component (red, green, blue, alpha) that should be forced to a
230
- * specific value. If a property is undefined or not provided, a
231
- * random value will be used for that component.
232
- * @param {number} force.red - The red component (0-255).
233
- * @param {number} force.green - The green component (0-255).
234
- * @param {number} force.blue - The blue component (0-255).
235
- * @param {number} force.alpha - The alpha component (0.0-1.0).
236
- *
237
- * @returns {string} A random RGBA color code.
238
- *
239
- * @example
240
- * const randomColor = StringExtensions.randomRGBA();
241
- * console.log(randomColor); // Output: "rgba(58,95,76,0.50)"
242
- *
243
- * const forcedGreen = StringExtensions.randomRGBA({ green: 255 });
244
- * console.log(forcedGreen); // Output: "rgba(58,255,76,0.50)"
245
- */
246
- randomRGBA(force = {
247
- red: undefined,
248
- green: undefined,
249
- blue: undefined,
250
- alpha: undefined
251
- }) {
252
- const hex = Math.random().toString(16).slice(2).padEnd(8, '0');
253
- const red = force.red ?? parseInt(hex.substring(0, 2), 16);
254
- const green = force.green ?? parseInt(hex.substring(2, 4), 16);
255
- const blue = force.blue ?? parseInt(hex.substring(4, 6), 16);
256
- const alpha = force.alpha ??
257
- (parseInt(hex.substring(6, 8), 16) / 255.0) * 1.0;
258
- return `rgba(${red}, ${green}, ${blue}, ${alpha.toFixed(2)})`;
259
- },
260
- wrap(object = globalThis, options = {
261
- indent: 2,
262
- separator: ', ',
263
- indentCharacter: ' ',
264
- lineEnding: '\n',
265
- inspector: [Object, 'getOwnPropertyNames'],
266
- mapValues: undefined,
267
- mapLine: undefined,
268
- }) {
269
- const { indent = 2, separator = ', ', indentCharacter = ' ', lineEnding = '\n', inspector = [Object, 'getOwnPropertyNames'], mapValues, mapLine, } = options ?? {};
270
- let tab = indent === 0 ? '' : indentCharacter.repeat(Number(indent) || 2);
271
- let maxLen = 76 - tab.length;
272
- let line = [];
273
- let getElements = inspector[0][inspector[1]];
274
- let values = Array.isArray(object) ? object : getElements(Object(object));
275
- if (typeof mapValues === 'function') {
276
- values = values.map(mapValues);
277
- }
278
- return values.reduce((acc, key, index, { length }) => {
279
- const endOfLine = index < (length - 1) ? separator : '';
280
- let ifCombined = [
281
- tab, ...line.join(separator), key, endOfLine
282
- ].join('').trim();
283
- if (ifCombined.length < maxLen) {
284
- line.push(key);
15
+ [extension_1.Patch.kMutablyHidden]: {
16
+ /**
17
+ * The `isString` method does exactly what one would it expect. It returns
18
+ * true if the string matches typeof or instanceof as a string.
19
+ *
20
+ * @param {*} value checks to see if the `value` is a string
21
+ * @returns {boolean} `true` if it is a `String`, `false` otherwise
22
+ */
23
+ isString(value) {
24
+ return (value !== null && value !== undefined &&
25
+ (typeof value === 'string' || value instanceof String));
26
+ },
27
+ /**
28
+ * Conditionally returns a value based on whether the supplied
29
+ * `value` is a `String` or not. If the `value` is a `String`,
30
+ * the `thenValue` will be returned. If it is not a `String`,
31
+ * the `elseValue` will be returned instead.
32
+ *
33
+ * @param {any} value the value to check to determine if it is a
34
+ * `String`
35
+ * @param {any} thenValue the value to return if the supplied
36
+ * `value` is a `String`
37
+ * @param {any} elseValue the value to return if the supplied
38
+ * `value` is not a `String`
39
+ * @returns {any} either the `thenValue` or `elseValue` depending
40
+ * on if the supplied `value` is a `String`
41
+ *
42
+ * @example
43
+ * const str = 'hello'
44
+ * const num = 42
45
+ * ifString(str, 'is a string', 'not a string') // 'is a string'
46
+ * ifString(num, 'is a string', 'not a string') // 'not a string'
47
+ */
48
+ ifString(value, thenValue, elseValue) {
49
+ return isThenElse(String.isString(value), thenValue, elseValue);
50
+ },
51
+ /**
52
+ * A getter property that returns a pair of parentheses as an array.
53
+ * This property can be used when operations require a clear distinction
54
+ * between the opening and closing parentheses, such as parsing or
55
+ * matching balanced expressions in strings.
56
+ *
57
+ * @returns {[string, string]} An array containing a pair of strings: the
58
+ * opening parenthesis '(' as the first element, and the closing parenthesis
59
+ * ')' as the second element.
60
+ */
61
+ get parenthesisPair() {
62
+ return ['(', ')'];
63
+ },
64
+ /**
65
+ * A getter property that returns a pair of square brackets as an array.
66
+ * This property is particularly useful for operations that require a clear
67
+ * distinction between the opening and closing square brackets, such as
68
+ * parsing arrays in strings or matching balanced expressions within
69
+ * square brackets.
70
+ *
71
+ * @returns {[string, string]} An array containing a pair of strings: the
72
+ * opening square bracket '[' as the first element, and the closing square
73
+ * bracket ']' as the second element.
74
+ */
75
+ get squareBracketsPair() {
76
+ return ['[', ']'];
77
+ },
78
+ /**
79
+ * A getter property that returns a pair of curly brackets as an array.
80
+ * This property is particularly useful for operations that require a clear
81
+ * distinction between the opening and closing curly brackets, such as
82
+ * parsing objects in strings or matching balanced expressions within
83
+ * curly brackets. The returned array consists of the opening curly bracket
84
+ * '{' as the first element, and the closing curly bracket '}' as the
85
+ * second element.
86
+ *
87
+ * @returns {[string, string]} An array containing a pair of strings: the
88
+ * opening curly bracket '{' as the first element, and the closing curly
89
+ * bracket '}' as the second element.
90
+ */
91
+ get curlyBracketsPair() {
92
+ return ['{', '}'];
93
+ },
94
+ /**
95
+ * Generates a random string using base 36 (numbers and lowercase letters).
96
+ * This method is useful when you need a random string that includes both
97
+ * numbers and letters. The generated string does not include the leading
98
+ * '0.' that is part of the string representation of a random number in
99
+ * base 36.
100
+ *
101
+ * @returns {string} A random string of characters in base 36.
102
+ *
103
+ * @example
104
+ * const randomStr = StringExtensions.random36();
105
+ * console.log(randomStr); // Output: "3n5yzxjkf2o"
106
+ */
107
+ random36() {
108
+ return Math.random().toString(36).slice(2);
109
+ },
110
+ /**
111
+ * Generates a random string using base 16 (hexadecimal numbers).
112
+ * This method is useful when you need a random string that includes both
113
+ * numbers and letters in hexadecimal format. The generated string does not
114
+ * include the leading '0.' that is part of the string representation of a
115
+ * random number in base 16.
116
+ *
117
+ * @returns {string} A random string of characters in base 16.
118
+ *
119
+ * @example
120
+ * const randomStr = StringExtensions.random16();
121
+ * console.log(randomStr); // Output: "3a5f4c"
122
+ */
123
+ random16() {
124
+ return Math.random().toString(16).slice(2);
125
+ },
126
+ /**
127
+ * Generates a random RGB color code.
128
+ *
129
+ * This method generates a random hexadecimal number, slices off the
130
+ * leading '0.' and takes the first 6 characters. It then pads the
131
+ * end of the string with '0' until it is 6 characters long. The
132
+ * result is a string that can be used as a color code in CSS.
133
+ *
134
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
135
+ * code. Defaults to '#'.
136
+ *
137
+ * @returns {string} A random RGB color code.
138
+ *
139
+ * @example
140
+ * const randomColor = StringExtensions.randomRGB();
141
+ * console.log(randomColor); // Output: "#3a5f4c"
142
+ */
143
+ randomRGBHex(prefix = '#') {
144
+ const hex = Math.random().toString(16).slice(2).substring(0, 6);
145
+ return `${prefix}${hex.padEnd(6, '0')}`;
146
+ },
147
+ /**
148
+ * Generates a random ARGB color code.
149
+ *
150
+ * This method generates a random hexadecimal number, slices off the
151
+ * leading '0.' and takes the first 8 characters. It then pads the
152
+ * start of the string with '0' until it is 6 characters long and the
153
+ * end of the string with '0' until it is 8 characters long. The
154
+ * result is a string that can be used as a color code in CSS.
155
+ *
156
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
157
+ * code. Defaults to '#'.
158
+ *
159
+ * @returns {string} A random ARGB color code.
160
+ *
161
+ * @example
162
+ * const randomColor = StringExtensions.randomARGB();
163
+ * console.log(randomColor); // Output: "#3a5f4c00"
164
+ */
165
+ randomARGBHex(prefix = '#') {
166
+ const hex = Math.random().toString(16).slice(2).substring(0, 8);
167
+ return `${prefix}${hex.padStart(6, '0').padEnd(8, '0')}`;
168
+ },
169
+ /**
170
+ * Generates a random RGBA color code.
171
+ *
172
+ * This method generates a random hexadecimal number, slices off the
173
+ * leading '0.' and takes the first 8 characters. It then pads the
174
+ * start of the string with '0' until it is 6 characters long and the
175
+ * end of the string with '0' until it is 8 characters long. The
176
+ * result is a string that can be used as a color code in CSS.
177
+ *
178
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
179
+ * code. Defaults to '#'.
180
+ *
181
+ * @returns {string} A random RGBA color code.
182
+ *
183
+ * @example
184
+ * const randomColor = StringExtensions.randomRGBA();
185
+ * console.log(randomColor); // Output: "#3a5f4c00"
186
+ */
187
+ randomRGBAHex(prefix = '#') {
188
+ const hex = Math.random().toString(16).slice(2).substring(0, 8);
189
+ return `${prefix}${hex.padStart(6, '0').padStart(8, '0')}`;
190
+ },
191
+ /**
192
+ * Generates a random RGB color code.
193
+ *
194
+ * This method generates a random hexadecimal number, slices off the
195
+ * leading '0.' and pads the end of the string with '0' until it is
196
+ * 8 characters long. It then parses the first 6 characters into
197
+ * three separate 2-character strings, each representing a color
198
+ * component (red, green, blue) in hexadecimal format. These strings
199
+ * are then converted into decimal format and used to construct an
200
+ * RGB color code.
201
+ *
202
+ * @returns {string} A random RGB color code.
203
+ *
204
+ * @example
205
+ * const randomColor = StringExtensions.randomRGB();
206
+ * console.log(randomColor); // Output: "rgb(58,95,76)"
207
+ */
208
+ randomRGB() {
209
+ const hex = Math.random().toString(16).slice(2).padEnd(8, '0');
210
+ const red = parseInt(hex.substring(0, 2), 16);
211
+ const green = parseInt(hex.substring(2, 4), 16);
212
+ const blue = parseInt(hex.substring(4, 6), 16);
213
+ return `rgb(${red}, ${green}, ${blue})`;
214
+ },
215
+ /**
216
+ * Generates a random RGBA color code with optional forced color values.
217
+ *
218
+ * This method generates a random hexadecimal number, slices off the
219
+ * leading '0.' and pads the end of the string with '0' until it is
220
+ * 8 characters long. It then parses the first 8 characters into
221
+ * four separate 2-character strings, each representing a color
222
+ * component (red, green, blue, alpha) in hexadecimal format. These strings
223
+ * are then converted into decimal format and used to construct an
224
+ * RGBA color code.
225
+ *
226
+ * If a color component is provided in the `force` parameter, it will
227
+ * be used instead of a random value for that component.
228
+ *
229
+ * @param {Object} force - An object with properties for each color
230
+ * component (red, green, blue, alpha) that should be forced to a
231
+ * specific value. If a property is undefined or not provided, a
232
+ * random value will be used for that component.
233
+ * @param {number} force.red - The red component (0-255).
234
+ * @param {number} force.green - The green component (0-255).
235
+ * @param {number} force.blue - The blue component (0-255).
236
+ * @param {number} force.alpha - The alpha component (0.0-1.0).
237
+ *
238
+ * @returns {string} A random RGBA color code.
239
+ *
240
+ * @example
241
+ * const randomColor = StringExtensions.randomRGBA();
242
+ * console.log(randomColor); // Output: "rgba(58,95,76,0.50)"
243
+ *
244
+ * const forcedGreen = StringExtensions.randomRGBA({ green: 255 });
245
+ * console.log(forcedGreen); // Output: "rgba(58,255,76,0.50)"
246
+ */
247
+ randomRGBA(force = {
248
+ red: undefined,
249
+ green: undefined,
250
+ blue: undefined,
251
+ alpha: undefined
252
+ }) {
253
+ const hex = Math.random().toString(16).slice(2).padEnd(8, '0');
254
+ const red = force.red ?? parseInt(hex.substring(0, 2), 16);
255
+ const green = force.green ?? parseInt(hex.substring(2, 4), 16);
256
+ const blue = force.blue ?? parseInt(hex.substring(4, 6), 16);
257
+ const alpha = force.alpha ??
258
+ (parseInt(hex.substring(6, 8), 16) / 255.0) * 1.0;
259
+ return `rgba(${red}, ${green}, ${blue}, ${alpha.toFixed(2)})`;
260
+ },
261
+ /**
262
+ * Applies Select Graphic Rendition (SGR) parameters to a given message for
263
+ * styling in terminal environments. This function allows for the dynamic
264
+ * styling of text output using ANSI escape codes. It supports a variety of
265
+ * modes such as color, brightness, and text decorations like bold or underline.
266
+ *
267
+ * @param {string} message The message to be styled.
268
+ * @param {...string} useModes A series of strings representing the desired
269
+ * styling modes. Modes can include colors (e.g., 'red', 'blue'), brightness
270
+ * ('bright'), foreground/background ('fg', 'bg'), and text decorations
271
+ * ('bold', 'underline'). Modes can be combined in a single string using
272
+ * commas or passed as separate arguments.
273
+ *
274
+ * Colors:
275
+ * ```
276
+ * 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
277
+ * ```
278
+ * Color Specifiers:
279
+ * ```
280
+ * 'fg' -> foreground | 'bg' -> background | 'bright' -> bright colors
281
+ * ```
282
+ *
283
+ * Modes:
284
+ * ```
285
+ * 'blink' or 'k' | 'conceal' or 'c' | 'italics' or 'i' | 'strike' or 's'
286
+ * 'bold' or 'b' | 'dim' or 'd' | 'negative' or 'n' | 'underline' or 'u'
287
+ * ```
288
+ *
289
+ * Examples:
290
+ * - `sgr('Hello', 'red')` applies red color to 'Hello'.
291
+ * - `sgr('World', 'green,bold')` applies green color and bold styling
292
+ * to 'World'.
293
+ * - `sgr('Example', 'bluebgbright')` applies bright blue
294
+ * background color.
295
+ *
296
+ * Short hand syntax is also allowed:
297
+ * - `sgr('hello', 'biu')` applies bold, italics and underline
298
+ * - `sgr('hello', 'bi,redfg')` applies bold, italics and red foreground
299
+ *
300
+ * As a bonus, there is a secret getter applied to the return string that
301
+ * allows you to invoke `sgr(...).show` to automatically log the output to
302
+ * `console.log`. This is done by wrapping the output string in `Object()`
303
+ * to make it a `String` instance and then adding the property descriptor.
304
+ * A custom `Symbol` is applied to make it evaluate in nodejs as though it
305
+ * were a normal string. To strip the extras, wrap the output in `String()`
306
+ *
307
+ * @returns {string} The message wrapped in ANSI escape codes corresponding
308
+ * to the specified modes. The returned string, when printed to a terminal,
309
+ * displays the styled message. Additional properties are attached to the
310
+ * result for utility purposes, such as 'show' for immediate console output.
311
+ */
312
+ sgr(message, ...useModes) {
313
+ const colors = Object.assign(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'], {
314
+ isBG: a => !!/bg/i.exec(a),
315
+ isBright: a => !!/bright/i.exec(a),
316
+ isColor: a => {
317
+ let color = colors.find(c => new RegExp(c, 'i').exec(a));
318
+ return [!!color, colors.indexOf(color)];
319
+ },
320
+ });
321
+ const arrayifyString = s => {
322
+ if (Array.isArray(s)) {
323
+ let results = [];
324
+ for (const i of s) {
325
+ results = [...results, ...arrayifyString(i)];
326
+ }
327
+ return results.flat().filter(i => i.length);
328
+ }
329
+ if (!s || typeof s !== 'string') {
330
+ return [''];
331
+ }
332
+ else if (s.includes(',')) {
333
+ return arrayifyString(s.split(','));
334
+ }
335
+ else {
336
+ if (!colors.isColor(s)[0] && s.length > 1) {
337
+ return [...s];
338
+ }
339
+ else
340
+ return [s];
341
+ }
342
+ };
343
+ let modes = arrayifyString(useModes);
344
+ const sgrModes = {
345
+ blink: ['\x1b[5m', '\x1b[25m', 'k'],
346
+ bold: ['\x1b[1m', '\x1b[22m', 'b'],
347
+ conceal: ['\x1b[8m', '\x1b[28m', 'c'],
348
+ dim: ['\x1b[2m', '\x1b[22m', 'd'],
349
+ italics: ['\x1b[3m', '\x1b[23m', 'i'],
350
+ negative: ['\x1b[7m', '\x1b[27m', 'n'],
351
+ strike: ['\x1b[9m', '\x1b[29m', 's'],
352
+ underline: ['\x1b[4m', '\x1b[24m', 'u'],
353
+ };
354
+ Object.values(sgrModes).forEach(mode => sgrModes[mode[2]] = mode);
355
+ const codes = a => {
356
+ let open = '', close = '', mode = String(a).toLowerCase();
357
+ let [_isColor, colorIndex] = colors.isColor(mode);
358
+ if (_isColor) {
359
+ open = colors.isBG(mode)
360
+ ? `\x1b[${colors.isBright(mode) ? 10 : 4}${colorIndex}m`
361
+ : `\x1b[${colors.isBright(mode) ? 9 : 3}${colorIndex}m`;
362
+ close = colors.isBG(mode) ? '\x1b[49m' : `\x1b[39m`;
363
+ }
364
+ else if (sgrModes[mode]) {
365
+ open = sgrModes[mode][0];
366
+ close = sgrModes[mode][1];
367
+ }
368
+ return [open, close];
369
+ };
370
+ const onOrder = modes.map(key => codes(key)[0]).join('');
371
+ const offOrder = modes.map(key => codes(key)[1]).reverse().join('');
372
+ let result = Object(`${onOrder}${message}${offOrder}`);
373
+ Object.defineProperties(result, {
374
+ show: {
375
+ get() { console.log(String(this)); return this; },
376
+ enumerable: false,
377
+ },
378
+ [Symbol.for('nodejs.util.inspect.custom')]: {
379
+ value(depth, options, inspect) { return String(this); },
380
+ enumerable: false,
381
+ },
382
+ [Symbol.toStringTag]: {
383
+ get() { return "SgrString"; },
384
+ enumerable: false,
385
+ }
386
+ });
387
+ return result;
388
+ },
389
+ /**
390
+ * Wraps an object's properties into a formatted string.
391
+ *
392
+ * This method takes an object and a set of options to format the
393
+ * object's properties into a string. It allows customization of
394
+ * indentation, line endings, maximum line length, and more.
395
+ *
396
+ * @param {Object} [object=globalThis] - The object to wrap.
397
+ * @param {Object} [options={}] - The formatting options.
398
+ * @param {number} [options.indent=2] - The number of indentation
399
+ * characters to use.
400
+ * @param {string} [options.indentCharacter=' '] - The character to use
401
+ * for indentation.
402
+ * @param {Array} [options.inspector=[Object, 'getOwnPropertyNames']] -
403
+ * The inspector to use for retrieving object properties.
404
+ * @param {string} [options.lineEnding='\n'] - The line ending character.
405
+ * @param {number} [options.maxLen=78] - The maximum line length.
406
+ * @param {Function} [options.perLine=undefined] - A function to apply
407
+ * per line of output.
408
+ * @param {Function} [options.perLinePerProperty=undefined] - A function
409
+ * to apply per property per line of output.
410
+ * @param {Function} [options.preProcess=undefined] - A function to
411
+ * preprocess the object's properties.
412
+ * @param {Function} [options.preReturn=undefined] - A function to apply
413
+ * to the final output before returning.
414
+ * @param {string} [options.separator=', '] - The separator to use
415
+ * between properties.
416
+ *
417
+ * @returns {string} The formatted string representation of the object.
418
+ *
419
+ * @example
420
+ * const obj = { a: 1, b: 2, c: 3 }
421
+ * const wrapped = StringExtensions.wrap(obj, { maxLen: 20 })
422
+ * console.log(wrapped)
423
+ * // Output:
424
+ * // {
425
+ * // a: 1,
426
+ * // b: 2,
427
+ * // c: 3
428
+ * // }
429
+ */
430
+ wrap(objectOrLines, options = {
431
+ colorProperties: undefined,
432
+ indent: 2,
433
+ indentCharacter: ' ',
434
+ inspector: [Object, 'getOwnPropertyNames'],
435
+ lineEnding: '\n',
436
+ maxLen: 78,
437
+ perLine: undefined,
438
+ perLinePerProperty: undefined,
439
+ preProcess: undefined,
440
+ preReturn: undefined,
441
+ separator: ', ',
442
+ }) {
443
+ let { colorProperties = undefined, indent = options?.indent ?? 2, indentCharacter = options?.indentCharacter ?? ' ', inspector = options?.inspector ?? [Object, 'getOwnPropertyNames'], lineEnding = options?.lineEnding ?? '\n', maxLen = options?.maxLen ?? 78, perLine = options?.perLine ?? undefined, perLinePerProperty = options?.perLinePerProperty ?? undefined, preProcess = options?.preProcess ?? undefined, preReturn = options?.preReturn ?? undefined, separator = options?.separator ?? ', ', } = options ?? {};
444
+ let tab = indent === 0 ? ''
445
+ : indentCharacter.repeat(Number(indent) || 2);
446
+ maxLen = 78 - tab.length;
447
+ const sgr = this.sgr;
448
+ const validMapper = f => typeof f === 'function';
449
+ let line = [];
450
+ let getElements = inspector[0][inspector[1]];
451
+ let values = Array.isArray(objectOrLines)
452
+ ? objectOrLines : getElements(Object(objectOrLines));
453
+ if (validMapper(preProcess)) {
454
+ values = preProcess(values);
285
455
  }
286
- else {
287
- let lineElements = [...line, key];
288
- let debug = `<-- (len: ${[tab, ...line.join(separator), key, endOfLine].join('').trim().length})`;
289
- if (typeof mapLine === 'function') {
290
- lineElements = lineElements.map(mapLine);
456
+ const context = { indent, indentCharacter, lineEnding, maxLen, tab, sgr };
457
+ let finalLines = values.reduce((acc, nextProp) => {
458
+ let ifCombined = [...line, nextProp].join(separator);
459
+ if ((tab.length + ifCombined.length) <= maxLen) {
460
+ line.push(nextProp);
291
461
  }
292
- const completeLine = (tab +
293
- lineElements.slice(1).join(separator) +
294
- endOfLine).trim();
295
- acc.push(completeLine + debug);
296
- line = [];
462
+ else {
463
+ let lineProps = [...line];
464
+ if (validMapper(perLinePerProperty)) {
465
+ lineProps = lineProps.map((value, index, array) => {
466
+ return perLinePerProperty(value, index, array, context);
467
+ });
468
+ }
469
+ if (colorProperties) {
470
+ const sgrArgs = (Array.isArray(colorProperties)
471
+ ? colorProperties
472
+ : [colorProperties]);
473
+ lineProps = lineProps.map(v => sgr(v, ...sgrArgs));
474
+ }
475
+ lineProps = [tab, lineProps.join(separator)].join('');
476
+ if (validMapper(perLine)) {
477
+ lineProps = perLine(lineProps[0], 0, lineProps)?.[0] ?? lineProps[0];
478
+ }
479
+ acc.push(lineProps);
480
+ line = [];
481
+ }
482
+ return acc;
483
+ }, []);
484
+ if (validMapper(preReturn)) {
485
+ finalLines = finalLines.map((value, index, array) => {
486
+ return preReturn(value, index, array, context);
487
+ });
488
+ }
489
+ Symbol.for(`@nejs.string.wrap ${JSON.stringify({ lines: finalLines })}`);
490
+ if (lineEnding) {
491
+ finalLines = finalLines.join(lineEnding);
297
492
  }
298
- ;
299
- return acc;
300
- }, []).join(lineEnding);
493
+ return finalLines;
494
+ },
301
495
  },
302
496
  });
303
497
  const { isString: pIsString, ifString: pIfString } = exports.StringExtensions.patches;
@@ -458,6 +652,7 @@ exports.StringPrototypeExtensions = new extension_1.Patch(String.prototype, {
458
652
  // Object<->Function<->Global occurs. See original source in global.this.js
459
653
  // {@see globalThis.isThenElse}
460
654
  function isThenElse(bv, tv, ev) {
655
+ function isFunction(value) { typeof value === 'function'; }
461
656
  if (arguments.length > 1) {
462
657
  var _then = isFunction(tv) ? tv(bv) : tv;
463
658
  if (arguments.length > 2) {