@nejs/basic-extensions 2.21.5 → 2.22.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/README.md +78 -4
  2. package/dist/@nejs/basic-extensions.bundle.2.22.6.js +25 -0
  3. package/dist/@nejs/basic-extensions.bundle.2.22.6.js.map +7 -0
  4. package/dist/cjs/classes/index.cjs +11129 -0
  5. package/dist/cjs/classes/index.cjs.map +7 -0
  6. package/dist/cjs/index.cjs +15191 -0
  7. package/dist/cjs/index.cjs.map +7 -0
  8. package/dist/cjs/utils/index.cjs +3954 -0
  9. package/dist/cjs/utils/index.cjs.map +7 -0
  10. package/dist/esm/basic-extensions.mjs +25 -0
  11. package/dist/esm/basic-extensions.mjs.map +7 -0
  12. package/package.json +16 -22
  13. package/repl.bootstrap.js +4 -7
  14. package/repl.history +26 -26
  15. package/src/classes/enumeration.js +466 -0
  16. package/src/classes/index.js +5 -1
  17. package/src/index.js +3 -1
  18. package/src/regular.expression.extensions.js +0 -35
  19. package/src/utils/toolkit.js +699 -516
  20. package/tests/arrayextensions.test.js +3 -3
  21. package/tests/index.test.js +3 -1
  22. package/tests/newClasses/asyncIterable.test.js +3 -3
  23. package/tests/newClasses/deferred.test.js +3 -3
  24. package/tests/newClasses/descriptor.test.js +3 -3
  25. package/tests/newClasses/iterable.test.js +3 -3
  26. package/tests/newClasses/refmap.test.js +3 -3
  27. package/tests/newClasses/refset.test.js +3 -3
  28. package/tests/objectextensions.test.js +3 -3
  29. package/tests/setextensions.test.js +3 -3
  30. package/tests/stringextensions.test.js +3 -2
  31. package/tests/utils/descriptor.utils.test.js +1 -1
  32. package/tests/utils/toolkit.test.js +429 -163
  33. package/.esdoc.json +0 -9
  34. package/.vscode/settings.json +0 -5
  35. package/bin/build +0 -27
  36. package/bin/clean +0 -14
  37. package/bin/esbuild +0 -91
  38. package/bin/fixup +0 -13
  39. package/bin/repl.basics.js +0 -584
  40. package/bin/repl.signature.js +0 -63
  41. package/bin/version +0 -100
  42. package/dist/@nejs/basic-extensions.bundle.2.21.5.js +0 -25
  43. package/dist/@nejs/basic-extensions.bundle.2.21.5.js.map +0 -7
  44. package/dist/cjs/array.extensions.d.ts +0 -39
  45. package/dist/cjs/array.extensions.js +0 -477
  46. package/dist/cjs/array.extensions.js.map +0 -1
  47. package/dist/cjs/big.int.extension.d.ts +0 -31
  48. package/dist/cjs/big.int.extension.js +0 -273
  49. package/dist/cjs/big.int.extension.js.map +0 -1
  50. package/dist/cjs/classes/asyncIterable.d.ts +0 -126
  51. package/dist/cjs/classes/asyncIterable.js +0 -209
  52. package/dist/cjs/classes/asyncIterable.js.map +0 -1
  53. package/dist/cjs/classes/deferred.d.ts +0 -146
  54. package/dist/cjs/classes/deferred.js +0 -291
  55. package/dist/cjs/classes/deferred.js.map +0 -1
  56. package/dist/cjs/classes/descriptor.d.ts +0 -334
  57. package/dist/cjs/classes/descriptor.js +0 -537
  58. package/dist/cjs/classes/descriptor.js.map +0 -1
  59. package/dist/cjs/classes/enum.d.ts +0 -50
  60. package/dist/cjs/classes/enum.js +0 -405
  61. package/dist/cjs/classes/enum.js.map +0 -1
  62. package/dist/cjs/classes/index.d.ts +0 -15
  63. package/dist/cjs/classes/index.js +0 -63
  64. package/dist/cjs/classes/index.js.map +0 -1
  65. package/dist/cjs/classes/introspector.d.ts +0 -20
  66. package/dist/cjs/classes/introspector.js +0 -130
  67. package/dist/cjs/classes/introspector.js.map +0 -1
  68. package/dist/cjs/classes/iterable.d.ts +0 -169
  69. package/dist/cjs/classes/iterable.js +0 -268
  70. package/dist/cjs/classes/iterable.js.map +0 -1
  71. package/dist/cjs/classes/param.parser.d.ts +0 -221
  72. package/dist/cjs/classes/param.parser.js +0 -242
  73. package/dist/cjs/classes/param.parser.js.map +0 -1
  74. package/dist/cjs/classes/pluggable.proxy.d.ts +0 -153
  75. package/dist/cjs/classes/pluggable.proxy.js +0 -444
  76. package/dist/cjs/classes/pluggable.proxy.js.map +0 -1
  77. package/dist/cjs/classes/property.d.ts +0 -79
  78. package/dist/cjs/classes/property.js +0 -284
  79. package/dist/cjs/classes/property.js.map +0 -1
  80. package/dist/cjs/classes/refmap.d.ts +0 -238
  81. package/dist/cjs/classes/refmap.js +0 -421
  82. package/dist/cjs/classes/refmap.js.map +0 -1
  83. package/dist/cjs/classes/refset.d.ts +0 -186
  84. package/dist/cjs/classes/refset.js +0 -370
  85. package/dist/cjs/classes/refset.js.map +0 -1
  86. package/dist/cjs/classes/symkeys.d.ts +0 -349
  87. package/dist/cjs/classes/symkeys.js +0 -510
  88. package/dist/cjs/classes/symkeys.js.map +0 -1
  89. package/dist/cjs/classes/type.d.ts +0 -56
  90. package/dist/cjs/classes/type.js +0 -405
  91. package/dist/cjs/classes/type.js.map +0 -1
  92. package/dist/cjs/function.extensions.d.ts +0 -12
  93. package/dist/cjs/function.extensions.js +0 -758
  94. package/dist/cjs/function.extensions.js.map +0 -1
  95. package/dist/cjs/global.this.d.ts +0 -2
  96. package/dist/cjs/global.this.js +0 -300
  97. package/dist/cjs/global.this.js.map +0 -1
  98. package/dist/cjs/index.d.ts +0 -31
  99. package/dist/cjs/index.js +0 -228
  100. package/dist/cjs/index.js.map +0 -1
  101. package/dist/cjs/json.extensions.d.ts +0 -2
  102. package/dist/cjs/json.extensions.js +0 -109
  103. package/dist/cjs/json.extensions.js.map +0 -1
  104. package/dist/cjs/map.extensions.d.ts +0 -3
  105. package/dist/cjs/map.extensions.js +0 -143
  106. package/dist/cjs/map.extensions.js.map +0 -1
  107. package/dist/cjs/math.extension.d.ts +0 -14
  108. package/dist/cjs/math.extension.js +0 -71
  109. package/dist/cjs/math.extension.js.map +0 -1
  110. package/dist/cjs/number.extension.d.ts +0 -44
  111. package/dist/cjs/number.extension.js +0 -278
  112. package/dist/cjs/number.extension.js.map +0 -1
  113. package/dist/cjs/object.extensions.d.ts +0 -33
  114. package/dist/cjs/object.extensions.js +0 -1091
  115. package/dist/cjs/object.extensions.js.map +0 -1
  116. package/dist/cjs/package.json +0 -3
  117. package/dist/cjs/proxy.extensions.d.ts +0 -2
  118. package/dist/cjs/proxy.extensions.js +0 -207
  119. package/dist/cjs/proxy.extensions.js.map +0 -1
  120. package/dist/cjs/reflect.extensions.d.ts +0 -14
  121. package/dist/cjs/reflect.extensions.js +0 -316
  122. package/dist/cjs/reflect.extensions.js.map +0 -1
  123. package/dist/cjs/regular.expression.extensions.d.ts +0 -2
  124. package/dist/cjs/regular.expression.extensions.js +0 -423
  125. package/dist/cjs/regular.expression.extensions.js.map +0 -1
  126. package/dist/cjs/set.extensions.d.ts +0 -40
  127. package/dist/cjs/set.extensions.js +0 -355
  128. package/dist/cjs/set.extensions.js.map +0 -1
  129. package/dist/cjs/string.extensions.d.ts +0 -23
  130. package/dist/cjs/string.extensions.js +0 -704
  131. package/dist/cjs/string.extensions.js.map +0 -1
  132. package/dist/cjs/symbol.extensions.d.ts +0 -11
  133. package/dist/cjs/symbol.extensions.js +0 -735
  134. package/dist/cjs/symbol.extensions.js.map +0 -1
  135. package/dist/cjs/utils/copy.object.d.ts +0 -408
  136. package/dist/cjs/utils/copy.object.js +0 -720
  137. package/dist/cjs/utils/copy.object.js.map +0 -1
  138. package/dist/cjs/utils/descriptor.utils.d.ts +0 -298
  139. package/dist/cjs/utils/descriptor.utils.js +0 -889
  140. package/dist/cjs/utils/descriptor.utils.js.map +0 -1
  141. package/dist/cjs/utils/index.d.ts +0 -75
  142. package/dist/cjs/utils/index.js +0 -61
  143. package/dist/cjs/utils/index.js.map +0 -1
  144. package/dist/cjs/utils/stdout.d.ts +0 -742
  145. package/dist/cjs/utils/stdout.js +0 -1042
  146. package/dist/cjs/utils/stdout.js.map +0 -1
  147. package/dist/cjs/utils/toolkit.d.ts +0 -1898
  148. package/dist/cjs/utils/toolkit.js +0 -1378
  149. package/dist/cjs/utils/toolkit.js.map +0 -1
  150. package/dist/cjs/weakref.extensions.d.ts +0 -2
  151. package/dist/cjs/weakref.extensions.js +0 -19
  152. package/dist/cjs/weakref.extensions.js.map +0 -1
  153. package/dist/mjs/array.extensions.d.ts +0 -39
  154. package/dist/mjs/array.extensions.js +0 -474
  155. package/dist/mjs/array.extensions.js.map +0 -1
  156. package/dist/mjs/big.int.extension.d.ts +0 -31
  157. package/dist/mjs/big.int.extension.js +0 -270
  158. package/dist/mjs/big.int.extension.js.map +0 -1
  159. package/dist/mjs/classes/asyncIterable.d.ts +0 -126
  160. package/dist/mjs/classes/asyncIterable.js +0 -204
  161. package/dist/mjs/classes/asyncIterable.js.map +0 -1
  162. package/dist/mjs/classes/deferred.d.ts +0 -146
  163. package/dist/mjs/classes/deferred.js +0 -287
  164. package/dist/mjs/classes/deferred.js.map +0 -1
  165. package/dist/mjs/classes/descriptor.d.ts +0 -334
  166. package/dist/mjs/classes/descriptor.js +0 -533
  167. package/dist/mjs/classes/descriptor.js.map +0 -1
  168. package/dist/mjs/classes/enum.d.ts +0 -50
  169. package/dist/mjs/classes/enum.js +0 -400
  170. package/dist/mjs/classes/enum.js.map +0 -1
  171. package/dist/mjs/classes/index.d.ts +0 -15
  172. package/dist/mjs/classes/index.js +0 -46
  173. package/dist/mjs/classes/index.js.map +0 -1
  174. package/dist/mjs/classes/introspector.d.ts +0 -20
  175. package/dist/mjs/classes/introspector.js +0 -126
  176. package/dist/mjs/classes/introspector.js.map +0 -1
  177. package/dist/mjs/classes/iterable.d.ts +0 -169
  178. package/dist/mjs/classes/iterable.js +0 -263
  179. package/dist/mjs/classes/iterable.js.map +0 -1
  180. package/dist/mjs/classes/param.parser.d.ts +0 -221
  181. package/dist/mjs/classes/param.parser.js +0 -238
  182. package/dist/mjs/classes/param.parser.js.map +0 -1
  183. package/dist/mjs/classes/pluggable.proxy.d.ts +0 -153
  184. package/dist/mjs/classes/pluggable.proxy.js +0 -438
  185. package/dist/mjs/classes/pluggable.proxy.js.map +0 -1
  186. package/dist/mjs/classes/property.d.ts +0 -79
  187. package/dist/mjs/classes/property.js +0 -280
  188. package/dist/mjs/classes/property.js.map +0 -1
  189. package/dist/mjs/classes/refmap.d.ts +0 -238
  190. package/dist/mjs/classes/refmap.js +0 -417
  191. package/dist/mjs/classes/refmap.js.map +0 -1
  192. package/dist/mjs/classes/refset.d.ts +0 -186
  193. package/dist/mjs/classes/refset.js +0 -366
  194. package/dist/mjs/classes/refset.js.map +0 -1
  195. package/dist/mjs/classes/symkeys.d.ts +0 -349
  196. package/dist/mjs/classes/symkeys.js +0 -506
  197. package/dist/mjs/classes/symkeys.js.map +0 -1
  198. package/dist/mjs/classes/type.d.ts +0 -56
  199. package/dist/mjs/classes/type.js +0 -401
  200. package/dist/mjs/classes/type.js.map +0 -1
  201. package/dist/mjs/function.extensions.d.ts +0 -12
  202. package/dist/mjs/function.extensions.js +0 -755
  203. package/dist/mjs/function.extensions.js.map +0 -1
  204. package/dist/mjs/global.this.d.ts +0 -2
  205. package/dist/mjs/global.this.js +0 -264
  206. package/dist/mjs/global.this.js.map +0 -1
  207. package/dist/mjs/index.d.ts +0 -31
  208. package/dist/mjs/index.js +0 -206
  209. package/dist/mjs/index.js.map +0 -1
  210. package/dist/mjs/json.extensions.d.ts +0 -2
  211. package/dist/mjs/json.extensions.js +0 -106
  212. package/dist/mjs/json.extensions.js.map +0 -1
  213. package/dist/mjs/map.extensions.d.ts +0 -3
  214. package/dist/mjs/map.extensions.js +0 -140
  215. package/dist/mjs/map.extensions.js.map +0 -1
  216. package/dist/mjs/math.extension.d.ts +0 -14
  217. package/dist/mjs/math.extension.js +0 -68
  218. package/dist/mjs/math.extension.js.map +0 -1
  219. package/dist/mjs/number.extension.d.ts +0 -44
  220. package/dist/mjs/number.extension.js +0 -275
  221. package/dist/mjs/number.extension.js.map +0 -1
  222. package/dist/mjs/object.extensions.d.ts +0 -33
  223. package/dist/mjs/object.extensions.js +0 -1088
  224. package/dist/mjs/object.extensions.js.map +0 -1
  225. package/dist/mjs/package.json +0 -3
  226. package/dist/mjs/proxy.extensions.d.ts +0 -2
  227. package/dist/mjs/proxy.extensions.js +0 -204
  228. package/dist/mjs/proxy.extensions.js.map +0 -1
  229. package/dist/mjs/reflect.extensions.d.ts +0 -14
  230. package/dist/mjs/reflect.extensions.js +0 -313
  231. package/dist/mjs/reflect.extensions.js.map +0 -1
  232. package/dist/mjs/regular.expression.extensions.d.ts +0 -2
  233. package/dist/mjs/regular.expression.extensions.js +0 -420
  234. package/dist/mjs/regular.expression.extensions.js.map +0 -1
  235. package/dist/mjs/set.extensions.d.ts +0 -40
  236. package/dist/mjs/set.extensions.js +0 -352
  237. package/dist/mjs/set.extensions.js.map +0 -1
  238. package/dist/mjs/string.extensions.d.ts +0 -23
  239. package/dist/mjs/string.extensions.js +0 -701
  240. package/dist/mjs/string.extensions.js.map +0 -1
  241. package/dist/mjs/symbol.extensions.d.ts +0 -11
  242. package/dist/mjs/symbol.extensions.js +0 -732
  243. package/dist/mjs/symbol.extensions.js.map +0 -1
  244. package/dist/mjs/utils/copy.object.d.ts +0 -408
  245. package/dist/mjs/utils/copy.object.js +0 -702
  246. package/dist/mjs/utils/copy.object.js.map +0 -1
  247. package/dist/mjs/utils/descriptor.utils.d.ts +0 -298
  248. package/dist/mjs/utils/descriptor.utils.js +0 -875
  249. package/dist/mjs/utils/descriptor.utils.js.map +0 -1
  250. package/dist/mjs/utils/index.d.ts +0 -75
  251. package/dist/mjs/utils/index.js +0 -45
  252. package/dist/mjs/utils/index.js.map +0 -1
  253. package/dist/mjs/utils/stdout.d.ts +0 -742
  254. package/dist/mjs/utils/stdout.js +0 -1037
  255. package/dist/mjs/utils/stdout.js.map +0 -1
  256. package/dist/mjs/utils/toolkit.d.ts +0 -1898
  257. package/dist/mjs/utils/toolkit.js +0 -1373
  258. package/dist/mjs/utils/toolkit.js.map +0 -1
  259. package/dist/mjs/weakref.extensions.d.ts +0 -2
  260. package/dist/mjs/weakref.extensions.js +0 -16
  261. package/dist/mjs/weakref.extensions.js.map +0 -1
  262. package/jsdoc-config.json +0 -31
  263. package/tsconfig.base.json +0 -28
  264. package/tsconfig.cjs.json +0 -8
  265. package/tsconfig.esm.json +0 -8
  266. package/vitest.config.js +0 -7
@@ -1,1373 +0,0 @@
1
- const map = new Map([
2
- ['object', Object], [Object, 'object'], ['Object', Object],
3
- ['number', Number], [Number, 'number'], ['Number', Number],
4
- ['string', String], [String, 'string'], ['String', String],
5
- ['function', Function], [Function, 'function'], ['Function', Function],
6
- ['boolean', Boolean], [Boolean, 'boolean'], ['Boolean', Boolean],
7
- ['bigint', BigInt], [BigInt, 'bigint'], ['BigInt', BigInt],
8
- ['symbol', Symbol], [Symbol, 'symbol'], ['Symbol', Symbol],
9
- ['undefined', undefined], [undefined, 'undefined'],
10
- ['null', null], [null, 'null'],
11
- ]);
12
- /**
13
- * Utility functions to check the type and properties of a value.
14
- */
15
- export const is = {
16
- /**
17
- * Checks if a value matches a specified type or class.
18
- *
19
- * This function determines if the provided value matches the specified
20
- * type or class. It supports both primitive types and class constructors.
21
- *
22
- * @param {*} value - The value to check.
23
- * @param {*} typeOrClass - The type or class to compare against.
24
- * @param {boolean} [alreadyReversed=false] - Internal flag to prevent
25
- * infinite recursion. Not intended for external use.
26
- * @returns {boolean} True if the value matches the type or class,
27
- * false otherwise.
28
- *
29
- * @example
30
- * // Returns true
31
- * is.a(42, 'number')
32
- *
33
- * @example
34
- * // Returns true
35
- * is.a(new Date(), Date)
36
- *
37
- * @example
38
- * // Returns false
39
- * is.a('string', Number)
40
- */
41
- a(value, typeOrClass) {
42
- const valueType = typeof value;
43
- const valueTag = this.object(value) && value[Symbol.toStringTag];
44
- if (value === typeOrClass)
45
- return true;
46
- if (this.function(typeOrClass)) {
47
- const typeTag = this.object(typeOrClass) && typeOrClass[Symbol.toStringTag];
48
- if (valueTag && valueTag === typeOrClass.name)
49
- return true;
50
- if (typeOrClass?.prototype && value instanceof typeOrClass)
51
- return true;
52
- return map.get(valueType) === typeOrClass;
53
- }
54
- else if (map.get(valueType)?.name === typeOrClass)
55
- return true;
56
- else if (valueType === typeOrClass || valueTag === typeOrClass)
57
- return true;
58
- return false;
59
- },
60
- /**
61
- * Check if a value is an accessor descriptor.
62
- *
63
- * An accessor descriptor is an object that describes the configuration of a
64
- * property on an object, specifically focusing on the 'get' and 'set'
65
- * attributes. Computed accessor descriptors are invalid if they also have
66
- * a `value` and/or `writable` property.
67
- *
68
- * @param value The value to check.
69
- * @returns True if the value is an accessor descriptor, false otherwise.
70
- *
71
- * @example
72
- * // Returns true
73
- * is.accessorDescriptor({ get: () => 42, set: () => {} });
74
- *
75
- * // Returns false
76
- * is.accessorDescriptor({ value: 42, writable: true });
77
- */
78
- accessorDescriptor(value) {
79
- return !!(this.descriptor(value) &&
80
- (value?.get || value?.set) &&
81
- value?.writable === undefined &&
82
- value?.value === undefined);
83
- },
84
- /**
85
- * Check if a value is an array.
86
- *
87
- * @param value The value to check.
88
- * @returns True if the value is an array, false otherwise.
89
- *
90
- * @example
91
- * is.array([1, 2, 3]); // true
92
- * is.array('string'); // false
93
- */
94
- array(value) {
95
- return Array.isArray(value);
96
- },
97
- /**
98
- * Check if a value is a bigint.
99
- *
100
- * @param value The value to check.
101
- * @returns True if the value is a bigint, false otherwise.
102
- *
103
- * @example
104
- * is.bigint(123n); // true
105
- * is.bigint(123); // false
106
- */
107
- bigint(value) {
108
- return typeof value === "bigint" || value instanceof globalThis?.BigInt;
109
- },
110
- /**
111
- * Checks if a value is strictly a boolean (true or false).
112
- *
113
- * This method verifies if the provided value is either `true` or `false`.
114
- *
115
- * @param {*} value - The value to check.
116
- * @returns {boolean} True if the value is a boolean, false otherwise.
117
- *
118
- * @example
119
- * is.boolean(true); // true
120
- * is.boolean(false); // true
121
- * is.boolean(1); // false
122
- * is.boolean("true"); // false
123
- */
124
- boolean(value) {
125
- return [true, false].some(bool => bool === value);
126
- },
127
- /**
128
- * Check if an object is callable. This function is more or less a
129
- * synonym or alias for `is.function()`.
130
- *
131
- * @param object The object to check.
132
- * @returns True if the object is callable, false otherwise.
133
- *
134
- * @note if you wish to know if a descriptor has a callable `value`,
135
- * `get`, or `set` function, use `is.callableDescriptor` instead.
136
- *
137
- * @example
138
- * is.callable(function() {}); // true
139
- */
140
- callable(object) {
141
- return this.function(object);
142
- },
143
- /**
144
- * Check if an object is callable. It looks to see if the object
145
- * represents a descriptor that is callable by checking object
146
- * properties named `value`, `get`, and `set`. If any three variations
147
- * yields a function type, true is returned.
148
- *
149
- * @param object The object to check.
150
- * @returns True if the object is callable, false otherwise.
151
- *
152
- * @example
153
- * is.callable({ get: function() {} }); // true
154
- * is.callable(123); // false
155
- *
156
- * // Note the differences between these
157
- * const object = { get name() { return "Brie"; } };
158
- * const descriptor = Object.getOwnPropertyDescriptor(object, 'name');
159
- * is.callable(object); // false
160
- * is.callable(descriptor); // true
161
- */
162
- callableDescriptor(object) {
163
- const { value, get, set } = this.shiny(object) ? object : {};
164
- return [value, get, set].some((val) => this.function(val));
165
- },
166
- /**
167
- * Check if a value is a data property descriptor.
168
- *
169
- * A data descriptor is an object that describes the configuration of a
170
- * property on an object, specifically focusing on the 'value' and
171
- * 'writable' attributes. The descriptor is invalid if it contains
172
- * thew accessor descriptors `get` or `set`.
173
- *
174
- * @param value The value to check.
175
- * @returns True if the value is a data descriptor, false otherwise.
176
- *
177
- * @example
178
- * // Returns true
179
- * is.dataDescriptor({ value: 42, writable: true });
180
- *
181
- * // Returns false
182
- * is.dataDescriptor({ get: () => 42, set: () => {} });
183
- */
184
- dataDescriptor(value) {
185
- return (this.descriptor(value) &&
186
- (value?.value || value?.writable) &&
187
- value?.get === undefined &&
188
- value?.set === undefined);
189
- },
190
- /**
191
- * Check if a value is a property descriptor.
192
- *
193
- * A property descriptor is an object that describes the configuration of a
194
- * property on an object. This function checks if the provided value is an
195
- * object and contains any of the standard property descriptor keys.
196
- *
197
- * @param value The value to check.
198
- * @returns True if the value is a property descriptor, false otherwise.
199
- *
200
- * @example
201
- * is.descriptor({ configurable: true, enumerable: false }); // true
202
- * is.descriptor({ get: () => {}, set: () => {} }); // true
203
- * is.descriptor({}); // false
204
- */
205
- descriptor(value) {
206
- if (!is.object(value)) {
207
- return false;
208
- }
209
- const _has = (key) => Reflect.has(value, key);
210
- const hasBase = ["configurable", "enumerable"].some((key) => _has(key));
211
- const hasData = ["value", "writable"].some((key) => _has(key));
212
- const hasAccess = ["get", "set"].some((key) => _has(key));
213
- return hasBase || hasData || hasAccess;
214
- },
215
- /**
216
- * Checks if a value is strictly false.
217
- *
218
- * This method verifies if the provided value is strictly `false`.
219
- *
220
- * @param {*} value - The value to check.
221
- * @returns {boolean} True if the value is strictly false, false otherwise.
222
- *
223
- * @example
224
- * is.false(false); // true
225
- * is.false(true); // false
226
- * is.false(0); // false
227
- */
228
- false(value) {
229
- return value === false;
230
- },
231
- /**
232
- * Checks if a value is falsy.
233
- *
234
- * This method converts the provided value to a boolean and returns
235
- * `true` if the value is falsy (i.e., false, 0, "", null, undefined,
236
- * or NaN).
237
- *
238
- * @param {*} value - The value to check.
239
- * @returns {boolean} True if the value is falsy, false otherwise.
240
- *
241
- * @example
242
- * is.falsy(0); // true
243
- * is.falsy(""); // true
244
- * is.falsy(1); // false
245
- * is.falsy("hello"); // false
246
- */
247
- falsy(value) {
248
- return !!!value;
249
- },
250
- /**
251
- * Alias for the `falsy` method.
252
- *
253
- * This method is an alias for `is.falsy` and performs the same check.
254
- *
255
- * @param {*} value - The value to check.
256
- * @returns {boolean} True if the value is falsy, false otherwise.
257
- *
258
- * @example
259
- * is.falsey(0); // true
260
- * is.falsey(""); // true
261
- * is.falsey(1); // false
262
- * is.falsey("hello"); // false
263
- */
264
- falsey(value) {
265
- return this.falsy(value);
266
- },
267
- /**
268
- * Check if a value is a function.
269
- *
270
- * @param value The value to check.
271
- * @returns True if the value is a function, false otherwise.
272
- *
273
- * @example
274
- * is.function(function() {}); // true
275
- * is.function(123); // false
276
- */
277
- function(value) {
278
- return typeof value === "function" || value instanceof Function;
279
- },
280
- /**
281
- * Check if a value is iterable. Depending on the environment, JavaScript
282
- * will permit `'string'[Symbol.iterator]()` whereas in some places, you
283
- * will need to wrap string in an object first. Since other JSVM provided
284
- * environments may or may not be leniant with this, we play it safe by
285
- * implicitly object converting values before checking for the symbol. If
286
- * a value is already an object, it will simply be passed through.
287
- *
288
- * @param value The value to check.
289
- * @returns True if the value is iterable, false otherwise.
290
- *
291
- * @example
292
- * is.iterable([1, 2, 3]); // true
293
- * is.iterable('string'); // true
294
- * is.iterable(123); // false
295
- */
296
- iterable(value) {
297
- const object = Object(value);
298
- return object && Reflect.has(object, Symbol.iterator);
299
- },
300
- /**
301
- * Check if a value is null or undefined.
302
- *
303
- * @param value The value to check.
304
- * @returns True if the value is null or undefined, false otherwise.
305
- *
306
- * @example
307
- * is.nullish(null); // true
308
- * is.nullish(undefined); // true
309
- * is.nullish('value'); // false
310
- */
311
- nullish(value) {
312
- return value === null || value === undefined;
313
- },
314
- /**
315
- * Check if a value is a number.
316
- *
317
- * @param value The value to check.
318
- * @returns True if the value is a number, false otherwise.
319
- *
320
- * @example
321
- * is.number(123); // true
322
- * is.number('123'); // false
323
- */
324
- number(value) {
325
- return typeof value === "number" || value instanceof Number;
326
- },
327
- /**
328
- * Check if a value is an object.
329
- *
330
- * @param value The value to check.
331
- * @returns True if the value is an object, false otherwise.
332
- *
333
- * @example
334
- * is.object({}); // true
335
- * is.object(null); // false
336
- */
337
- object(value) {
338
- return !!(value && typeof value === "object");
339
- },
340
- /**
341
- * Check if a value is a primitive type.
342
- *
343
- * This function determines if the provided value is one of the JavaScript
344
- * primitive types: string, number, boolean, bigint, or symbol.
345
- *
346
- * @param value The value to check.
347
- * @returns True if the value is a primitive type, false otherwise.
348
- *
349
- * @example
350
- * // Returns true
351
- * is.primitive('hello');
352
- *
353
- * // Returns true
354
- * is.primitive(123);
355
- *
356
- * // Returns true
357
- * is.primitive(true);
358
- *
359
- * // Returns true
360
- * is.primitive(123n);
361
- *
362
- * // Returns true
363
- * is.primitive(Symbol('symbol'));
364
- *
365
- * // Returns false
366
- * is.primitive({});
367
- *
368
- * // Returns false
369
- * is.primitive([]);
370
- */
371
- primitive(value) {
372
- if (this.nullish(value))
373
- return true;
374
- return ["string", "number", "boolean", "bigint", "symbol"].some((type) => typeof value === type);
375
- },
376
- /**
377
- * The use of `typeof` is not a safe guarantor when it comes to Reflect
378
- * supported values. Any non-null value that returns a `typeof` either
379
- * `object` or `function` should suffice. Note that arrays return 'object'
380
- * when run through `typeof`. Shiny is clearly a reference to something
381
- * reflective and is much shorter to type. Also, Mal says shiny. :)
382
- *
383
- * @param value The value to check.
384
- * @returns True if the value is an object or a function, false otherwise.
385
- *
386
- * @example
387
- * is.shiny({}); // true
388
- * is.shiny(function() {}); // true
389
- * is.shiny(123); // false
390
- */
391
- shiny(value) {
392
- return !!(this.object(value) || this.function(value));
393
- },
394
- /**
395
- * Check if a value is a string.
396
- *
397
- * @param value The value to check.
398
- * @returns True if the value is a string, false otherwise.
399
- *
400
- * @example
401
- * is.string('hello'); // true
402
- * is.string(123); // false
403
- */
404
- string(value) {
405
- return typeof value === "string" || value instanceof String;
406
- },
407
- /**
408
- * Checks if a value is a symbol.
409
- *
410
- * This function determines whether the provided value is of type
411
- * 'symbol' or an instance of the Symbol object.
412
- *
413
- * @param value - The value to check.
414
- * @returns True if the value is a symbol, false otherwise.
415
- *
416
- * @example
417
- * is.symbol(Symbol('foo')); // Returns true
418
- * is.symbol('foo'); // Returns false
419
- */
420
- symbol(value) {
421
- return typeof value === "symbol" || value instanceof Symbol;
422
- },
423
- /**
424
- * Checks if a value is strictly true.
425
- *
426
- * This method verifies if the provided value is strictly `true`.
427
- *
428
- * @param {*} value - The value to check.
429
- * @returns {boolean} True if the value is strictly true, false otherwise.
430
- *
431
- * @example
432
- * is.true(true); // true
433
- * is.true(false); // false
434
- * is.true(1); // false
435
- */
436
- true(value) {
437
- return value === true;
438
- },
439
- /**
440
- * Checks if a value is truthy.
441
- *
442
- * This method converts the provided value to a boolean and returns
443
- * `true` if the value is truthy (i.e., not false, 0, "", null, undefined,
444
- * or NaN).
445
- *
446
- * @param {*} value - The value to check.
447
- * @returns {boolean} True if the value is truthy, false otherwise.
448
- *
449
- * @example
450
- * is.truthy(1); // true
451
- * is.truthy("hello"); // true
452
- * is.truthy(0); // false
453
- * is.truthy(""); // false
454
- */
455
- truthy(value) {
456
- return !!value;
457
- },
458
- };
459
- export const si = {
460
- /**
461
- * Checks if a value matches a specified type or class.
462
- *
463
- * This function determines if the provided value matches the specified
464
- * type or class. It supports both primitive types and class constructors.
465
- *
466
- * @param {*} value - The value to check.
467
- * @param {*} typeOrClass - The type or class to compare against.
468
- * @param {boolean} [alreadyReversed=false] - Internal flag to prevent
469
- * infinite recursion. Not intended for external use.
470
- * @returns {boolean} True if the value matches the type or class,
471
- * false otherwise.
472
- *
473
- * @example
474
- * // Returns true
475
- * is.a(42, 'number')
476
- *
477
- * @example
478
- * // Returns true
479
- * is.a(new Date(), Date)
480
- *
481
- * @example
482
- * // Returns false
483
- * is.a('string', Number)
484
- */
485
- a(value, typeOrClass, thenValue, elseValue) {
486
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
487
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
488
- return is.a(value, typeOrClass) ? thenValue : elseValue;
489
- },
490
- /**
491
- * Check if a value is an accessor descriptor.
492
- *
493
- * An accessor descriptor is an object that describes the configuration of a
494
- * property on an object, specifically focusing on the 'get' and 'set'
495
- * attributes. Computed accessor descriptors are invalid if they also have
496
- * a `value` and/or `writable` property.
497
- *
498
- * @param value The value to check.
499
- * @returns True if the value is an accessor descriptor, false otherwise.
500
- *
501
- * @example
502
- * // Returns true
503
- * is.accessorDescriptor({ get: () => 42, set: () => {} });
504
- *
505
- * // Returns false
506
- * is.accessorDescriptor({ value: 42, writable: true });
507
- */
508
- accessorDescriptor(value, thenValue, elseValue) {
509
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
510
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
511
- return is.accessorDescriptor(value) ? thenVal : elseVal;
512
- },
513
- /**
514
- * Check if a value is an array.
515
- *
516
- * @param value The value to check.
517
- * @returns True if the value is an array, false otherwise.
518
- *
519
- * @example
520
- * is.array([1, 2, 3]); // true
521
- * is.array('string'); // false
522
- */
523
- array(value, thenValue, elseValue) {
524
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
525
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
526
- return is.array(value) ? thenVal : elseVal;
527
- },
528
- /**
529
- * Check if a value is a bigint.
530
- *
531
- * @param value The value to check.
532
- * @returns True if the value is a bigint, false otherwise.
533
- *
534
- * @example
535
- * is.bigint(123n); // true
536
- * is.bigint(123); // false
537
- */
538
- bigint(value, thenValue, elseValue) {
539
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
540
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
541
- return is.bigint(value) ? thenVal : elseVal;
542
- },
543
- /**
544
- * Checks if a value is strictly a boolean (true or false).
545
- *
546
- * This method verifies if the provided value is either `true` or `false`.
547
- *
548
- * @param {*} value - The value to check.
549
- * @returns {boolean} True if the value is a boolean, false otherwise.
550
- *
551
- * @example
552
- * is.boolean(true); // true
553
- * is.boolean(false); // true
554
- * is.boolean(1); // false
555
- * is.boolean("true"); // false
556
- */
557
- boolean(value, thenValue, elseValue) {
558
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
559
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
560
- return is.boolean(value) ? thenVal : elseVal;
561
- },
562
- /**
563
- * Check if an object is callable. This function is more or less a
564
- * synonym or alias for `is.function()`.
565
- *
566
- * @param object The object to check.
567
- * @returns True if the object is callable, false otherwise.
568
- *
569
- * @note if you wish to know if a descriptor has a callable `value`,
570
- * `get`, or `set` function, use `is.callableDescriptor` instead.
571
- *
572
- * @example
573
- * is.callable(function() {}); // true
574
- */
575
- callable(object, thenValue, elseValue) {
576
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
577
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
578
- return is.callable(object) ? thenVal : elseVal;
579
- },
580
- /**
581
- * Check if an object is callable. It looks to see if the object
582
- * represents a descriptor that is callable by checking object
583
- * properties named `value`, `get`, and `set`. If any three variations
584
- * yields a function type, true is returned.
585
- *
586
- * @param object The object to check.
587
- * @returns True if the object is callable, false otherwise.
588
- *
589
- * @example
590
- * is.callable({ get: function() {} }); // true
591
- * is.callable(123); // false
592
- *
593
- * // Note the differences between these
594
- * const object = { get name() { return "Brie"; } };
595
- * const descriptor = Object.getOwnPropertyDescriptor(object, 'name');
596
- * is.callable(object); // false
597
- * is.callable(descriptor); // true
598
- */
599
- callableDescriptor(object, thenValue, elseValue) {
600
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
601
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
602
- return is.callableDescriptor(object) ? thenVal : elseVal;
603
- },
604
- /**
605
- * Check if a value is a data property descriptor.
606
- *
607
- * A data descriptor is an object that describes the configuration of a
608
- * property on an object, specifically focusing on the 'value' and
609
- * 'writable' attributes. The descriptor is invalid if it contains
610
- * thew accessor descriptors `get` or `set`.
611
- *
612
- * @param value The value to check.
613
- * @returns True if the value is a data descriptor, false otherwise.
614
- *
615
- * @example
616
- * // Returns true
617
- * is.dataDescriptor({ value: 42, writable: true });
618
- *
619
- * // Returns false
620
- * is.dataDescriptor({ get: () => 42, set: () => {} });
621
- */
622
- dataDescriptor(value, thenValue, elseValue) {
623
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
624
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
625
- return is.dataDescriptor(value) ? thenVal : elseVal;
626
- },
627
- /**
628
- * Check if a value is a property descriptor.
629
- *
630
- * A property descriptor is an object that describes the configuration of a
631
- * property on an object. This function checks if the provided value is an
632
- * object and contains any of the standard property descriptor keys.
633
- *
634
- * @param value The value to check.
635
- * @returns True if the value is a property descriptor, false otherwise.
636
- *
637
- * @example
638
- * is.descriptor({ configurable: true, enumerable: false }); // true
639
- * is.descriptor({ get: () => {}, set: () => {} }); // true
640
- * is.descriptor({}); // false
641
- */
642
- descriptor(value, thenValue, elseValue) {
643
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
644
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
645
- return is.descriptor(value) ? thenVal : elseVal;
646
- },
647
- /**
648
- * Checks if a value is strictly false.
649
- *
650
- * This method verifies if the provided value is strictly `false`.
651
- *
652
- * @param {*} value - The value to check.
653
- * @returns {boolean} True if the value is strictly false, false otherwise.
654
- *
655
- * @example
656
- * is.false(false); // true
657
- * is.false(true); // false
658
- * is.false(0); // false
659
- */
660
- false(value, thenValue, elseValue) {
661
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
662
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
663
- return is.false(value) ? thenVal : elseVal;
664
- },
665
- /**
666
- * Checks if a value is falsy.
667
- *
668
- * This method converts the provided value to a boolean and returns
669
- * `true` if the value is falsy (i.e., false, 0, "", null, undefined,
670
- * or NaN).
671
- *
672
- * @param {*} value - The value to check.
673
- * @returns {boolean} True if the value is falsy, false otherwise.
674
- *
675
- * @example
676
- * is.falsy(0); // true
677
- * is.falsy(""); // true
678
- * is.falsy(1); // false
679
- * is.falsy("hello"); // false
680
- */
681
- falsy(value, thenValue, elseValue) {
682
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
683
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
684
- return is.falsy(value) ? thenVal : elseVal;
685
- },
686
- /**
687
- * Alias for the `falsy` method.
688
- *
689
- * This method is an alias for `is.falsy` and performs the same check.
690
- *
691
- * @param {*} value - The value to check.
692
- * @returns {boolean} True if the value is falsy, false otherwise.
693
- *
694
- * @example
695
- * is.falsey(0); // true
696
- * is.falsey(""); // true
697
- * is.falsey(1); // false
698
- * is.falsey("hello"); // false
699
- */
700
- falsey(value, thenValue, elseValue) {
701
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
702
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
703
- return is.falsey(value) ? thenVal : elseVal;
704
- },
705
- /**
706
- * Check if a value is a function.
707
- *
708
- * @param value The value to check.
709
- * @returns True if the value is a function, false otherwise.
710
- *
711
- * @example
712
- * is.function(function() {}); // true
713
- * is.function(123); // false
714
- */
715
- function(value, thenValue, elseValue) {
716
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
717
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
718
- return is.function(value) ? thenVal : elseVal;
719
- },
720
- /**
721
- * Check if a value is iterable. Depending on the environment, JavaScript
722
- * will permit `'string'[Symbol.iterator]()` whereas in some places, you
723
- * will need to wrap string in an object first. Since other JSVM provided
724
- * environments may or may not be leniant with this, we play it safe by
725
- * implicitly object converting values before checking for the symbol. If
726
- * a value is already an object, it will simply be passed through.
727
- *
728
- * @param value The value to check.
729
- * @returns True if the value is iterable, false otherwise.
730
- *
731
- * @example
732
- * is.iterable([1, 2, 3]); // true
733
- * is.iterable('string'); // true
734
- * is.iterable(123); // false
735
- */
736
- iterable(value, thenValue, elseValue) {
737
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
738
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
739
- return is.iterable(value) ? thenVal : elseVal;
740
- },
741
- /**
742
- * Check if a value is null or undefined.
743
- *
744
- * @param value The value to check.
745
- * @returns True if the value is null or undefined, false otherwise.
746
- *
747
- * @example
748
- * is.nullish(null); // true
749
- * is.nullish(undefined); // true
750
- * is.nullish('value'); // false
751
- */
752
- nullish(value, thenValue, elseValue) {
753
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
754
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
755
- return is.nullish(value) ? thenVal : elseVal;
756
- },
757
- /**
758
- * Check if a value is a number.
759
- *
760
- * @param value The value to check.
761
- * @returns True if the value is a number, false otherwise.
762
- *
763
- * @example
764
- * is.number(123); // true
765
- * is.number('123'); // false
766
- */
767
- number(value, thenValue, elseValue) {
768
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
769
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
770
- return is.number(value) ? thenVal : elseVal;
771
- },
772
- /**
773
- * Check if a value is an object.
774
- *
775
- * @param value The value to check.
776
- * @returns True if the value is an object, false otherwise.
777
- *
778
- * @example
779
- * is.object({}); // true
780
- * is.object(null); // false
781
- */
782
- object(value, thenValue, elseValue) {
783
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
784
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
785
- return is.object(value) ? thenVal : elseVal;
786
- },
787
- /**
788
- * Check if a value is a primitive type.
789
- *
790
- * This function determines if the provided value is one of the JavaScript
791
- * primitive types: string, number, boolean, bigint, or symbol.
792
- *
793
- * @param value The value to check.
794
- * @returns True if the value is a primitive type, false otherwise.
795
- *
796
- * @example
797
- * // Returns true
798
- * is.primitive('hello');
799
- *
800
- * // Returns true
801
- * is.primitive(123);
802
- *
803
- * // Returns true
804
- * is.primitive(true);
805
- *
806
- * // Returns true
807
- * is.primitive(123n);
808
- *
809
- * // Returns true
810
- * is.primitive(Symbol('symbol'));
811
- *
812
- * // Returns false
813
- * is.primitive({});
814
- *
815
- * // Returns false
816
- * is.primitive([]);
817
- */
818
- primitive(value, thenValue, elseValue) {
819
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
820
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
821
- return is.primitive(value) ? thenVal : elseVal;
822
- },
823
- /**
824
- * The use of `typeof` is not a safe guarantor when it comes to Reflect
825
- * supported values. Any non-null value that returns a `typeof` either
826
- * `object` or `function` should suffice. Note that arrays return 'object'
827
- * when run through `typeof`. Shiny is clearly a reference to something
828
- * reflective and is much shorter to type. Also, Mal says shiny. :)
829
- *
830
- * @param value The value to check.
831
- * @returns True if the value is an object or a function, false otherwise.
832
- *
833
- * @example
834
- * is.shiny({}); // true
835
- * is.shiny(function() {}); // true
836
- * is.shiny(123); // false
837
- */
838
- shiny(value, thenValue, elseValue) {
839
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
840
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
841
- return is.shiny(value) ? thenVal : elseVal;
842
- },
843
- /**
844
- * Check if a value is a string.
845
- *
846
- * @param value The value to check.
847
- * @returns True if the value is a string, false otherwise.
848
- *
849
- * @example
850
- * is.string('hello'); // true
851
- * is.string(123); // false
852
- */
853
- string(value, thenValue, elseValue) {
854
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
855
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
856
- return is.string(value) ? thenVal : elseVal;
857
- },
858
- /**
859
- * Checks if a value is a symbol.
860
- *
861
- * This function determines whether the provided value is of type
862
- * 'symbol' or an instance of the Symbol object.
863
- *
864
- * @param value - The value to check.
865
- * @returns True if the value is a symbol, false otherwise.
866
- *
867
- * @example
868
- * is.symbol(Symbol('foo')); // Returns true
869
- * is.symbol('foo'); // Returns false
870
- */
871
- symbol(value, thenValue, elseValue) {
872
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
873
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
874
- return is.symbol(value) ? thenVal : elseVal;
875
- },
876
- then(condition, thenValue, elseValue) {
877
- const condVal = is.function(condition) ? condition() : condition;
878
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
879
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
880
- return condVal ? thenVal : elseVal;
881
- },
882
- /**
883
- * Checks if a value is strictly true.
884
- *
885
- * This method verifies if the provided value is strictly `true`.
886
- *
887
- * @param {*} value - The value to check.
888
- * @returns {boolean} True if the value is strictly true, false otherwise.
889
- *
890
- * @example
891
- * is.true(true); // true
892
- * is.true(false); // false
893
- * is.true(1); // false
894
- */
895
- true(value, thenValue, elseValue) {
896
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
897
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
898
- return is.true(value) ? thenVal : elseVal;
899
- },
900
- /**
901
- * Checks if a value is truthy.
902
- *
903
- * This method converts the provided value to a boolean and returns
904
- * `true` if the value is truthy (i.e., not false, 0, "", null, undefined,
905
- * or NaN).
906
- *
907
- * @param {*} value - The value to check.
908
- * @returns {boolean} True if the value is truthy, false otherwise.
909
- *
910
- * @example
911
- * is.truthy(1); // true
912
- * is.truthy("hello"); // true
913
- * is.truthy(0); // false
914
- * is.truthy(""); // false
915
- */
916
- truthy(value, thenValue, elseValue) {
917
- const thenVal = is.function(thenValue) ? thenValue() : thenValue;
918
- const elseVal = is.function(elseValue) ? elseValue() : elseValue;
919
- return is.truthy(value) ? thenVal : elseVal;
920
- },
921
- };
922
- /**
923
-
924
- * Checks if an object contains a specific key.
925
- *
926
- * This function determines if the provided object contains the specified key.
927
- * It supports various types of objects including Map, Set, WeakMap, and
928
- * WeakSet. For other objects, it uses the Reflect API to check for the key.
929
- *
930
- * @param object The object to check.
931
- * @param key The key to look for in the object.
932
- * @returns True if the object contains the key, false otherwise.
933
- *
934
- * @example
935
- * // Returns true
936
- * has(new Map([['key', 'value']]), 'key');
937
- *
938
- * @example
939
- * // Returns false
940
- * has({}, 'key');
941
- */
942
- export const has = function has(object, key) {
943
- if ([Map, Set, WeakMap, WeakSet].some((i) => object instanceof i)) {
944
- return object.has(key);
945
- }
946
- return is.shiny(object) && Reflect.has(object, key);
947
- };
948
- Object.assign(has, {
949
- /**
950
- * Checks if an object contains all specified keys.
951
- *
952
- * This function determines if the provided object contains all the keys
953
- * specified in the keys array. It supports various types of objects
954
- * including Map, Set, WeakMap, and WeakSet. For other objects, it uses
955
- * the Reflect API to check for the keys.
956
- *
957
- * @param object The object to check.
958
- * @param keys The array of keys to look for in the object.
959
- * @returns True if the object contains all the keys, false otherwise.
960
- *
961
- * @example
962
- * // Returns true
963
- * has.all(new Map([
964
- * ['key1', 'value1'], ['key2', 'value2']
965
- * ]), ['key1', 'key2']);
966
- *
967
- * @example
968
- * // Returns false
969
- * has.all({}, ['key1', 'key2']);
970
- */
971
- all(object, keys) {
972
- if (!is.shiny(object) || !is.array(keys) || !keys.length) {
973
- return false;
974
- }
975
- return keys.every((key) => has(object, key));
976
- },
977
- /**
978
- * Checks if an object contains at least one of the specified keys.
979
- *
980
- * This function determines if the provided object contains at least one
981
- * of the keys specified in the keys array. It supports various types of
982
- * objects including Map, Set, WeakMap, and WeakSet. For other objects,
983
- * it uses the Reflect API to check for the keys.
984
- *
985
- * @param object The object to check.
986
- * @param keys The array of keys to look for in the object.
987
- * @returns True if the object contains at least one of the keys, false
988
- * otherwise.
989
- *
990
- * @example
991
- * // Returns true
992
- * has.some(new Map([['key1', 'value1'], ['key2', 'value2']]), ['key1']);
993
- *
994
- * @example
995
- * // Returns false
996
- * has.some({}, ['key1', 'key2']);
997
- */
998
- some(object, keys) {
999
- if (!is.shiny(object) || !is.array(keys) || !keys.length) {
1000
- return false;
1001
- }
1002
- return keys.some((key) => has(object, key));
1003
- },
1004
- /**
1005
- * Checks if an object has a 'prototype' property.
1006
- *
1007
- * This function is one way to determine if a supplied function is a big
1008
- * arrow function or not. Regular functions and class functions, both
1009
- * static and instance level, all have prototypes. Only big arrow functions
1010
- * do not.
1011
- *
1012
- * @param {Object} object - The object to check.
1013
- * @returns {boolean} True if the object has a 'prototype' property,
1014
- * false otherwise.
1015
- *
1016
- * @example
1017
- * // Returns true
1018
- * has.prototype(function() {})
1019
- *
1020
- * @example
1021
- * // Returns false
1022
- * has.prototype(() => {})
1023
- * has.prototype(5)
1024
- * has.prototype(true)
1025
- */
1026
- prototype(object) {
1027
- // Shiny objects work with calls to Reflect.has
1028
- return is.shiny(object) && has(object, 'prototype');
1029
- },
1030
- /**
1031
- * Checks if an object has a custom string tag.
1032
- *
1033
- * This method determines if the object has a Symbol.toStringTag property,
1034
- * which is used to customize the behavior of Object.prototype.toString().
1035
- *
1036
- * @param object - The object to check.
1037
- * @returns True if the object has a custom string tag, false otherwise.
1038
- *
1039
- * @example
1040
- * const obj = { [Symbol.toStringTag]: 'CustomObject' };
1041
- * has.stringTag(obj); // Returns true
1042
- */
1043
- stringTag(object) {
1044
- return is.object(object) && has(object, Symbol.toStringTag);
1045
- },
1046
- /**
1047
- * Checks if an object has a custom toPrimitive method.
1048
- *
1049
- * This method determines if the object has a Symbol.toPrimitive property,
1050
- * which is used to convert an object to a primitive value.
1051
- *
1052
- * @param object - The object to check.
1053
- * @returns True if the object has a custom toPrimitive method,
1054
- * false otherwise.
1055
- *
1056
- * @example
1057
- * const obj = { [Symbol.toPrimitive]: () => 42 };
1058
- * has.toPrimitive(obj); // Returns true
1059
- */
1060
- toPrimitive(object) {
1061
- return is.object(object) && has(object, Symbol.toPrimitive);
1062
- },
1063
- /**
1064
- * Checks if an object has a custom valueOf method.
1065
- *
1066
- * This method determines if the object has a valueOf property that is a
1067
- * function, which is used to convert an object to a primitive value.
1068
- *
1069
- * @param object - The object to check.
1070
- * @returns True if the object has a custom valueOf method, false otherwise.
1071
- *
1072
- * @example
1073
- * const obj = { valueOf: () => 42 };
1074
- * has.valueOf(obj); // Returns true
1075
- */
1076
- valueOfFn(object) {
1077
- return (is.object(object) && has(object, "valueOf") && is.function(object.valueOf));
1078
- },
1079
- });
1080
- export const as = {
1081
- /**
1082
- * Converts a value to an array if it is iterable.
1083
- *
1084
- * @param value The value to convert.
1085
- * @returns The converted array if the value is iterable, otherwise undefined.
1086
- *
1087
- * @example
1088
- * // Returns [1, 2, 3]
1089
- * as.array([1, 2, 3]);
1090
- *
1091
- * @example
1092
- * // Returns ['s', 't', 'r', 'i', 'n', 'g']
1093
- * as.array('string');
1094
- *
1095
- * @example
1096
- * // Returns undefined
1097
- * as.array(123);
1098
- */
1099
- array(value) {
1100
- return (is.iterable(value) && Array.from(value)) || undefined;
1101
- },
1102
- /**
1103
- * Converts a value to an object. If the supplied value is a primitive
1104
- * value, in many cases, this will convert it to an object instance of
1105
- * that type. Numbers, strings, symbols, and big integers, all have
1106
- * object instance variants. Wrapping them in a call to `Object()` will
1107
- * convert the primitive into this instance variant.
1108
- *
1109
- * @param value The value to convert.
1110
- * @returns The converted object.
1111
- *
1112
- * @example
1113
- * // Returns { key: 'value' }
1114
- * as.object({ key: 'value' });
1115
- *
1116
- * @example
1117
- * // String instance as oppposed to primitive string
1118
- * typeof as.object('string') // 'object'
1119
- * as.object('string') instanceof String // true
1120
- * 'string' instanceof String // false
1121
- *
1122
- * @example
1123
- * // Returns {}
1124
- * as.object(null);
1125
- */
1126
- object(value) {
1127
- return Object(value);
1128
- },
1129
- /**
1130
- * Converts a given value to a string. This function handles various types
1131
- * of values, including null, undefined, objects with custom
1132
- * [Symbol.toPrimitive] methods, and objects with toString or valueOf
1133
- * methods.
1134
- *
1135
- * @param value The value to convert to a string.
1136
- * @param use Optional configuration object:
1137
- * - description: If true, returns the description of a Symbol.
1138
- * - stringTag: If true, returns the [Symbol.toStringTag] value if present.
1139
- * @returns The string representation of the value.
1140
- *
1141
- * @example
1142
- * // Returns 'null'
1143
- * as.string(null);
1144
- *
1145
- * @example
1146
- * // Returns '123'
1147
- * as.string(123);
1148
- *
1149
- * @example
1150
- * // Returns 'custom'
1151
- * const obj = {
1152
- * [Symbol.toPrimitive](hint) {
1153
- * if (hint === 'string') return 'custom';
1154
- * return null;
1155
- * }
1156
- * };
1157
- * as.string(obj);
1158
- *
1159
- * @example
1160
- * // Returns 'mySymbol'
1161
- * as.string(Symbol('mySymbol'), { description: true });
1162
- *
1163
- * @example
1164
- * // Returns 'Array'
1165
- * as.string([], { stringTag: true });
1166
- */
1167
- string(value, use = {
1168
- description: false,
1169
- stringTag: false,
1170
- }) {
1171
- // Check if the value is null or undefined directly
1172
- if (value === null || value === undefined) {
1173
- return String(value);
1174
- }
1175
- if (is.symbol(value) && use?.description) {
1176
- return value.description;
1177
- }
1178
- if (has.stringTag(value) && use?.stringTag) {
1179
- return value[Symbol.toStringTag];
1180
- }
1181
- // Check if the value has a [Symbol.toPrimitive] method
1182
- if (is.function(value?.[Symbol.toPrimitive])) {
1183
- const primitiveValue = value[Symbol.toPrimitive]("string");
1184
- if (is.string(primitiveValue)) {
1185
- return primitiveValue;
1186
- }
1187
- }
1188
- // Check if the value has a valueOf method
1189
- if (is.function(value?.valueOf)) {
1190
- const valueOfValue = value.valueOf();
1191
- if (is.string(valueOfValue)) {
1192
- return valueOfValue;
1193
- }
1194
- // If valueOf returns a primitive other than string, convert it to string
1195
- if (!is.object(valueOfValue)) {
1196
- return String(valueOfValue);
1197
- }
1198
- }
1199
- // Check if the value has a toString method
1200
- if (is.function(value?.toString)) {
1201
- const stringValue = value.toString();
1202
- if (is.string(stringValue)) {
1203
- return stringValue;
1204
- }
1205
- }
1206
- // Fallback to String() function
1207
- return String(value);
1208
- },
1209
- /**
1210
- * Converts a given value to a string representing an integer.
1211
- *
1212
- * This method first converts the value to a number string and then extracts
1213
- * the integer part by splitting the string at the decimal point.
1214
- *
1215
- * @param value The value to convert to an integer string.
1216
- * @returns The integer part of the value as a string.
1217
- *
1218
- * @example
1219
- * // Returns '123'
1220
- * as.integerString(123.456);
1221
- *
1222
- * @example
1223
- * // Returns '0'
1224
- * as.integerString('0.789');
1225
- */
1226
- integerString(value) {
1227
- return this.numberString(value).split(".")[0];
1228
- },
1229
- /**
1230
- * Converts a given value to a string representing a number.
1231
- *
1232
- * This method first converts the value to a string, trims any whitespace,
1233
- * and removes any non-numeric characters except for '.', 'e', 'E', '+',
1234
- * and '-'. It then uses a regular expression to match a floating-point
1235
- * number, allowing an optional leading '+' or '-' sign, digits before
1236
- * and after a single decimal point.
1237
- *
1238
- * @param value The value to convert to a number string.
1239
- * @returns The sanitized number string or an empty string if no valid
1240
- * float was found.
1241
- *
1242
- * @example
1243
- * // Returns '123.456'
1244
- * numberString(' 123.456abc ');
1245
- *
1246
- * @example
1247
- * // Returns '-0.789'
1248
- * numberString('-0.789xyz');
1249
- */
1250
- numberString(value) {
1251
- // Trim the input string
1252
- const string = this.string(value)
1253
- .trim()
1254
- .replace(/[^0-9.eE+-]/g, "");
1255
- // Use a regular expression to match a floating-point number
1256
- // Allow an optional leading '+' or '-' sign, digits before and after a
1257
- // single decimal point
1258
- const sanitizedStr = string.match(/^[-+]?\d*\.?\d+([eE][-+]?\d+)?/);
1259
- // Return the sanitized string or an empty string if no valid float
1260
- // was found
1261
- return sanitizedStr ? sanitizedStr[0] : "";
1262
- },
1263
- /**
1264
- * Converts a given value to a number.
1265
- *
1266
- * This method uses the `numberString` method to sanitize the input value
1267
- * and then converts it to a number.
1268
- *
1269
- * @param value The value to convert to a number.
1270
- * @returns The numeric representation of the value.
1271
- *
1272
- * @example
1273
- * // Returns 123.456
1274
- * number('123.456abc');
1275
- *
1276
- * @example
1277
- * // Returns -0.789
1278
- * number('-0.789xyz');
1279
- */
1280
- number(value) {
1281
- return Number(this.numberString(value));
1282
- },
1283
- /**
1284
- * Converts a given value to a bigint.
1285
- *
1286
- * This method uses the `integerString` method to sanitize the input value
1287
- * and then converts it to a bigint.
1288
- *
1289
- * @param value The value to convert to a bigint.
1290
- * @returns The bigint representation of the value.
1291
- *
1292
- * @example
1293
- * // Returns 123n
1294
- * bigint('123.456abc');
1295
- *
1296
- * @example
1297
- * // Returns 0n
1298
- * bigint('0.789xyz');
1299
- */
1300
- bigint(value) {
1301
- const BigInt = globalThis?.BigInt;
1302
- return BigInt(this.integerString(value));
1303
- },
1304
- /**
1305
- * Converts a given value to a boolean.
1306
- *
1307
- * This method takes a value, converts it to a string, and then checks
1308
- * if it matches common representations of boolean values. It returns
1309
- * `true` for "1", "yes", and "true" (case insensitive), and `false`
1310
- * for "0", "no", and "false" (case insensitive). For any other values,
1311
- * it returns the boolean representation of the value.
1312
- *
1313
- * @param {*} value - The value to convert to a boolean.
1314
- * @returns {boolean} The boolean representation of the value.
1315
- *
1316
- * @example
1317
- * // Returns true
1318
- * is.boolean("yes")
1319
- *
1320
- * @example
1321
- * // Returns false
1322
- * is.boolean("no")
1323
- *
1324
- * @example
1325
- * // Returns true
1326
- * is.boolean(1)
1327
- *
1328
- * @example
1329
- * // Returns false
1330
- * is.boolean(0)
1331
- *
1332
- * @example
1333
- * // Returns true
1334
- * is.boolean("true")
1335
- *
1336
- * @example
1337
- * // Returns false
1338
- * is.boolean("false")
1339
- *
1340
- * @example
1341
- * // Returns true
1342
- * is.boolean({})
1343
- *
1344
- * @example
1345
- * // Returns false
1346
- * is.boolean(null)
1347
- */
1348
- boolean(value) {
1349
- switch (String(value).toLowerCase()) {
1350
- case "1":
1351
- case "yes":
1352
- case "true":
1353
- return true;
1354
- case "0":
1355
- case "no":
1356
- case "false":
1357
- return false;
1358
- default:
1359
- return Boolean(value);
1360
- }
1361
- },
1362
- };
1363
- export function createToolkit() {
1364
- return { si, is, has, as };
1365
- }
1366
- export default {
1367
- as,
1368
- has,
1369
- is,
1370
- si,
1371
- createToolkit,
1372
- };
1373
- //# sourceMappingURL=toolkit.js.map