@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.
- package/README.md +268 -147
- package/bin/version +100 -0
- package/dist/@nejs/basic-extensions.bundle.2.10.0.js +19 -0
- package/dist/@nejs/basic-extensions.bundle.2.10.0.js.map +7 -0
- package/dist/cjs/array.extensions.js +174 -0
- package/dist/cjs/array.extensions.js.map +1 -1
- package/dist/cjs/big.int.extension.js +1 -0
- package/dist/cjs/big.int.extension.js.map +1 -1
- package/dist/cjs/classes/descriptor.js +1 -1
- package/dist/cjs/classes/descriptor.js.map +1 -1
- package/dist/cjs/classes/index.d.ts +1 -0
- package/dist/cjs/classes/index.js +3 -0
- package/dist/cjs/classes/index.js.map +1 -1
- package/dist/cjs/classes/iterable.d.ts +44 -0
- package/dist/cjs/classes/iterable.js +64 -0
- package/dist/cjs/classes/iterable.js.map +1 -1
- package/dist/cjs/classes/param.parser.d.ts +10 -10
- package/dist/cjs/classes/property.d.ts +86 -0
- package/dist/cjs/classes/property.js +284 -0
- package/dist/cjs/classes/property.js.map +1 -0
- package/dist/cjs/classes/symkeys.d.ts +68 -11
- package/dist/cjs/classes/symkeys.js +103 -17
- package/dist/cjs/classes/symkeys.js.map +1 -1
- package/dist/cjs/classes/type.d.ts +4 -4
- package/dist/cjs/function.extensions.js +1 -0
- package/dist/cjs/function.extensions.js.map +1 -1
- package/dist/cjs/global.this.js +29 -0
- package/dist/cjs/global.this.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/json.extensions.js +22 -20
- package/dist/cjs/json.extensions.js.map +1 -1
- package/dist/cjs/map.extensions.js +1 -0
- package/dist/cjs/map.extensions.js.map +1 -1
- package/dist/cjs/number.extension.js +1 -0
- package/dist/cjs/number.extension.js.map +1 -1
- package/dist/cjs/object.extensions.d.ts +0 -29
- package/dist/cjs/object.extensions.js +218 -243
- package/dist/cjs/object.extensions.js.map +1 -1
- package/dist/cjs/set.extensions.js +1 -0
- package/dist/cjs/set.extensions.js.map +1 -1
- package/dist/cjs/string.extensions.js +478 -283
- package/dist/cjs/string.extensions.js.map +1 -1
- package/dist/cjs/symbol.extensions.js +500 -24
- package/dist/cjs/symbol.extensions.js.map +1 -1
- package/dist/cjs/utils/copy.object.d.ts +408 -0
- package/dist/cjs/utils/copy.object.js +720 -0
- package/dist/cjs/utils/copy.object.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.js +19 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/utils/toolkit.d.ts +1897 -0
- package/dist/cjs/utils/toolkit.js +1377 -0
- package/dist/cjs/utils/toolkit.js.map +1 -0
- package/dist/mjs/array.extensions.js +174 -0
- package/dist/mjs/array.extensions.js.map +1 -1
- package/dist/mjs/big.int.extension.js +1 -0
- package/dist/mjs/big.int.extension.js.map +1 -1
- package/dist/mjs/classes/descriptor.js +1 -1
- package/dist/mjs/classes/descriptor.js.map +1 -1
- package/dist/mjs/classes/index.d.ts +1 -0
- package/dist/mjs/classes/index.js +3 -0
- package/dist/mjs/classes/index.js.map +1 -1
- package/dist/mjs/classes/iterable.d.ts +44 -0
- package/dist/mjs/classes/iterable.js +64 -0
- package/dist/mjs/classes/iterable.js.map +1 -1
- package/dist/mjs/classes/param.parser.d.ts +10 -10
- package/dist/mjs/classes/property.d.ts +86 -0
- package/dist/mjs/classes/property.js +280 -0
- package/dist/mjs/classes/property.js.map +1 -0
- package/dist/mjs/classes/symkeys.d.ts +68 -11
- package/dist/mjs/classes/symkeys.js +103 -17
- package/dist/mjs/classes/symkeys.js.map +1 -1
- package/dist/mjs/classes/type.d.ts +4 -4
- package/dist/mjs/function.extensions.js +1 -0
- package/dist/mjs/function.extensions.js.map +1 -1
- package/dist/mjs/global.this.js +6 -0
- package/dist/mjs/global.this.js.map +1 -1
- package/dist/mjs/index.d.ts +2 -0
- package/dist/mjs/index.js +4 -0
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/json.extensions.js +22 -20
- package/dist/mjs/json.extensions.js.map +1 -1
- package/dist/mjs/map.extensions.js +1 -0
- package/dist/mjs/map.extensions.js.map +1 -1
- package/dist/mjs/number.extension.js +1 -0
- package/dist/mjs/number.extension.js.map +1 -1
- package/dist/mjs/object.extensions.d.ts +0 -29
- package/dist/mjs/object.extensions.js +215 -239
- package/dist/mjs/object.extensions.js.map +1 -1
- package/dist/mjs/set.extensions.js +1 -0
- package/dist/mjs/set.extensions.js.map +1 -1
- package/dist/mjs/string.extensions.js +478 -283
- package/dist/mjs/string.extensions.js.map +1 -1
- package/dist/mjs/symbol.extensions.js +501 -25
- package/dist/mjs/symbol.extensions.js.map +1 -1
- package/dist/mjs/utils/copy.object.d.ts +408 -0
- package/dist/mjs/utils/copy.object.js +702 -0
- package/dist/mjs/utils/copy.object.js.map +1 -0
- package/dist/mjs/utils/index.d.ts +1 -0
- package/dist/mjs/utils/index.js +3 -0
- package/dist/mjs/utils/index.js.map +1 -0
- package/dist/mjs/utils/toolkit.d.ts +1897 -0
- package/dist/mjs/utils/toolkit.js +1372 -0
- package/dist/mjs/utils/toolkit.js.map +1 -0
- package/package.json +30 -38
- package/repl.bootstrap.js +12 -1
- package/src/array.extensions.js +191 -1
- package/src/big.int.extension.js +3 -1
- package/src/classes/descriptor.js +1 -1
- package/src/classes/index.js +4 -0
- package/src/classes/iterable.js +74 -0
- package/src/classes/property.js +333 -0
- package/src/classes/symkeys.js +120 -19
- package/src/function.extensions.js +2 -0
- package/src/global.this.js +8 -0
- package/src/index.js +5 -0
- package/src/json.extensions.js +21 -21
- package/src/map.extensions.js +3 -1
- package/src/number.extension.js +3 -1
- package/src/object.extensions.js +240 -262
- package/src/set.extensions.js +3 -1
- package/src/string.extensions.js +531 -306
- package/src/symbol.extensions.js +529 -25
- package/src/utils/copy.object.js +780 -0
- package/src/utils/index.js +2 -0
- package/src/utils/toolkit.js +1471 -0
- package/tests/arrayextensions.test.js +2 -0
- package/tests/index.test.js +1 -0
- package/tests/newClasses/asyncIterable.test.js +2 -0
- package/tests/newClasses/deferred.test.js +5 -3
- package/tests/newClasses/descriptor.test.js +2 -0
- package/tests/newClasses/iterable.test.js +2 -0
- package/tests/newClasses/refmap.test.js +2 -1
- package/tests/newClasses/refset.test.js +2 -0
- package/tests/objectextensions.test.js +2 -0
- package/tests/setextensions.test.js +2 -0
- package/tests/stringextensions.test.js +1 -0
- package/tests/utils/toolkit.test.js +223 -0
- package/tsconfig.base.json +1 -1
- package/vitest.config.js +7 -0
- package/dist/@nejs/basic-extensions.bundle.2.7.0.js +0 -19
- package/dist/@nejs/basic-extensions.bundle.2.7.0.js.map +0 -7
- package/docs/assets/anchor.js +0 -350
- package/docs/assets/bass-addons.css +0 -12
- package/docs/assets/bass.css +0 -544
- package/docs/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/docs/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/docs/assets/fonts/LICENSE.txt +0 -93
- package/docs/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/docs/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/docs/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/docs/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/docs/assets/fonts/source-code-pro.css +0 -23
- package/docs/assets/github.css +0 -123
- package/docs/assets/site.js +0 -168
- package/docs/assets/split.css +0 -15
- package/docs/assets/split.js +0 -782
- package/docs/assets/style.css +0 -147
- 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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
287
|
-
|
|
288
|
-
let
|
|
289
|
-
if (
|
|
290
|
-
|
|
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
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
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
|
-
|
|
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) {
|