@budsbox/lib-es 2.3.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array.d.ts +49 -27
- package/dist/array.js +32 -20
- package/dist/function.d.ts +63 -6
- package/dist/function.js +16 -0
- package/dist/guards/assert.d.ts +213 -0
- package/dist/guards/assert.js +245 -0
- package/dist/guards/check.d.ts +470 -0
- package/dist/guards/check.js +524 -0
- package/dist/guards/describe.d.ts +7 -0
- package/dist/guards/describe.js +34 -0
- package/dist/guards/format.d.ts +109 -0
- package/dist/guards/format.js +295 -0
- package/dist/guards/hlf.d.ts +257 -0
- package/dist/guards/hlf.js +178 -0
- package/dist/guards/index.d.ts +73 -0
- package/dist/guards/index.js +124 -0
- package/dist/guards/message.d.ts +102 -0
- package/dist/guards/message.js +224 -0
- package/dist/guards/prop.d.ts +232 -0
- package/dist/guards/prop.js +45 -0
- package/dist/guards/types.d.ts +256 -0
- package/dist/guards/types.js +2 -0
- package/dist/logical.d.ts +23 -15
- package/dist/logical.js +14 -35
- package/dist/map.d.ts +48 -0
- package/dist/map.js +74 -0
- package/dist/object.d.ts +93 -14
- package/dist/object.js +61 -22
- package/dist/set.d.ts +70 -0
- package/dist/set.js +116 -0
- package/dist/string.d.ts +61 -35
- package/dist/string.js +98 -12
- package/dist/types.d.ts +66 -81
- package/package.json +72 -119
- package/dist/class-name.d.ts +0 -2
- package/dist/class-name.js +0 -2
- package/dist/guards.d.ts +0 -195
- package/dist/guards.js +0 -136
- package/dist/random.d.ts +0 -19
- package/dist/random.js +0 -35
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* This module provides type guards for various JavaScript types.
|
|
4
|
+
* @categoryDescription Checks
|
|
5
|
+
* Type guards for JavaScript primitives and common types.
|
|
6
|
+
*/
|
|
7
|
+
import { describePredicate, describeTypePredicate } from './describe.js';
|
|
8
|
+
/* ─────────────────────────────── Nullables ──────────────────────────────── */
|
|
9
|
+
/**
|
|
10
|
+
* Checks if the provided value is `undefined`.
|
|
11
|
+
*
|
|
12
|
+
* @param value - The value to check for `undefined`.
|
|
13
|
+
* @returns `true` if the value is `undefined`, otherwise `false`.
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* isUndef(undefined); // true
|
|
17
|
+
* isUndef(null); // false
|
|
18
|
+
* isUndef(0); // false
|
|
19
|
+
* ```
|
|
20
|
+
* @category Checks
|
|
21
|
+
*/
|
|
22
|
+
export function isUndef(value) {
|
|
23
|
+
return value === undefined;
|
|
24
|
+
}
|
|
25
|
+
describeTypePredicate(isUndef, 'undefined');
|
|
26
|
+
/**
|
|
27
|
+
* Checks if a given value is defined (not `undefined`).
|
|
28
|
+
*
|
|
29
|
+
* @param value - The value to check.
|
|
30
|
+
* @returns `true` if the value is defined, otherwise `false`.
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* isDef(undefined); // false
|
|
34
|
+
* isDef(null); // true
|
|
35
|
+
* isDef(42); // true
|
|
36
|
+
* ```
|
|
37
|
+
* @category Checks
|
|
38
|
+
*/
|
|
39
|
+
export function isDef(value) {
|
|
40
|
+
return value !== undefined;
|
|
41
|
+
}
|
|
42
|
+
describeTypePredicate(isDef, 'not undefined');
|
|
43
|
+
/**
|
|
44
|
+
* Checks if the provided value is `null` or `undefined`.
|
|
45
|
+
*
|
|
46
|
+
* @param value - The value to be checked.
|
|
47
|
+
* @returns `true` if the value is `null` or `undefined`, otherwise `false`.
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* isNil(null); // true
|
|
51
|
+
* isNil(undefined); // true
|
|
52
|
+
* isNil(false); // false
|
|
53
|
+
* isNil(''); // false
|
|
54
|
+
* ```
|
|
55
|
+
* @category Checks
|
|
56
|
+
*/
|
|
57
|
+
export function isNil(value) {
|
|
58
|
+
return value == null;
|
|
59
|
+
}
|
|
60
|
+
describeTypePredicate(isNil, 'null or undefined');
|
|
61
|
+
/**
|
|
62
|
+
* {@label OVERLOAD_UNKNOWN}
|
|
63
|
+
*
|
|
64
|
+
* Checks if the provided value is not `null` or `undefined`.
|
|
65
|
+
*
|
|
66
|
+
* @param value - The value to be checked.
|
|
67
|
+
* @returns `true` if the value is not `null` or `undefined`; otherwise, `false`.
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* isNotNil(null); // false
|
|
71
|
+
* isNotNil(undefined); // false
|
|
72
|
+
* isNotNil(0); // true
|
|
73
|
+
* isNotNil(''); // true
|
|
74
|
+
* ```
|
|
75
|
+
* @category Checks
|
|
76
|
+
*/
|
|
77
|
+
export function isNotNil(value) {
|
|
78
|
+
return !isNil(value);
|
|
79
|
+
}
|
|
80
|
+
describeTypePredicate(isNotNil, 'non-nullable');
|
|
81
|
+
/* ──────────────────────────────── Boolean ───────────────────────────────── */
|
|
82
|
+
/**
|
|
83
|
+
* Checks if the given value is of type ``boolean``.
|
|
84
|
+
*
|
|
85
|
+
* @param value - The value to check.
|
|
86
|
+
* @returns `true` if the value is a `boolean`, otherwise `false`.
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* isBoolean(true); // true
|
|
90
|
+
* isBoolean(false); // true
|
|
91
|
+
* isBoolean(1); // false
|
|
92
|
+
* isBoolean('true'); // false
|
|
93
|
+
* ```
|
|
94
|
+
* @category Checks
|
|
95
|
+
*/
|
|
96
|
+
export function isBoolean(value) {
|
|
97
|
+
return typeof value === 'boolean';
|
|
98
|
+
}
|
|
99
|
+
describeTypePredicate(isBoolean, 'boolean');
|
|
100
|
+
/**
|
|
101
|
+
* Checks if the provided value is strictly equal to `true`.
|
|
102
|
+
*
|
|
103
|
+
* @param value - The value to check.
|
|
104
|
+
* @returns `true` if the value is strictly `true`, otherwise `false`.
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* isTrue(true); // true
|
|
108
|
+
* isTrue(1); // false
|
|
109
|
+
* isTrue('true'); // false
|
|
110
|
+
* isTrue(false); // false
|
|
111
|
+
* ```
|
|
112
|
+
* @category Checks
|
|
113
|
+
*/
|
|
114
|
+
export function isTrue(value) {
|
|
115
|
+
return value === true;
|
|
116
|
+
}
|
|
117
|
+
describeTypePredicate(isTrue, 'true');
|
|
118
|
+
/**
|
|
119
|
+
* Determines if the provided value is strictly `false`.
|
|
120
|
+
*
|
|
121
|
+
* @param value - The value to be checked.
|
|
122
|
+
* @returns `true` if the value is `false`, otherwise `false`.
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* isFalse(false); // true
|
|
126
|
+
* isFalse(0); // false
|
|
127
|
+
* isFalse(''); // false
|
|
128
|
+
* isFalse(true); // false
|
|
129
|
+
* ```
|
|
130
|
+
* @category Checks
|
|
131
|
+
*/
|
|
132
|
+
export function isFalse(value) {
|
|
133
|
+
return value === false;
|
|
134
|
+
}
|
|
135
|
+
describeTypePredicate(isFalse, 'false');
|
|
136
|
+
/**
|
|
137
|
+
* Determines if the given value is truthy, i.e., converts to `true` when used in a `boolean` context.
|
|
138
|
+
*
|
|
139
|
+
* @param value - The value to be tested for truthiness.
|
|
140
|
+
* @returns `true` if the value is truthy, `false` otherwise.
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* isTruly(true); // true
|
|
144
|
+
* isTruly(1); // true
|
|
145
|
+
* isTruly('hello'); // true
|
|
146
|
+
* isTruly(0); // false
|
|
147
|
+
* isTruly(''); // false
|
|
148
|
+
* isTruly(null); // false
|
|
149
|
+
* ```
|
|
150
|
+
* @category Checks
|
|
151
|
+
*/
|
|
152
|
+
export function isTruly(value) {
|
|
153
|
+
return Boolean(value);
|
|
154
|
+
}
|
|
155
|
+
describePredicate(isTruly, 'to evaluates to true when coerced to a boolean');
|
|
156
|
+
/**
|
|
157
|
+
* Determines if a given value is falsy.
|
|
158
|
+
* A value is considered falsy if it evaluates to `false` when coerced to a `boolean`.
|
|
159
|
+
*
|
|
160
|
+
* @param value - The value to be tested.
|
|
161
|
+
* @returns `true` if the value is falsy, otherwise `false`.
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* isFalsy(false); // true
|
|
165
|
+
* isFalsy(0); // true
|
|
166
|
+
* isFalsy(''); // true
|
|
167
|
+
* isFalsy(null); // true
|
|
168
|
+
* isFalsy(undefined); // true
|
|
169
|
+
* isFalsy('hello'); // false
|
|
170
|
+
* ```
|
|
171
|
+
* @category Checks
|
|
172
|
+
*/
|
|
173
|
+
export function isFalsy(value) {
|
|
174
|
+
return !isTruly(value);
|
|
175
|
+
}
|
|
176
|
+
describePredicate(isFalsy, 'to evaluates to false when coerced to a boolean');
|
|
177
|
+
/* ──────────────────────────────── Numeric ───────────────────────────────── */
|
|
178
|
+
/**
|
|
179
|
+
* Checks if the provided value is a `number` and not {@link NaN}.
|
|
180
|
+
*
|
|
181
|
+
* @param value - The value to be checked.
|
|
182
|
+
* @returns `true` if the value is a `number` and not {@link NaN}, otherwise `false`.
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* isNumber(42); // true
|
|
186
|
+
* isNumber(3.14); // true
|
|
187
|
+
* isNumber(NaN); // false
|
|
188
|
+
* isNumber('42'); // false
|
|
189
|
+
* isNumber(null); // false
|
|
190
|
+
* ```
|
|
191
|
+
* @category Checks
|
|
192
|
+
*/
|
|
193
|
+
export function isNumber(value) {
|
|
194
|
+
return typeof value === 'number' && !Number.isNaN(value);
|
|
195
|
+
}
|
|
196
|
+
describeTypePredicate(isNumber, 'number');
|
|
197
|
+
/**
|
|
198
|
+
* Checks if a given value is of type `bigint`.
|
|
199
|
+
*
|
|
200
|
+
* @param value - The value to be checked.
|
|
201
|
+
* @returns `true` if the value is a `bigint`, otherwise `false`.
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* isBigint(42n); // true
|
|
205
|
+
* isBigint(42); // false
|
|
206
|
+
* isBigint('42'); // false
|
|
207
|
+
* isBigint(BigInt(42)); // true
|
|
208
|
+
* ```
|
|
209
|
+
* @category Checks
|
|
210
|
+
*/
|
|
211
|
+
export function isBigint(value) {
|
|
212
|
+
return typeof value === 'bigint';
|
|
213
|
+
}
|
|
214
|
+
describeTypePredicate(isBigint, 'bigint');
|
|
215
|
+
/**
|
|
216
|
+
* Determines whether a given number is positive.
|
|
217
|
+
*
|
|
218
|
+
* @param num - The `number` or `bigint` to check.
|
|
219
|
+
* @returns `true` if the input is greater than zero, otherwise `false`.
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* isPositive(42); // true
|
|
223
|
+
* isPositive(0); // false
|
|
224
|
+
* isPositive(-5); // false
|
|
225
|
+
* isPositive(100n); // true
|
|
226
|
+
* ```
|
|
227
|
+
* @category Checks
|
|
228
|
+
*/
|
|
229
|
+
export function isPositive(num) {
|
|
230
|
+
return isBigint(num) ? num > 0n : num > 0;
|
|
231
|
+
}
|
|
232
|
+
describePredicate(isPositive, 'to be greater than zero');
|
|
233
|
+
/**
|
|
234
|
+
* Determines whether a given number is non-negative.
|
|
235
|
+
*
|
|
236
|
+
* @param num - The number to check. It can be a `bigint` or a `number`.
|
|
237
|
+
* @returns `true` if the provided number is greater than or equal to zero, otherwise `false`.
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* isNonNegative(42); // true
|
|
241
|
+
* isNonNegative(0); // true
|
|
242
|
+
* isNonNegative(-1); // false
|
|
243
|
+
* isNonNegative(0n); // true
|
|
244
|
+
* ```
|
|
245
|
+
* @category Checks
|
|
246
|
+
*/
|
|
247
|
+
export function isNonNegative(num) {
|
|
248
|
+
return isBigint(num) ? num >= 0n : num >= 0;
|
|
249
|
+
}
|
|
250
|
+
describePredicate(isNonNegative, 'to be greater than or equal to zero');
|
|
251
|
+
/**
|
|
252
|
+
* Checks if the provided value is an integer.
|
|
253
|
+
*
|
|
254
|
+
* @param value - The value to check, which can be a `bigint` or `number`.
|
|
255
|
+
* @returns `true` if the value is an integer, otherwise `false`.
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* isInteger(42); // true
|
|
259
|
+
* isInteger(3.14); // false
|
|
260
|
+
* isInteger(42n); // true
|
|
261
|
+
* ```
|
|
262
|
+
* @category Checks
|
|
263
|
+
*/
|
|
264
|
+
export function isInteger(value) {
|
|
265
|
+
return isBigint(value) || Number.isInteger(value);
|
|
266
|
+
}
|
|
267
|
+
describeTypePredicate(isInteger, 'integer');
|
|
268
|
+
/* ────────────────────────────────── Misc ────────────────────────────────── */
|
|
269
|
+
/**
|
|
270
|
+
* Determines if the provided value is a `string`.
|
|
271
|
+
*
|
|
272
|
+
* @param value - The value to check.
|
|
273
|
+
* @returns `true` if the value is a `string`, otherwise `false`.
|
|
274
|
+
* @example
|
|
275
|
+
* ```typescript
|
|
276
|
+
* isString('hello'); // true
|
|
277
|
+
* isString(42); // false
|
|
278
|
+
* ```
|
|
279
|
+
* @category Checks
|
|
280
|
+
*/
|
|
281
|
+
export function isString(value) {
|
|
282
|
+
return typeof value === 'string';
|
|
283
|
+
}
|
|
284
|
+
describeTypePredicate(isString, 'string');
|
|
285
|
+
/**
|
|
286
|
+
* Checks if the provided value is of type ``symbol``.
|
|
287
|
+
*
|
|
288
|
+
* @param value - The value to check.
|
|
289
|
+
* @returns `true` if the value is a `symbol`, otherwise `false`.
|
|
290
|
+
* @example
|
|
291
|
+
* ```typescript
|
|
292
|
+
* isSymbol(Symbol('foo')); // true
|
|
293
|
+
* isSymbol('foo'); // false
|
|
294
|
+
* ```
|
|
295
|
+
* @category Checks
|
|
296
|
+
*/
|
|
297
|
+
export const isSymbol = (value) => typeof value === 'symbol';
|
|
298
|
+
describeTypePredicate(isSymbol, 'symbol');
|
|
299
|
+
/**
|
|
300
|
+
* Determines whether the given value is a valid JavaScript property key.
|
|
301
|
+
*
|
|
302
|
+
* A property key in JavaScript can be a `string`, `symbol`, or `number`, as these are
|
|
303
|
+
* the types allowed for indexing object properties.
|
|
304
|
+
*
|
|
305
|
+
* @param value - The value to be checked as a potential property key.
|
|
306
|
+
* @returns `true` if the value is a valid property key, otherwise `false`.
|
|
307
|
+
* @example
|
|
308
|
+
* ```typescript
|
|
309
|
+
* isPropKey('foo'); // true
|
|
310
|
+
* isPropKey(123); // true
|
|
311
|
+
* isPropKey(Symbol('bar')); // true
|
|
312
|
+
* isPropKey({}); // false
|
|
313
|
+
* ```
|
|
314
|
+
* @category Checks
|
|
315
|
+
*/
|
|
316
|
+
export const isPropKey = (value) => isString(value) || isSymbol(value) || isNumber(value);
|
|
317
|
+
describeTypePredicate(isPropKey, 'valid property key (string, symbol, or number)');
|
|
318
|
+
/**
|
|
319
|
+
* Checks if a given value is a {@link Primitive} type (`null`, `undefined`, `boolean`, `string`, `number`, `symbol`, or `bigint`).
|
|
320
|
+
*
|
|
321
|
+
* @param value - The value to check.
|
|
322
|
+
* @returns `true` if the value is a {@link Primitive} type, otherwise `false`.
|
|
323
|
+
* @example
|
|
324
|
+
* ```typescript
|
|
325
|
+
* isPrimitive(42); // true
|
|
326
|
+
* isPrimitive('foo'); // true
|
|
327
|
+
* isPrimitive(null); // true
|
|
328
|
+
* isPrimitive({}); // false
|
|
329
|
+
* ```
|
|
330
|
+
* @category Checks
|
|
331
|
+
*/
|
|
332
|
+
export const isPrimitive = (value) => isNil(value) ||
|
|
333
|
+
isBoolean(value) ||
|
|
334
|
+
isString(value) ||
|
|
335
|
+
isNumber(value) ||
|
|
336
|
+
Number.isNaN(value) ||
|
|
337
|
+
isSymbol(value) ||
|
|
338
|
+
isBigint(value);
|
|
339
|
+
describeTypePredicate(isPrimitive, 'a primitive (null, undefined, boolean, string, number, symbol, or bigint)');
|
|
340
|
+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OBJECT-LIKE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
341
|
+
/**
|
|
342
|
+
* Checks if the given value is an {@link object}.
|
|
343
|
+
*
|
|
344
|
+
* @param value - The value to check.
|
|
345
|
+
* @returns `true` if the value is an {@link object}, `false` otherwise.
|
|
346
|
+
* @example
|
|
347
|
+
* ```typescript
|
|
348
|
+
* isObject({}); // true
|
|
349
|
+
* isObject([]); // true
|
|
350
|
+
* isObject(null); // false
|
|
351
|
+
* ```
|
|
352
|
+
* @category Checks
|
|
353
|
+
*/
|
|
354
|
+
export function isObject(value) {
|
|
355
|
+
return value !== null && typeof value === 'object';
|
|
356
|
+
}
|
|
357
|
+
describeTypePredicate(isObject, 'object');
|
|
358
|
+
/**
|
|
359
|
+
* Checks if the given value is an {@link Array}.
|
|
360
|
+
*
|
|
361
|
+
* @param value - The value to check.
|
|
362
|
+
* @returns `true` if the value is an {@link Array}, otherwise `false`.
|
|
363
|
+
* @typeParam T - Type of the value to be checked.
|
|
364
|
+
* @example
|
|
365
|
+
* ```typescript
|
|
366
|
+
* isArray([1, 2, 3]); // true
|
|
367
|
+
* isArray('foo'); // false
|
|
368
|
+
* ```
|
|
369
|
+
* @category Checks
|
|
370
|
+
*/
|
|
371
|
+
export function isArray(value) {
|
|
372
|
+
return Array.isArray(value);
|
|
373
|
+
}
|
|
374
|
+
describeTypePredicate(isArray, 'array');
|
|
375
|
+
/**
|
|
376
|
+
* {@label OVERLOAD_BOOLEAN}
|
|
377
|
+
*
|
|
378
|
+
* Determines if the provided value is a function.
|
|
379
|
+
*
|
|
380
|
+
* @param value - The value to check.
|
|
381
|
+
* @returns `true` if the value is a function, otherwise `false`.
|
|
382
|
+
* @category Checks
|
|
383
|
+
*/
|
|
384
|
+
export function isFunction(value) {
|
|
385
|
+
return typeof value === 'function';
|
|
386
|
+
}
|
|
387
|
+
describeTypePredicate(isFunction, 'function');
|
|
388
|
+
/**
|
|
389
|
+
* Checks if the given value is a {@link Date} instance.
|
|
390
|
+
*
|
|
391
|
+
* @param value - The value to check.
|
|
392
|
+
* @returns `true` if the value is a {@link Date} instance, otherwise `false`.
|
|
393
|
+
* @example
|
|
394
|
+
* ```typescript
|
|
395
|
+
* isDate(new Date()); // true
|
|
396
|
+
* isDate('2023-01-01'); // false
|
|
397
|
+
* ```
|
|
398
|
+
* @category Checks
|
|
399
|
+
*/
|
|
400
|
+
export function isDate(value) {
|
|
401
|
+
return value instanceof Date;
|
|
402
|
+
}
|
|
403
|
+
describeTypePredicate(isDate, 'Date');
|
|
404
|
+
/**
|
|
405
|
+
* Checks if the given value is a {@link RegExp} instance.
|
|
406
|
+
*
|
|
407
|
+
* @param value - The value to check.
|
|
408
|
+
* @returns `true` if the value is a {@link RegExp} instance, otherwise `false`.
|
|
409
|
+
* @example
|
|
410
|
+
* ```typescript
|
|
411
|
+
* isRegExp(/foo/); // true
|
|
412
|
+
* isRegExp('foo'); // false
|
|
413
|
+
* ```
|
|
414
|
+
* @category Checks
|
|
415
|
+
*/
|
|
416
|
+
export function isRegExp(value) {
|
|
417
|
+
return value instanceof RegExp;
|
|
418
|
+
}
|
|
419
|
+
describeTypePredicate(isRegExp, 'RegExp');
|
|
420
|
+
/**
|
|
421
|
+
* Checks if the given value is an {@link Error} instance.
|
|
422
|
+
*
|
|
423
|
+
* @param value - The value to check.
|
|
424
|
+
* @returns `true` if the value is an {@link Error} instance, otherwise `false`.
|
|
425
|
+
* @example
|
|
426
|
+
* ```typescript
|
|
427
|
+
* isError(new Error('foo')); // true
|
|
428
|
+
* isError({ message: 'foo' }); // false
|
|
429
|
+
* ```
|
|
430
|
+
* @category Checks
|
|
431
|
+
*/
|
|
432
|
+
export function isError(value) {
|
|
433
|
+
return value instanceof Error;
|
|
434
|
+
}
|
|
435
|
+
describeTypePredicate(isError, 'Error');
|
|
436
|
+
/**
|
|
437
|
+
* Checks if the given value is a {@link Map} instance.
|
|
438
|
+
*
|
|
439
|
+
* @param value - The value to check.
|
|
440
|
+
* @returns `true` if the value is a {@link Map} instance, otherwise `false`.
|
|
441
|
+
* @example
|
|
442
|
+
* ```typescript
|
|
443
|
+
* isMap(new Map()); // true
|
|
444
|
+
* isMap({}); // false
|
|
445
|
+
* ```
|
|
446
|
+
* @category Checks
|
|
447
|
+
*/
|
|
448
|
+
export function isMap(value) {
|
|
449
|
+
return value instanceof Map;
|
|
450
|
+
}
|
|
451
|
+
describeTypePredicate(isMap, 'Map');
|
|
452
|
+
/**
|
|
453
|
+
* Checks if the given value is a {@link Set} instance.
|
|
454
|
+
*
|
|
455
|
+
* @param value - The value to check.
|
|
456
|
+
* @returns `true` if the value is a {@link Set} instance, otherwise `false`.
|
|
457
|
+
* @example
|
|
458
|
+
* ```typescript
|
|
459
|
+
* isSet(new Set()); // true
|
|
460
|
+
* isSet([]); // false
|
|
461
|
+
* ```
|
|
462
|
+
* @category Checks
|
|
463
|
+
*/
|
|
464
|
+
export function isSet(value) {
|
|
465
|
+
return value instanceof Set;
|
|
466
|
+
}
|
|
467
|
+
describeTypePredicate(isSet, 'Set');
|
|
468
|
+
/**
|
|
469
|
+
* Checks if the given value is a {@link WeakMap} or {@link Map} instance.
|
|
470
|
+
*
|
|
471
|
+
* @param value - The value to check.
|
|
472
|
+
* @returns `true` if the value is a {@link WeakMap} or {@link Map} instance, otherwise `false`.
|
|
473
|
+
* @example
|
|
474
|
+
* ```typescript
|
|
475
|
+
* isWeakMapLike(new WeakMap()); // true
|
|
476
|
+
* isWeakMapLike(new Map()); // true
|
|
477
|
+
* isWeakMapLike({}); // false
|
|
478
|
+
* ```
|
|
479
|
+
* @category Checks
|
|
480
|
+
*/
|
|
481
|
+
export function isWeakMapLike(value) {
|
|
482
|
+
return value instanceof WeakMap || isMap(value);
|
|
483
|
+
}
|
|
484
|
+
describeTypePredicate(isWeakMapLike, 'WeakMap or Map');
|
|
485
|
+
/**
|
|
486
|
+
* Checks if the given value is a {@link WeakSet} or {@link Set} instance.
|
|
487
|
+
*
|
|
488
|
+
* @param value - The value to check.
|
|
489
|
+
* @returns `true` if the value is a {@link WeakSet} or {@link Set} instance, otherwise `false`.
|
|
490
|
+
* @example
|
|
491
|
+
* ```typescript
|
|
492
|
+
* isWeakSetLike(new WeakSet()); // true
|
|
493
|
+
* isWeakSetLike(new Set()); // true
|
|
494
|
+
* isWeakSetLike([]); // false
|
|
495
|
+
* ```
|
|
496
|
+
* @category Checks
|
|
497
|
+
*/
|
|
498
|
+
export function isWeakSetLike(value) {
|
|
499
|
+
return value instanceof WeakSet || value instanceof Set;
|
|
500
|
+
}
|
|
501
|
+
describeTypePredicate(isWeakSetLike, 'WeakSet or Set');
|
|
502
|
+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ABSTRACT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
503
|
+
/**
|
|
504
|
+
* Determines whether two values are the same value using the
|
|
505
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness#same-value-zero_equality "SameValueZero" comparison algorithm}.
|
|
506
|
+
*
|
|
507
|
+
* @param x - The first value to be compared.
|
|
508
|
+
* @param y - The second value to be compared.
|
|
509
|
+
* @returns `true` if `x` and `y` are the same value according to the "SameValueZero" comparison, otherwise `false`.
|
|
510
|
+
* @remarks
|
|
511
|
+
* - It differs from strict equality (`===`) in that {@link NaN} is considered equal to itself.
|
|
512
|
+
* - It differs from {@link Object.is} in that it considers `-0` and `+0` equal.
|
|
513
|
+
* - It's the same algorithm used by {@link Set} and {@link Map} to compare elements.
|
|
514
|
+
* @typeParam TValue - The type of the first value being compared.
|
|
515
|
+
* @example
|
|
516
|
+
* ```typescript
|
|
517
|
+
* sameValueZero(0, -0); // true
|
|
518
|
+
* sameValueZero(NaN, NaN); // true
|
|
519
|
+
* sameValueZero(1, '1'); // false
|
|
520
|
+
* ```
|
|
521
|
+
* @category Checks
|
|
522
|
+
*/
|
|
523
|
+
export const sameValueZero = (x, y) => Object.is(x, y) || (x === 0 && y === 0);
|
|
524
|
+
//# sourceMappingURL=check.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Predicate } from '@budsbox/lib-types';
|
|
2
|
+
import type { PredicateDescriptor } from './types.js';
|
|
3
|
+
export declare const describeTypePredicate: <TPredicate extends Predicate>(predicate: TPredicate, typeDescription: string) => TPredicate;
|
|
4
|
+
export declare const describePredicate: <TPredicate extends Predicate>(predicate: TPredicate, conditionOrDescriptor: string | Readonly<PredicateDescriptor>) => TPredicate;
|
|
5
|
+
export declare const describeComplexPredicate: <TPredicate extends Predicate>(predicate: TPredicate, and: boolean, ...subPredicates: readonly Predicate[]) => TPredicate;
|
|
6
|
+
export declare const getPredicateDescriptor: (predicate: Predicate) => PredicateDescriptor | undefined;
|
|
7
|
+
//# sourceMappingURL=describe.d.ts.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const descriptionSymbol = Symbol.for('@budsbox/lib-es/guards#predicateDescription');
|
|
2
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
3
|
+
export const describeTypePredicate = (predicate, typeDescription) => {
|
|
4
|
+
describePredicate(predicate, { type: typeDescription });
|
|
5
|
+
return predicate;
|
|
6
|
+
};
|
|
7
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
8
|
+
export const describePredicate = (predicate, conditionOrDescriptor) => {
|
|
9
|
+
Object.defineProperty(predicate, descriptionSymbol, {
|
|
10
|
+
configurable: true,
|
|
11
|
+
value: typeof conditionOrDescriptor === 'string' ?
|
|
12
|
+
{
|
|
13
|
+
condition: conditionOrDescriptor,
|
|
14
|
+
}
|
|
15
|
+
: conditionOrDescriptor,
|
|
16
|
+
});
|
|
17
|
+
return predicate;
|
|
18
|
+
};
|
|
19
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
20
|
+
export const describeComplexPredicate = (predicate, and, ...subPredicates) => {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
22
|
+
const value = {
|
|
23
|
+
[and ? 'and' : 'or']: subPredicates,
|
|
24
|
+
};
|
|
25
|
+
describePredicate(predicate, value);
|
|
26
|
+
return predicate;
|
|
27
|
+
};
|
|
28
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
29
|
+
export const getPredicateDescriptor = (predicate) => {
|
|
30
|
+
if (Object.hasOwn(predicate, descriptionSymbol))
|
|
31
|
+
return predicate[descriptionSymbol];
|
|
32
|
+
return;
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=describe.js.map
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module provides utilities for formatting error messages and values related to predicate checks.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
* @categoryDescription Formatting
|
|
6
|
+
* Utilities for formatting error messages and values related to predicate checks.
|
|
7
|
+
*/
|
|
8
|
+
import type { Predicate } from '@budsbox/lib-types';
|
|
9
|
+
import type { FormatDebugValueFn, FormatErrorFn, FormatPredicateExpectedMessageFn } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* {@link FormatPredicateExpectedMessageFn} implementation.
|
|
12
|
+
*
|
|
13
|
+
* @inheritDoc {@link FormatPredicateExpectedMessageFn}
|
|
14
|
+
* @ignore
|
|
15
|
+
*/
|
|
16
|
+
export declare const formatPredicateExpectedMessage: FormatPredicateExpectedMessageFn;
|
|
17
|
+
/**
|
|
18
|
+
* Generates a description of the conditions associated with a given predicate.
|
|
19
|
+
* The description includes whether the predicate is a type guard, a value check, or both.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
* @param predicate - The predicate whose conditions are to be analyzed and described.
|
|
23
|
+
* @param nested - A flag indicating if the predicate is part of a nested structure. Defaults to `false`.
|
|
24
|
+
* @returns An object comprising:
|
|
25
|
+
* - `condition`: A string describing the predicate's conditions.
|
|
26
|
+
* - `isType`: A boolean indicating whether the predicate is a type guard.
|
|
27
|
+
* - `isValue`: A boolean indicating whether the predicate checks a value.
|
|
28
|
+
* @typeParam Predicate - The expected type of the predicate parameter.
|
|
29
|
+
* @category Formatting
|
|
30
|
+
*/
|
|
31
|
+
export declare const getPredicateConditions: (predicate: Predicate, nested?: boolean) => {
|
|
32
|
+
condition: string;
|
|
33
|
+
isType: boolean;
|
|
34
|
+
isValue: boolean;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Provides a human-readable description of a value's type.
|
|
38
|
+
*
|
|
39
|
+
* - If the value is `null`, `undefined`, or a `Symbol`, it will return the string representation.
|
|
40
|
+
* - If the value is a primitive type (e.g., `number`, `string`, `boolean`), it will return the result of `typeof value`.
|
|
41
|
+
* - If the value is a function, it will return `'function'`.
|
|
42
|
+
* - If the value is an array, it will return `'array'`.
|
|
43
|
+
* - For objects, it will return the `Symbol.toStringTag` or the internal `[[Class]]` property of the object.
|
|
44
|
+
*
|
|
45
|
+
* @param value - The value whose type needs to be determined.
|
|
46
|
+
* @returns A string representation of the value's type.
|
|
47
|
+
* @category Formatting
|
|
48
|
+
*/
|
|
49
|
+
export declare const formatDebugType: (value: unknown) => string;
|
|
50
|
+
/**
|
|
51
|
+
* {@link FormatDebugValueFn} implementation.
|
|
52
|
+
*
|
|
53
|
+
* @inheritDoc {@link FormatDebugValueFn}
|
|
54
|
+
* @category Formatting
|
|
55
|
+
*/
|
|
56
|
+
export declare const formatDebugValue: FormatDebugValueFn;
|
|
57
|
+
/**
|
|
58
|
+
* {@link FormatErrorFn} implementation.
|
|
59
|
+
*
|
|
60
|
+
* @inheritDoc {@link FormatErrorFn}
|
|
61
|
+
* @category Formatting
|
|
62
|
+
*/
|
|
63
|
+
export declare const formatError: FormatErrorFn;
|
|
64
|
+
/**
|
|
65
|
+
* Constructs a dot-notation or bracket-notation string representation
|
|
66
|
+
* for accessing nested properties of an object based on the provided keys.
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
* @param sourceName - The base name of the object or source from which properties are being accessed.
|
|
70
|
+
* @param keys - A variadic list of property keys representing the nested path.
|
|
71
|
+
* Each key will be used to generate the accessor string.
|
|
72
|
+
* @returns A string representing the accessor path in dot-notation for valid identifiers
|
|
73
|
+
* or bracket-notation for non-identifier keys.
|
|
74
|
+
* @remarks The function is intended to be used for generating error messages.
|
|
75
|
+
*/
|
|
76
|
+
export declare const formatAccessString: (sourceName: string, ...keys: readonly PropertyKey[]) => string;
|
|
77
|
+
/**
|
|
78
|
+
* Extracts and returns the `Symbol.toStringTag` or internal `[[Class]]` property of a given object as a string.
|
|
79
|
+
*
|
|
80
|
+
* The method utilizes `Object.prototype.toString` to determine the object type
|
|
81
|
+
* and removes the surrounding `[object ...]` syntax to provide the type name.
|
|
82
|
+
*
|
|
83
|
+
* @param value - The input value from which the object tag will be extracted.
|
|
84
|
+
* @returns The name of the internal `[[Class]]` corresponding to the input value.
|
|
85
|
+
* @category Formatting
|
|
86
|
+
*/
|
|
87
|
+
export declare const shortObjectTag: (value: unknown) => string;
|
|
88
|
+
/**
|
|
89
|
+
* Returns a descriptive tag string for a given value based on its constructor
|
|
90
|
+
* and `Symbol.toStringTag`.
|
|
91
|
+
*
|
|
92
|
+
* For objects, combine the constructor name with the `Symbol.toStringTag` if they differ
|
|
93
|
+
* (e.g., `"Map"` or `"CustomClass[Tag]"`). For non-objects, falls back to {@link shortObjectTag}.
|
|
94
|
+
*
|
|
95
|
+
* @param value - The value to extract the tag from.
|
|
96
|
+
* @returns A string describing the object's type and tag.
|
|
97
|
+
* @category Formatting
|
|
98
|
+
*/
|
|
99
|
+
export declare const objectTag: (value: unknown) => string;
|
|
100
|
+
/**
|
|
101
|
+
* Joins an array of strings into a single formatted string using the specified conjunction.
|
|
102
|
+
*
|
|
103
|
+
* @param items - An array of strings to be joined. If the array is empty, an empty string is returned.
|
|
104
|
+
* @param conjunction - A word or phrase to be used as the conjunction between the last two items.
|
|
105
|
+
* @returns A formatted string where the items are separated by commas and the conjunction is added before the final item.
|
|
106
|
+
* @category Formatting
|
|
107
|
+
*/
|
|
108
|
+
export declare const joinWithConjunction: (items: readonly string[], conjunction: string) => string;
|
|
109
|
+
//# sourceMappingURL=format.d.ts.map
|