@nejs/basic-extensions 2.6.0 → 2.8.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 +6129 -1574
  3. package/dist/@nejs/basic-extensions.bundle.2.7.0.js +19 -0
  4. package/dist/@nejs/basic-extensions.bundle.2.7.0.js.map +7 -0
  5. package/dist/cjs/array.extensions.d.ts +39 -0
  6. package/dist/cjs/array.extensions.js +303 -0
  7. package/dist/cjs/array.extensions.js.map +1 -0
  8. package/dist/cjs/big.int.extension.d.ts +31 -0
  9. package/dist/cjs/big.int.extension.js +164 -0
  10. package/dist/cjs/big.int.extension.js.map +1 -0
  11. package/dist/cjs/{newClasses → classes}/asyncIterable.js +32 -44
  12. package/dist/cjs/classes/asyncIterable.js.map +1 -0
  13. package/dist/cjs/{newClasses → classes}/deferred.js +66 -138
  14. package/dist/cjs/classes/deferred.js.map +1 -0
  15. package/dist/cjs/{newClasses → classes}/descriptor.js +56 -90
  16. package/dist/cjs/classes/descriptor.js.map +1 -0
  17. package/dist/cjs/classes/index.d.ts +13 -0
  18. package/dist/cjs/classes/index.js +57 -0
  19. package/dist/cjs/classes/index.js.map +1 -0
  20. package/dist/cjs/classes/introspector.d.ts +20 -0
  21. package/dist/cjs/classes/introspector.js +130 -0
  22. package/dist/cjs/classes/introspector.js.map +1 -0
  23. package/dist/cjs/{newClasses → classes}/iterable.js +42 -63
  24. package/dist/cjs/classes/iterable.js.map +1 -0
  25. package/dist/cjs/classes/param.parser.d.ts +227 -0
  26. package/dist/cjs/classes/param.parser.js +242 -0
  27. package/dist/cjs/classes/param.parser.js.map +1 -0
  28. package/dist/cjs/classes/pluggable.proxy.d.ts +152 -0
  29. package/dist/cjs/classes/pluggable.proxy.js +444 -0
  30. package/dist/cjs/classes/pluggable.proxy.js.map +1 -0
  31. package/dist/cjs/{newClasses → classes}/refmap.js +18 -30
  32. package/dist/cjs/classes/refmap.js.map +1 -0
  33. package/dist/cjs/{newClasses → classes}/refset.js +28 -47
  34. package/dist/cjs/classes/refset.js.map +1 -0
  35. package/dist/cjs/classes/symkeys.d.ts +292 -0
  36. package/dist/cjs/classes/symkeys.js +424 -0
  37. package/dist/cjs/classes/symkeys.js.map +1 -0
  38. package/dist/cjs/classes/type.d.ts +56 -0
  39. package/dist/cjs/classes/type.js +405 -0
  40. package/dist/cjs/classes/type.js.map +1 -0
  41. package/dist/cjs/function.extensions.js +757 -0
  42. package/dist/cjs/function.extensions.js.map +1 -0
  43. package/dist/cjs/global.this.js +261 -0
  44. package/dist/cjs/global.this.js.map +1 -0
  45. package/dist/cjs/index.d.ts +4 -3
  46. package/dist/cjs/index.js +62 -32
  47. package/dist/cjs/index.js.map +1 -1
  48. package/dist/cjs/json.extensions.d.ts +2 -0
  49. package/dist/cjs/json.extensions.js +107 -0
  50. package/dist/cjs/json.extensions.js.map +1 -0
  51. package/dist/{mjs/mapextensions.d.ts → cjs/map.extensions.d.ts} +1 -0
  52. package/dist/cjs/map.extensions.js +142 -0
  53. package/dist/cjs/map.extensions.js.map +1 -0
  54. package/dist/cjs/number.extension.d.ts +44 -0
  55. package/dist/cjs/number.extension.js +260 -0
  56. package/dist/cjs/number.extension.js.map +1 -0
  57. package/dist/cjs/object.extensions.d.ts +62 -0
  58. package/dist/cjs/object.extensions.js +1116 -0
  59. package/dist/cjs/object.extensions.js.map +1 -0
  60. package/dist/cjs/proxy.extensions.d.ts +2 -0
  61. package/dist/cjs/proxy.extensions.js +207 -0
  62. package/dist/cjs/proxy.extensions.js.map +1 -0
  63. package/dist/cjs/reflect.extensions.js +316 -0
  64. package/dist/cjs/reflect.extensions.js.map +1 -0
  65. package/dist/cjs/regular.expression.extensions.d.ts +2 -0
  66. package/dist/cjs/regular.expression.extensions.js +423 -0
  67. package/dist/cjs/regular.expression.extensions.js.map +1 -0
  68. package/dist/cjs/set.extensions.d.ts +40 -0
  69. package/dist/cjs/{setextensions.js → set.extensions.js} +150 -2
  70. package/dist/cjs/set.extensions.js.map +1 -0
  71. package/dist/cjs/string.extensions.js +471 -0
  72. package/dist/cjs/string.extensions.js.map +1 -0
  73. package/dist/{mjs/symbolextensions.d.ts → cjs/symbol.extensions.d.ts} +1 -0
  74. package/dist/cjs/symbol.extensions.js +259 -0
  75. package/dist/cjs/symbol.extensions.js.map +1 -0
  76. package/dist/cjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
  77. package/dist/cjs/weakref.extensions.js.map +1 -0
  78. package/dist/mjs/array.extensions.d.ts +39 -0
  79. package/dist/mjs/array.extensions.js +300 -0
  80. package/dist/mjs/array.extensions.js.map +1 -0
  81. package/dist/mjs/big.int.extension.d.ts +31 -0
  82. package/dist/mjs/big.int.extension.js +161 -0
  83. package/dist/mjs/big.int.extension.js.map +1 -0
  84. package/dist/mjs/classes/asyncIterable.js.map +1 -0
  85. package/dist/mjs/classes/deferred.js.map +1 -0
  86. package/dist/mjs/{newClasses → classes}/descriptor.js +7 -4
  87. package/dist/mjs/classes/descriptor.js.map +1 -0
  88. package/dist/mjs/classes/index.d.ts +13 -0
  89. package/dist/mjs/classes/index.js +40 -0
  90. package/dist/mjs/classes/index.js.map +1 -0
  91. package/dist/mjs/classes/introspector.d.ts +20 -0
  92. package/dist/mjs/classes/introspector.js +126 -0
  93. package/dist/mjs/classes/introspector.js.map +1 -0
  94. package/dist/mjs/classes/iterable.js.map +1 -0
  95. package/dist/mjs/classes/param.parser.d.ts +227 -0
  96. package/dist/mjs/classes/param.parser.js +238 -0
  97. package/dist/mjs/classes/param.parser.js.map +1 -0
  98. package/dist/mjs/classes/pluggable.proxy.d.ts +152 -0
  99. package/dist/mjs/classes/pluggable.proxy.js +438 -0
  100. package/dist/mjs/classes/pluggable.proxy.js.map +1 -0
  101. package/dist/mjs/{newClasses → classes}/refmap.js +3 -3
  102. package/dist/mjs/classes/refmap.js.map +1 -0
  103. package/dist/mjs/classes/refset.js.map +1 -0
  104. package/dist/mjs/classes/symkeys.d.ts +292 -0
  105. package/dist/mjs/classes/symkeys.js +420 -0
  106. package/dist/mjs/classes/symkeys.js.map +1 -0
  107. package/dist/mjs/classes/type.d.ts +56 -0
  108. package/dist/mjs/classes/type.js +401 -0
  109. package/dist/mjs/classes/type.js.map +1 -0
  110. package/dist/mjs/function.extensions.js +754 -0
  111. package/dist/mjs/function.extensions.js.map +1 -0
  112. package/dist/mjs/global.this.js +258 -0
  113. package/dist/mjs/global.this.js.map +1 -0
  114. package/dist/mjs/index.d.ts +4 -3
  115. package/dist/mjs/index.js +49 -19
  116. package/dist/mjs/index.js.map +1 -1
  117. package/dist/mjs/json.extensions.d.ts +2 -0
  118. package/dist/mjs/json.extensions.js +104 -0
  119. package/dist/mjs/json.extensions.js.map +1 -0
  120. package/dist/{cjs/mapextensions.d.ts → mjs/map.extensions.d.ts} +1 -0
  121. package/dist/mjs/map.extensions.js +139 -0
  122. package/dist/mjs/map.extensions.js.map +1 -0
  123. package/dist/mjs/number.extension.d.ts +44 -0
  124. package/dist/mjs/number.extension.js +257 -0
  125. package/dist/mjs/number.extension.js.map +1 -0
  126. package/dist/mjs/object.extensions.d.ts +62 -0
  127. package/dist/mjs/object.extensions.js +1112 -0
  128. package/dist/mjs/object.extensions.js.map +1 -0
  129. package/dist/mjs/proxy.extensions.d.ts +2 -0
  130. package/dist/mjs/proxy.extensions.js +204 -0
  131. package/dist/mjs/proxy.extensions.js.map +1 -0
  132. package/dist/mjs/reflect.extensions.js +313 -0
  133. package/dist/mjs/reflect.extensions.js.map +1 -0
  134. package/dist/mjs/regular.expression.extensions.d.ts +2 -0
  135. package/dist/mjs/regular.expression.extensions.js +420 -0
  136. package/dist/mjs/regular.expression.extensions.js.map +1 -0
  137. package/dist/mjs/set.extensions.d.ts +40 -0
  138. package/dist/mjs/{setextensions.js → set.extensions.js} +149 -1
  139. package/dist/mjs/set.extensions.js.map +1 -0
  140. package/dist/mjs/string.extensions.js +468 -0
  141. package/dist/mjs/string.extensions.js.map +1 -0
  142. package/dist/{cjs/symbolextensions.d.ts → mjs/symbol.extensions.d.ts} +1 -0
  143. package/dist/mjs/symbol.extensions.js +256 -0
  144. package/dist/mjs/symbol.extensions.js.map +1 -0
  145. package/dist/mjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
  146. package/dist/mjs/weakref.extensions.js.map +1 -0
  147. package/docs/index.html +24045 -5805
  148. package/package.json +6 -4
  149. package/repl.bootstrap.js +21 -0
  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 +108 -0
  164. package/src/map.extensions.js +144 -0
  165. package/src/number.extension.js +273 -0
  166. package/src/object.extensions.js +1222 -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 +515 -0
  172. package/src/symbol.extensions.js +268 -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.5.0.js +0 -8
  178. package/dist/@nejs/basic-extensions.bundle.2.5.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,515 @@
1
+ import { Patch } from '@nejs/extension'
2
+
3
+ const parenthesisPair = ['(', ')']
4
+
5
+ /**
6
+ * `StringExtensions` is a patch for the JavaScript built-in `String` class. It
7
+ * adds utility methods to the `String` class without modifying the global namespace
8
+ * directly. This patch includes methods for key validation, object type checking,
9
+ * and retrieving the string tag of an object. These methods are useful for
10
+ * enhancing the capabilities of the standard `String` class with additional
11
+ * utility functions.
12
+ */
13
+ export const StringExtensions = new Patch(String, {
14
+ /**
15
+ * The `isString` method does exactly what one would it expect. It returns
16
+ * true if the string matches typeof or instanceof as a string.
17
+ *
18
+ * @param {*} value checks to see if the `value` is a string
19
+ * @returns {boolean} `true` if it is a `String`, `false` otherwise
20
+ */
21
+ isString(value) {
22
+ return (
23
+ value !== null && value !== undefined &&
24
+ (typeof value === 'string' || value instanceof String)
25
+ )
26
+ },
27
+
28
+ /**
29
+ * Conditionally returns a value based on whether the supplied
30
+ * `value` is a `String` or not. If the `value` is a `String`,
31
+ * the `thenValue` will be returned. If it is not a `String`,
32
+ * the `elseValue` will be returned instead.
33
+ *
34
+ * @param {any} value the value to check to determine if it is a
35
+ * `String`
36
+ * @param {any} thenValue the value to return if the supplied
37
+ * `value` is a `String`
38
+ * @param {any} elseValue the value to return if the supplied
39
+ * `value` is not a `String`
40
+ * @returns {any} either the `thenValue` or `elseValue` depending
41
+ * on if the supplied `value` is a `String`
42
+ *
43
+ * @example
44
+ * const str = 'hello'
45
+ * const num = 42
46
+ * ifString(str, 'is a string', 'not a string') // 'is a string'
47
+ * ifString(num, 'is a string', 'not a string') // 'not a string'
48
+ */
49
+ ifString(value, thenValue, elseValue) {
50
+ return isThenElse(this.isString(value), thenValue, elseValue)
51
+ },
52
+
53
+ /**
54
+ * A getter property that returns a pair of parentheses as an array.
55
+ * This property can be used when operations require a clear distinction
56
+ * between the opening and closing parentheses, such as parsing or
57
+ * matching balanced expressions in strings.
58
+ *
59
+ * @returns {[string, string]} An array containing a pair of strings: the
60
+ * opening parenthesis '(' as the first element, and the closing parenthesis
61
+ * ')' as the second element.
62
+ */
63
+ get parenthesisPair() {
64
+ return ['(', ')'];
65
+ },
66
+
67
+ /**
68
+ * A getter property that returns a pair of square brackets as an array.
69
+ * This property is particularly useful for operations that require a clear
70
+ * distinction between the opening and closing square brackets, such as
71
+ * parsing arrays in strings or matching balanced expressions within
72
+ * square brackets.
73
+ *
74
+ * @returns {[string, string]} An array containing a pair of strings: the
75
+ * opening square bracket '[' as the first element, and the closing square
76
+ * bracket ']' as the second element.
77
+ */
78
+ get squareBracketsPair() {
79
+ return ['[', ']'];
80
+ },
81
+
82
+ /**
83
+ * A getter property that returns a pair of curly brackets as an array.
84
+ * This property is particularly useful for operations that require a clear
85
+ * distinction between the opening and closing curly brackets, such as
86
+ * parsing objects in strings or matching balanced expressions within
87
+ * curly brackets. The returned array consists of the opening curly bracket
88
+ * '{' as the first element, and the closing curly bracket '}' as the
89
+ * second element.
90
+ *
91
+ * @returns {[string, string]} An array containing a pair of strings: the
92
+ * opening curly bracket '{' as the first element, and the closing curly
93
+ * bracket '}' as the second element.
94
+ */
95
+ get curlyBracketsPair() {
96
+ return ['{', '}'];
97
+ },
98
+
99
+ /**
100
+ * Generates a random string using base 36 (numbers and lowercase letters).
101
+ * This method is useful when you need a random string that includes both
102
+ * numbers and letters. The generated string does not include the leading
103
+ * '0.' that is part of the string representation of a random number in
104
+ * base 36.
105
+ *
106
+ * @returns {string} A random string of characters in base 36.
107
+ *
108
+ * @example
109
+ * const randomStr = StringExtensions.random36();
110
+ * console.log(randomStr); // Output: "3n5yzxjkf2o"
111
+ */
112
+ random36() {
113
+ return Math.random().toString(36).slice(2)
114
+ },
115
+
116
+ /**
117
+ * Generates a random string using base 16 (hexadecimal numbers).
118
+ * This method is useful when you need a random string that includes both
119
+ * numbers and letters in hexadecimal format. The generated string does not
120
+ * include the leading '0.' that is part of the string representation of a
121
+ * random number in base 16.
122
+ *
123
+ * @returns {string} A random string of characters in base 16.
124
+ *
125
+ * @example
126
+ * const randomStr = StringExtensions.random16();
127
+ * console.log(randomStr); // Output: "3a5f4c"
128
+ */
129
+ random16() {
130
+ return Math.random().toString(16).slice(2)
131
+ },
132
+
133
+ /**
134
+ * Generates a random RGB color code.
135
+ *
136
+ * This method generates a random hexadecimal number, slices off the
137
+ * leading '0.' and takes the first 6 characters. It then pads the
138
+ * end of the string with '0' until it is 6 characters long. The
139
+ * result is a string that can be used as a color code in CSS.
140
+ *
141
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
142
+ * code. Defaults to '#'.
143
+ *
144
+ * @returns {string} A random RGB color code.
145
+ *
146
+ * @example
147
+ * const randomColor = StringExtensions.randomRGB();
148
+ * console.log(randomColor); // Output: "#3a5f4c"
149
+ */
150
+ randomRGBHex(prefix = '#') {
151
+ const hex = Math.random().toString(16).slice(2).substring(0,6)
152
+ return `${prefix}${hex.padEnd(6, '0')}`
153
+ },
154
+
155
+ /**
156
+ * Generates a random ARGB color code.
157
+ *
158
+ * This method generates a random hexadecimal number, slices off the
159
+ * leading '0.' and takes the first 8 characters. It then pads the
160
+ * start of the string with '0' until it is 6 characters long and the
161
+ * end of the string with '0' until it is 8 characters long. The
162
+ * result is a string that can be used as a color code in CSS.
163
+ *
164
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
165
+ * code. Defaults to '#'.
166
+ *
167
+ * @returns {string} A random ARGB color code.
168
+ *
169
+ * @example
170
+ * const randomColor = StringExtensions.randomARGB();
171
+ * console.log(randomColor); // Output: "#3a5f4c00"
172
+ */
173
+ randomARGBHex(prefix = '#') {
174
+ const hex = Math.random().toString(16).slice(2).substring(0,8)
175
+ return `${prefix}${hex.padStart(6, '0').padEnd(8, '0')}`
176
+ },
177
+
178
+ /**
179
+ * Generates a random RGBA color code.
180
+ *
181
+ * This method generates a random hexadecimal number, slices off the
182
+ * leading '0.' and takes the first 8 characters. It then pads the
183
+ * start of the string with '0' until it is 6 characters long and the
184
+ * end of the string with '0' until it is 8 characters long. The
185
+ * result is a string that can be used as a color code in CSS.
186
+ *
187
+ * @param {string} [prefix='#'] - The prefix to prepend to the color
188
+ * code. Defaults to '#'.
189
+ *
190
+ * @returns {string} A random RGBA color code.
191
+ *
192
+ * @example
193
+ * const randomColor = StringExtensions.randomRGBA();
194
+ * console.log(randomColor); // Output: "#3a5f4c00"
195
+ */
196
+ randomRGBAHex(prefix = '#') {
197
+ const hex = Math.random().toString(16).slice(2).substring(0,8)
198
+ return `${prefix}${hex.padStart(6, '0').padStart(8, '0')}`
199
+ },
200
+
201
+ /**
202
+ * Generates a random RGB color code.
203
+ *
204
+ * This method generates a random hexadecimal number, slices off the
205
+ * leading '0.' and pads the end of the string with '0' until it is
206
+ * 8 characters long. It then parses the first 6 characters into
207
+ * three separate 2-character strings, each representing a color
208
+ * component (red, green, blue) in hexadecimal format. These strings
209
+ * are then converted into decimal format and used to construct an
210
+ * RGB color code.
211
+ *
212
+ * @returns {string} A random RGB color code.
213
+ *
214
+ * @example
215
+ * const randomColor = StringExtensions.randomRGB();
216
+ * console.log(randomColor); // Output: "rgb(58,95,76)"
217
+ */
218
+ randomRGB() {
219
+ const hex = Math.random().toString(16).slice(2).padEnd(8, '0')
220
+ const red = parseInt(hex.substring(0,2), 16)
221
+ const green = parseInt(hex.substring(2,4), 16)
222
+ const blue = parseInt(hex.substring(4, 6), 16)
223
+ return `rgb(${red}, ${green}, ${blue})`
224
+ },
225
+
226
+ /**
227
+ * Generates a random RGBA color code with optional forced color values.
228
+ *
229
+ * This method generates a random hexadecimal number, slices off the
230
+ * leading '0.' and pads the end of the string with '0' until it is
231
+ * 8 characters long. It then parses the first 8 characters into
232
+ * four separate 2-character strings, each representing a color
233
+ * component (red, green, blue, alpha) in hexadecimal format. These strings
234
+ * are then converted into decimal format and used to construct an
235
+ * RGBA color code.
236
+ *
237
+ * If a color component is provided in the `force` parameter, it will
238
+ * be used instead of a random value for that component.
239
+ *
240
+ * @param {Object} force - An object with properties for each color
241
+ * component (red, green, blue, alpha) that should be forced to a
242
+ * specific value. If a property is undefined or not provided, a
243
+ * random value will be used for that component.
244
+ * @param {number} force.red - The red component (0-255).
245
+ * @param {number} force.green - The green component (0-255).
246
+ * @param {number} force.blue - The blue component (0-255).
247
+ * @param {number} force.alpha - The alpha component (0.0-1.0).
248
+ *
249
+ * @returns {string} A random RGBA color code.
250
+ *
251
+ * @example
252
+ * const randomColor = StringExtensions.randomRGBA();
253
+ * console.log(randomColor); // Output: "rgba(58,95,76,0.50)"
254
+ *
255
+ * const forcedGreen = StringExtensions.randomRGBA({ green: 255 });
256
+ * console.log(forcedGreen); // Output: "rgba(58,255,76,0.50)"
257
+ */
258
+ randomRGBA(
259
+ force = {
260
+ red: undefined,
261
+ green: undefined,
262
+ blue: undefined,
263
+ alpha: undefined
264
+ }
265
+ ) {
266
+ const hex = Math.random().toString(16).slice(2).padEnd(8, '0')
267
+ const red = force.red ?? parseInt(hex.substring(0,2), 16)
268
+ const green = force.green ?? parseInt(hex.substring(2,4), 16)
269
+ const blue = force.blue ?? parseInt(hex.substring(4, 6), 16)
270
+ const alpha = force.alpha ??
271
+ (parseInt(hex.substring(6, 8), 16) / 255.0) * 1.0
272
+
273
+ return `rgba(${red}, ${green}, ${blue}, ${alpha.toFixed(2)})`
274
+ },
275
+
276
+ wrap(
277
+ object = globalThis,
278
+ options = {
279
+ indent: 2,
280
+ separator: ', ',
281
+ indentCharacter: ' ',
282
+ lineEnding: '\n',
283
+ inspector: [Object, 'getOwnPropertyNames'],
284
+ mapValues: undefined,
285
+ mapLine: undefined,
286
+ }
287
+ ) {
288
+ const {
289
+ indent = 2,
290
+ separator = ', ',
291
+ indentCharacter = ' ',
292
+ lineEnding = '\n',
293
+ inspector = [Object, 'getOwnPropertyNames'],
294
+ mapValues,
295
+ mapLine,
296
+ } = options ?? {}
297
+
298
+ let tab = indent === 0 ? '' : indentCharacter.repeat(Number(indent) || 2);
299
+ let maxLen = 76 - tab.length;
300
+ let line = [];
301
+ let getElements = inspector[0][inspector[1]]
302
+ let values = Array.isArray(object) ? object : getElements(Object(object));
303
+
304
+ if (typeof mapValues === 'function') {
305
+ values = values.map(mapValues)
306
+ }
307
+
308
+ return values.reduce((acc,key,index,{length}) => {
309
+ const endOfLine = index < (length - 1) ? separator : '';
310
+ let ifCombined = [
311
+ tab,...line.join(separator), key, endOfLine
312
+ ].join('').trim();
313
+
314
+ if (ifCombined.length < maxLen) {
315
+ line.push(key)
316
+ }
317
+ else {
318
+
319
+ let lineElements = [...line, key ]
320
+ let debug = `<-- (len: ${[tab,...line.join(separator),key,endOfLine].join('').trim().length})`
321
+ if (typeof mapLine === 'function') {
322
+ lineElements = lineElements.map(mapLine)
323
+ }
324
+
325
+ const completeLine = (
326
+ tab +
327
+ lineElements.slice(1).join(separator) +
328
+ endOfLine
329
+ ).trim()
330
+
331
+ acc.push(completeLine + debug);
332
+
333
+ line = [];
334
+ };
335
+
336
+ return acc;
337
+ }, []).join(lineEnding)
338
+ },
339
+ });
340
+
341
+ const { isString: pIsString, ifString: pIfString } = StringExtensions.patches
342
+
343
+ /**
344
+ * `StringPrototypeExtensions` provides a set of utility methods that are
345
+ * added to the `String` prototype. This allows all string instances to
346
+ * access new functionality directly, enhancing their capabilities beyond
347
+ * the standard `String` class methods. These extensions are applied using
348
+ * the `Patch` class from '@nejs/extension', ensuring that they do not
349
+ * interfere with the global namespace or existing properties.
350
+ *
351
+ * The extensions include methods for extracting substrings based on
352
+ * specific tokens, checking the presence of certain patterns, and more,
353
+ * making string manipulation tasks more convenient and expressive.
354
+ */
355
+ export const StringPrototypeExtensions = new Patch(String.prototype, {
356
+ [Patch.kMutablyHidden]: {
357
+ /**
358
+ * Determines if the current object is a string.
359
+ *
360
+ * This getter uses the `pIsString` function from the
361
+ * `StringExtensions` patch to check if the current object
362
+ * (`this`) is a string.
363
+ *
364
+ * @type {boolean}
365
+ * @readonly
366
+ *
367
+ * @example
368
+ * const str = "Hello, World!"
369
+ * console.log(str.isString) // Output: true
370
+ *
371
+ * const notStr = 123
372
+ * console.log(notStr.isString) // Output: false
373
+ */
374
+ get isString() {
375
+ return pIsString(this)
376
+ },
377
+
378
+ /**
379
+ * Checks if the current object is a string and returns the
380
+ * corresponding value based on the result.
381
+ *
382
+ * This method uses the `pIfString` function from the
383
+ * `StringExtensions` patch to determine if the current object
384
+ * (`this`) is a string. If it is a string, the `thenValue` is returned.
385
+ * Otherwise, the `elseValue` is returned.
386
+ *
387
+ * @param {any} thenValue - The value to return if the current object
388
+ * is a string.
389
+ * @param {any} elseValue - The value to return if the current object
390
+ * is not a string.
391
+ * @returns {any} The `thenValue` if the current object is a string, or
392
+ * the `elseValue` if it is not a string.
393
+ *
394
+ * @example
395
+ * const str = "Hello, World!"
396
+ * // 'Is a string'
397
+ * console.log(str.ifString('Is a string', 'Not a string'))
398
+ *
399
+ * const notStr = 123
400
+ * // 'Not a string'
401
+ * console.log(notStr.ifString('Is a string', 'Not a string'))
402
+ */
403
+ ifString(thenValue, elseValue) {
404
+ return pIfString(this, thenValue, elseValue)
405
+ },
406
+
407
+ /**
408
+ * Returns an object representation of the string instance.
409
+ *
410
+ * This getter method creates and returns an object that wraps
411
+ * the string instance, allowing it to be treated as an object.
412
+ * The returned object is created using the `Object()`
413
+ * constructor, which takes the string instance as its argument.
414
+ *
415
+ * @type {Object}
416
+ * @readonly
417
+ *
418
+ * @example
419
+ * const str = 'Hello, World!'
420
+ * console.log(typeof str) // 'string'
421
+ * console.log(typeof str.instance) // 'object'
422
+ */
423
+ get instance() {
424
+ return Object(this)
425
+ },
426
+
427
+ /**
428
+ * Extracts a substring from the current string, starting at a given offset
429
+ * and bounded by specified opening and closing tokens. This method is
430
+ * particularly useful for parsing nested structures or quoted strings,
431
+ * where the level of nesting or the presence of escape characters must
432
+ * be considered.
433
+ *
434
+ * @param {number} offset The position in the string from which to start the
435
+ * search for the substring.
436
+ * @param {[string, string]} tokens An array containing two strings: the
437
+ * opening and closing tokens that define the boundaries of the substring
438
+ * to be extracted.
439
+ * @returns {Object} An object with two properties: `extracted`, the
440
+ * extracted substring, and `newOffset`, the position in the original
441
+ * string immediately after the end of the extracted substring. If no
442
+ * substring is found, `extracted` is `null` and `newOffset` is the same
443
+ * as the input offset.
444
+ */
445
+ extractSubstring(offset = 0, tokens = parenthesisPair) {
446
+ let [openToken, closeToken] = tokens;
447
+ let depth = 0;
448
+ let start = -1;
449
+ let end = -1;
450
+ let leadingToken = '';
451
+ let firstToken = 0;
452
+
453
+ for (let i = offset; i < this.length; i++) {
454
+ const char = this[i];
455
+
456
+ if (char === openToken) {
457
+ depth++;
458
+ if (start === -1)
459
+ start = i;
460
+ }
461
+ else if (char === closeToken) {
462
+ depth--;
463
+ if (depth === 0) {
464
+ end = i;
465
+ break;
466
+ }
467
+ }
468
+ }
469
+
470
+ let lRange = [
471
+ Math.max(0, start - 100),
472
+ start
473
+ ];
474
+ let leading = [...this.substring(lRange[0], lRange[1])].reverse().join('')
475
+ let reversedLeadingToken;
476
+
477
+ try {
478
+ reversedLeadingToken = /([^ \,\"\'\`]+)/.exec(leading)[1] ?? '';
479
+ leadingToken = [...reversedLeadingToken].reverse().join('');
480
+ }
481
+ catch(ignored) { }
482
+
483
+ if (start !== -1 && end !== -1) {
484
+ const sliceRange = [start, end + 1];
485
+ const extracted = this.slice(sliceRange[0], sliceRange[1]);
486
+
487
+ return {
488
+ extracted,
489
+ range: [start, end],
490
+ newOffset: end + 1,
491
+ leadingToken,
492
+ };
493
+ }
494
+ else {
495
+ return {
496
+ extracted: null,
497
+ range: [start, end],
498
+ newOffset: offset,
499
+ leadingToken,
500
+ };
501
+ }
502
+ },
503
+ },
504
+ })
505
+
506
+ // NOTE to self; this is repeated here otherwise a circular reference from
507
+ // Object<->Function<->Global occurs. See original source in global.this.js
508
+ // {@see globalThis.isThenElse}
509
+ function isThenElse(bv, tv, ev) {
510
+ if (arguments.length > 1) {
511
+ var _then = isFunction(tv) ? tv(bv) : tv; if (arguments.length > 2) {
512
+ var _else = isFunction(ev) ? tv(bv) : ev; return bv ? _then : _else
513
+ } return bv || _then;
514
+ } return bv
515
+ }