@nejs/basic-extensions 2.21.0 → 2.22.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/markdown.xml +8 -0
- package/.idea/modules.xml +8 -0
- package/.idea/ne-basic-extensions.iml +8 -0
- package/.idea/vcs.xml +6 -0
- package/CODE_STYLE.md +393 -0
- package/CODING_PHILOSOPHY.md +36 -0
- package/DOCUMENTATION_GUIDELINES.md +221 -0
- package/README.md +78 -4
- package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
- package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
- package/dist/cjs/classes/index.cjs +11129 -0
- package/dist/cjs/classes/index.cjs.map +7 -0
- package/dist/cjs/index.cjs +15191 -0
- package/dist/cjs/index.cjs.map +7 -0
- package/dist/cjs/utils/index.cjs +3954 -0
- package/dist/cjs/utils/index.cjs.map +7 -0
- package/dist/esm/basic-extensions.mjs +25 -0
- package/dist/esm/basic-extensions.mjs.map +7 -0
- package/package.json +16 -22
- package/repl.bootstrap.js +4 -7
- package/repl.history +30 -30
- package/src/big.int.extension.js +171 -45
- package/src/classes/enumeration.js +466 -0
- package/src/classes/index.js +5 -1
- package/src/index.js +5 -1
- package/src/math.extension.js +73 -0
- package/src/number.extension.js +18 -0
- package/src/regular.expression.extensions.js +0 -35
- package/src/utils/toolkit.js +699 -516
- package/tests/arrayextensions.test.js +3 -3
- package/tests/index.test.js +3 -1
- package/tests/newClasses/asyncIterable.test.js +3 -3
- package/tests/newClasses/deferred.test.js +3 -3
- package/tests/newClasses/descriptor.test.js +3 -3
- package/tests/newClasses/iterable.test.js +3 -3
- package/tests/newClasses/refmap.test.js +3 -3
- package/tests/newClasses/refset.test.js +3 -3
- package/tests/objectextensions.test.js +3 -3
- package/tests/setextensions.test.js +3 -3
- package/tests/stringextensions.test.js +3 -2
- package/tests/utils/descriptor.utils.test.js +1 -1
- package/tests/utils/toolkit.test.js +429 -163
- package/.esdoc.json +0 -9
- package/.vscode/settings.json +0 -5
- package/bin/build +0 -27
- package/bin/clean +0 -14
- package/bin/esbuild +0 -91
- package/bin/fixup +0 -13
- package/bin/repl.basics.js +0 -584
- package/bin/repl.signature.js +0 -63
- package/bin/version +0 -100
- package/dist/@nejs/basic-extensions.bundle.2.21.0.js +0 -25
- package/dist/@nejs/basic-extensions.bundle.2.21.0.js.map +0 -7
- package/dist/cjs/array.extensions.d.ts +0 -39
- package/dist/cjs/array.extensions.js +0 -477
- package/dist/cjs/array.extensions.js.map +0 -1
- package/dist/cjs/big.int.extension.d.ts +0 -31
- package/dist/cjs/big.int.extension.js +0 -165
- package/dist/cjs/big.int.extension.js.map +0 -1
- package/dist/cjs/classes/asyncIterable.d.ts +0 -126
- package/dist/cjs/classes/asyncIterable.js +0 -209
- package/dist/cjs/classes/asyncIterable.js.map +0 -1
- package/dist/cjs/classes/deferred.d.ts +0 -146
- package/dist/cjs/classes/deferred.js +0 -291
- package/dist/cjs/classes/deferred.js.map +0 -1
- package/dist/cjs/classes/descriptor.d.ts +0 -334
- package/dist/cjs/classes/descriptor.js +0 -537
- package/dist/cjs/classes/descriptor.js.map +0 -1
- package/dist/cjs/classes/enum.d.ts +0 -50
- package/dist/cjs/classes/enum.js +0 -405
- package/dist/cjs/classes/enum.js.map +0 -1
- package/dist/cjs/classes/index.d.ts +0 -15
- package/dist/cjs/classes/index.js +0 -63
- package/dist/cjs/classes/index.js.map +0 -1
- package/dist/cjs/classes/introspector.d.ts +0 -20
- package/dist/cjs/classes/introspector.js +0 -130
- package/dist/cjs/classes/introspector.js.map +0 -1
- package/dist/cjs/classes/iterable.d.ts +0 -169
- package/dist/cjs/classes/iterable.js +0 -268
- package/dist/cjs/classes/iterable.js.map +0 -1
- package/dist/cjs/classes/param.parser.d.ts +0 -221
- package/dist/cjs/classes/param.parser.js +0 -242
- package/dist/cjs/classes/param.parser.js.map +0 -1
- package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
- package/dist/cjs/classes/pluggable.proxy.js +0 -444
- package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
- package/dist/cjs/classes/property.d.ts +0 -79
- package/dist/cjs/classes/property.js +0 -284
- package/dist/cjs/classes/property.js.map +0 -1
- package/dist/cjs/classes/refmap.d.ts +0 -238
- package/dist/cjs/classes/refmap.js +0 -421
- package/dist/cjs/classes/refmap.js.map +0 -1
- package/dist/cjs/classes/refset.d.ts +0 -186
- package/dist/cjs/classes/refset.js +0 -370
- package/dist/cjs/classes/refset.js.map +0 -1
- package/dist/cjs/classes/symkeys.d.ts +0 -349
- package/dist/cjs/classes/symkeys.js +0 -510
- package/dist/cjs/classes/symkeys.js.map +0 -1
- package/dist/cjs/classes/type.d.ts +0 -56
- package/dist/cjs/classes/type.js +0 -405
- package/dist/cjs/classes/type.js.map +0 -1
- package/dist/cjs/function.extensions.d.ts +0 -12
- package/dist/cjs/function.extensions.js +0 -758
- package/dist/cjs/function.extensions.js.map +0 -1
- package/dist/cjs/global.this.d.ts +0 -2
- package/dist/cjs/global.this.js +0 -300
- package/dist/cjs/global.this.js.map +0 -1
- package/dist/cjs/index.d.ts +0 -31
- package/dist/cjs/index.js +0 -226
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/json.extensions.d.ts +0 -2
- package/dist/cjs/json.extensions.js +0 -109
- package/dist/cjs/json.extensions.js.map +0 -1
- package/dist/cjs/map.extensions.d.ts +0 -3
- package/dist/cjs/map.extensions.js +0 -143
- package/dist/cjs/map.extensions.js.map +0 -1
- package/dist/cjs/number.extension.d.ts +0 -44
- package/dist/cjs/number.extension.js +0 -261
- package/dist/cjs/number.extension.js.map +0 -1
- package/dist/cjs/object.extensions.d.ts +0 -33
- package/dist/cjs/object.extensions.js +0 -1091
- package/dist/cjs/object.extensions.js.map +0 -1
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/proxy.extensions.d.ts +0 -2
- package/dist/cjs/proxy.extensions.js +0 -207
- package/dist/cjs/proxy.extensions.js.map +0 -1
- package/dist/cjs/reflect.extensions.d.ts +0 -14
- package/dist/cjs/reflect.extensions.js +0 -316
- package/dist/cjs/reflect.extensions.js.map +0 -1
- package/dist/cjs/regular.expression.extensions.d.ts +0 -2
- package/dist/cjs/regular.expression.extensions.js +0 -423
- package/dist/cjs/regular.expression.extensions.js.map +0 -1
- package/dist/cjs/set.extensions.d.ts +0 -40
- package/dist/cjs/set.extensions.js +0 -355
- package/dist/cjs/set.extensions.js.map +0 -1
- package/dist/cjs/string.extensions.d.ts +0 -23
- package/dist/cjs/string.extensions.js +0 -704
- package/dist/cjs/string.extensions.js.map +0 -1
- package/dist/cjs/symbol.extensions.d.ts +0 -11
- package/dist/cjs/symbol.extensions.js +0 -735
- package/dist/cjs/symbol.extensions.js.map +0 -1
- package/dist/cjs/utils/copy.object.d.ts +0 -408
- package/dist/cjs/utils/copy.object.js +0 -720
- package/dist/cjs/utils/copy.object.js.map +0 -1
- package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
- package/dist/cjs/utils/descriptor.utils.js +0 -889
- package/dist/cjs/utils/descriptor.utils.js.map +0 -1
- package/dist/cjs/utils/index.d.ts +0 -75
- package/dist/cjs/utils/index.js +0 -61
- package/dist/cjs/utils/index.js.map +0 -1
- package/dist/cjs/utils/stdout.d.ts +0 -742
- package/dist/cjs/utils/stdout.js +0 -1042
- package/dist/cjs/utils/stdout.js.map +0 -1
- package/dist/cjs/utils/toolkit.d.ts +0 -1898
- package/dist/cjs/utils/toolkit.js +0 -1378
- package/dist/cjs/utils/toolkit.js.map +0 -1
- package/dist/cjs/weakref.extensions.d.ts +0 -2
- package/dist/cjs/weakref.extensions.js +0 -19
- package/dist/cjs/weakref.extensions.js.map +0 -1
- package/dist/mjs/array.extensions.d.ts +0 -39
- package/dist/mjs/array.extensions.js +0 -474
- package/dist/mjs/array.extensions.js.map +0 -1
- package/dist/mjs/big.int.extension.d.ts +0 -31
- package/dist/mjs/big.int.extension.js +0 -162
- package/dist/mjs/big.int.extension.js.map +0 -1
- package/dist/mjs/classes/asyncIterable.d.ts +0 -126
- package/dist/mjs/classes/asyncIterable.js +0 -204
- package/dist/mjs/classes/asyncIterable.js.map +0 -1
- package/dist/mjs/classes/deferred.d.ts +0 -146
- package/dist/mjs/classes/deferred.js +0 -287
- package/dist/mjs/classes/deferred.js.map +0 -1
- package/dist/mjs/classes/descriptor.d.ts +0 -334
- package/dist/mjs/classes/descriptor.js +0 -533
- package/dist/mjs/classes/descriptor.js.map +0 -1
- package/dist/mjs/classes/enum.d.ts +0 -50
- package/dist/mjs/classes/enum.js +0 -400
- package/dist/mjs/classes/enum.js.map +0 -1
- package/dist/mjs/classes/index.d.ts +0 -15
- package/dist/mjs/classes/index.js +0 -46
- package/dist/mjs/classes/index.js.map +0 -1
- package/dist/mjs/classes/introspector.d.ts +0 -20
- package/dist/mjs/classes/introspector.js +0 -126
- package/dist/mjs/classes/introspector.js.map +0 -1
- package/dist/mjs/classes/iterable.d.ts +0 -169
- package/dist/mjs/classes/iterable.js +0 -263
- package/dist/mjs/classes/iterable.js.map +0 -1
- package/dist/mjs/classes/param.parser.d.ts +0 -221
- package/dist/mjs/classes/param.parser.js +0 -238
- package/dist/mjs/classes/param.parser.js.map +0 -1
- package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
- package/dist/mjs/classes/pluggable.proxy.js +0 -438
- package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
- package/dist/mjs/classes/property.d.ts +0 -79
- package/dist/mjs/classes/property.js +0 -280
- package/dist/mjs/classes/property.js.map +0 -1
- package/dist/mjs/classes/refmap.d.ts +0 -238
- package/dist/mjs/classes/refmap.js +0 -417
- package/dist/mjs/classes/refmap.js.map +0 -1
- package/dist/mjs/classes/refset.d.ts +0 -186
- package/dist/mjs/classes/refset.js +0 -366
- package/dist/mjs/classes/refset.js.map +0 -1
- package/dist/mjs/classes/symkeys.d.ts +0 -349
- package/dist/mjs/classes/symkeys.js +0 -506
- package/dist/mjs/classes/symkeys.js.map +0 -1
- package/dist/mjs/classes/type.d.ts +0 -56
- package/dist/mjs/classes/type.js +0 -401
- package/dist/mjs/classes/type.js.map +0 -1
- package/dist/mjs/function.extensions.d.ts +0 -12
- package/dist/mjs/function.extensions.js +0 -755
- package/dist/mjs/function.extensions.js.map +0 -1
- package/dist/mjs/global.this.d.ts +0 -2
- package/dist/mjs/global.this.js +0 -264
- package/dist/mjs/global.this.js.map +0 -1
- package/dist/mjs/index.d.ts +0 -31
- package/dist/mjs/index.js +0 -204
- package/dist/mjs/index.js.map +0 -1
- package/dist/mjs/json.extensions.d.ts +0 -2
- package/dist/mjs/json.extensions.js +0 -106
- package/dist/mjs/json.extensions.js.map +0 -1
- package/dist/mjs/map.extensions.d.ts +0 -3
- package/dist/mjs/map.extensions.js +0 -140
- package/dist/mjs/map.extensions.js.map +0 -1
- package/dist/mjs/number.extension.d.ts +0 -44
- package/dist/mjs/number.extension.js +0 -258
- package/dist/mjs/number.extension.js.map +0 -1
- package/dist/mjs/object.extensions.d.ts +0 -33
- package/dist/mjs/object.extensions.js +0 -1088
- package/dist/mjs/object.extensions.js.map +0 -1
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/proxy.extensions.d.ts +0 -2
- package/dist/mjs/proxy.extensions.js +0 -204
- package/dist/mjs/proxy.extensions.js.map +0 -1
- package/dist/mjs/reflect.extensions.d.ts +0 -14
- package/dist/mjs/reflect.extensions.js +0 -313
- package/dist/mjs/reflect.extensions.js.map +0 -1
- package/dist/mjs/regular.expression.extensions.d.ts +0 -2
- package/dist/mjs/regular.expression.extensions.js +0 -420
- package/dist/mjs/regular.expression.extensions.js.map +0 -1
- package/dist/mjs/set.extensions.d.ts +0 -40
- package/dist/mjs/set.extensions.js +0 -352
- package/dist/mjs/set.extensions.js.map +0 -1
- package/dist/mjs/string.extensions.d.ts +0 -23
- package/dist/mjs/string.extensions.js +0 -701
- package/dist/mjs/string.extensions.js.map +0 -1
- package/dist/mjs/symbol.extensions.d.ts +0 -11
- package/dist/mjs/symbol.extensions.js +0 -732
- package/dist/mjs/symbol.extensions.js.map +0 -1
- package/dist/mjs/utils/copy.object.d.ts +0 -408
- package/dist/mjs/utils/copy.object.js +0 -702
- package/dist/mjs/utils/copy.object.js.map +0 -1
- package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
- package/dist/mjs/utils/descriptor.utils.js +0 -875
- package/dist/mjs/utils/descriptor.utils.js.map +0 -1
- package/dist/mjs/utils/index.d.ts +0 -75
- package/dist/mjs/utils/index.js +0 -45
- package/dist/mjs/utils/index.js.map +0 -1
- package/dist/mjs/utils/stdout.d.ts +0 -742
- package/dist/mjs/utils/stdout.js +0 -1037
- package/dist/mjs/utils/stdout.js.map +0 -1
- package/dist/mjs/utils/toolkit.d.ts +0 -1898
- package/dist/mjs/utils/toolkit.js +0 -1373
- package/dist/mjs/utils/toolkit.js.map +0 -1
- package/dist/mjs/weakref.extensions.d.ts +0 -2
- package/dist/mjs/weakref.extensions.js +0 -16
- package/dist/mjs/weakref.extensions.js.map +0 -1
- package/jsdoc-config.json +0 -31
- package/tsconfig.base.json +0 -28
- package/tsconfig.cjs.json +0 -8
- package/tsconfig.esm.json +0 -8
- package/vitest.config.js +0 -7
|
@@ -1,701 +0,0 @@
|
|
|
1
|
-
import { Patch } from '@nejs/extension';
|
|
2
|
-
const parenthesisPair = ['(', ')'];
|
|
3
|
-
/**
|
|
4
|
-
* `StringExtensions` is a patch for the JavaScript built-in `String` class. It
|
|
5
|
-
* adds utility methods to the `String` class without modifying the global namespace
|
|
6
|
-
* directly. This patch includes methods for key validation, object type checking,
|
|
7
|
-
* and retrieving the string tag of an object. These methods are useful for
|
|
8
|
-
* enhancing the capabilities of the standard `String` class with additional
|
|
9
|
-
* utility functions.
|
|
10
|
-
*/
|
|
11
|
-
export const StringExtensions = new Patch(String, {
|
|
12
|
-
[Patch.kMutablyHidden]: {
|
|
13
|
-
/**
|
|
14
|
-
* The `isString` method does exactly what one would it expect. It returns
|
|
15
|
-
* true if the string matches typeof or instanceof as a string.
|
|
16
|
-
*
|
|
17
|
-
* @param {*} value checks to see if the `value` is a string
|
|
18
|
-
* @returns {boolean} `true` if it is a `String`, `false` otherwise
|
|
19
|
-
*/
|
|
20
|
-
isString(value) {
|
|
21
|
-
return (value !== null && value !== undefined &&
|
|
22
|
-
(typeof value === 'string' || value instanceof String));
|
|
23
|
-
},
|
|
24
|
-
/**
|
|
25
|
-
* Conditionally returns a value based on whether the supplied
|
|
26
|
-
* `value` is a `String` or not. If the `value` is a `String`,
|
|
27
|
-
* the `thenValue` will be returned. If it is not a `String`,
|
|
28
|
-
* the `elseValue` will be returned instead.
|
|
29
|
-
*
|
|
30
|
-
* @param {any} value the value to check to determine if it is a
|
|
31
|
-
* `String`
|
|
32
|
-
* @param {any} thenValue the value to return if the supplied
|
|
33
|
-
* `value` is a `String`
|
|
34
|
-
* @param {any} elseValue the value to return if the supplied
|
|
35
|
-
* `value` is not a `String`
|
|
36
|
-
* @returns {any} either the `thenValue` or `elseValue` depending
|
|
37
|
-
* on if the supplied `value` is a `String`
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* const str = 'hello'
|
|
41
|
-
* const num = 42
|
|
42
|
-
* ifString(str, 'is a string', 'not a string') // 'is a string'
|
|
43
|
-
* ifString(num, 'is a string', 'not a string') // 'not a string'
|
|
44
|
-
*/
|
|
45
|
-
ifString(value, thenValue, elseValue) {
|
|
46
|
-
return isThenElse(String.isString(value), thenValue, elseValue);
|
|
47
|
-
},
|
|
48
|
-
/**
|
|
49
|
-
* A getter property that returns a pair of parentheses as an array.
|
|
50
|
-
* This property can be used when operations require a clear distinction
|
|
51
|
-
* between the opening and closing parentheses, such as parsing or
|
|
52
|
-
* matching balanced expressions in strings.
|
|
53
|
-
*
|
|
54
|
-
* @returns {[string, string]} An array containing a pair of strings: the
|
|
55
|
-
* opening parenthesis '(' as the first element, and the closing parenthesis
|
|
56
|
-
* ')' as the second element.
|
|
57
|
-
*/
|
|
58
|
-
get parenthesisPair() {
|
|
59
|
-
return ['(', ')'];
|
|
60
|
-
},
|
|
61
|
-
/**
|
|
62
|
-
* A getter property that returns a pair of square brackets as an array.
|
|
63
|
-
* This property is particularly useful for operations that require a clear
|
|
64
|
-
* distinction between the opening and closing square brackets, such as
|
|
65
|
-
* parsing arrays in strings or matching balanced expressions within
|
|
66
|
-
* square brackets.
|
|
67
|
-
*
|
|
68
|
-
* @returns {[string, string]} An array containing a pair of strings: the
|
|
69
|
-
* opening square bracket '[' as the first element, and the closing square
|
|
70
|
-
* bracket ']' as the second element.
|
|
71
|
-
*/
|
|
72
|
-
get squareBracketsPair() {
|
|
73
|
-
return ['[', ']'];
|
|
74
|
-
},
|
|
75
|
-
/**
|
|
76
|
-
* A getter property that returns a pair of curly brackets as an array.
|
|
77
|
-
* This property is particularly useful for operations that require a clear
|
|
78
|
-
* distinction between the opening and closing curly brackets, such as
|
|
79
|
-
* parsing objects in strings or matching balanced expressions within
|
|
80
|
-
* curly brackets. The returned array consists of the opening curly bracket
|
|
81
|
-
* '{' as the first element, and the closing curly bracket '}' as the
|
|
82
|
-
* second element.
|
|
83
|
-
*
|
|
84
|
-
* @returns {[string, string]} An array containing a pair of strings: the
|
|
85
|
-
* opening curly bracket '{' as the first element, and the closing curly
|
|
86
|
-
* bracket '}' as the second element.
|
|
87
|
-
*/
|
|
88
|
-
get curlyBracketsPair() {
|
|
89
|
-
return ['{', '}'];
|
|
90
|
-
},
|
|
91
|
-
/**
|
|
92
|
-
* Generates a random string using base 36 (numbers and lowercase letters).
|
|
93
|
-
* This method is useful when you need a random string that includes both
|
|
94
|
-
* numbers and letters. The generated string does not include the leading
|
|
95
|
-
* '0.' that is part of the string representation of a random number in
|
|
96
|
-
* base 36.
|
|
97
|
-
*
|
|
98
|
-
* @returns {string} A random string of characters in base 36.
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* const randomStr = StringExtensions.random36();
|
|
102
|
-
* console.log(randomStr); // Output: "3n5yzxjkf2o"
|
|
103
|
-
*/
|
|
104
|
-
random36() {
|
|
105
|
-
return Math.random().toString(36).slice(2);
|
|
106
|
-
},
|
|
107
|
-
/**
|
|
108
|
-
* Generates a random string using base 16 (hexadecimal numbers).
|
|
109
|
-
* This method is useful when you need a random string that includes both
|
|
110
|
-
* numbers and letters in hexadecimal format. The generated string does not
|
|
111
|
-
* include the leading '0.' that is part of the string representation of a
|
|
112
|
-
* random number in base 16.
|
|
113
|
-
*
|
|
114
|
-
* @returns {string} A random string of characters in base 16.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* const randomStr = StringExtensions.random16();
|
|
118
|
-
* console.log(randomStr); // Output: "3a5f4c"
|
|
119
|
-
*/
|
|
120
|
-
random16() {
|
|
121
|
-
return Math.random().toString(16).slice(2);
|
|
122
|
-
},
|
|
123
|
-
/**
|
|
124
|
-
* Generates a random RGB color code.
|
|
125
|
-
*
|
|
126
|
-
* This method generates a random hexadecimal number, slices off the
|
|
127
|
-
* leading '0.' and takes the first 6 characters. It then pads the
|
|
128
|
-
* end of the string with '0' until it is 6 characters long. The
|
|
129
|
-
* result is a string that can be used as a color code in CSS.
|
|
130
|
-
*
|
|
131
|
-
* @param {string} [prefix='#'] - The prefix to prepend to the color
|
|
132
|
-
* code. Defaults to '#'.
|
|
133
|
-
*
|
|
134
|
-
* @returns {string} A random RGB color code.
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* const randomColor = StringExtensions.randomRGB();
|
|
138
|
-
* console.log(randomColor); // Output: "#3a5f4c"
|
|
139
|
-
*/
|
|
140
|
-
randomRGBHex(prefix = '#') {
|
|
141
|
-
const hex = Math.random().toString(16).slice(2).substring(0, 6);
|
|
142
|
-
return `${prefix}${hex.padEnd(6, '0')}`;
|
|
143
|
-
},
|
|
144
|
-
/**
|
|
145
|
-
* Generates a random ARGB color code.
|
|
146
|
-
*
|
|
147
|
-
* This method generates a random hexadecimal number, slices off the
|
|
148
|
-
* leading '0.' and takes the first 8 characters. It then pads the
|
|
149
|
-
* start of the string with '0' until it is 6 characters long and the
|
|
150
|
-
* end of the string with '0' until it is 8 characters long. The
|
|
151
|
-
* result is a string that can be used as a color code in CSS.
|
|
152
|
-
*
|
|
153
|
-
* @param {string} [prefix='#'] - The prefix to prepend to the color
|
|
154
|
-
* code. Defaults to '#'.
|
|
155
|
-
*
|
|
156
|
-
* @returns {string} A random ARGB color code.
|
|
157
|
-
*
|
|
158
|
-
* @example
|
|
159
|
-
* const randomColor = StringExtensions.randomARGB();
|
|
160
|
-
* console.log(randomColor); // Output: "#3a5f4c00"
|
|
161
|
-
*/
|
|
162
|
-
randomARGBHex(prefix = '#') {
|
|
163
|
-
const hex = Math.random().toString(16).slice(2).substring(0, 8);
|
|
164
|
-
return `${prefix}${hex.padStart(6, '0').padEnd(8, '0')}`;
|
|
165
|
-
},
|
|
166
|
-
/**
|
|
167
|
-
* Generates a random RGBA color code.
|
|
168
|
-
*
|
|
169
|
-
* This method generates a random hexadecimal number, slices off the
|
|
170
|
-
* leading '0.' and takes the first 8 characters. It then pads the
|
|
171
|
-
* start of the string with '0' until it is 6 characters long and the
|
|
172
|
-
* end of the string with '0' until it is 8 characters long. The
|
|
173
|
-
* result is a string that can be used as a color code in CSS.
|
|
174
|
-
*
|
|
175
|
-
* @param {string} [prefix='#'] - The prefix to prepend to the color
|
|
176
|
-
* code. Defaults to '#'.
|
|
177
|
-
*
|
|
178
|
-
* @returns {string} A random RGBA color code.
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* const randomColor = StringExtensions.randomRGBA();
|
|
182
|
-
* console.log(randomColor); // Output: "#3a5f4c00"
|
|
183
|
-
*/
|
|
184
|
-
randomRGBAHex(prefix = '#') {
|
|
185
|
-
const hex = Math.random().toString(16).slice(2).substring(0, 8);
|
|
186
|
-
return `${prefix}${hex.padStart(6, '0').padStart(8, '0')}`;
|
|
187
|
-
},
|
|
188
|
-
/**
|
|
189
|
-
* Generates a random RGB color code.
|
|
190
|
-
*
|
|
191
|
-
* This method generates a random hexadecimal number, slices off the
|
|
192
|
-
* leading '0.' and pads the end of the string with '0' until it is
|
|
193
|
-
* 8 characters long. It then parses the first 6 characters into
|
|
194
|
-
* three separate 2-character strings, each representing a color
|
|
195
|
-
* component (red, green, blue) in hexadecimal format. These strings
|
|
196
|
-
* are then converted into decimal format and used to construct an
|
|
197
|
-
* RGB color code.
|
|
198
|
-
*
|
|
199
|
-
* @returns {string} A random RGB color code.
|
|
200
|
-
*
|
|
201
|
-
* @example
|
|
202
|
-
* const randomColor = StringExtensions.randomRGB();
|
|
203
|
-
* console.log(randomColor); // Output: "rgb(58,95,76)"
|
|
204
|
-
*/
|
|
205
|
-
randomRGB() {
|
|
206
|
-
const hex = Math.random().toString(16).slice(2).padEnd(8, '0');
|
|
207
|
-
const red = parseInt(hex.substring(0, 2), 16);
|
|
208
|
-
const green = parseInt(hex.substring(2, 4), 16);
|
|
209
|
-
const blue = parseInt(hex.substring(4, 6), 16);
|
|
210
|
-
return `rgb(${red}, ${green}, ${blue})`;
|
|
211
|
-
},
|
|
212
|
-
/**
|
|
213
|
-
* Generates a random RGBA color code with optional forced color values.
|
|
214
|
-
*
|
|
215
|
-
* This method generates a random hexadecimal number, slices off the
|
|
216
|
-
* leading '0.' and pads the end of the string with '0' until it is
|
|
217
|
-
* 8 characters long. It then parses the first 8 characters into
|
|
218
|
-
* four separate 2-character strings, each representing a color
|
|
219
|
-
* component (red, green, blue, alpha) in hexadecimal format. These strings
|
|
220
|
-
* are then converted into decimal format and used to construct an
|
|
221
|
-
* RGBA color code.
|
|
222
|
-
*
|
|
223
|
-
* If a color component is provided in the `force` parameter, it will
|
|
224
|
-
* be used instead of a random value for that component.
|
|
225
|
-
*
|
|
226
|
-
* @param {Object} force - An object with properties for each color
|
|
227
|
-
* component (red, green, blue, alpha) that should be forced to a
|
|
228
|
-
* specific value. If a property is undefined or not provided, a
|
|
229
|
-
* random value will be used for that component.
|
|
230
|
-
* @param {number} force.red - The red component (0-255).
|
|
231
|
-
* @param {number} force.green - The green component (0-255).
|
|
232
|
-
* @param {number} force.blue - The blue component (0-255).
|
|
233
|
-
* @param {number} force.alpha - The alpha component (0.0-1.0).
|
|
234
|
-
*
|
|
235
|
-
* @returns {string} A random RGBA color code.
|
|
236
|
-
*
|
|
237
|
-
* @example
|
|
238
|
-
* const randomColor = StringExtensions.randomRGBA();
|
|
239
|
-
* console.log(randomColor); // Output: "rgba(58,95,76,0.50)"
|
|
240
|
-
*
|
|
241
|
-
* const forcedGreen = StringExtensions.randomRGBA({ green: 255 });
|
|
242
|
-
* console.log(forcedGreen); // Output: "rgba(58,255,76,0.50)"
|
|
243
|
-
*/
|
|
244
|
-
randomRGBA(force = {
|
|
245
|
-
red: undefined,
|
|
246
|
-
green: undefined,
|
|
247
|
-
blue: undefined,
|
|
248
|
-
alpha: undefined
|
|
249
|
-
}) {
|
|
250
|
-
const hex = Math.random().toString(16).slice(2).padEnd(8, '0');
|
|
251
|
-
const red = force.red ?? parseInt(hex.substring(0, 2), 16);
|
|
252
|
-
const green = force.green ?? parseInt(hex.substring(2, 4), 16);
|
|
253
|
-
const blue = force.blue ?? parseInt(hex.substring(4, 6), 16);
|
|
254
|
-
const alpha = force.alpha ??
|
|
255
|
-
(parseInt(hex.substring(6, 8), 16) / 255.0) * 1.0;
|
|
256
|
-
return `rgba(${red}, ${green}, ${blue}, ${alpha.toFixed(2)})`;
|
|
257
|
-
},
|
|
258
|
-
/**
|
|
259
|
-
* Applies Select Graphic Rendition (SGR) parameters to a given message for
|
|
260
|
-
* styling in terminal environments. This function allows for the dynamic
|
|
261
|
-
* styling of text output using ANSI escape codes. It supports a variety of
|
|
262
|
-
* modes such as color, brightness, and text decorations like bold or underline.
|
|
263
|
-
*
|
|
264
|
-
* @param {string} message The message to be styled.
|
|
265
|
-
* @param {...string} useModes A series of strings representing the desired
|
|
266
|
-
* styling modes. Modes can include colors (e.g., 'red', 'blue'), brightness
|
|
267
|
-
* ('bright'), foreground/background ('fg', 'bg'), and text decorations
|
|
268
|
-
* ('bold', 'underline'). Modes can be combined in a single string using
|
|
269
|
-
* commas or passed as separate arguments.
|
|
270
|
-
*
|
|
271
|
-
* Colors:
|
|
272
|
-
* ```
|
|
273
|
-
* 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
|
|
274
|
-
* ```
|
|
275
|
-
* Color Specifiers:
|
|
276
|
-
* ```
|
|
277
|
-
* 'fg' -> foreground | 'bg' -> background | 'bright' -> bright colors
|
|
278
|
-
* ```
|
|
279
|
-
*
|
|
280
|
-
* Modes:
|
|
281
|
-
* ```
|
|
282
|
-
* 'blink' or 'k' | 'conceal' or 'c' | 'italics' or 'i' | 'strike' or 's'
|
|
283
|
-
* 'bold' or 'b' | 'dim' or 'd' | 'negative' or 'n' | 'underline' or 'u'
|
|
284
|
-
* ```
|
|
285
|
-
*
|
|
286
|
-
* Examples:
|
|
287
|
-
* - `sgr('Hello', 'red')` applies red color to 'Hello'.
|
|
288
|
-
* - `sgr('World', 'green,bold')` applies green color and bold styling
|
|
289
|
-
* to 'World'.
|
|
290
|
-
* - `sgr('Example', 'bluebgbright')` applies bright blue
|
|
291
|
-
* background color.
|
|
292
|
-
*
|
|
293
|
-
* Short hand syntax is also allowed:
|
|
294
|
-
* - `sgr('hello', 'biu')` applies bold, italics and underline
|
|
295
|
-
* - `sgr('hello', 'bi,redfg')` applies bold, italics and red foreground
|
|
296
|
-
*
|
|
297
|
-
* As a bonus, there is a secret getter applied to the return string that
|
|
298
|
-
* allows you to invoke `sgr(...).show` to automatically log the output to
|
|
299
|
-
* `console.log`. This is done by wrapping the output string in `Object()`
|
|
300
|
-
* to make it a `String` instance and then adding the property descriptor.
|
|
301
|
-
* A custom `Symbol` is applied to make it evaluate in nodejs as though it
|
|
302
|
-
* were a normal string. To strip the extras, wrap the output in `String()`
|
|
303
|
-
*
|
|
304
|
-
* @returns {string} The message wrapped in ANSI escape codes corresponding
|
|
305
|
-
* to the specified modes. The returned string, when printed to a terminal,
|
|
306
|
-
* displays the styled message. Additional properties are attached to the
|
|
307
|
-
* result for utility purposes, such as 'show' for immediate console output.
|
|
308
|
-
*/
|
|
309
|
-
sgr(message, ...useModes) {
|
|
310
|
-
const colors = Object.assign(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'], {
|
|
311
|
-
isBG: a => !!/bg/i.exec(a),
|
|
312
|
-
isBright: a => !!/bright/i.exec(a),
|
|
313
|
-
isColor: a => {
|
|
314
|
-
let color = colors.find(c => new RegExp(c, 'i').exec(a));
|
|
315
|
-
return [!!color, colors.indexOf(color)];
|
|
316
|
-
},
|
|
317
|
-
});
|
|
318
|
-
const arrayifyString = s => {
|
|
319
|
-
if (Array.isArray(s)) {
|
|
320
|
-
let results = [];
|
|
321
|
-
for (const i of s) {
|
|
322
|
-
results = [...results, ...arrayifyString(i)];
|
|
323
|
-
}
|
|
324
|
-
return results.flat().filter(i => i.length);
|
|
325
|
-
}
|
|
326
|
-
if (!s || typeof s !== 'string') {
|
|
327
|
-
return [''];
|
|
328
|
-
}
|
|
329
|
-
else if (s.includes(',')) {
|
|
330
|
-
return arrayifyString(s.split(','));
|
|
331
|
-
}
|
|
332
|
-
else {
|
|
333
|
-
if (!colors.isColor(s)[0] && s.length > 1) {
|
|
334
|
-
return [...s];
|
|
335
|
-
}
|
|
336
|
-
else
|
|
337
|
-
return [s];
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
let modes = arrayifyString(useModes);
|
|
341
|
-
const sgrModes = {
|
|
342
|
-
blink: ['\x1b[5m', '\x1b[25m', 'k'],
|
|
343
|
-
bold: ['\x1b[1m', '\x1b[22m', 'b'],
|
|
344
|
-
conceal: ['\x1b[8m', '\x1b[28m', 'c'],
|
|
345
|
-
dim: ['\x1b[2m', '\x1b[22m', 'd'],
|
|
346
|
-
italics: ['\x1b[3m', '\x1b[23m', 'i'],
|
|
347
|
-
negative: ['\x1b[7m', '\x1b[27m', 'n'],
|
|
348
|
-
strike: ['\x1b[9m', '\x1b[29m', 's'],
|
|
349
|
-
underline: ['\x1b[4m', '\x1b[24m', 'u'],
|
|
350
|
-
};
|
|
351
|
-
Object.values(sgrModes).forEach(mode => sgrModes[mode[2]] = mode);
|
|
352
|
-
const codes = a => {
|
|
353
|
-
let open = '', close = '', mode = String(a).toLowerCase();
|
|
354
|
-
let [_isColor, colorIndex] = colors.isColor(mode);
|
|
355
|
-
if (_isColor) {
|
|
356
|
-
open = colors.isBG(mode)
|
|
357
|
-
? `\x1b[${colors.isBright(mode) ? 10 : 4}${colorIndex}m`
|
|
358
|
-
: `\x1b[${colors.isBright(mode) ? 9 : 3}${colorIndex}m`;
|
|
359
|
-
close = colors.isBG(mode) ? '\x1b[49m' : `\x1b[39m`;
|
|
360
|
-
}
|
|
361
|
-
else if (sgrModes[mode]) {
|
|
362
|
-
open = sgrModes[mode][0];
|
|
363
|
-
close = sgrModes[mode][1];
|
|
364
|
-
}
|
|
365
|
-
return [open, close];
|
|
366
|
-
};
|
|
367
|
-
const onOrder = modes.map(key => codes(key)[0]).join('');
|
|
368
|
-
const offOrder = modes.map(key => codes(key)[1]).reverse().join('');
|
|
369
|
-
let result = Object(`${onOrder}${message}${offOrder}`);
|
|
370
|
-
Object.defineProperties(result, {
|
|
371
|
-
show: {
|
|
372
|
-
get() { console.log(String(this)); return this; },
|
|
373
|
-
enumerable: false,
|
|
374
|
-
},
|
|
375
|
-
[Symbol.for('nodejs.util.inspect.custom')]: {
|
|
376
|
-
value(depth, options, inspect) { return String(this); },
|
|
377
|
-
enumerable: false,
|
|
378
|
-
},
|
|
379
|
-
[Symbol.toStringTag]: {
|
|
380
|
-
get() { return "SgrString"; },
|
|
381
|
-
enumerable: false,
|
|
382
|
-
}
|
|
383
|
-
});
|
|
384
|
-
return result;
|
|
385
|
-
},
|
|
386
|
-
/**
|
|
387
|
-
* Wraps an object's properties into a formatted string.
|
|
388
|
-
*
|
|
389
|
-
* This method takes an object and a set of options to format the
|
|
390
|
-
* object's properties into a string. It allows customization of
|
|
391
|
-
* indentation, line endings, maximum line length, and more.
|
|
392
|
-
*
|
|
393
|
-
* @param {Object} [object=globalThis] - The object to wrap.
|
|
394
|
-
* @param {Object} [options={}] - The formatting options.
|
|
395
|
-
* @param {number} [options.indent=2] - The number of indentation
|
|
396
|
-
* characters to use.
|
|
397
|
-
* @param {string} [options.indentCharacter=' '] - The character to use
|
|
398
|
-
* for indentation.
|
|
399
|
-
* @param {Array} [options.inspector=[Object, 'getOwnPropertyNames']] -
|
|
400
|
-
* The inspector to use for retrieving object properties.
|
|
401
|
-
* @param {string} [options.lineEnding='\n'] - The line ending character.
|
|
402
|
-
* @param {number} [options.maxLen=78] - The maximum line length.
|
|
403
|
-
* @param {Function} [options.perLine=undefined] - A function to apply
|
|
404
|
-
* per line of output.
|
|
405
|
-
* @param {Function} [options.perLinePerProperty=undefined] - A function
|
|
406
|
-
* to apply per property per line of output.
|
|
407
|
-
* @param {Function} [options.preProcess=undefined] - A function to
|
|
408
|
-
* preprocess the object's properties.
|
|
409
|
-
* @param {Function} [options.preReturn=undefined] - A function to apply
|
|
410
|
-
* to the final output before returning.
|
|
411
|
-
* @param {string} [options.separator=', '] - The separator to use
|
|
412
|
-
* between properties.
|
|
413
|
-
*
|
|
414
|
-
* @returns {string} The formatted string representation of the object.
|
|
415
|
-
*
|
|
416
|
-
* @example
|
|
417
|
-
* const obj = { a: 1, b: 2, c: 3 }
|
|
418
|
-
* const wrapped = StringExtensions.wrap(obj, { maxLen: 20 })
|
|
419
|
-
* console.log(wrapped)
|
|
420
|
-
* // Output:
|
|
421
|
-
* // {
|
|
422
|
-
* // a: 1,
|
|
423
|
-
* // b: 2,
|
|
424
|
-
* // c: 3
|
|
425
|
-
* // }
|
|
426
|
-
*/
|
|
427
|
-
wrap(objectOrLines, options = {
|
|
428
|
-
colorProperties: undefined,
|
|
429
|
-
indent: 2,
|
|
430
|
-
indentCharacter: ' ',
|
|
431
|
-
inspector: [Object, 'getOwnPropertyNames'],
|
|
432
|
-
lineEnding: '\n',
|
|
433
|
-
maxLen: 78,
|
|
434
|
-
perLine: undefined,
|
|
435
|
-
perLinePerProperty: undefined,
|
|
436
|
-
preProcess: undefined,
|
|
437
|
-
preReturn: undefined,
|
|
438
|
-
separator: ', ',
|
|
439
|
-
}) {
|
|
440
|
-
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 ?? {};
|
|
441
|
-
let tab = indent === 0 ? ''
|
|
442
|
-
: indentCharacter.repeat(Number(indent) || 2);
|
|
443
|
-
maxLen = 78 - tab.length;
|
|
444
|
-
const sgr = this.sgr;
|
|
445
|
-
const validMapper = f => typeof f === 'function';
|
|
446
|
-
let line = [];
|
|
447
|
-
let getElements = inspector[0][inspector[1]];
|
|
448
|
-
let values = Array.isArray(objectOrLines)
|
|
449
|
-
? objectOrLines : getElements(Object(objectOrLines));
|
|
450
|
-
if (validMapper(preProcess)) {
|
|
451
|
-
values = preProcess(values);
|
|
452
|
-
}
|
|
453
|
-
const context = { indent, indentCharacter, lineEnding, maxLen, tab, sgr };
|
|
454
|
-
let finalLines = values.reduce((acc, nextProp) => {
|
|
455
|
-
let ifCombined = [...line, nextProp].join(separator);
|
|
456
|
-
if ((tab.length + ifCombined.length) <= maxLen) {
|
|
457
|
-
line.push(nextProp);
|
|
458
|
-
}
|
|
459
|
-
else {
|
|
460
|
-
let lineProps = [...line];
|
|
461
|
-
if (validMapper(perLinePerProperty)) {
|
|
462
|
-
lineProps = lineProps.map((value, index, array) => {
|
|
463
|
-
return perLinePerProperty(value, index, array, context);
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
|
-
if (colorProperties) {
|
|
467
|
-
const sgrArgs = (Array.isArray(colorProperties)
|
|
468
|
-
? colorProperties
|
|
469
|
-
: [colorProperties]);
|
|
470
|
-
lineProps = lineProps.map(v => sgr(v, ...sgrArgs));
|
|
471
|
-
}
|
|
472
|
-
lineProps = [tab, lineProps.join(separator)].join('');
|
|
473
|
-
if (validMapper(perLine)) {
|
|
474
|
-
lineProps = perLine(lineProps[0], 0, lineProps)?.[0] ?? lineProps[0];
|
|
475
|
-
}
|
|
476
|
-
acc.push(lineProps);
|
|
477
|
-
line = [];
|
|
478
|
-
}
|
|
479
|
-
return acc;
|
|
480
|
-
}, []);
|
|
481
|
-
if (validMapper(preReturn)) {
|
|
482
|
-
finalLines = finalLines.map((value, index, array) => {
|
|
483
|
-
return preReturn(value, index, array, context);
|
|
484
|
-
});
|
|
485
|
-
}
|
|
486
|
-
Symbol.for(`@nejs.string.wrap ${JSON.stringify({ lines: finalLines })}`);
|
|
487
|
-
if (lineEnding) {
|
|
488
|
-
finalLines = finalLines.join(lineEnding);
|
|
489
|
-
}
|
|
490
|
-
return finalLines;
|
|
491
|
-
},
|
|
492
|
-
},
|
|
493
|
-
});
|
|
494
|
-
const { isString: pIsString, ifString: pIfString } = StringExtensions.patches;
|
|
495
|
-
/**
|
|
496
|
-
* `StringPrototypeExtensions` provides a set of utility methods that are
|
|
497
|
-
* added to the `String` prototype. This allows all string instances to
|
|
498
|
-
* access new functionality directly, enhancing their capabilities beyond
|
|
499
|
-
* the standard `String` class methods. These extensions are applied using
|
|
500
|
-
* the `Patch` class from '@nejs/extension', ensuring that they do not
|
|
501
|
-
* interfere with the global namespace or existing properties.
|
|
502
|
-
*
|
|
503
|
-
* The extensions include methods for extracting substrings based on
|
|
504
|
-
* specific tokens, checking the presence of certain patterns, and more,
|
|
505
|
-
* making string manipulation tasks more convenient and expressive.
|
|
506
|
-
*/
|
|
507
|
-
export const StringPrototypeExtensions = new Patch(String.prototype, {
|
|
508
|
-
[Patch.kMutablyHidden]: {
|
|
509
|
-
/**
|
|
510
|
-
* Determines if the current object is a string.
|
|
511
|
-
*
|
|
512
|
-
* This getter uses the `pIsString` function from the
|
|
513
|
-
* `StringExtensions` patch to check if the current object
|
|
514
|
-
* (`this`) is a string.
|
|
515
|
-
*
|
|
516
|
-
* @type {boolean}
|
|
517
|
-
* @readonly
|
|
518
|
-
*
|
|
519
|
-
* @example
|
|
520
|
-
* const str = "Hello, World!"
|
|
521
|
-
* console.log(str.isString) // Output: true
|
|
522
|
-
*
|
|
523
|
-
* const notStr = 123
|
|
524
|
-
* console.log(notStr.isString) // Output: false
|
|
525
|
-
*/
|
|
526
|
-
get isString() {
|
|
527
|
-
return pIsString(this);
|
|
528
|
-
},
|
|
529
|
-
/**
|
|
530
|
-
* Checks if the current object is a string and returns the
|
|
531
|
-
* corresponding value based on the result.
|
|
532
|
-
*
|
|
533
|
-
* This method uses the `pIfString` function from the
|
|
534
|
-
* `StringExtensions` patch to determine if the current object
|
|
535
|
-
* (`this`) is a string. If it is a string, the `thenValue` is returned.
|
|
536
|
-
* Otherwise, the `elseValue` is returned.
|
|
537
|
-
*
|
|
538
|
-
* @param {any} thenValue - The value to return if the current object
|
|
539
|
-
* is a string.
|
|
540
|
-
* @param {any} elseValue - The value to return if the current object
|
|
541
|
-
* is not a string.
|
|
542
|
-
* @returns {any} The `thenValue` if the current object is a string, or
|
|
543
|
-
* the `elseValue` if it is not a string.
|
|
544
|
-
*
|
|
545
|
-
* @example
|
|
546
|
-
* const str = "Hello, World!"
|
|
547
|
-
* // 'Is a string'
|
|
548
|
-
* console.log(str.ifString('Is a string', 'Not a string'))
|
|
549
|
-
*
|
|
550
|
-
* const notStr = 123
|
|
551
|
-
* // 'Not a string'
|
|
552
|
-
* console.log(notStr.ifString('Is a string', 'Not a string'))
|
|
553
|
-
*/
|
|
554
|
-
ifString(thenValue, elseValue) {
|
|
555
|
-
return pIfString(this, thenValue, elseValue);
|
|
556
|
-
},
|
|
557
|
-
/**
|
|
558
|
-
* Returns an object representation of the string instance.
|
|
559
|
-
*
|
|
560
|
-
* This getter method creates and returns an object that wraps
|
|
561
|
-
* the string instance, allowing it to be treated as an object.
|
|
562
|
-
* The returned object is created using the `Object()`
|
|
563
|
-
* constructor, which takes the string instance as its argument.
|
|
564
|
-
*
|
|
565
|
-
* @type {Object}
|
|
566
|
-
* @readonly
|
|
567
|
-
*
|
|
568
|
-
* @example
|
|
569
|
-
* const str = 'Hello, World!'
|
|
570
|
-
* console.log(typeof str) // 'string'
|
|
571
|
-
* console.log(typeof str.instance) // 'object'
|
|
572
|
-
*/
|
|
573
|
-
get instance() {
|
|
574
|
-
return Object(this);
|
|
575
|
-
},
|
|
576
|
-
/**
|
|
577
|
-
* Extracts a substring from the current string, starting at a given offset
|
|
578
|
-
* and bounded by specified opening and closing tokens. This method is
|
|
579
|
-
* particularly useful for parsing nested structures or quoted strings,
|
|
580
|
-
* where the level of nesting or the presence of escape characters must
|
|
581
|
-
* be considered.
|
|
582
|
-
*
|
|
583
|
-
* @param {number} offset The position in the string from which to start the
|
|
584
|
-
* search for the substring.
|
|
585
|
-
* @param {[string, string]} tokens An array containing two strings: the
|
|
586
|
-
* opening and closing tokens that define the boundaries of the substring
|
|
587
|
-
* to be extracted.
|
|
588
|
-
* @returns {Object} An object with two properties: `extracted`, the
|
|
589
|
-
* extracted substring, and `newOffset`, the position in the original
|
|
590
|
-
* string immediately after the end of the extracted substring. If no
|
|
591
|
-
* substring is found, `extracted` is `null` and `newOffset` is the same
|
|
592
|
-
* as the input offset.
|
|
593
|
-
*/
|
|
594
|
-
extractSubstring(offset = 0, tokens = parenthesisPair) {
|
|
595
|
-
let [openToken, closeToken] = tokens;
|
|
596
|
-
let depth = 0;
|
|
597
|
-
let start = -1;
|
|
598
|
-
let end = -1;
|
|
599
|
-
let leadingToken = '';
|
|
600
|
-
let firstToken = 0;
|
|
601
|
-
for (let i = offset; i < this.length; i++) {
|
|
602
|
-
const char = this[i];
|
|
603
|
-
if (char === openToken) {
|
|
604
|
-
depth++;
|
|
605
|
-
if (start === -1)
|
|
606
|
-
start = i;
|
|
607
|
-
}
|
|
608
|
-
else if (char === closeToken) {
|
|
609
|
-
depth--;
|
|
610
|
-
if (depth === 0) {
|
|
611
|
-
end = i;
|
|
612
|
-
break;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
let lRange = [
|
|
617
|
-
Math.max(0, start - 100),
|
|
618
|
-
start
|
|
619
|
-
];
|
|
620
|
-
let leading = [...this.substring(lRange[0], lRange[1])].reverse().join('');
|
|
621
|
-
let reversedLeadingToken;
|
|
622
|
-
try {
|
|
623
|
-
reversedLeadingToken = /([^ \,\"\'\`]+)/.exec(leading)[1] ?? '';
|
|
624
|
-
leadingToken = [...reversedLeadingToken].reverse().join('');
|
|
625
|
-
}
|
|
626
|
-
catch (ignored) { }
|
|
627
|
-
if (start !== -1 && end !== -1) {
|
|
628
|
-
const sliceRange = [start, end + 1];
|
|
629
|
-
const extracted = this.slice(sliceRange[0], sliceRange[1]);
|
|
630
|
-
return {
|
|
631
|
-
extracted,
|
|
632
|
-
range: [start, end],
|
|
633
|
-
newOffset: end + 1,
|
|
634
|
-
leadingToken,
|
|
635
|
-
};
|
|
636
|
-
}
|
|
637
|
-
else {
|
|
638
|
-
return {
|
|
639
|
-
extracted: null,
|
|
640
|
-
range: [start, end],
|
|
641
|
-
newOffset: offset,
|
|
642
|
-
leadingToken,
|
|
643
|
-
};
|
|
644
|
-
}
|
|
645
|
-
},
|
|
646
|
-
/**
|
|
647
|
-
* Prints the string representation of the current object using a specified
|
|
648
|
-
* function and context.
|
|
649
|
-
*
|
|
650
|
-
* This method converts the current object to a string and then prints it
|
|
651
|
-
* using the provided function. By default, it uses `console.log` to print
|
|
652
|
-
* to the console. The function is called with a specified `thisArg` to
|
|
653
|
-
* set the context in which the function is executed.
|
|
654
|
-
*
|
|
655
|
-
* @param {Function} [fn=console.log] - The function used to print the
|
|
656
|
-
* string. Defaults to `console.log`.
|
|
657
|
-
* @param {Object} [thisArg=console] - The value of `this` provided for
|
|
658
|
-
* the call to `fn`. Defaults to the global `console` object.
|
|
659
|
-
*
|
|
660
|
-
* @returns {string} The string representation of the current object.
|
|
661
|
-
*
|
|
662
|
-
* @example
|
|
663
|
-
* // assuming extensions are applied
|
|
664
|
-
* const str = 'Hello, World!'
|
|
665
|
-
* str.print() // Logs: 'Hello, World!' to the console
|
|
666
|
-
*
|
|
667
|
-
* @example
|
|
668
|
-
* // assuming extensions are applied
|
|
669
|
-
* const str = 'Hello, World!'
|
|
670
|
-
* const customLogger = {
|
|
671
|
-
* time: new Date(),
|
|
672
|
-
* log: (msg) => {
|
|
673
|
-
* console.info('[%s]: %s', this.time..toLocaleDateString(), msg)
|
|
674
|
-
* }
|
|
675
|
-
* }
|
|
676
|
-
* str.print(customLogger.log, customLogger)
|
|
677
|
-
* // prints "[10/16/2024] Hello, World!"
|
|
678
|
-
*/
|
|
679
|
-
print(fn = console.log, thisArg = console) {
|
|
680
|
-
const string = this.toString();
|
|
681
|
-
fn.call(thisArg, string);
|
|
682
|
-
return string;
|
|
683
|
-
},
|
|
684
|
-
},
|
|
685
|
-
});
|
|
686
|
-
// NOTE to self; this is repeated here otherwise a circular reference from
|
|
687
|
-
// Object<->Function<->Global occurs. See original source in global.this.js
|
|
688
|
-
// {@see globalThis.isThenElse}
|
|
689
|
-
function isThenElse(bv, tv, ev) {
|
|
690
|
-
function isFunction(value) { typeof value === 'function'; }
|
|
691
|
-
if (arguments.length > 1) {
|
|
692
|
-
var _then = isFunction(tv) ? tv(bv) : tv;
|
|
693
|
-
if (arguments.length > 2) {
|
|
694
|
-
var _else = isFunction(ev) ? tv(bv) : ev;
|
|
695
|
-
return bv ? _then : _else;
|
|
696
|
-
}
|
|
697
|
-
return bv || _then;
|
|
698
|
-
}
|
|
699
|
-
return bv;
|
|
700
|
-
}
|
|
701
|
-
//# sourceMappingURL=string.extensions.js.map
|