@nejs/basic-extensions 2.7.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/.vscode/settings.json +5 -0
  2. package/README.md +6250 -1574
  3. package/bin/version +100 -0
  4. package/dist/@nejs/basic-extensions.bundle.2.8.0.js +19 -0
  5. package/dist/@nejs/basic-extensions.bundle.2.8.0.js.map +7 -0
  6. package/dist/cjs/array.extensions.d.ts +39 -0
  7. package/dist/cjs/array.extensions.js +303 -0
  8. package/dist/cjs/array.extensions.js.map +1 -0
  9. package/dist/cjs/big.int.extension.d.ts +31 -0
  10. package/dist/cjs/big.int.extension.js +164 -0
  11. package/dist/cjs/big.int.extension.js.map +1 -0
  12. package/dist/cjs/{newClasses → classes}/asyncIterable.js +32 -44
  13. package/dist/cjs/classes/asyncIterable.js.map +1 -0
  14. package/dist/cjs/{newClasses → classes}/deferred.js +66 -138
  15. package/dist/cjs/classes/deferred.js.map +1 -0
  16. package/dist/cjs/{newClasses → classes}/descriptor.js +56 -90
  17. package/dist/cjs/classes/descriptor.js.map +1 -0
  18. package/dist/cjs/classes/index.d.ts +13 -0
  19. package/dist/cjs/classes/index.js +57 -0
  20. package/dist/cjs/classes/index.js.map +1 -0
  21. package/dist/cjs/classes/introspector.d.ts +20 -0
  22. package/dist/cjs/classes/introspector.js +130 -0
  23. package/dist/cjs/classes/introspector.js.map +1 -0
  24. package/dist/cjs/{newClasses → classes}/iterable.js +42 -63
  25. package/dist/cjs/classes/iterable.js.map +1 -0
  26. package/dist/cjs/classes/param.parser.d.ts +227 -0
  27. package/dist/cjs/classes/param.parser.js +242 -0
  28. package/dist/cjs/classes/param.parser.js.map +1 -0
  29. package/dist/cjs/classes/pluggable.proxy.d.ts +152 -0
  30. package/dist/cjs/classes/pluggable.proxy.js +444 -0
  31. package/dist/cjs/classes/pluggable.proxy.js.map +1 -0
  32. package/dist/cjs/{newClasses → classes}/refmap.js +18 -30
  33. package/dist/cjs/classes/refmap.js.map +1 -0
  34. package/dist/cjs/{newClasses → classes}/refset.js +28 -47
  35. package/dist/cjs/classes/refset.js.map +1 -0
  36. package/dist/cjs/classes/symkeys.d.ts +292 -0
  37. package/dist/cjs/classes/symkeys.js +424 -0
  38. package/dist/cjs/classes/symkeys.js.map +1 -0
  39. package/dist/cjs/classes/type.d.ts +56 -0
  40. package/dist/cjs/classes/type.js +405 -0
  41. package/dist/cjs/classes/type.js.map +1 -0
  42. package/dist/cjs/function.extensions.js +757 -0
  43. package/dist/cjs/function.extensions.js.map +1 -0
  44. package/dist/cjs/global.this.js +261 -0
  45. package/dist/cjs/global.this.js.map +1 -0
  46. package/dist/cjs/index.d.ts +4 -3
  47. package/dist/cjs/index.js +62 -32
  48. package/dist/cjs/index.js.map +1 -1
  49. package/dist/cjs/json.extensions.d.ts +2 -0
  50. package/dist/cjs/json.extensions.js +108 -0
  51. package/dist/cjs/json.extensions.js.map +1 -0
  52. package/dist/{mjs/mapextensions.d.ts → cjs/map.extensions.d.ts} +1 -0
  53. package/dist/cjs/map.extensions.js +142 -0
  54. package/dist/cjs/map.extensions.js.map +1 -0
  55. package/dist/cjs/number.extension.d.ts +44 -0
  56. package/dist/cjs/number.extension.js +260 -0
  57. package/dist/cjs/number.extension.js.map +1 -0
  58. package/dist/cjs/object.extensions.d.ts +62 -0
  59. package/dist/cjs/object.extensions.js +1128 -0
  60. package/dist/cjs/object.extensions.js.map +1 -0
  61. package/dist/cjs/proxy.extensions.d.ts +2 -0
  62. package/dist/cjs/proxy.extensions.js +207 -0
  63. package/dist/cjs/proxy.extensions.js.map +1 -0
  64. package/dist/cjs/reflect.extensions.js +316 -0
  65. package/dist/cjs/reflect.extensions.js.map +1 -0
  66. package/dist/cjs/regular.expression.extensions.d.ts +2 -0
  67. package/dist/cjs/regular.expression.extensions.js +423 -0
  68. package/dist/cjs/regular.expression.extensions.js.map +1 -0
  69. package/dist/cjs/set.extensions.d.ts +40 -0
  70. package/dist/cjs/{setextensions.js → set.extensions.js} +150 -2
  71. package/dist/cjs/set.extensions.js.map +1 -0
  72. package/dist/cjs/string.extensions.js +661 -0
  73. package/dist/cjs/string.extensions.js.map +1 -0
  74. package/dist/{mjs/symbolextensions.d.ts → cjs/symbol.extensions.d.ts} +1 -0
  75. package/dist/cjs/symbol.extensions.js +380 -0
  76. package/dist/cjs/symbol.extensions.js.map +1 -0
  77. package/dist/cjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
  78. package/dist/cjs/weakref.extensions.js.map +1 -0
  79. package/dist/mjs/array.extensions.d.ts +39 -0
  80. package/dist/mjs/array.extensions.js +300 -0
  81. package/dist/mjs/array.extensions.js.map +1 -0
  82. package/dist/mjs/big.int.extension.d.ts +31 -0
  83. package/dist/mjs/big.int.extension.js +161 -0
  84. package/dist/mjs/big.int.extension.js.map +1 -0
  85. package/dist/mjs/classes/asyncIterable.js.map +1 -0
  86. package/dist/mjs/classes/deferred.js.map +1 -0
  87. package/dist/mjs/{newClasses → classes}/descriptor.js +7 -4
  88. package/dist/mjs/classes/descriptor.js.map +1 -0
  89. package/dist/mjs/classes/index.d.ts +13 -0
  90. package/dist/mjs/classes/index.js +40 -0
  91. package/dist/mjs/classes/index.js.map +1 -0
  92. package/dist/mjs/classes/introspector.d.ts +20 -0
  93. package/dist/mjs/classes/introspector.js +126 -0
  94. package/dist/mjs/classes/introspector.js.map +1 -0
  95. package/dist/mjs/classes/iterable.js.map +1 -0
  96. package/dist/mjs/classes/param.parser.d.ts +227 -0
  97. package/dist/mjs/classes/param.parser.js +238 -0
  98. package/dist/mjs/classes/param.parser.js.map +1 -0
  99. package/dist/mjs/classes/pluggable.proxy.d.ts +152 -0
  100. package/dist/mjs/classes/pluggable.proxy.js +438 -0
  101. package/dist/mjs/classes/pluggable.proxy.js.map +1 -0
  102. package/dist/mjs/{newClasses → classes}/refmap.js +3 -3
  103. package/dist/mjs/classes/refmap.js.map +1 -0
  104. package/dist/mjs/classes/refset.js.map +1 -0
  105. package/dist/mjs/classes/symkeys.d.ts +292 -0
  106. package/dist/mjs/classes/symkeys.js +420 -0
  107. package/dist/mjs/classes/symkeys.js.map +1 -0
  108. package/dist/mjs/classes/type.d.ts +56 -0
  109. package/dist/mjs/classes/type.js +401 -0
  110. package/dist/mjs/classes/type.js.map +1 -0
  111. package/dist/mjs/function.extensions.js +754 -0
  112. package/dist/mjs/function.extensions.js.map +1 -0
  113. package/dist/mjs/global.this.js +258 -0
  114. package/dist/mjs/global.this.js.map +1 -0
  115. package/dist/mjs/index.d.ts +4 -3
  116. package/dist/mjs/index.js +49 -19
  117. package/dist/mjs/index.js.map +1 -1
  118. package/dist/mjs/json.extensions.d.ts +2 -0
  119. package/dist/mjs/json.extensions.js +105 -0
  120. package/dist/mjs/json.extensions.js.map +1 -0
  121. package/dist/{cjs/mapextensions.d.ts → mjs/map.extensions.d.ts} +1 -0
  122. package/dist/mjs/map.extensions.js +139 -0
  123. package/dist/mjs/map.extensions.js.map +1 -0
  124. package/dist/mjs/number.extension.d.ts +44 -0
  125. package/dist/mjs/number.extension.js +257 -0
  126. package/dist/mjs/number.extension.js.map +1 -0
  127. package/dist/mjs/object.extensions.d.ts +62 -0
  128. package/dist/mjs/object.extensions.js +1124 -0
  129. package/dist/mjs/object.extensions.js.map +1 -0
  130. package/dist/mjs/proxy.extensions.d.ts +2 -0
  131. package/dist/mjs/proxy.extensions.js +204 -0
  132. package/dist/mjs/proxy.extensions.js.map +1 -0
  133. package/dist/mjs/reflect.extensions.js +313 -0
  134. package/dist/mjs/reflect.extensions.js.map +1 -0
  135. package/dist/mjs/regular.expression.extensions.d.ts +2 -0
  136. package/dist/mjs/regular.expression.extensions.js +420 -0
  137. package/dist/mjs/regular.expression.extensions.js.map +1 -0
  138. package/dist/mjs/set.extensions.d.ts +40 -0
  139. package/dist/mjs/{setextensions.js → set.extensions.js} +149 -1
  140. package/dist/mjs/set.extensions.js.map +1 -0
  141. package/dist/mjs/string.extensions.js +658 -0
  142. package/dist/mjs/string.extensions.js.map +1 -0
  143. package/dist/{cjs/symbolextensions.d.ts → mjs/symbol.extensions.d.ts} +1 -0
  144. package/dist/mjs/symbol.extensions.js +377 -0
  145. package/dist/mjs/symbol.extensions.js.map +1 -0
  146. package/dist/mjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
  147. package/dist/mjs/weakref.extensions.js.map +1 -0
  148. package/docs/index.html +24452 -5692
  149. package/package.json +7 -5
  150. package/src/array.extensions.js +322 -0
  151. package/src/big.int.extension.js +163 -0
  152. package/src/{newClasses → classes}/descriptor.js +16 -12
  153. package/src/classes/index.js +51 -0
  154. package/src/classes/introspector.js +167 -0
  155. package/src/classes/param.parser.js +253 -0
  156. package/src/classes/pluggable.proxy.js +485 -0
  157. package/src/{newClasses → classes}/refmap.js +5 -3
  158. package/src/classes/symkeys.js +464 -0
  159. package/src/classes/type.js +427 -0
  160. package/src/function.extensions.js +818 -0
  161. package/src/global.this.js +304 -0
  162. package/src/index.js +56 -23
  163. package/src/json.extensions.js +109 -0
  164. package/src/map.extensions.js +144 -0
  165. package/src/number.extension.js +273 -0
  166. package/src/object.extensions.js +1237 -0
  167. package/src/proxy.extensions.js +229 -0
  168. package/src/reflect.extensions.js +346 -0
  169. package/src/regular.expression.extensions.js +451 -0
  170. package/src/{setextensions.js → set.extensions.js} +151 -2
  171. package/src/string.extensions.js +734 -0
  172. package/src/symbol.extensions.js +389 -0
  173. package/tests/newClasses/refmap.test.js +3 -2
  174. package/tsconfig.base.json +5 -3
  175. package/tsconfig.cjs.json +2 -2
  176. package/tsconfig.esm.json +2 -2
  177. package/dist/@nejs/basic-extensions.bundle.2.6.0.js +0 -17
  178. package/dist/@nejs/basic-extensions.bundle.2.6.0.js.map +0 -7
  179. package/dist/cjs/arrayextensions.d.ts +0 -10
  180. package/dist/cjs/arrayextensions.js +0 -73
  181. package/dist/cjs/arrayextensions.js.map +0 -1
  182. package/dist/cjs/functionextensions.js +0 -202
  183. package/dist/cjs/functionextensions.js.map +0 -1
  184. package/dist/cjs/globals.js +0 -166
  185. package/dist/cjs/globals.js.map +0 -1
  186. package/dist/cjs/mapextensions.js +0 -32
  187. package/dist/cjs/mapextensions.js.map +0 -1
  188. package/dist/cjs/newClasses/asyncIterable.js.map +0 -1
  189. package/dist/cjs/newClasses/deferred.js.map +0 -1
  190. package/dist/cjs/newClasses/descriptor.js.map +0 -1
  191. package/dist/cjs/newClasses/iterable.js.map +0 -1
  192. package/dist/cjs/newClasses/refmap.js.map +0 -1
  193. package/dist/cjs/newClasses/refset.js.map +0 -1
  194. package/dist/cjs/objectextensions.d.ts +0 -11
  195. package/dist/cjs/objectextensions.js +0 -232
  196. package/dist/cjs/objectextensions.js.map +0 -1
  197. package/dist/cjs/reflectextensions.js +0 -111
  198. package/dist/cjs/reflectextensions.js.map +0 -1
  199. package/dist/cjs/setextensions.d.ts +0 -2
  200. package/dist/cjs/setextensions.js.map +0 -1
  201. package/dist/cjs/stringextensions.js +0 -158
  202. package/dist/cjs/stringextensions.js.map +0 -1
  203. package/dist/cjs/symbolextensions.js +0 -69
  204. package/dist/cjs/symbolextensions.js.map +0 -1
  205. package/dist/cjs/weakrefextensions.js.map +0 -1
  206. package/dist/mjs/arrayextensions.d.ts +0 -10
  207. package/dist/mjs/arrayextensions.js +0 -70
  208. package/dist/mjs/arrayextensions.js.map +0 -1
  209. package/dist/mjs/functionextensions.js +0 -199
  210. package/dist/mjs/functionextensions.js.map +0 -1
  211. package/dist/mjs/globals.js +0 -163
  212. package/dist/mjs/globals.js.map +0 -1
  213. package/dist/mjs/mapextensions.js +0 -29
  214. package/dist/mjs/mapextensions.js.map +0 -1
  215. package/dist/mjs/newClasses/asyncIterable.js.map +0 -1
  216. package/dist/mjs/newClasses/deferred.js.map +0 -1
  217. package/dist/mjs/newClasses/descriptor.js.map +0 -1
  218. package/dist/mjs/newClasses/iterable.js.map +0 -1
  219. package/dist/mjs/newClasses/refmap.js.map +0 -1
  220. package/dist/mjs/newClasses/refset.js.map +0 -1
  221. package/dist/mjs/objectextensions.d.ts +0 -11
  222. package/dist/mjs/objectextensions.js +0 -229
  223. package/dist/mjs/objectextensions.js.map +0 -1
  224. package/dist/mjs/reflectextensions.js +0 -108
  225. package/dist/mjs/reflectextensions.js.map +0 -1
  226. package/dist/mjs/setextensions.d.ts +0 -2
  227. package/dist/mjs/setextensions.js.map +0 -1
  228. package/dist/mjs/stringextensions.js +0 -155
  229. package/dist/mjs/stringextensions.js.map +0 -1
  230. package/dist/mjs/symbolextensions.js +0 -66
  231. package/dist/mjs/symbolextensions.js.map +0 -1
  232. package/dist/mjs/weakrefextensions.js.map +0 -1
  233. package/src/arrayextensions.js +0 -75
  234. package/src/functionextensions.js +0 -225
  235. package/src/globals.js +0 -196
  236. package/src/mapextensions.js +0 -32
  237. package/src/objectextensions.js +0 -256
  238. package/src/reflectextensions.js +0 -118
  239. package/src/stringextensions.js +0 -166
  240. package/src/symbolextensions.js +0 -69
  241. /package/dist/cjs/{newClasses → classes}/asyncIterable.d.ts +0 -0
  242. /package/dist/cjs/{newClasses → classes}/deferred.d.ts +0 -0
  243. /package/dist/cjs/{newClasses → classes}/descriptor.d.ts +0 -0
  244. /package/dist/cjs/{newClasses → classes}/iterable.d.ts +0 -0
  245. /package/dist/cjs/{newClasses → classes}/refmap.d.ts +0 -0
  246. /package/dist/cjs/{newClasses → classes}/refset.d.ts +0 -0
  247. /package/dist/cjs/{functionextensions.d.ts → function.extensions.d.ts} +0 -0
  248. /package/dist/cjs/{globals.d.ts → global.this.d.ts} +0 -0
  249. /package/dist/cjs/{reflectextensions.d.ts → reflect.extensions.d.ts} +0 -0
  250. /package/dist/cjs/{stringextensions.d.ts → string.extensions.d.ts} +0 -0
  251. /package/dist/cjs/{weakrefextensions.d.ts → weakref.extensions.d.ts} +0 -0
  252. /package/dist/mjs/{newClasses → classes}/asyncIterable.d.ts +0 -0
  253. /package/dist/mjs/{newClasses → classes}/asyncIterable.js +0 -0
  254. /package/dist/mjs/{newClasses → classes}/deferred.d.ts +0 -0
  255. /package/dist/mjs/{newClasses → classes}/deferred.js +0 -0
  256. /package/dist/mjs/{newClasses → classes}/descriptor.d.ts +0 -0
  257. /package/dist/mjs/{newClasses → classes}/iterable.d.ts +0 -0
  258. /package/dist/mjs/{newClasses → classes}/iterable.js +0 -0
  259. /package/dist/mjs/{newClasses → classes}/refmap.d.ts +0 -0
  260. /package/dist/mjs/{newClasses → classes}/refset.d.ts +0 -0
  261. /package/dist/mjs/{newClasses → classes}/refset.js +0 -0
  262. /package/dist/mjs/{functionextensions.d.ts → function.extensions.d.ts} +0 -0
  263. /package/dist/mjs/{globals.d.ts → global.this.d.ts} +0 -0
  264. /package/dist/mjs/{reflectextensions.d.ts → reflect.extensions.d.ts} +0 -0
  265. /package/dist/mjs/{stringextensions.d.ts → string.extensions.d.ts} +0 -0
  266. /package/dist/mjs/{weakrefextensions.d.ts → weakref.extensions.d.ts} +0 -0
  267. /package/src/{newClasses → classes}/asyncIterable.js +0 -0
  268. /package/src/{newClasses → classes}/deferred.js +0 -0
  269. /package/src/{newClasses → classes}/iterable.js +0 -0
  270. /package/src/{newClasses → classes}/refset.js +0 -0
  271. /package/src/{weakrefextensions.js → weakref.extensions.js} +0 -0
@@ -0,0 +1,661 @@
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
+ /**
16
+ * The `isString` method does exactly what one would it expect. It returns
17
+ * true if the string matches typeof or instanceof as a string.
18
+ *
19
+ * @param {*} value checks to see if the `value` is a string
20
+ * @returns {boolean} `true` if it is a `String`, `false` otherwise
21
+ */
22
+ isString(value) {
23
+ return (value !== null && value !== undefined &&
24
+ (typeof value === 'string' || value instanceof String));
25
+ },
26
+ /**
27
+ * Conditionally returns a value based on whether the supplied
28
+ * `value` is a `String` or not. If the `value` is a `String`,
29
+ * the `thenValue` will be returned. If it is not a `String`,
30
+ * the `elseValue` will be returned instead.
31
+ *
32
+ * @param {any} value the value to check to determine if it is a
33
+ * `String`
34
+ * @param {any} thenValue the value to return if the supplied
35
+ * `value` is a `String`
36
+ * @param {any} elseValue the value to return if the supplied
37
+ * `value` is not a `String`
38
+ * @returns {any} either the `thenValue` or `elseValue` depending
39
+ * on if the supplied `value` is a `String`
40
+ *
41
+ * @example
42
+ * const str = 'hello'
43
+ * const num = 42
44
+ * ifString(str, 'is a string', 'not a string') // 'is a string'
45
+ * ifString(num, 'is a string', 'not a string') // 'not a string'
46
+ */
47
+ ifString(value, thenValue, elseValue) {
48
+ return isThenElse(this.isString(value), thenValue, elseValue);
49
+ },
50
+ /**
51
+ * A getter property that returns a pair of parentheses as an array.
52
+ * This property can be used when operations require a clear distinction
53
+ * between the opening and closing parentheses, such as parsing or
54
+ * matching balanced expressions in strings.
55
+ *
56
+ * @returns {[string, string]} An array containing a pair of strings: the
57
+ * opening parenthesis '(' as the first element, and the closing parenthesis
58
+ * ')' as the second element.
59
+ */
60
+ get parenthesisPair() {
61
+ return ['(', ')'];
62
+ },
63
+ /**
64
+ * A getter property that returns a pair of square brackets as an array.
65
+ * This property is particularly useful for operations that require a clear
66
+ * distinction between the opening and closing square brackets, such as
67
+ * parsing arrays in strings or matching balanced expressions within
68
+ * square brackets.
69
+ *
70
+ * @returns {[string, string]} An array containing a pair of strings: the
71
+ * opening square bracket '[' as the first element, and the closing square
72
+ * bracket ']' as the second element.
73
+ */
74
+ get squareBracketsPair() {
75
+ return ['[', ']'];
76
+ },
77
+ /**
78
+ * A getter property that returns a pair of curly brackets as an array.
79
+ * This property is particularly useful for operations that require a clear
80
+ * distinction between the opening and closing curly brackets, such as
81
+ * parsing objects in strings or matching balanced expressions within
82
+ * curly brackets. The returned array consists of the opening curly bracket
83
+ * '{' as the first element, and the closing curly bracket '}' as the
84
+ * second element.
85
+ *
86
+ * @returns {[string, string]} An array containing a pair of strings: the
87
+ * opening curly bracket '{' as the first element, and the closing curly
88
+ * bracket '}' as the second element.
89
+ */
90
+ get curlyBracketsPair() {
91
+ return ['{', '}'];
92
+ },
93
+ /**
94
+ * Generates a random string using base 36 (numbers and lowercase letters).
95
+ * This method is useful when you need a random string that includes both
96
+ * numbers and letters. The generated string does not include the leading
97
+ * '0.' that is part of the string representation of a random number in
98
+ * base 36.
99
+ *
100
+ * @returns {string} A random string of characters in base 36.
101
+ *
102
+ * @example
103
+ * const randomStr = StringExtensions.random36();
104
+ * console.log(randomStr); // Output: "3n5yzxjkf2o"
105
+ */
106
+ random36() {
107
+ return Math.random().toString(36).slice(2);
108
+ },
109
+ /**
110
+ * Generates a random string using base 16 (hexadecimal numbers).
111
+ * This method is useful when you need a random string that includes both
112
+ * numbers and letters in hexadecimal format. The generated string does not
113
+ * include the leading '0.' that is part of the string representation of a
114
+ * random number in base 16.
115
+ *
116
+ * @returns {string} A random string of characters in base 16.
117
+ *
118
+ * @example
119
+ * const randomStr = StringExtensions.random16();
120
+ * console.log(randomStr); // Output: "3a5f4c"
121
+ */
122
+ random16() {
123
+ return Math.random().toString(16).slice(2);
124
+ },
125
+ /**
126
+ * Generates a random RGB color code.
127
+ *
128
+ * This method generates a random hexadecimal number, slices off the
129
+ * leading '0.' and takes the first 6 characters. It then pads the
130
+ * end of the string with '0' until it is 6 characters long. The
131
+ * result is a string that can be used as a color code in CSS.
132
+ *
133
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
134
+ * code. Defaults to '#'.
135
+ *
136
+ * @returns {string} A random RGB color code.
137
+ *
138
+ * @example
139
+ * const randomColor = StringExtensions.randomRGB();
140
+ * console.log(randomColor); // Output: "#3a5f4c"
141
+ */
142
+ randomRGBHex(prefix = '#') {
143
+ const hex = Math.random().toString(16).slice(2).substring(0, 6);
144
+ return `${prefix}${hex.padEnd(6, '0')}`;
145
+ },
146
+ /**
147
+ * Generates a random ARGB color code.
148
+ *
149
+ * This method generates a random hexadecimal number, slices off the
150
+ * leading '0.' and takes the first 8 characters. It then pads the
151
+ * start of the string with '0' until it is 6 characters long and the
152
+ * end of the string with '0' until it is 8 characters long. The
153
+ * result is a string that can be used as a color code in CSS.
154
+ *
155
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
156
+ * code. Defaults to '#'.
157
+ *
158
+ * @returns {string} A random ARGB color code.
159
+ *
160
+ * @example
161
+ * const randomColor = StringExtensions.randomARGB();
162
+ * console.log(randomColor); // Output: "#3a5f4c00"
163
+ */
164
+ randomARGBHex(prefix = '#') {
165
+ const hex = Math.random().toString(16).slice(2).substring(0, 8);
166
+ return `${prefix}${hex.padStart(6, '0').padEnd(8, '0')}`;
167
+ },
168
+ /**
169
+ * Generates a random RGBA color code.
170
+ *
171
+ * This method generates a random hexadecimal number, slices off the
172
+ * leading '0.' and takes the first 8 characters. It then pads the
173
+ * start of the string with '0' until it is 6 characters long and the
174
+ * end of the string with '0' until it is 8 characters long. The
175
+ * result is a string that can be used as a color code in CSS.
176
+ *
177
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
178
+ * code. Defaults to '#'.
179
+ *
180
+ * @returns {string} A random RGBA color code.
181
+ *
182
+ * @example
183
+ * const randomColor = StringExtensions.randomRGBA();
184
+ * console.log(randomColor); // Output: "#3a5f4c00"
185
+ */
186
+ randomRGBAHex(prefix = '#') {
187
+ const hex = Math.random().toString(16).slice(2).substring(0, 8);
188
+ return `${prefix}${hex.padStart(6, '0').padStart(8, '0')}`;
189
+ },
190
+ /**
191
+ * Generates a random RGB color code.
192
+ *
193
+ * This method generates a random hexadecimal number, slices off the
194
+ * leading '0.' and pads the end of the string with '0' until it is
195
+ * 8 characters long. It then parses the first 6 characters into
196
+ * three separate 2-character strings, each representing a color
197
+ * component (red, green, blue) in hexadecimal format. These strings
198
+ * are then converted into decimal format and used to construct an
199
+ * RGB color code.
200
+ *
201
+ * @returns {string} A random RGB color code.
202
+ *
203
+ * @example
204
+ * const randomColor = StringExtensions.randomRGB();
205
+ * console.log(randomColor); // Output: "rgb(58,95,76)"
206
+ */
207
+ randomRGB() {
208
+ const hex = Math.random().toString(16).slice(2).padEnd(8, '0');
209
+ const red = parseInt(hex.substring(0, 2), 16);
210
+ const green = parseInt(hex.substring(2, 4), 16);
211
+ const blue = parseInt(hex.substring(4, 6), 16);
212
+ return `rgb(${red}, ${green}, ${blue})`;
213
+ },
214
+ /**
215
+ * Generates a random RGBA color code with optional forced color values.
216
+ *
217
+ * This method generates a random hexadecimal number, slices off the
218
+ * leading '0.' and pads the end of the string with '0' until it is
219
+ * 8 characters long. It then parses the first 8 characters into
220
+ * four separate 2-character strings, each representing a color
221
+ * component (red, green, blue, alpha) in hexadecimal format. These strings
222
+ * are then converted into decimal format and used to construct an
223
+ * RGBA color code.
224
+ *
225
+ * If a color component is provided in the `force` parameter, it will
226
+ * be used instead of a random value for that component.
227
+ *
228
+ * @param {Object} force - An object with properties for each color
229
+ * component (red, green, blue, alpha) that should be forced to a
230
+ * specific value. If a property is undefined or not provided, a
231
+ * random value will be used for that component.
232
+ * @param {number} force.red - The red component (0-255).
233
+ * @param {number} force.green - The green component (0-255).
234
+ * @param {number} force.blue - The blue component (0-255).
235
+ * @param {number} force.alpha - The alpha component (0.0-1.0).
236
+ *
237
+ * @returns {string} A random RGBA color code.
238
+ *
239
+ * @example
240
+ * const randomColor = StringExtensions.randomRGBA();
241
+ * console.log(randomColor); // Output: "rgba(58,95,76,0.50)"
242
+ *
243
+ * const forcedGreen = StringExtensions.randomRGBA({ green: 255 });
244
+ * console.log(forcedGreen); // Output: "rgba(58,255,76,0.50)"
245
+ */
246
+ randomRGBA(force = {
247
+ red: undefined,
248
+ green: undefined,
249
+ blue: undefined,
250
+ alpha: undefined
251
+ }) {
252
+ const hex = Math.random().toString(16).slice(2).padEnd(8, '0');
253
+ const red = force.red ?? parseInt(hex.substring(0, 2), 16);
254
+ const green = force.green ?? parseInt(hex.substring(2, 4), 16);
255
+ const blue = force.blue ?? parseInt(hex.substring(4, 6), 16);
256
+ const alpha = force.alpha ??
257
+ (parseInt(hex.substring(6, 8), 16) / 255.0) * 1.0;
258
+ return `rgba(${red}, ${green}, ${blue}, ${alpha.toFixed(2)})`;
259
+ },
260
+ /**
261
+ * Applies Select Graphic Rendition (SGR) parameters to a given message for
262
+ * styling in terminal environments. This function allows for the dynamic
263
+ * styling of text output using ANSI escape codes. It supports a variety of
264
+ * modes such as color, brightness, and text decorations like bold or underline.
265
+ *
266
+ * @param {string} message The message to be styled.
267
+ * @param {...string} useModes A series of strings representing the desired
268
+ * styling modes. Modes can include colors (e.g., 'red', 'blue'), brightness
269
+ * ('bright'), foreground/background ('fg', 'bg'), and text decorations
270
+ * ('bold', 'underline'). Modes can be combined in a single string using
271
+ * commas or passed as separate arguments.
272
+ *
273
+ * Colors:
274
+ * ```
275
+ * 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
276
+ * ```
277
+ * Color Specifiers:
278
+ * ```
279
+ * 'fg' -> foreground | 'bg' -> background | 'bright' -> bright colors
280
+ * ```
281
+ *
282
+ * Modes:
283
+ * ```
284
+ * 'blink' or 'k' | 'conceal' or 'c' | 'italics' or 'i' | 'strike' or 's'
285
+ * 'bold' or 'b' | 'dim' or 'd' | 'negative' or 'n' | 'underline' or 'u'
286
+ * ```
287
+ *
288
+ * Examples:
289
+ * - `sgr('Hello', 'red')` applies red color to 'Hello'.
290
+ * - `sgr('World', 'green,bold')` applies green color and bold styling
291
+ * to 'World'.
292
+ * - `sgr('Example', 'bluebgbright')` applies bright blue
293
+ * background color.
294
+ *
295
+ * Short hand syntax is also allowed:
296
+ * - `sgr('hello', 'biu')` applies bold, italics and underline
297
+ * - `sgr('hello', 'bi,redfg')` applies bold, italics and red foreground
298
+ *
299
+ * As a bonus, there is a secret getter applied to the return string that
300
+ * allows you to invoke `sgr(...).show` to automatically log the output to
301
+ * `console.log`. This is done by wrapping the output string in `Object()`
302
+ * to make it a `String` instance and then adding the property descriptor.
303
+ * A custom `Symbol` is applied to make it evaluate in nodejs as though it
304
+ * were a normal string. To strip the extras, wrap the output in `String()`
305
+ *
306
+ * @returns {string} The message wrapped in ANSI escape codes corresponding
307
+ * to the specified modes. The returned string, when printed to a terminal,
308
+ * displays the styled message. Additional properties are attached to the
309
+ * result for utility purposes, such as 'show' for immediate console output.
310
+ */
311
+ sgr(message, ...useModes) {
312
+ const colors = Object.assign(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'], {
313
+ isBG: a => !!/bg/i.exec(a),
314
+ isBright: a => !!/bright/i.exec(a),
315
+ isColor: a => {
316
+ let color = colors.find(c => new RegExp(c, 'i').exec(a));
317
+ return [!!color, colors.indexOf(color)];
318
+ },
319
+ });
320
+ const arrayifyString = s => {
321
+ if (Array.isArray(s)) {
322
+ let results = [];
323
+ for (const i of s) {
324
+ results = [...results, ...arrayifyString(i)];
325
+ }
326
+ return results.flat().filter(i => i.length);
327
+ }
328
+ if (!s || typeof s !== 'string') {
329
+ return [''];
330
+ }
331
+ else if (s.includes(',')) {
332
+ return arrayifyString(s.split(','));
333
+ }
334
+ else {
335
+ if (!colors.isColor(s)[0] && s.length > 1) {
336
+ return [...s];
337
+ }
338
+ else
339
+ return [s];
340
+ }
341
+ };
342
+ let modes = arrayifyString(useModes);
343
+ const sgrModes = {
344
+ blink: ['\x1b[5m', '\x1b[25m', 'k'],
345
+ bold: ['\x1b[1m', '\x1b[22m', 'b'],
346
+ conceal: ['\x1b[8m', '\x1b[28m', 'c'],
347
+ dim: ['\x1b[2m', '\x1b[22m', 'd'],
348
+ italics: ['\x1b[3m', '\x1b[23m', 'i'],
349
+ negative: ['\x1b[7m', '\x1b[27m', 'n'],
350
+ strike: ['\x1b[9m', '\x1b[29m', 's'],
351
+ underline: ['\x1b[4m', '\x1b[24m', 'u'],
352
+ };
353
+ Object.values(sgrModes).forEach(mode => sgrModes[mode[2]] = mode);
354
+ const codes = a => {
355
+ let open = '', close = '', mode = String(a).toLowerCase();
356
+ let [_isColor, colorIndex] = colors.isColor(mode);
357
+ if (_isColor) {
358
+ open = colors.isBG(mode)
359
+ ? `\x1b[${colors.isBright(mode) ? 10 : 4}${colorIndex}m`
360
+ : `\x1b[${colors.isBright(mode) ? 9 : 3}${colorIndex}m`;
361
+ close = colors.isBG(mode) ? '\x1b[49m' : `\x1b[39m`;
362
+ }
363
+ else if (sgrModes[mode]) {
364
+ open = sgrModes[mode][0];
365
+ close = sgrModes[mode][1];
366
+ }
367
+ return [open, close];
368
+ };
369
+ const onOrder = modes.map(key => codes(key)[0]).join('');
370
+ const offOrder = modes.map(key => codes(key)[1]).reverse().join('');
371
+ let result = Object(`${onOrder}${message}${offOrder}`);
372
+ Object.defineProperties(result, {
373
+ show: {
374
+ get() { console.log(String(this)); return this; },
375
+ enumerable: false,
376
+ },
377
+ [Symbol.for('nodejs.util.inspect.custom')]: {
378
+ value(depth, options, inspect) {
379
+ return inspect(String(this), options);
380
+ },
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
+ const { isString: pIsString, ifString: pIfString } = exports.StringExtensions.patches;
494
+ /**
495
+ * `StringPrototypeExtensions` provides a set of utility methods that are
496
+ * added to the `String` prototype. This allows all string instances to
497
+ * access new functionality directly, enhancing their capabilities beyond
498
+ * the standard `String` class methods. These extensions are applied using
499
+ * the `Patch` class from '@nejs/extension', ensuring that they do not
500
+ * interfere with the global namespace or existing properties.
501
+ *
502
+ * The extensions include methods for extracting substrings based on
503
+ * specific tokens, checking the presence of certain patterns, and more,
504
+ * making string manipulation tasks more convenient and expressive.
505
+ */
506
+ exports.StringPrototypeExtensions = new extension_1.Patch(String.prototype, {
507
+ [extension_1.Patch.kMutablyHidden]: {
508
+ /**
509
+ * Determines if the current object is a string.
510
+ *
511
+ * This getter uses the `pIsString` function from the
512
+ * `StringExtensions` patch to check if the current object
513
+ * (`this`) is a string.
514
+ *
515
+ * @type {boolean}
516
+ * @readonly
517
+ *
518
+ * @example
519
+ * const str = "Hello, World!"
520
+ * console.log(str.isString) // Output: true
521
+ *
522
+ * const notStr = 123
523
+ * console.log(notStr.isString) // Output: false
524
+ */
525
+ get isString() {
526
+ return pIsString(this);
527
+ },
528
+ /**
529
+ * Checks if the current object is a string and returns the
530
+ * corresponding value based on the result.
531
+ *
532
+ * This method uses the `pIfString` function from the
533
+ * `StringExtensions` patch to determine if the current object
534
+ * (`this`) is a string. If it is a string, the `thenValue` is returned.
535
+ * Otherwise, the `elseValue` is returned.
536
+ *
537
+ * @param {any} thenValue - The value to return if the current object
538
+ * is a string.
539
+ * @param {any} elseValue - The value to return if the current object
540
+ * is not a string.
541
+ * @returns {any} The `thenValue` if the current object is a string, or
542
+ * the `elseValue` if it is not a string.
543
+ *
544
+ * @example
545
+ * const str = "Hello, World!"
546
+ * // 'Is a string'
547
+ * console.log(str.ifString('Is a string', 'Not a string'))
548
+ *
549
+ * const notStr = 123
550
+ * // 'Not a string'
551
+ * console.log(notStr.ifString('Is a string', 'Not a string'))
552
+ */
553
+ ifString(thenValue, elseValue) {
554
+ return pIfString(this, thenValue, elseValue);
555
+ },
556
+ /**
557
+ * Returns an object representation of the string instance.
558
+ *
559
+ * This getter method creates and returns an object that wraps
560
+ * the string instance, allowing it to be treated as an object.
561
+ * The returned object is created using the `Object()`
562
+ * constructor, which takes the string instance as its argument.
563
+ *
564
+ * @type {Object}
565
+ * @readonly
566
+ *
567
+ * @example
568
+ * const str = 'Hello, World!'
569
+ * console.log(typeof str) // 'string'
570
+ * console.log(typeof str.instance) // 'object'
571
+ */
572
+ get instance() {
573
+ return Object(this);
574
+ },
575
+ /**
576
+ * Extracts a substring from the current string, starting at a given offset
577
+ * and bounded by specified opening and closing tokens. This method is
578
+ * particularly useful for parsing nested structures or quoted strings,
579
+ * where the level of nesting or the presence of escape characters must
580
+ * be considered.
581
+ *
582
+ * @param {number} offset The position in the string from which to start the
583
+ * search for the substring.
584
+ * @param {[string, string]} tokens An array containing two strings: the
585
+ * opening and closing tokens that define the boundaries of the substring
586
+ * to be extracted.
587
+ * @returns {Object} An object with two properties: `extracted`, the
588
+ * extracted substring, and `newOffset`, the position in the original
589
+ * string immediately after the end of the extracted substring. If no
590
+ * substring is found, `extracted` is `null` and `newOffset` is the same
591
+ * as the input offset.
592
+ */
593
+ extractSubstring(offset = 0, tokens = parenthesisPair) {
594
+ let [openToken, closeToken] = tokens;
595
+ let depth = 0;
596
+ let start = -1;
597
+ let end = -1;
598
+ let leadingToken = '';
599
+ let firstToken = 0;
600
+ for (let i = offset; i < this.length; i++) {
601
+ const char = this[i];
602
+ if (char === openToken) {
603
+ depth++;
604
+ if (start === -1)
605
+ start = i;
606
+ }
607
+ else if (char === closeToken) {
608
+ depth--;
609
+ if (depth === 0) {
610
+ end = i;
611
+ break;
612
+ }
613
+ }
614
+ }
615
+ let lRange = [
616
+ Math.max(0, start - 100),
617
+ start
618
+ ];
619
+ let leading = [...this.substring(lRange[0], lRange[1])].reverse().join('');
620
+ let reversedLeadingToken;
621
+ try {
622
+ reversedLeadingToken = /([^ \,\"\'\`]+)/.exec(leading)[1] ?? '';
623
+ leadingToken = [...reversedLeadingToken].reverse().join('');
624
+ }
625
+ catch (ignored) { }
626
+ if (start !== -1 && end !== -1) {
627
+ const sliceRange = [start, end + 1];
628
+ const extracted = this.slice(sliceRange[0], sliceRange[1]);
629
+ return {
630
+ extracted,
631
+ range: [start, end],
632
+ newOffset: end + 1,
633
+ leadingToken,
634
+ };
635
+ }
636
+ else {
637
+ return {
638
+ extracted: null,
639
+ range: [start, end],
640
+ newOffset: offset,
641
+ leadingToken,
642
+ };
643
+ }
644
+ },
645
+ },
646
+ });
647
+ // NOTE to self; this is repeated here otherwise a circular reference from
648
+ // Object<->Function<->Global occurs. See original source in global.this.js
649
+ // {@see globalThis.isThenElse}
650
+ function isThenElse(bv, tv, ev) {
651
+ if (arguments.length > 1) {
652
+ var _then = isFunction(tv) ? tv(bv) : tv;
653
+ if (arguments.length > 2) {
654
+ var _else = isFunction(ev) ? tv(bv) : ev;
655
+ return bv ? _then : _else;
656
+ }
657
+ return bv || _then;
658
+ }
659
+ return bv;
660
+ }
661
+ //# sourceMappingURL=string.extensions.js.map