@nejs/basic-extensions 2.21.5 → 2.22.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/README.md +78 -4
  2. package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
  3. package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
  4. package/dist/cjs/classes/index.cjs +11129 -0
  5. package/dist/cjs/classes/index.cjs.map +7 -0
  6. package/dist/cjs/index.cjs +15191 -0
  7. package/dist/cjs/index.cjs.map +7 -0
  8. package/dist/cjs/utils/index.cjs +3954 -0
  9. package/dist/cjs/utils/index.cjs.map +7 -0
  10. package/dist/esm/basic-extensions.mjs +25 -0
  11. package/dist/esm/basic-extensions.mjs.map +7 -0
  12. package/package.json +16 -22
  13. package/repl.bootstrap.js +4 -7
  14. package/repl.history +26 -26
  15. package/src/classes/enumeration.js +466 -0
  16. package/src/classes/index.js +5 -1
  17. package/src/index.js +3 -1
  18. package/src/regular.expression.extensions.js +0 -35
  19. package/src/utils/toolkit.js +699 -516
  20. package/tests/arrayextensions.test.js +3 -3
  21. package/tests/index.test.js +3 -1
  22. package/tests/newClasses/asyncIterable.test.js +3 -3
  23. package/tests/newClasses/deferred.test.js +3 -3
  24. package/tests/newClasses/descriptor.test.js +3 -3
  25. package/tests/newClasses/iterable.test.js +3 -3
  26. package/tests/newClasses/refmap.test.js +3 -3
  27. package/tests/newClasses/refset.test.js +3 -3
  28. package/tests/objectextensions.test.js +3 -3
  29. package/tests/setextensions.test.js +3 -3
  30. package/tests/stringextensions.test.js +3 -2
  31. package/tests/utils/descriptor.utils.test.js +1 -1
  32. package/tests/utils/toolkit.test.js +429 -163
  33. package/.esdoc.json +0 -9
  34. package/.vscode/settings.json +0 -5
  35. package/bin/build +0 -27
  36. package/bin/clean +0 -14
  37. package/bin/esbuild +0 -91
  38. package/bin/fixup +0 -13
  39. package/bin/repl.basics.js +0 -584
  40. package/bin/repl.signature.js +0 -63
  41. package/bin/version +0 -100
  42. package/dist/@nejs/basic-extensions.bundle.2.21.5.js +0 -25
  43. package/dist/@nejs/basic-extensions.bundle.2.21.5.js.map +0 -7
  44. package/dist/cjs/array.extensions.d.ts +0 -39
  45. package/dist/cjs/array.extensions.js +0 -477
  46. package/dist/cjs/array.extensions.js.map +0 -1
  47. package/dist/cjs/big.int.extension.d.ts +0 -31
  48. package/dist/cjs/big.int.extension.js +0 -273
  49. package/dist/cjs/big.int.extension.js.map +0 -1
  50. package/dist/cjs/classes/asyncIterable.d.ts +0 -126
  51. package/dist/cjs/classes/asyncIterable.js +0 -209
  52. package/dist/cjs/classes/asyncIterable.js.map +0 -1
  53. package/dist/cjs/classes/deferred.d.ts +0 -146
  54. package/dist/cjs/classes/deferred.js +0 -291
  55. package/dist/cjs/classes/deferred.js.map +0 -1
  56. package/dist/cjs/classes/descriptor.d.ts +0 -334
  57. package/dist/cjs/classes/descriptor.js +0 -537
  58. package/dist/cjs/classes/descriptor.js.map +0 -1
  59. package/dist/cjs/classes/enum.d.ts +0 -50
  60. package/dist/cjs/classes/enum.js +0 -405
  61. package/dist/cjs/classes/enum.js.map +0 -1
  62. package/dist/cjs/classes/index.d.ts +0 -15
  63. package/dist/cjs/classes/index.js +0 -63
  64. package/dist/cjs/classes/index.js.map +0 -1
  65. package/dist/cjs/classes/introspector.d.ts +0 -20
  66. package/dist/cjs/classes/introspector.js +0 -130
  67. package/dist/cjs/classes/introspector.js.map +0 -1
  68. package/dist/cjs/classes/iterable.d.ts +0 -169
  69. package/dist/cjs/classes/iterable.js +0 -268
  70. package/dist/cjs/classes/iterable.js.map +0 -1
  71. package/dist/cjs/classes/param.parser.d.ts +0 -221
  72. package/dist/cjs/classes/param.parser.js +0 -242
  73. package/dist/cjs/classes/param.parser.js.map +0 -1
  74. package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
  75. package/dist/cjs/classes/pluggable.proxy.js +0 -444
  76. package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
  77. package/dist/cjs/classes/property.d.ts +0 -79
  78. package/dist/cjs/classes/property.js +0 -284
  79. package/dist/cjs/classes/property.js.map +0 -1
  80. package/dist/cjs/classes/refmap.d.ts +0 -238
  81. package/dist/cjs/classes/refmap.js +0 -421
  82. package/dist/cjs/classes/refmap.js.map +0 -1
  83. package/dist/cjs/classes/refset.d.ts +0 -186
  84. package/dist/cjs/classes/refset.js +0 -370
  85. package/dist/cjs/classes/refset.js.map +0 -1
  86. package/dist/cjs/classes/symkeys.d.ts +0 -349
  87. package/dist/cjs/classes/symkeys.js +0 -510
  88. package/dist/cjs/classes/symkeys.js.map +0 -1
  89. package/dist/cjs/classes/type.d.ts +0 -56
  90. package/dist/cjs/classes/type.js +0 -405
  91. package/dist/cjs/classes/type.js.map +0 -1
  92. package/dist/cjs/function.extensions.d.ts +0 -12
  93. package/dist/cjs/function.extensions.js +0 -758
  94. package/dist/cjs/function.extensions.js.map +0 -1
  95. package/dist/cjs/global.this.d.ts +0 -2
  96. package/dist/cjs/global.this.js +0 -300
  97. package/dist/cjs/global.this.js.map +0 -1
  98. package/dist/cjs/index.d.ts +0 -31
  99. package/dist/cjs/index.js +0 -228
  100. package/dist/cjs/index.js.map +0 -1
  101. package/dist/cjs/json.extensions.d.ts +0 -2
  102. package/dist/cjs/json.extensions.js +0 -109
  103. package/dist/cjs/json.extensions.js.map +0 -1
  104. package/dist/cjs/map.extensions.d.ts +0 -3
  105. package/dist/cjs/map.extensions.js +0 -143
  106. package/dist/cjs/map.extensions.js.map +0 -1
  107. package/dist/cjs/math.extension.d.ts +0 -14
  108. package/dist/cjs/math.extension.js +0 -71
  109. package/dist/cjs/math.extension.js.map +0 -1
  110. package/dist/cjs/number.extension.d.ts +0 -44
  111. package/dist/cjs/number.extension.js +0 -278
  112. package/dist/cjs/number.extension.js.map +0 -1
  113. package/dist/cjs/object.extensions.d.ts +0 -33
  114. package/dist/cjs/object.extensions.js +0 -1091
  115. package/dist/cjs/object.extensions.js.map +0 -1
  116. package/dist/cjs/package.json +0 -3
  117. package/dist/cjs/proxy.extensions.d.ts +0 -2
  118. package/dist/cjs/proxy.extensions.js +0 -207
  119. package/dist/cjs/proxy.extensions.js.map +0 -1
  120. package/dist/cjs/reflect.extensions.d.ts +0 -14
  121. package/dist/cjs/reflect.extensions.js +0 -316
  122. package/dist/cjs/reflect.extensions.js.map +0 -1
  123. package/dist/cjs/regular.expression.extensions.d.ts +0 -2
  124. package/dist/cjs/regular.expression.extensions.js +0 -423
  125. package/dist/cjs/regular.expression.extensions.js.map +0 -1
  126. package/dist/cjs/set.extensions.d.ts +0 -40
  127. package/dist/cjs/set.extensions.js +0 -355
  128. package/dist/cjs/set.extensions.js.map +0 -1
  129. package/dist/cjs/string.extensions.d.ts +0 -23
  130. package/dist/cjs/string.extensions.js +0 -704
  131. package/dist/cjs/string.extensions.js.map +0 -1
  132. package/dist/cjs/symbol.extensions.d.ts +0 -11
  133. package/dist/cjs/symbol.extensions.js +0 -735
  134. package/dist/cjs/symbol.extensions.js.map +0 -1
  135. package/dist/cjs/utils/copy.object.d.ts +0 -408
  136. package/dist/cjs/utils/copy.object.js +0 -720
  137. package/dist/cjs/utils/copy.object.js.map +0 -1
  138. package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
  139. package/dist/cjs/utils/descriptor.utils.js +0 -889
  140. package/dist/cjs/utils/descriptor.utils.js.map +0 -1
  141. package/dist/cjs/utils/index.d.ts +0 -75
  142. package/dist/cjs/utils/index.js +0 -61
  143. package/dist/cjs/utils/index.js.map +0 -1
  144. package/dist/cjs/utils/stdout.d.ts +0 -742
  145. package/dist/cjs/utils/stdout.js +0 -1042
  146. package/dist/cjs/utils/stdout.js.map +0 -1
  147. package/dist/cjs/utils/toolkit.d.ts +0 -1898
  148. package/dist/cjs/utils/toolkit.js +0 -1378
  149. package/dist/cjs/utils/toolkit.js.map +0 -1
  150. package/dist/cjs/weakref.extensions.d.ts +0 -2
  151. package/dist/cjs/weakref.extensions.js +0 -19
  152. package/dist/cjs/weakref.extensions.js.map +0 -1
  153. package/dist/mjs/array.extensions.d.ts +0 -39
  154. package/dist/mjs/array.extensions.js +0 -474
  155. package/dist/mjs/array.extensions.js.map +0 -1
  156. package/dist/mjs/big.int.extension.d.ts +0 -31
  157. package/dist/mjs/big.int.extension.js +0 -270
  158. package/dist/mjs/big.int.extension.js.map +0 -1
  159. package/dist/mjs/classes/asyncIterable.d.ts +0 -126
  160. package/dist/mjs/classes/asyncIterable.js +0 -204
  161. package/dist/mjs/classes/asyncIterable.js.map +0 -1
  162. package/dist/mjs/classes/deferred.d.ts +0 -146
  163. package/dist/mjs/classes/deferred.js +0 -287
  164. package/dist/mjs/classes/deferred.js.map +0 -1
  165. package/dist/mjs/classes/descriptor.d.ts +0 -334
  166. package/dist/mjs/classes/descriptor.js +0 -533
  167. package/dist/mjs/classes/descriptor.js.map +0 -1
  168. package/dist/mjs/classes/enum.d.ts +0 -50
  169. package/dist/mjs/classes/enum.js +0 -400
  170. package/dist/mjs/classes/enum.js.map +0 -1
  171. package/dist/mjs/classes/index.d.ts +0 -15
  172. package/dist/mjs/classes/index.js +0 -46
  173. package/dist/mjs/classes/index.js.map +0 -1
  174. package/dist/mjs/classes/introspector.d.ts +0 -20
  175. package/dist/mjs/classes/introspector.js +0 -126
  176. package/dist/mjs/classes/introspector.js.map +0 -1
  177. package/dist/mjs/classes/iterable.d.ts +0 -169
  178. package/dist/mjs/classes/iterable.js +0 -263
  179. package/dist/mjs/classes/iterable.js.map +0 -1
  180. package/dist/mjs/classes/param.parser.d.ts +0 -221
  181. package/dist/mjs/classes/param.parser.js +0 -238
  182. package/dist/mjs/classes/param.parser.js.map +0 -1
  183. package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
  184. package/dist/mjs/classes/pluggable.proxy.js +0 -438
  185. package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
  186. package/dist/mjs/classes/property.d.ts +0 -79
  187. package/dist/mjs/classes/property.js +0 -280
  188. package/dist/mjs/classes/property.js.map +0 -1
  189. package/dist/mjs/classes/refmap.d.ts +0 -238
  190. package/dist/mjs/classes/refmap.js +0 -417
  191. package/dist/mjs/classes/refmap.js.map +0 -1
  192. package/dist/mjs/classes/refset.d.ts +0 -186
  193. package/dist/mjs/classes/refset.js +0 -366
  194. package/dist/mjs/classes/refset.js.map +0 -1
  195. package/dist/mjs/classes/symkeys.d.ts +0 -349
  196. package/dist/mjs/classes/symkeys.js +0 -506
  197. package/dist/mjs/classes/symkeys.js.map +0 -1
  198. package/dist/mjs/classes/type.d.ts +0 -56
  199. package/dist/mjs/classes/type.js +0 -401
  200. package/dist/mjs/classes/type.js.map +0 -1
  201. package/dist/mjs/function.extensions.d.ts +0 -12
  202. package/dist/mjs/function.extensions.js +0 -755
  203. package/dist/mjs/function.extensions.js.map +0 -1
  204. package/dist/mjs/global.this.d.ts +0 -2
  205. package/dist/mjs/global.this.js +0 -264
  206. package/dist/mjs/global.this.js.map +0 -1
  207. package/dist/mjs/index.d.ts +0 -31
  208. package/dist/mjs/index.js +0 -206
  209. package/dist/mjs/index.js.map +0 -1
  210. package/dist/mjs/json.extensions.d.ts +0 -2
  211. package/dist/mjs/json.extensions.js +0 -106
  212. package/dist/mjs/json.extensions.js.map +0 -1
  213. package/dist/mjs/map.extensions.d.ts +0 -3
  214. package/dist/mjs/map.extensions.js +0 -140
  215. package/dist/mjs/map.extensions.js.map +0 -1
  216. package/dist/mjs/math.extension.d.ts +0 -14
  217. package/dist/mjs/math.extension.js +0 -68
  218. package/dist/mjs/math.extension.js.map +0 -1
  219. package/dist/mjs/number.extension.d.ts +0 -44
  220. package/dist/mjs/number.extension.js +0 -275
  221. package/dist/mjs/number.extension.js.map +0 -1
  222. package/dist/mjs/object.extensions.d.ts +0 -33
  223. package/dist/mjs/object.extensions.js +0 -1088
  224. package/dist/mjs/object.extensions.js.map +0 -1
  225. package/dist/mjs/package.json +0 -3
  226. package/dist/mjs/proxy.extensions.d.ts +0 -2
  227. package/dist/mjs/proxy.extensions.js +0 -204
  228. package/dist/mjs/proxy.extensions.js.map +0 -1
  229. package/dist/mjs/reflect.extensions.d.ts +0 -14
  230. package/dist/mjs/reflect.extensions.js +0 -313
  231. package/dist/mjs/reflect.extensions.js.map +0 -1
  232. package/dist/mjs/regular.expression.extensions.d.ts +0 -2
  233. package/dist/mjs/regular.expression.extensions.js +0 -420
  234. package/dist/mjs/regular.expression.extensions.js.map +0 -1
  235. package/dist/mjs/set.extensions.d.ts +0 -40
  236. package/dist/mjs/set.extensions.js +0 -352
  237. package/dist/mjs/set.extensions.js.map +0 -1
  238. package/dist/mjs/string.extensions.d.ts +0 -23
  239. package/dist/mjs/string.extensions.js +0 -701
  240. package/dist/mjs/string.extensions.js.map +0 -1
  241. package/dist/mjs/symbol.extensions.d.ts +0 -11
  242. package/dist/mjs/symbol.extensions.js +0 -732
  243. package/dist/mjs/symbol.extensions.js.map +0 -1
  244. package/dist/mjs/utils/copy.object.d.ts +0 -408
  245. package/dist/mjs/utils/copy.object.js +0 -702
  246. package/dist/mjs/utils/copy.object.js.map +0 -1
  247. package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
  248. package/dist/mjs/utils/descriptor.utils.js +0 -875
  249. package/dist/mjs/utils/descriptor.utils.js.map +0 -1
  250. package/dist/mjs/utils/index.d.ts +0 -75
  251. package/dist/mjs/utils/index.js +0 -45
  252. package/dist/mjs/utils/index.js.map +0 -1
  253. package/dist/mjs/utils/stdout.d.ts +0 -742
  254. package/dist/mjs/utils/stdout.js +0 -1037
  255. package/dist/mjs/utils/stdout.js.map +0 -1
  256. package/dist/mjs/utils/toolkit.d.ts +0 -1898
  257. package/dist/mjs/utils/toolkit.js +0 -1373
  258. package/dist/mjs/utils/toolkit.js.map +0 -1
  259. package/dist/mjs/weakref.extensions.d.ts +0 -2
  260. package/dist/mjs/weakref.extensions.js +0 -16
  261. package/dist/mjs/weakref.extensions.js.map +0 -1
  262. package/jsdoc-config.json +0 -31
  263. package/tsconfig.base.json +0 -28
  264. package/tsconfig.cjs.json +0 -8
  265. package/tsconfig.esm.json +0 -8
  266. package/vitest.config.js +0 -7
@@ -1,704 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StringPrototypeExtensions = exports.StringExtensions = void 0;
4
- const extension_1 = require("@nejs/extension");
5
- const parenthesisPair = ['(', ')'];
6
- /**
7
- * `StringExtensions` is a patch for the JavaScript built-in `String` class. It
8
- * adds utility methods to the `String` class without modifying the global namespace
9
- * directly. This patch includes methods for key validation, object type checking,
10
- * and retrieving the string tag of an object. These methods are useful for
11
- * enhancing the capabilities of the standard `String` class with additional
12
- * utility functions.
13
- */
14
- exports.StringExtensions = new extension_1.Patch(String, {
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);
455
- }
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);
461
- }
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);
492
- }
493
- return finalLines;
494
- },
495
- },
496
- });
497
- const { isString: pIsString, ifString: pIfString } = exports.StringExtensions.patches;
498
- /**
499
- * `StringPrototypeExtensions` provides a set of utility methods that are
500
- * added to the `String` prototype. This allows all string instances to
501
- * access new functionality directly, enhancing their capabilities beyond
502
- * the standard `String` class methods. These extensions are applied using
503
- * the `Patch` class from '@nejs/extension', ensuring that they do not
504
- * interfere with the global namespace or existing properties.
505
- *
506
- * The extensions include methods for extracting substrings based on
507
- * specific tokens, checking the presence of certain patterns, and more,
508
- * making string manipulation tasks more convenient and expressive.
509
- */
510
- exports.StringPrototypeExtensions = new extension_1.Patch(String.prototype, {
511
- [extension_1.Patch.kMutablyHidden]: {
512
- /**
513
- * Determines if the current object is a string.
514
- *
515
- * This getter uses the `pIsString` function from the
516
- * `StringExtensions` patch to check if the current object
517
- * (`this`) is a string.
518
- *
519
- * @type {boolean}
520
- * @readonly
521
- *
522
- * @example
523
- * const str = "Hello, World!"
524
- * console.log(str.isString) // Output: true
525
- *
526
- * const notStr = 123
527
- * console.log(notStr.isString) // Output: false
528
- */
529
- get isString() {
530
- return pIsString(this);
531
- },
532
- /**
533
- * Checks if the current object is a string and returns the
534
- * corresponding value based on the result.
535
- *
536
- * This method uses the `pIfString` function from the
537
- * `StringExtensions` patch to determine if the current object
538
- * (`this`) is a string. If it is a string, the `thenValue` is returned.
539
- * Otherwise, the `elseValue` is returned.
540
- *
541
- * @param {any} thenValue - The value to return if the current object
542
- * is a string.
543
- * @param {any} elseValue - The value to return if the current object
544
- * is not a string.
545
- * @returns {any} The `thenValue` if the current object is a string, or
546
- * the `elseValue` if it is not a string.
547
- *
548
- * @example
549
- * const str = "Hello, World!"
550
- * // 'Is a string'
551
- * console.log(str.ifString('Is a string', 'Not a string'))
552
- *
553
- * const notStr = 123
554
- * // 'Not a string'
555
- * console.log(notStr.ifString('Is a string', 'Not a string'))
556
- */
557
- ifString(thenValue, elseValue) {
558
- return pIfString(this, thenValue, elseValue);
559
- },
560
- /**
561
- * Returns an object representation of the string instance.
562
- *
563
- * This getter method creates and returns an object that wraps
564
- * the string instance, allowing it to be treated as an object.
565
- * The returned object is created using the `Object()`
566
- * constructor, which takes the string instance as its argument.
567
- *
568
- * @type {Object}
569
- * @readonly
570
- *
571
- * @example
572
- * const str = 'Hello, World!'
573
- * console.log(typeof str) // 'string'
574
- * console.log(typeof str.instance) // 'object'
575
- */
576
- get instance() {
577
- return Object(this);
578
- },
579
- /**
580
- * Extracts a substring from the current string, starting at a given offset
581
- * and bounded by specified opening and closing tokens. This method is
582
- * particularly useful for parsing nested structures or quoted strings,
583
- * where the level of nesting or the presence of escape characters must
584
- * be considered.
585
- *
586
- * @param {number} offset The position in the string from which to start the
587
- * search for the substring.
588
- * @param {[string, string]} tokens An array containing two strings: the
589
- * opening and closing tokens that define the boundaries of the substring
590
- * to be extracted.
591
- * @returns {Object} An object with two properties: `extracted`, the
592
- * extracted substring, and `newOffset`, the position in the original
593
- * string immediately after the end of the extracted substring. If no
594
- * substring is found, `extracted` is `null` and `newOffset` is the same
595
- * as the input offset.
596
- */
597
- extractSubstring(offset = 0, tokens = parenthesisPair) {
598
- let [openToken, closeToken] = tokens;
599
- let depth = 0;
600
- let start = -1;
601
- let end = -1;
602
- let leadingToken = '';
603
- let firstToken = 0;
604
- for (let i = offset; i < this.length; i++) {
605
- const char = this[i];
606
- if (char === openToken) {
607
- depth++;
608
- if (start === -1)
609
- start = i;
610
- }
611
- else if (char === closeToken) {
612
- depth--;
613
- if (depth === 0) {
614
- end = i;
615
- break;
616
- }
617
- }
618
- }
619
- let lRange = [
620
- Math.max(0, start - 100),
621
- start
622
- ];
623
- let leading = [...this.substring(lRange[0], lRange[1])].reverse().join('');
624
- let reversedLeadingToken;
625
- try {
626
- reversedLeadingToken = /([^ \,\"\'\`]+)/.exec(leading)[1] ?? '';
627
- leadingToken = [...reversedLeadingToken].reverse().join('');
628
- }
629
- catch (ignored) { }
630
- if (start !== -1 && end !== -1) {
631
- const sliceRange = [start, end + 1];
632
- const extracted = this.slice(sliceRange[0], sliceRange[1]);
633
- return {
634
- extracted,
635
- range: [start, end],
636
- newOffset: end + 1,
637
- leadingToken,
638
- };
639
- }
640
- else {
641
- return {
642
- extracted: null,
643
- range: [start, end],
644
- newOffset: offset,
645
- leadingToken,
646
- };
647
- }
648
- },
649
- /**
650
- * Prints the string representation of the current object using a specified
651
- * function and context.
652
- *
653
- * This method converts the current object to a string and then prints it
654
- * using the provided function. By default, it uses `console.log` to print
655
- * to the console. The function is called with a specified `thisArg` to
656
- * set the context in which the function is executed.
657
- *
658
- * @param {Function} [fn=console.log] - The function used to print the
659
- * string. Defaults to `console.log`.
660
- * @param {Object} [thisArg=console] - The value of `this` provided for
661
- * the call to `fn`. Defaults to the global `console` object.
662
- *
663
- * @returns {string} The string representation of the current object.
664
- *
665
- * @example
666
- * // assuming extensions are applied
667
- * const str = 'Hello, World!'
668
- * str.print() // Logs: 'Hello, World!' to the console
669
- *
670
- * @example
671
- * // assuming extensions are applied
672
- * const str = 'Hello, World!'
673
- * const customLogger = {
674
- * time: new Date(),
675
- * log: (msg) => {
676
- * console.info('[%s]: %s', this.time..toLocaleDateString(), msg)
677
- * }
678
- * }
679
- * str.print(customLogger.log, customLogger)
680
- * // prints "[10/16/2024] Hello, World!"
681
- */
682
- print(fn = console.log, thisArg = console) {
683
- const string = this.toString();
684
- fn.call(thisArg, string);
685
- return string;
686
- },
687
- },
688
- });
689
- // NOTE to self; this is repeated here otherwise a circular reference from
690
- // Object<->Function<->Global occurs. See original source in global.this.js
691
- // {@see globalThis.isThenElse}
692
- function isThenElse(bv, tv, ev) {
693
- function isFunction(value) { typeof value === 'function'; }
694
- if (arguments.length > 1) {
695
- var _then = isFunction(tv) ? tv(bv) : tv;
696
- if (arguments.length > 2) {
697
- var _else = isFunction(ev) ? tv(bv) : ev;
698
- return bv ? _then : _else;
699
- }
700
- return bv || _then;
701
- }
702
- return bv;
703
- }
704
- //# sourceMappingURL=string.extensions.js.map