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