@nejs/basic-extensions 2.21.0 → 2.22.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. package/.idea/markdown.xml +8 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/ne-basic-extensions.iml +8 -0
  4. package/.idea/vcs.xml +6 -0
  5. package/CODE_STYLE.md +393 -0
  6. package/CODING_PHILOSOPHY.md +36 -0
  7. package/DOCUMENTATION_GUIDELINES.md +221 -0
  8. package/README.md +78 -4
  9. package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
  10. package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
  11. package/dist/cjs/classes/index.cjs +11129 -0
  12. package/dist/cjs/classes/index.cjs.map +7 -0
  13. package/dist/cjs/index.cjs +15191 -0
  14. package/dist/cjs/index.cjs.map +7 -0
  15. package/dist/cjs/utils/index.cjs +3954 -0
  16. package/dist/cjs/utils/index.cjs.map +7 -0
  17. package/dist/esm/basic-extensions.mjs +25 -0
  18. package/dist/esm/basic-extensions.mjs.map +7 -0
  19. package/package.json +16 -22
  20. package/repl.bootstrap.js +4 -7
  21. package/repl.history +30 -30
  22. package/src/big.int.extension.js +171 -45
  23. package/src/classes/enumeration.js +466 -0
  24. package/src/classes/index.js +5 -1
  25. package/src/index.js +5 -1
  26. package/src/math.extension.js +73 -0
  27. package/src/number.extension.js +18 -0
  28. package/src/regular.expression.extensions.js +0 -35
  29. package/src/utils/toolkit.js +699 -516
  30. package/tests/arrayextensions.test.js +3 -3
  31. package/tests/index.test.js +3 -1
  32. package/tests/newClasses/asyncIterable.test.js +3 -3
  33. package/tests/newClasses/deferred.test.js +3 -3
  34. package/tests/newClasses/descriptor.test.js +3 -3
  35. package/tests/newClasses/iterable.test.js +3 -3
  36. package/tests/newClasses/refmap.test.js +3 -3
  37. package/tests/newClasses/refset.test.js +3 -3
  38. package/tests/objectextensions.test.js +3 -3
  39. package/tests/setextensions.test.js +3 -3
  40. package/tests/stringextensions.test.js +3 -2
  41. package/tests/utils/descriptor.utils.test.js +1 -1
  42. package/tests/utils/toolkit.test.js +429 -163
  43. package/.esdoc.json +0 -9
  44. package/.vscode/settings.json +0 -5
  45. package/bin/build +0 -27
  46. package/bin/clean +0 -14
  47. package/bin/esbuild +0 -91
  48. package/bin/fixup +0 -13
  49. package/bin/repl.basics.js +0 -584
  50. package/bin/repl.signature.js +0 -63
  51. package/bin/version +0 -100
  52. package/dist/@nejs/basic-extensions.bundle.2.21.0.js +0 -25
  53. package/dist/@nejs/basic-extensions.bundle.2.21.0.js.map +0 -7
  54. package/dist/cjs/array.extensions.d.ts +0 -39
  55. package/dist/cjs/array.extensions.js +0 -477
  56. package/dist/cjs/array.extensions.js.map +0 -1
  57. package/dist/cjs/big.int.extension.d.ts +0 -31
  58. package/dist/cjs/big.int.extension.js +0 -165
  59. package/dist/cjs/big.int.extension.js.map +0 -1
  60. package/dist/cjs/classes/asyncIterable.d.ts +0 -126
  61. package/dist/cjs/classes/asyncIterable.js +0 -209
  62. package/dist/cjs/classes/asyncIterable.js.map +0 -1
  63. package/dist/cjs/classes/deferred.d.ts +0 -146
  64. package/dist/cjs/classes/deferred.js +0 -291
  65. package/dist/cjs/classes/deferred.js.map +0 -1
  66. package/dist/cjs/classes/descriptor.d.ts +0 -334
  67. package/dist/cjs/classes/descriptor.js +0 -537
  68. package/dist/cjs/classes/descriptor.js.map +0 -1
  69. package/dist/cjs/classes/enum.d.ts +0 -50
  70. package/dist/cjs/classes/enum.js +0 -405
  71. package/dist/cjs/classes/enum.js.map +0 -1
  72. package/dist/cjs/classes/index.d.ts +0 -15
  73. package/dist/cjs/classes/index.js +0 -63
  74. package/dist/cjs/classes/index.js.map +0 -1
  75. package/dist/cjs/classes/introspector.d.ts +0 -20
  76. package/dist/cjs/classes/introspector.js +0 -130
  77. package/dist/cjs/classes/introspector.js.map +0 -1
  78. package/dist/cjs/classes/iterable.d.ts +0 -169
  79. package/dist/cjs/classes/iterable.js +0 -268
  80. package/dist/cjs/classes/iterable.js.map +0 -1
  81. package/dist/cjs/classes/param.parser.d.ts +0 -221
  82. package/dist/cjs/classes/param.parser.js +0 -242
  83. package/dist/cjs/classes/param.parser.js.map +0 -1
  84. package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
  85. package/dist/cjs/classes/pluggable.proxy.js +0 -444
  86. package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
  87. package/dist/cjs/classes/property.d.ts +0 -79
  88. package/dist/cjs/classes/property.js +0 -284
  89. package/dist/cjs/classes/property.js.map +0 -1
  90. package/dist/cjs/classes/refmap.d.ts +0 -238
  91. package/dist/cjs/classes/refmap.js +0 -421
  92. package/dist/cjs/classes/refmap.js.map +0 -1
  93. package/dist/cjs/classes/refset.d.ts +0 -186
  94. package/dist/cjs/classes/refset.js +0 -370
  95. package/dist/cjs/classes/refset.js.map +0 -1
  96. package/dist/cjs/classes/symkeys.d.ts +0 -349
  97. package/dist/cjs/classes/symkeys.js +0 -510
  98. package/dist/cjs/classes/symkeys.js.map +0 -1
  99. package/dist/cjs/classes/type.d.ts +0 -56
  100. package/dist/cjs/classes/type.js +0 -405
  101. package/dist/cjs/classes/type.js.map +0 -1
  102. package/dist/cjs/function.extensions.d.ts +0 -12
  103. package/dist/cjs/function.extensions.js +0 -758
  104. package/dist/cjs/function.extensions.js.map +0 -1
  105. package/dist/cjs/global.this.d.ts +0 -2
  106. package/dist/cjs/global.this.js +0 -300
  107. package/dist/cjs/global.this.js.map +0 -1
  108. package/dist/cjs/index.d.ts +0 -31
  109. package/dist/cjs/index.js +0 -226
  110. package/dist/cjs/index.js.map +0 -1
  111. package/dist/cjs/json.extensions.d.ts +0 -2
  112. package/dist/cjs/json.extensions.js +0 -109
  113. package/dist/cjs/json.extensions.js.map +0 -1
  114. package/dist/cjs/map.extensions.d.ts +0 -3
  115. package/dist/cjs/map.extensions.js +0 -143
  116. package/dist/cjs/map.extensions.js.map +0 -1
  117. package/dist/cjs/number.extension.d.ts +0 -44
  118. package/dist/cjs/number.extension.js +0 -261
  119. package/dist/cjs/number.extension.js.map +0 -1
  120. package/dist/cjs/object.extensions.d.ts +0 -33
  121. package/dist/cjs/object.extensions.js +0 -1091
  122. package/dist/cjs/object.extensions.js.map +0 -1
  123. package/dist/cjs/package.json +0 -3
  124. package/dist/cjs/proxy.extensions.d.ts +0 -2
  125. package/dist/cjs/proxy.extensions.js +0 -207
  126. package/dist/cjs/proxy.extensions.js.map +0 -1
  127. package/dist/cjs/reflect.extensions.d.ts +0 -14
  128. package/dist/cjs/reflect.extensions.js +0 -316
  129. package/dist/cjs/reflect.extensions.js.map +0 -1
  130. package/dist/cjs/regular.expression.extensions.d.ts +0 -2
  131. package/dist/cjs/regular.expression.extensions.js +0 -423
  132. package/dist/cjs/regular.expression.extensions.js.map +0 -1
  133. package/dist/cjs/set.extensions.d.ts +0 -40
  134. package/dist/cjs/set.extensions.js +0 -355
  135. package/dist/cjs/set.extensions.js.map +0 -1
  136. package/dist/cjs/string.extensions.d.ts +0 -23
  137. package/dist/cjs/string.extensions.js +0 -704
  138. package/dist/cjs/string.extensions.js.map +0 -1
  139. package/dist/cjs/symbol.extensions.d.ts +0 -11
  140. package/dist/cjs/symbol.extensions.js +0 -735
  141. package/dist/cjs/symbol.extensions.js.map +0 -1
  142. package/dist/cjs/utils/copy.object.d.ts +0 -408
  143. package/dist/cjs/utils/copy.object.js +0 -720
  144. package/dist/cjs/utils/copy.object.js.map +0 -1
  145. package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
  146. package/dist/cjs/utils/descriptor.utils.js +0 -889
  147. package/dist/cjs/utils/descriptor.utils.js.map +0 -1
  148. package/dist/cjs/utils/index.d.ts +0 -75
  149. package/dist/cjs/utils/index.js +0 -61
  150. package/dist/cjs/utils/index.js.map +0 -1
  151. package/dist/cjs/utils/stdout.d.ts +0 -742
  152. package/dist/cjs/utils/stdout.js +0 -1042
  153. package/dist/cjs/utils/stdout.js.map +0 -1
  154. package/dist/cjs/utils/toolkit.d.ts +0 -1898
  155. package/dist/cjs/utils/toolkit.js +0 -1378
  156. package/dist/cjs/utils/toolkit.js.map +0 -1
  157. package/dist/cjs/weakref.extensions.d.ts +0 -2
  158. package/dist/cjs/weakref.extensions.js +0 -19
  159. package/dist/cjs/weakref.extensions.js.map +0 -1
  160. package/dist/mjs/array.extensions.d.ts +0 -39
  161. package/dist/mjs/array.extensions.js +0 -474
  162. package/dist/mjs/array.extensions.js.map +0 -1
  163. package/dist/mjs/big.int.extension.d.ts +0 -31
  164. package/dist/mjs/big.int.extension.js +0 -162
  165. package/dist/mjs/big.int.extension.js.map +0 -1
  166. package/dist/mjs/classes/asyncIterable.d.ts +0 -126
  167. package/dist/mjs/classes/asyncIterable.js +0 -204
  168. package/dist/mjs/classes/asyncIterable.js.map +0 -1
  169. package/dist/mjs/classes/deferred.d.ts +0 -146
  170. package/dist/mjs/classes/deferred.js +0 -287
  171. package/dist/mjs/classes/deferred.js.map +0 -1
  172. package/dist/mjs/classes/descriptor.d.ts +0 -334
  173. package/dist/mjs/classes/descriptor.js +0 -533
  174. package/dist/mjs/classes/descriptor.js.map +0 -1
  175. package/dist/mjs/classes/enum.d.ts +0 -50
  176. package/dist/mjs/classes/enum.js +0 -400
  177. package/dist/mjs/classes/enum.js.map +0 -1
  178. package/dist/mjs/classes/index.d.ts +0 -15
  179. package/dist/mjs/classes/index.js +0 -46
  180. package/dist/mjs/classes/index.js.map +0 -1
  181. package/dist/mjs/classes/introspector.d.ts +0 -20
  182. package/dist/mjs/classes/introspector.js +0 -126
  183. package/dist/mjs/classes/introspector.js.map +0 -1
  184. package/dist/mjs/classes/iterable.d.ts +0 -169
  185. package/dist/mjs/classes/iterable.js +0 -263
  186. package/dist/mjs/classes/iterable.js.map +0 -1
  187. package/dist/mjs/classes/param.parser.d.ts +0 -221
  188. package/dist/mjs/classes/param.parser.js +0 -238
  189. package/dist/mjs/classes/param.parser.js.map +0 -1
  190. package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
  191. package/dist/mjs/classes/pluggable.proxy.js +0 -438
  192. package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
  193. package/dist/mjs/classes/property.d.ts +0 -79
  194. package/dist/mjs/classes/property.js +0 -280
  195. package/dist/mjs/classes/property.js.map +0 -1
  196. package/dist/mjs/classes/refmap.d.ts +0 -238
  197. package/dist/mjs/classes/refmap.js +0 -417
  198. package/dist/mjs/classes/refmap.js.map +0 -1
  199. package/dist/mjs/classes/refset.d.ts +0 -186
  200. package/dist/mjs/classes/refset.js +0 -366
  201. package/dist/mjs/classes/refset.js.map +0 -1
  202. package/dist/mjs/classes/symkeys.d.ts +0 -349
  203. package/dist/mjs/classes/symkeys.js +0 -506
  204. package/dist/mjs/classes/symkeys.js.map +0 -1
  205. package/dist/mjs/classes/type.d.ts +0 -56
  206. package/dist/mjs/classes/type.js +0 -401
  207. package/dist/mjs/classes/type.js.map +0 -1
  208. package/dist/mjs/function.extensions.d.ts +0 -12
  209. package/dist/mjs/function.extensions.js +0 -755
  210. package/dist/mjs/function.extensions.js.map +0 -1
  211. package/dist/mjs/global.this.d.ts +0 -2
  212. package/dist/mjs/global.this.js +0 -264
  213. package/dist/mjs/global.this.js.map +0 -1
  214. package/dist/mjs/index.d.ts +0 -31
  215. package/dist/mjs/index.js +0 -204
  216. package/dist/mjs/index.js.map +0 -1
  217. package/dist/mjs/json.extensions.d.ts +0 -2
  218. package/dist/mjs/json.extensions.js +0 -106
  219. package/dist/mjs/json.extensions.js.map +0 -1
  220. package/dist/mjs/map.extensions.d.ts +0 -3
  221. package/dist/mjs/map.extensions.js +0 -140
  222. package/dist/mjs/map.extensions.js.map +0 -1
  223. package/dist/mjs/number.extension.d.ts +0 -44
  224. package/dist/mjs/number.extension.js +0 -258
  225. package/dist/mjs/number.extension.js.map +0 -1
  226. package/dist/mjs/object.extensions.d.ts +0 -33
  227. package/dist/mjs/object.extensions.js +0 -1088
  228. package/dist/mjs/object.extensions.js.map +0 -1
  229. package/dist/mjs/package.json +0 -3
  230. package/dist/mjs/proxy.extensions.d.ts +0 -2
  231. package/dist/mjs/proxy.extensions.js +0 -204
  232. package/dist/mjs/proxy.extensions.js.map +0 -1
  233. package/dist/mjs/reflect.extensions.d.ts +0 -14
  234. package/dist/mjs/reflect.extensions.js +0 -313
  235. package/dist/mjs/reflect.extensions.js.map +0 -1
  236. package/dist/mjs/regular.expression.extensions.d.ts +0 -2
  237. package/dist/mjs/regular.expression.extensions.js +0 -420
  238. package/dist/mjs/regular.expression.extensions.js.map +0 -1
  239. package/dist/mjs/set.extensions.d.ts +0 -40
  240. package/dist/mjs/set.extensions.js +0 -352
  241. package/dist/mjs/set.extensions.js.map +0 -1
  242. package/dist/mjs/string.extensions.d.ts +0 -23
  243. package/dist/mjs/string.extensions.js +0 -701
  244. package/dist/mjs/string.extensions.js.map +0 -1
  245. package/dist/mjs/symbol.extensions.d.ts +0 -11
  246. package/dist/mjs/symbol.extensions.js +0 -732
  247. package/dist/mjs/symbol.extensions.js.map +0 -1
  248. package/dist/mjs/utils/copy.object.d.ts +0 -408
  249. package/dist/mjs/utils/copy.object.js +0 -702
  250. package/dist/mjs/utils/copy.object.js.map +0 -1
  251. package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
  252. package/dist/mjs/utils/descriptor.utils.js +0 -875
  253. package/dist/mjs/utils/descriptor.utils.js.map +0 -1
  254. package/dist/mjs/utils/index.d.ts +0 -75
  255. package/dist/mjs/utils/index.js +0 -45
  256. package/dist/mjs/utils/index.js.map +0 -1
  257. package/dist/mjs/utils/stdout.d.ts +0 -742
  258. package/dist/mjs/utils/stdout.js +0 -1037
  259. package/dist/mjs/utils/stdout.js.map +0 -1
  260. package/dist/mjs/utils/toolkit.d.ts +0 -1898
  261. package/dist/mjs/utils/toolkit.js +0 -1373
  262. package/dist/mjs/utils/toolkit.js.map +0 -1
  263. package/dist/mjs/weakref.extensions.d.ts +0 -2
  264. package/dist/mjs/weakref.extensions.js +0 -16
  265. package/dist/mjs/weakref.extensions.js.map +0 -1
  266. package/jsdoc-config.json +0 -31
  267. package/tsconfig.base.json +0 -28
  268. package/tsconfig.cjs.json +0 -8
  269. package/tsconfig.esm.json +0 -8
  270. package/vitest.config.js +0 -7
@@ -1,758 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FunctionPrototypeExtensions = exports.FunctionExtensions = void 0;
4
- const extension_1 = require("@nejs/extension");
5
- /**
6
- * The `FunctionExtensions` class is a patch applied to the built-in JavaScript
7
- * `Function` constructor. It extends `Function` with additional utility methods
8
- * for determining the specific type or nature of function-like objects. These
9
- * methods allow developers to distinguish between classes, regular functions,
10
- * async functions, and arrow functions in a more intuitive and straightforward
11
- * manner. This class is part of the `@nejs/extension` library and enhances the
12
- * capabilities of function handling and introspection in JavaScript.
13
- */
14
- exports.FunctionExtensions = new extension_1.Patch(Function, {
15
- [extension_1.Patch.kMutablyHidden]: {
16
- /**
17
- * Retrieves the properties of a function and its prototype.
18
- *
19
- * This method uses the `Reflect.ownKeys` function to get all the keys
20
- * (including non-enumerable and symbol keys) of the function and its
21
- * prototype. It then uses `Object.getOwnPropertyDescriptor` to get the
22
- * property descriptors for each key. The descriptors include information
23
- * about the property's value, writability, enumerability, and
24
- * configurability.
25
- *
26
- * @param {Function} fn - The function whose properties are to be retrieved.
27
- * @returns {Array} An array containing the function itself, its property
28
- * descriptors, its prototype, and the prototype's property descriptors.
29
- *
30
- * @example
31
- * function MyFunction() {}
32
- * MyFunction.myProp = 'hello';
33
- * MyFunction.prototype.myProtoProp = 'world';
34
- *
35
- * const result = getClassProperties(MyFunction);
36
- * console.log(result);
37
- * // Output: [MyFunction, { myProp: { value: 'hello', writable: true,
38
- * // enumerable: true, configurable: true } }, MyFunction.prototype,
39
- * // { myProtoProp: { value: 'world', writable: true, enumerable: true,
40
- * // configurable: true } }]
41
- */
42
- getClassProperties(fn) {
43
- const descriptors = Reflect.ownKeys(fn).reduce((acc, key) => {
44
- acc[key] = Object.getOwnPropertyDescriptor(fn, key);
45
- return acc;
46
- }, {});
47
- const prototypeDescriptors = Reflect.ownKeys(fn.prototype).reduce((acc, key) => {
48
- acc[key] = Object.getOwnPropertyDescriptor(fn.prototype, key);
49
- return acc;
50
- }, {});
51
- return [fn, descriptors, fn.prototype, prototypeDescriptors];
52
- },
53
- /**
54
- * Determines if a given value is an asynchronous function. It checks if the
55
- * value is an instance of `Function` and if its string representation
56
- * includes the keyword 'Async'. This method is particularly useful for
57
- * identifying async functions.
58
- *
59
- * @param {*} value - The value to be checked.
60
- * @returns {boolean} Returns `true` if the value is an async function,
61
- * otherwise `false`.
62
- */
63
- isAsync(value) {
64
- const stringTag = /(\w+)]/g.exec(Object.prototype.toString.call(value))[1];
65
- return (value instanceof Function &&
66
- stringTag.includes('Async'));
67
- },
68
- /**
69
- * The `ifAsync` function checks if a given value is an async function and
70
- * returns one of two provided values based on the result. This function is
71
- * a convenience method for performing conditional operations based on the
72
- * type of a value.
73
- *
74
- * @param {*} value - The value to be checked. If this is an async function,
75
- * `thenValue` is returned, otherwise `elseValue` is returned.
76
- * @param {*} thenValue - The value to be returned if `value` is an async
77
- * function.
78
- * @param {*} elseValue - The value to be returned if `value` is not an
79
- * async function.
80
- * @returns {*} Returns `thenValue` if `value` is an async function,
81
- * otherwise returns `elseValue`.
82
- * @see {@link isThenElse}
83
- *
84
- * @example
85
- * // Suppose we have an async function and a regular function
86
- * async function asyncFunc() { return 'I am async'; }
87
- * function regularFunc() { return 'I am regular'; }
88
- *
89
- * // Using ifAsync
90
- * console.log(Function.ifAsync(asyncFunc, 'Async', 'Not Async'));
91
- * // Output: 'Async'
92
- * console.log(Function.ifAsync(regularFunc, 'Async', 'Not Async'));
93
- * // Output: 'Not Async'
94
- */
95
- ifAsync(value, thenValue, elseValue) {
96
- return isThenElse(this.isAsync(value), thenValue, elseValue);
97
- },
98
- /**
99
- * The function checks if a given value is an async generator function
100
- *
101
- * @param {any} value - The `value` parameter is the value that we want to
102
- * check if it is a generator function.
103
- * @returns {boolean} `true` if the value is an instance of a function and
104
- * its string tag is 'AsyncGeneratorFunction', otherwise it returns `false`.
105
- */
106
- isAsyncGenerator(value) {
107
- const stringTag = getStringTag(value);
108
- return (value instanceof Function &&
109
- stringTag == 'AsyncGeneratorFunction');
110
- },
111
- /**
112
- * The `ifAsyncGenerator` function checks if a given value is an async
113
- * generator function and returns one of two provided values based on the
114
- * result. This function is a convenience method for performing conditional
115
- * operations based on the type of a value.
116
- *
117
- * @param {*} value - The value to be checked. If this is an async
118
- * generator function, `thenValue` is returned, otherwise `elseValue` is
119
- * returned.
120
- * @param {*} thenValue - The value to be returned if `value` is an async
121
- * generator function.
122
- * @param {*} elseValue - The value to be returned if `value` is not an
123
- * async generator function.
124
- * @returns {*} Returns `thenValue` if `value` is an async generator
125
- * function, otherwise returns `elseValue`.
126
- *
127
- * @example
128
- * // Suppose we have an async generator function and a regular function
129
- * async function* asyncGenFunc() { yield 'I am async'; }
130
- * function regularFunc() { return 'I am regular'; }
131
- *
132
- * // Using ifAsyncGenerator
133
- * console.log(Function.ifAsyncGenerator(asyncGenFunc, 'Async', 'Not Async'));
134
- * // Output: 'Async'
135
- * console.log(Function.ifAsyncGenerator(regularFunc, 'Async', 'Not Async'));
136
- * // Output: 'Not Async'
137
- */
138
- ifAsyncGenerator(value, thenValue, elseValue) {
139
- return isThenElse(this.isAsyncGenerator(value), thenValue, elseValue);
140
- },
141
- /**
142
- * Checks if a given value is an arrow function. It verifies if the value is
143
- * an instance of `Function`, if its string representation includes the '=>'
144
- * symbol, and if it lacks a prototype, which is a characteristic of arrow
145
- * functions in JavaScript.
146
- *
147
- * @param {*} value - The value to be checked.
148
- * @returns {boolean} Returns `true` if the value is an arrow function,
149
- * otherwise `false`.
150
- */
151
- isBigArrow(value) {
152
- return (value instanceof Function &&
153
- String(value).includes('=>') &&
154
- !String(value).startsWith('bound') &&
155
- !Reflect.has(value, 'prototype'));
156
- },
157
- /**
158
- * The `ifBigArrow` function checks if a given value is an arrow function
159
- * and returns one of two provided values based on the result. This function
160
- * is a convenience method for performing conditional operations based on
161
- * the type of a value.
162
- *
163
- * @param {*} value - The value to be checked. If this is an arrow function,
164
- * `thenValue` is returned, otherwise `elseValue` is returned.
165
- * @param {*} thenValue - The value to be returned if `value` is an arrow
166
- * function.
167
- * @param {*} elseValue - The value to be returned if `value` is not an
168
- * arrow function.
169
- * @returns {*} Returns `thenValue` if `value` is an arrow function,
170
- * otherwise returns `elseValue`.
171
- *
172
- * @example
173
- * // Suppose we have an arrow function and a regular function
174
- * const arrowFunc = () => 'I am an arrow function';
175
- * function regularFunc() { return 'I am a regular function'; }
176
- *
177
- * // Using ifBigArrow
178
- * console.log(Function.ifBigArrow(arrowFunc, 'Arrow', 'Not Arrow'));
179
- * // Output: 'Arrow'
180
- * console.log(Function.ifBigArrow(regularFunc, 'Arrow', 'Not Arrow'));
181
- * // Output: 'Not Arrow'
182
- */
183
- ifBigArrow(value, thenValue, elseValue) {
184
- return isThenElse(this.isBigArrow(value), thenValue, elseValue);
185
- },
186
- /**
187
- * Determines if a given value is a bound function. Bound functions are
188
- * created using the `Function.prototype.bind` method, which allows setting
189
- * the `this` value at the time of binding. This method checks if the value
190
- * is an instance of `Function`, if its string representation starts with
191
- * 'bound', and if it lacks a `prototype` property. These characteristics
192
- * are indicative of bound functions in JavaScript.
193
- *
194
- * @param {*} value - The value to be checked, typically a function.
195
- * @returns {boolean} Returns `true` if the value is a bound function,
196
- * otherwise `false`. Bound functions have a specific format in their
197
- * string representation and do not have their own `prototype` property.
198
- */
199
- isBound(value) {
200
- return (value instanceof Function &&
201
- String(value).startsWith('bound') &&
202
- !Reflect.has(value, 'prototype'));
203
- },
204
- /**
205
- * The `ifBound` function checks if a given value is a bound function and
206
- * returns one of two provided values based on the result. This function
207
- * is a convenience method for performing conditional operations based on
208
- * the type of a value.
209
- *
210
- * @param {*} value - The value to be checked. If this is a bound function,
211
- * `thenValue` is returned, otherwise `elseValue` is returned.
212
- * @param {*} thenValue - The value to be returned if `value` is a bound
213
- * function.
214
- * @param {*} elseValue - The value to be returned if `value` is not a
215
- * bound function.
216
- * @returns {*} Returns `thenValue` if `value` is a bound function,
217
- * otherwise returns `elseValue`.
218
- *
219
- * @example
220
- * // Suppose we have a bound function and a regular function
221
- * const boundFunc = function() { return this.x }.bind({x: 'I am bound'});
222
- * function regularFunc() { return 'I am a regular function'; }
223
- *
224
- * // Using ifBound
225
- * console.log(Function.ifBound(boundFunc, 'Bound', 'Not Bound'));
226
- * // Output: 'Bound'
227
- * console.log(Function.ifBound(regularFunc, 'Bound', 'Not Bound'));
228
- * // Output: 'Not Bound'
229
- */
230
- ifBound(value, thenValue, elseValue) {
231
- return isThenElse(this.isBound(value), thenValue, elseValue);
232
- },
233
- /**
234
- * Determines if a given value is a class. It checks if the value is an
235
- * instance of `Function` and if its string representation includes the
236
- * keyword 'class'. This method is useful for distinguishing classes from
237
- * other function types in JavaScript.
238
- *
239
- * @param {*} value - The value to be checked.
240
- * @returns {boolean} Returns `true` if the value is a class, otherwise
241
- * `false`.
242
- */
243
- isClass(value) {
244
- return value instanceof Function && !!/^class\s/.exec(String(value));
245
- },
246
- /**
247
- * The `ifClass` function checks if a given value is a class and returns
248
- * one of two provided values based on the result. This function is a
249
- * convenience method for performing conditional operations based on the
250
- * type of a value.
251
- *
252
- * @param {*} value - The value to be checked. If this is a class,
253
- * `thenValue` is returned, otherwise `elseValue` is returned.
254
- * @param {*} thenValue - The value to be returned if `value` is a class.
255
- * @param {*} elseValue - The value to be returned if `value` is not a
256
- * class.
257
- * @returns {*} Returns `thenValue` if `value` is a class, otherwise returns
258
- * `elseValue`.
259
- *
260
- * @example
261
- * // Suppose we have a class and a regular function
262
- * class MyClass {}
263
- * function myFunction() {}
264
- *
265
- * // Using ifClass
266
- * console.log(Function.ifClass(MyClass, 'Class', 'Not Class'));
267
- * // Output: 'Class'
268
- * console.log(Function.ifClass(myFunction, 'Class', 'Not Class'));
269
- * // Output: 'Not Class'
270
- */
271
- ifClass(value, thenValue, elseValue) {
272
- return isThenElse(this.isClass(value), thenValue, elseValue);
273
- },
274
- /**
275
- * Checks if a given value is a regular function. This method verifies if
276
- * the value is an instance of `Function`, which includes regular functions,
277
- * classes, and async functions but excludes arrow functions.
278
- *
279
- * @param {*} value - The value to be checked.
280
- * @returns {boolean} Returns `true` if the value is a regular function,
281
- * otherwise `false`.
282
- */
283
- isFunction(value) {
284
- return value instanceof Function && !Function.isClass(value);
285
- },
286
- /**
287
- * The `ifFunction` method checks if a given value is a regular function
288
- * and returns one of two provided values based on the result. This method
289
- * is a convenience for performing conditional operations based on the
290
- * type of a value.
291
- *
292
- * @param {*} value - The value to be checked. If this is a function,
293
- * `thenValue` is returned, otherwise `elseValue` is returned.
294
- * @param {*} thenValue - The value to be returned if `value` is a function.
295
- * @param {*} elseValue - The value to be returned if `value` is not a
296
- * function.
297
- * @returns {*} Returns `thenValue` if `value` is a function, otherwise
298
- * returns `elseValue`.
299
- *
300
- * @example
301
- * // Suppose we have a function and a non-function value
302
- * function myFunction() {}
303
- * let notFunction = "I'm not a function";
304
- *
305
- * // Using ifFunction
306
- * console.log(Function.ifFunction(myFunction, 'Function', 'Not Function'));
307
- * // Output: 'Function'
308
- * console.log(Function.ifFunction(notFunction, 'Function', 'Not Function'));
309
- * // Output: 'Not Function'
310
- */
311
- ifFunction(value, thenValue, elseValue) {
312
- return isThenElse(this.isFunction(value), thenValue, elseValue);
313
- },
314
- /**
315
- * The function checks if a given value is a generator function
316
- *
317
- * @param {any} value - The `value` parameter is the value that we want to
318
- * check if it is a generator function.
319
- * @returns {boolean} `true` if the value is an instance of a function and
320
- * its string tag is 'GeneratorFunction', otherwise it returns `false`.
321
- */
322
- isGenerator(value) {
323
- const stringTag = getStringTag(value);
324
- return (value instanceof Function &&
325
- stringTag == 'GeneratorFunction');
326
- },
327
- /**
328
- * The `ifGenerator` method checks if a given value is a generator function
329
- * and returns one of two provided values based on the result. This method
330
- * is a convenience for performing conditional operations based on the
331
- * type of a value.
332
- *
333
- * @param {*} value - The value to be checked. If this is a generator
334
- * function, `thenValue` is returned, otherwise `elseValue` is returned.
335
- * @param {*} thenValue - The value to be returned if `value` is a generator
336
- * function.
337
- * @param {*} elseValue - The value to be returned if `value` is not a
338
- * generator function.
339
- * @returns {*} Returns `thenValue` if `value` is a generator function,
340
- * otherwise returns `elseValue`.
341
- *
342
- * @example
343
- * // Suppose we have a generator function and a non-generator function
344
- * function* myGenerator() {}
345
- * function myFunction() {}
346
- *
347
- * // Using ifGenerator
348
- * console.log(Function.ifGenerator(myGenerator, 'Generator', 'Not Generator'));
349
- * // Output: 'Generator'
350
- * console.log(Function.ifGenerator(myFunction, 'Generator', 'Not Generator'));
351
- * // Output: 'Not Generator'
352
- */
353
- ifGenerator(value, thenValue, elseValue) {
354
- return isThenElse(this.isGenerator(value), thenValue, elseValue);
355
- },
356
- /**
357
- * This method modifies the behavior of the `instanceof` operator for a
358
- * given class. It does this by defining a custom `Symbol.hasInstance`
359
- * method on the class. The custom method checks if the string tag of the
360
- * instance matches the name of the class or if the instance is part of
361
- * the prototype chain of the class.
362
- *
363
- * @param {Function} Class - The class for which to modify the behavior
364
- * of the `instanceof` operator.
365
- *
366
- * @example
367
- * // Suppose we have a class `MyClass`
368
- * class MyClass {}
369
- *
370
- * // And an instance of the class
371
- * const myInstance = new MyClass();
372
- *
373
- * // Before applying `StringTagHasInstance`, `instanceof` works as usual
374
- * console.log(myInstance instanceof MyClass); // Output: true
375
- *
376
- * // Now we apply `StringTagHasInstance` to `MyClass`
377
- * FunctionExtensions.patches.StringTagHasInstance(MyClass);
378
- *
379
- * // `instanceof` now checks the string tag and the prototype chain
380
- * console.log(myInstance instanceof MyClass); // Output: true
381
- */
382
- StringTagHasInstance(Class) {
383
- Object.defineProperty(Class, Symbol.hasInstance, {
384
- value: function stringTagAwareHasInstance(fn) {
385
- const protoChain = getPrototypeChainEntries(fn);
386
- return (fn[Symbol.toStringTag] === this.name ||
387
- fn instanceof this);
388
- }
389
- });
390
- },
391
- },
392
- });
393
- const { isAsyncGenerator: pIsAsyncGenerator, ifAsyncGenerator: pIfAsyncGenerator, isAsync: pIsAsync, ifAsync: pIfAsync, isBigArrow: pIsBigArrow, ifBigArrow: pIfBigArrow, isBound: pIsBound, ifBound: pIfBound, isClass: pIsClass, ifClass: pIfClass, isFunction: pIsFunction, ifFunction: pIfFunction, isGenerator: pIsGenerator, ifGenerator: pIfGenerator, } = exports.FunctionExtensions.patches;
394
- exports.FunctionPrototypeExtensions = new extension_1.Patch(Function.prototype, {
395
- [extension_1.Patch.kMutablyHidden]: {
396
- /**
397
- * Determines if a given value is an asynchronous function. It checks if the
398
- * value is an instance of `Function` and if its string representation
399
- * includes the keyword 'Async'. This method is particularly useful for
400
- * identifying async functions.
401
- *
402
- * @returns {boolean} Returns `true` if the value is an async function,
403
- * otherwise `false`.
404
- */
405
- get isAsync() {
406
- return pIsAsync(this);
407
- },
408
- /**
409
- * The `ifAsync` method checks if the current function is asynchronous and
410
- * returns one of two provided values based on the result. This method is
411
- * a convenience for performing conditional operations based on the
412
- * type of a function.
413
- *
414
- * @param {*} thenValue - The value to be returned if the function is
415
- * asynchronous.
416
- * @param {*} elseValue - The value to be returned if the function is not
417
- * asynchronous.
418
- * @returns {*} Returns `thenValue` if the function is asynchronous,
419
- * otherwise returns `elseValue`.
420
- * @see {@link Function.ifAsync}
421
- *
422
- * @example
423
- * // Suppose we have an async function and a non-async function
424
- * async function myAsyncFunction() {}
425
- * function myFunction() {}
426
- *
427
- * // Using ifAsync
428
- * console.log(myAsyncFunction.ifAsync('Async', 'Not Async'));
429
- * // Output: 'Async'
430
- * console.log(myFunction.ifAsync('Async', 'Not Async'));
431
- * // Output: 'Not Async'
432
- */
433
- ifAsync(thenValue, elseValue) {
434
- return pIfAsync(this, thenValue, elseValue);
435
- },
436
- /**
437
- * The function checks if a given value is an async generator function
438
- *
439
- * @returns {boolean} `true` if the value is an instance of a function and
440
- * its string tag is 'AsyncGeneratorFunction', otherwise it returns `false`.
441
- */
442
- get isAsyncGenerator() {
443
- return pIsAsyncGenerator(this);
444
- },
445
- /**
446
- * The `ifAsyncGenerator` method checks if the current function is an
447
- * asynchronous generator and returns one of two provided values based on
448
- * the result. This method is a convenience for performing conditional
449
- * operations based on the type of a function.
450
- *
451
- * @param {*} thenValue - The value to be returned if the function is an
452
- * asynchronous generator.
453
- * @param {*} elseValue - The value to be returned if the function is not
454
- * an asynchronous generator.
455
- * @returns {*} Returns `thenValue` if the function is an asynchronous
456
- * generator, otherwise returns `elseValue`.
457
- * @see {@link Function.ifAsyncGenerator}
458
- *
459
- * @example
460
- * // Suppose we have an async generator function and a non-async function
461
- * async function* myAsyncGeneratorFunction() {}
462
- * function myFunction() {}
463
- *
464
- * // Using ifAsyncGenerator
465
- * console.log(myAsyncGeneratorFunction.ifAsyncGenerator(
466
- * 'Async Generator', 'Not Async Generator'
467
- * ));
468
- * // Output: 'Async Generator'
469
- * console.log(myFunction.ifAsyncGenerator(
470
- * 'Async Generator', 'Not Async Generator'
471
- * ));
472
- * // Output: 'Not Async Generator'
473
- */
474
- ifAsyncGenerator(thenValue, elseValue) {
475
- return pIfAsyncGenerator(this, thenValue, elseValue);
476
- },
477
- /**
478
- * Checks if a given value is an arrow function. It verifies if the value is
479
- * an instance of `Function`, if its string representation includes the '=>'
480
- * symbol, and if it lacks a prototype, which is a characteristic of arrow
481
- * functions in JavaScript.
482
- *
483
- * @returns {boolean} Returns `true` if the value is an arrow function,
484
- * otherwise `false`.
485
- */
486
- get isBigArrow() {
487
- return pIsBigArrow(this);
488
- },
489
- /**
490
- * Checks if the current function is a "big arrow" function and
491
- * returns one of two provided values based on the result.
492
- *
493
- * A "big arrow" function is an arrow function that is not bound
494
- * to a specific context and does not have its own `this` value.
495
- *
496
- * @param {*} thenValue - The value to be returned if the function
497
- * is a "big arrow" function.
498
- * @param {*} elseValue - The value to be returned if the function
499
- * is not a "big arrow" function.
500
- * @returns {*} Returns `thenValue` if the function is a "big arrow"
501
- * function, otherwise returns `elseValue`.
502
- *
503
- * @example
504
- * // Suppose we have a "big arrow" function and a regular function
505
- * const bigArrowFn = () => {}
506
- * function regularFn() {}
507
- *
508
- * // Using ifBigArrow
509
- * console.log(bigArrowFn.ifBigArrow('Big Arrow', 'Not Big Arrow'))
510
- * // Output: 'Big Arrow'
511
- * console.log(regularFn.ifBigArrow('Big Arrow', 'Not Big Arrow'))
512
- * // Output: 'Not Big Arrow'
513
- */
514
- ifBigArrow(thenValue, elseValue) {
515
- return pIfBigArrow(this, thenValue, elseValue);
516
- },
517
- /**
518
- * Determines if a given value is a bound function. Bound functions are
519
- * created using the `Function.prototype.bind` method, which allows setting
520
- * the `this` value at the time of binding. This method checks if the value
521
- * is an instance of `Function`, if its string representation starts with
522
- * 'bound', and if it lacks a `prototype` property. These characteristics
523
- * are indicative of bound functions in JavaScript.
524
- *
525
- * @returns {boolean} Returns `true` if the value is a bound function,
526
- * otherwise `false`. Bound functions have a specific format in their
527
- * string representation and do not have their own `prototype` property.
528
- */
529
- get isBound() {
530
- return pIsBound(this);
531
- },
532
- /**
533
- * Checks if the current function is bound and returns one of two
534
- * provided values based on the result.
535
- *
536
- * A bound function is a function that has a fixed `this` value and
537
- * may have preset arguments. It is created using the
538
- * `Function.prototype.bind` method.
539
- *
540
- * @param {*} thenValue - The value to be returned if the function
541
- * is bound.
542
- * @param {*} elseValue - The value to be returned if the function
543
- * is not bound.
544
- * @returns {*} Returns `thenValue` if the function is bound,
545
- * otherwise returns `elseValue`.
546
- *
547
- * @example
548
- * // Suppose we have a bound function and a regular function
549
- * const boundFn = function() {}.bind(null)
550
- * function regularFn() {}
551
- *
552
- * // Using ifBound
553
- * console.log(boundFn.ifBound('Bound', 'Not Bound'))
554
- * // Output: 'Bound'
555
- * console.log(regularFn.ifBound('Bound', 'Not Bound'))
556
- * // Output: 'Not Bound'
557
- */
558
- ifBound(thenValue, elseValue) {
559
- return pIfBound(this, thenValue, elseValue);
560
- },
561
- /**
562
- * Determines if a given value is a class. It checks if the value is an
563
- * instance of `Function` and if its string representation includes the
564
- * keyword 'class'. This method is useful for distinguishing classes from
565
- * other function types in JavaScript.
566
- *
567
- * @returns {boolean} Returns `true` if the value is a class, otherwise
568
- * `false`.
569
- */
570
- get isClass() {
571
- return pIsClass(this);
572
- },
573
- /**
574
- * Checks if the current function is a class and returns one of two
575
- * provided values based on the result.
576
- *
577
- * A class is a special type of function in JavaScript that is
578
- * defined using the `class` keyword. It serves as a blueprint for
579
- * creating objects and encapsulates data and behavior.
580
- *
581
- * @param {any} thenValue - The value to be returned if the function
582
- * is a class.
583
- * @param {any} elseValue - The value to be returned if the function
584
- * is not a class.
585
- * @returns {any} Returns `thenValue` if the function is a class,
586
- * otherwise returns `elseValue`.
587
- *
588
- * @example
589
- * // Suppose we have a class and a regular function
590
- * class MyClass {}
591
- * function myFunction() {}
592
- *
593
- * // Using ifClass
594
- * console.log(MyClass.ifClass('Class', 'Not Class'))
595
- * // Output: 'Class'
596
- * console.log(myFunction.ifClass('Class', 'Not Class'))
597
- * // Output: 'Not Class'
598
- */
599
- ifClass(thenValue, elseValue) {
600
- return pIfClass(this, thenValue, elseValue);
601
- },
602
- /**
603
- * Checks if a given value is a regular function. This method verifies if
604
- * the value is an instance of `Function`, which includes regular functions,
605
- * classes, and async functions but excludes arrow functions.
606
- *
607
- * @returns {boolean} Returns `true` if the value is a regular function,
608
- * otherwise `false`.
609
- */
610
- get isFunction() {
611
- return pIsFunction(this);
612
- },
613
- /**
614
- * Checks if the current function is a regular function and returns
615
- * one of two provided values based on the result.
616
- *
617
- * A regular function is an instance of `Function`, which includes
618
- * regular functions, classes, and async functions but excludes
619
- * arrow functions.
620
- *
621
- * @param {any} thenValue - The value to be returned if the function
622
- * is a regular function.
623
- * @param {any} elseValue - The value to be returned if the function
624
- * is not a regular function.
625
- * @returns {any} Returns `thenValue` if the function is a regular
626
- * function, otherwise returns `elseValue`.
627
- *
628
- * @example
629
- * // Suppose we have a regular function and an arrow function
630
- * function regularFunction() {}
631
- * const arrowFunction = () => {}
632
- *
633
- * // Using ifFunction
634
- * console.log(regularFunction.ifFunction('Regular', 'Not Regular'))
635
- * // Output: 'Regular'
636
- * console.log(arrowFunction.ifFunction('Regular', 'Not Regular'))
637
- * // Output: 'Not Regular'
638
- */
639
- ifFunction(thenValue, elseValue) {
640
- return pIfFunction(this, thenValue, elseValue);
641
- },
642
- /**
643
- * The function checks if a given value is a generator function
644
- *
645
- * @returns {boolean} `true` if the value is an instance of a function and
646
- * its string tag is 'GeneratorFunction', otherwise it returns `false`.
647
- */
648
- get isGenerator() {
649
- return pIsGenerator(this);
650
- },
651
- /**
652
- * Checks if the current function is a generator function and
653
- * returns one of two provided values based on the result.
654
- *
655
- * A generator function is a special type of function that can be
656
- * paused and resumed, allowing it to yield multiple values over
657
- * time rather than returning a single value.
658
- *
659
- * @param {any} thenValue - The value to be returned if the
660
- * function is a generator function.
661
- * @param {any} elseValue - The value to be returned if the
662
- * function is not a generator function.
663
- * @returns {any} Returns `thenValue` if the function is a
664
- * generator function, otherwise returns `elseValue`.
665
- *
666
- * @example
667
- * // Suppose we have a generator function and a regular function
668
- * function* generatorFunction() {
669
- * yield 1
670
- * yield 2
671
- * yield 3
672
- * }
673
- * function regularFunction() {}
674
- *
675
- * // Using ifGenerator
676
- * console.log(generatorFunction.ifGenerator('Generator', 'Regular'))
677
- * // Output: 'Generator'
678
- * console.log(regularFunction.ifGenerator('Generator', 'Regular'))
679
- * // Output: 'Regular'
680
- */
681
- ifGenerator(thenValue, elseValue) {
682
- return pIfGenerator(this, thenValue, elseValue);
683
- },
684
- /**
685
- * Retrieves the properties of the current function and its prototype.
686
- *
687
- * This method uses the `getClassProperties` function from the
688
- * `FunctionExtensions.patches` object to get all the properties of the
689
- * current function and its prototype. The properties include both
690
- * enumerable and non-enumerable properties, as well as properties
691
- * defined with symbols.
692
- *
693
- * @returns {Array} An array containing the function itself, its property
694
- * descriptors, its prototype, and the prototype's property descriptors.
695
- *
696
- * @example
697
- * // Suppose we have a function with a property and a prototype property
698
- * function MyFunction() {}
699
- * MyFunction.myProp = 'hello';
700
- * MyFunction.prototype.myProtoProp = 'world';
701
- *
702
- * // Using getClassProperties
703
- * const result = MyFunction.getClassProperties();
704
- * console.log(result);
705
- * // Output: [MyFunction, { myProp: { value: 'hello', writable: true,
706
- * // enumerable: true, configurable: true } }, MyFunction.prototype,
707
- * // { myProtoProp: { value: 'world', writable: true, enumerable: true,
708
- * // configurable: true } }]
709
- */
710
- getClassProperties() {
711
- return exports.FunctionExtensions.patches.getClassProperties(this);
712
- },
713
- },
714
- });
715
- // NOTE to self; this is repeated here otherwise a circular reference from
716
- // Object<->Function<->Global occurs. See original source in global.this.js
717
- // {@see globalThis.isThenElse}
718
- function isThenElse(bv, tv, ev) {
719
- function isFunction(value) { typeof value === 'function'; }
720
- if (arguments.length > 1) {
721
- var _then = isFunction(tv) ? tv(bv) : tv;
722
- if (arguments.length > 2) {
723
- var _else = isFunction(ev) ? tv(bv) : ev;
724
- return bv ? _then : _else;
725
- }
726
- return bv || _then;
727
- }
728
- return bv;
729
- }
730
- function hasStringTag(value) {
731
- return Object.isObject(value) && Reflect.has(value, Symbol.toStringTag);
732
- }
733
- function getStringTag(value, strict = false) {
734
- if (Object.hasStringTag(value)) {
735
- return value[Symbol.toStringTag];
736
- }
737
- if (strict) {
738
- return undefined;
739
- }
740
- if (value && (typeof value === 'function')) {
741
- return value.name;
742
- }
743
- return /\s(.+)]/.exec(Object.prototype.toString.call(value))[1];
744
- }
745
- function getPrototypeChainEntries(object) {
746
- const entries = [];
747
- let prototype = Object.getPrototypeOf(object);
748
- while (prototype) {
749
- const descriptors = Reflect.ownKeys(prototype).reduce((acc, key) => {
750
- acc[key] = Object.getOwnPropertyDescriptor(prototype, key);
751
- return acc;
752
- }, {});
753
- entries.push([prototype, descriptors]);
754
- prototype = Object.getPrototypeOf(prototype);
755
- }
756
- return entries;
757
- }
758
- //# sourceMappingURL=function.extensions.js.map