@budsbox/lib-es 2.2.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 +101 -0
- package/dist/function.js +38 -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 +76 -103
- 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,295 @@
|
|
|
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 { entries } from '#object';
|
|
9
|
+
import { isArray, isDate, isError, isFunction, isMap, isNil, isNotNil, isObject, isPrimitive, isSet, isString, isSymbol, isWeakMapLike, isWeakSetLike, } from './check.js';
|
|
10
|
+
import { getPredicateDescriptor } from './describe.js';
|
|
11
|
+
/**
|
|
12
|
+
* {@link FormatPredicateExpectedMessageFn} implementation.
|
|
13
|
+
*
|
|
14
|
+
* @inheritDoc {@link FormatPredicateExpectedMessageFn}
|
|
15
|
+
* @ignore
|
|
16
|
+
*/
|
|
17
|
+
export const formatPredicateExpectedMessage = (predicate, value, valueName = 'value') => {
|
|
18
|
+
const { condition, isType, isValue } = getPredicateConditions(predicate);
|
|
19
|
+
const formattedType = isType ? formatDebugType(value) : '';
|
|
20
|
+
const formattedValue = isValue ? formatDebugValue(value) : '';
|
|
21
|
+
return `Expected ${valueName} ${condition}, got${isType ? ` ${formatDebugType(value)}` : ''}${isValue && formattedValue !== formattedType ?
|
|
22
|
+
isType ? [' (', formatDebugValue(value), ')'].join('')
|
|
23
|
+
: ` ${formatDebugValue(value)}`
|
|
24
|
+
: ''} instead`;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Generates a description of the conditions associated with a given predicate.
|
|
28
|
+
* The description includes whether the predicate is a type guard, a value check, or both.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
* @param predicate - The predicate whose conditions are to be analyzed and described.
|
|
32
|
+
* @param nested - A flag indicating if the predicate is part of a nested structure. Defaults to `false`.
|
|
33
|
+
* @returns An object comprising:
|
|
34
|
+
* - `condition`: A string describing the predicate's conditions.
|
|
35
|
+
* - `isType`: A boolean indicating whether the predicate is a type guard.
|
|
36
|
+
* - `isValue`: A boolean indicating whether the predicate checks a value.
|
|
37
|
+
* @typeParam Predicate - The expected type of the predicate parameter.
|
|
38
|
+
* @category Formatting
|
|
39
|
+
*/
|
|
40
|
+
export const getPredicateConditions = (predicate, nested = false) => {
|
|
41
|
+
const descriptor = getPredicateDescriptor(predicate);
|
|
42
|
+
if (isNil(descriptor))
|
|
43
|
+
return {
|
|
44
|
+
condition: defaultDescription(predicate),
|
|
45
|
+
isType: false,
|
|
46
|
+
isValue: true,
|
|
47
|
+
};
|
|
48
|
+
const descriptorEntry = entries(descriptor)[0];
|
|
49
|
+
if (descriptorEntry[0] === 'condition') {
|
|
50
|
+
return { condition: descriptorEntry[1], isType: false, isValue: true };
|
|
51
|
+
}
|
|
52
|
+
else if (descriptorEntry[0] === 'type') {
|
|
53
|
+
return {
|
|
54
|
+
condition: nested ? descriptorEntry[1] : `to be ${descriptorEntry[1]}`,
|
|
55
|
+
isType: true,
|
|
56
|
+
isValue: false,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const [conjunction, predicates] = descriptorEntry;
|
|
61
|
+
let isType = false, isValue = false;
|
|
62
|
+
const typeGuards = [];
|
|
63
|
+
const plains = [];
|
|
64
|
+
for (const p of predicates) {
|
|
65
|
+
const pc = getPredicateConditions(p, true);
|
|
66
|
+
isType || (isType = pc.isType);
|
|
67
|
+
isValue || (isValue = pc.isValue);
|
|
68
|
+
if (pc.isValue) {
|
|
69
|
+
plains.push(pc.isType ? `(${pc.condition})` : pc.condition);
|
|
70
|
+
}
|
|
71
|
+
else if (pc.isType) {
|
|
72
|
+
typeGuards.push(pc.condition);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const typeGuardString = joinWithConjunction(typeGuards, conjunction);
|
|
76
|
+
const plainString = joinWithConjunction(plains, conjunction);
|
|
77
|
+
const isPrintableTypes = typeGuards.length > 0;
|
|
78
|
+
const condition = `${isPrintableTypes ? `to be ${typeGuardString}` : ''}${isValue ?
|
|
79
|
+
`${isPrintableTypes ? ` ${conjunction} ` : ''}${plainString}`
|
|
80
|
+
: ''}`;
|
|
81
|
+
return { condition, isType, isValue };
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Provides a human-readable description of a value's type.
|
|
86
|
+
*
|
|
87
|
+
* - If the value is `null`, `undefined`, or a `Symbol`, it will return the string representation.
|
|
88
|
+
* - If the value is a primitive type (e.g., `number`, `string`, `boolean`), it will return the result of `typeof value`.
|
|
89
|
+
* - If the value is a function, it will return `'function'`.
|
|
90
|
+
* - If the value is an array, it will return `'array'`.
|
|
91
|
+
* - For objects, it will return the `Symbol.toStringTag` or the internal `[[Class]]` property of the object.
|
|
92
|
+
*
|
|
93
|
+
* @param value - The value whose type needs to be determined.
|
|
94
|
+
* @returns A string representation of the value's type.
|
|
95
|
+
* @category Formatting
|
|
96
|
+
*/
|
|
97
|
+
export const formatDebugType = (value) => {
|
|
98
|
+
if (isNil(value) || isSymbol(value))
|
|
99
|
+
return String(value);
|
|
100
|
+
if (Number.isNaN(value))
|
|
101
|
+
return 'NaN';
|
|
102
|
+
if (isPrimitive(value))
|
|
103
|
+
return typeof value;
|
|
104
|
+
if (isFunction(value))
|
|
105
|
+
return 'function';
|
|
106
|
+
if (isArray(value))
|
|
107
|
+
return 'array';
|
|
108
|
+
const proto = Object.getPrototypeOf(value);
|
|
109
|
+
if (proto === Object.prototype || proto === null)
|
|
110
|
+
return 'object';
|
|
111
|
+
return `${objectTag(value)} object`;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* {@link FormatDebugValueFn} implementation.
|
|
115
|
+
*
|
|
116
|
+
* @inheritDoc {@link FormatDebugValueFn}
|
|
117
|
+
* @category Formatting
|
|
118
|
+
*/
|
|
119
|
+
export const formatDebugValue = (value, options = {}) => {
|
|
120
|
+
const { maxLength, maxDepth, maxItems, seen = new WeakSet(), } = { ...defaultOptions, ...options };
|
|
121
|
+
if (isString(value)) {
|
|
122
|
+
return JSON.stringify(clamp(value, maxLength));
|
|
123
|
+
}
|
|
124
|
+
if (isPrimitive(value))
|
|
125
|
+
return String(value);
|
|
126
|
+
if (seen.has(value))
|
|
127
|
+
return isArray(value) ? '[Circular]' : '{Circular}';
|
|
128
|
+
seen.add(value);
|
|
129
|
+
if (isFunction(value))
|
|
130
|
+
return value.name.length > 0 ?
|
|
131
|
+
`function ${value.name}`
|
|
132
|
+
: 'anonymous function';
|
|
133
|
+
const tag = objectTag(value);
|
|
134
|
+
const withTag = (str, omitTagIf = '') => tag === omitTagIf ? str : `${tag}(${str})`;
|
|
135
|
+
if (isArray(value)) {
|
|
136
|
+
if (maxDepth < 0)
|
|
137
|
+
return withTag(String(value.length));
|
|
138
|
+
const debugSlice = value.slice(0, maxItems).map((item) => formatDebugValue(item, {
|
|
139
|
+
maxLength,
|
|
140
|
+
maxDepth: maxDepth - 1,
|
|
141
|
+
maxItems,
|
|
142
|
+
seen,
|
|
143
|
+
}));
|
|
144
|
+
return withTag(`[${debugSlice.join(',')}${value.length > maxItems ? ', ...' : ''}]`, 'Array');
|
|
145
|
+
}
|
|
146
|
+
if (isDate(value))
|
|
147
|
+
return withTag(Number.isNaN(value.getTime()) ? 'Invalid' : value.toISOString());
|
|
148
|
+
if (isError(value))
|
|
149
|
+
return formatError(value, { maxDepth: maxDepth - 1 });
|
|
150
|
+
if (isMap(value) || isSet(value))
|
|
151
|
+
return withTag(String(value.size));
|
|
152
|
+
if (isWeakMapLike(value) || isWeakSetLike(value))
|
|
153
|
+
return withTag('?');
|
|
154
|
+
if ('toString' in value &&
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
156
|
+
isFunction(value.toString) &&
|
|
157
|
+
value.toString !== Object.prototype.toString) {
|
|
158
|
+
try {
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
160
|
+
const str = value.toString();
|
|
161
|
+
if (isString(str) && !/\[object [a-z0-9$_]+]/i.test(str))
|
|
162
|
+
return withTag(clamp(str, maxLength));
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
/* ignored */
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
const json = JSON.stringify(value);
|
|
170
|
+
if (isString(json))
|
|
171
|
+
return withTag(`${clamp(json, maxLength)}${json.length > maxLength ? '}' : ''}`, 'Object');
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
/* ignored */
|
|
175
|
+
}
|
|
176
|
+
return tag === 'Object' ? '{...}' : `${withTag('')} object`;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* {@link FormatErrorFn} implementation.
|
|
180
|
+
*
|
|
181
|
+
* @inheritDoc {@link FormatErrorFn}
|
|
182
|
+
* @category Formatting
|
|
183
|
+
*/
|
|
184
|
+
export const formatError = (error, options = {}) => {
|
|
185
|
+
const { maxDepth, maxLength, seen = new WeakSet(), } = { ...defaultOptions, ...options };
|
|
186
|
+
const { name, message, stack } = error;
|
|
187
|
+
if (maxDepth < 0)
|
|
188
|
+
return `${name}(${formatDebugValue(message)}...)`;
|
|
189
|
+
if (seen.has(error))
|
|
190
|
+
return `{Circular ${name}}`;
|
|
191
|
+
const args = [];
|
|
192
|
+
if (isNotNil(message) && message !== '') {
|
|
193
|
+
// message formatting is at the same depth by design
|
|
194
|
+
args.push(formatDebugValue(message, { ...options, seen }));
|
|
195
|
+
}
|
|
196
|
+
if ('code' in error && isNotNil(error.code))
|
|
197
|
+
args.push([
|
|
198
|
+
'code',
|
|
199
|
+
formatDebugValue(error.code, {
|
|
200
|
+
...options,
|
|
201
|
+
seen,
|
|
202
|
+
maxDepth: maxDepth - 1,
|
|
203
|
+
}),
|
|
204
|
+
]);
|
|
205
|
+
if ('cause' in error && isNotNil(error.cause))
|
|
206
|
+
args.push([
|
|
207
|
+
'cause',
|
|
208
|
+
formatDebugValue(error.cause, {
|
|
209
|
+
...options,
|
|
210
|
+
seen,
|
|
211
|
+
maxDepth: maxDepth - 1,
|
|
212
|
+
}),
|
|
213
|
+
]);
|
|
214
|
+
if (isString(stack) && stack.length > 0)
|
|
215
|
+
args.push([
|
|
216
|
+
'stack',
|
|
217
|
+
clamp(stack.replace(/^.*?\n\s*at\s/, 'at '), maxLength * 2),
|
|
218
|
+
]);
|
|
219
|
+
return `${name}(${args
|
|
220
|
+
.map((arg) => (isString(arg) ? arg : arg.join('=')))
|
|
221
|
+
.join(',')})`;
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Constructs a dot-notation or bracket-notation string representation
|
|
225
|
+
* for accessing nested properties of an object based on the provided keys.
|
|
226
|
+
*
|
|
227
|
+
* @internal
|
|
228
|
+
* @param sourceName - The base name of the object or source from which properties are being accessed.
|
|
229
|
+
* @param keys - A variadic list of property keys representing the nested path.
|
|
230
|
+
* Each key will be used to generate the accessor string.
|
|
231
|
+
* @returns A string representing the accessor path in dot-notation for valid identifiers
|
|
232
|
+
* or bracket-notation for non-identifier keys.
|
|
233
|
+
* @remarks The function is intended to be used for generating error messages.
|
|
234
|
+
*/
|
|
235
|
+
export const formatAccessString = (sourceName, ...keys) => keys.reduce((acc, key) => `${acc}${isString(key) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? `.${key}` : `[${formatDebugValue(key)}]`}`, sourceName);
|
|
236
|
+
/**
|
|
237
|
+
* Extracts and returns the `Symbol.toStringTag` or internal `[[Class]]` property of a given object as a string.
|
|
238
|
+
*
|
|
239
|
+
* The method utilizes `Object.prototype.toString` to determine the object type
|
|
240
|
+
* and removes the surrounding `[object ...]` syntax to provide the type name.
|
|
241
|
+
*
|
|
242
|
+
* @param value - The input value from which the object tag will be extracted.
|
|
243
|
+
* @returns The name of the internal `[[Class]]` corresponding to the input value.
|
|
244
|
+
* @category Formatting
|
|
245
|
+
*/
|
|
246
|
+
export const shortObjectTag = (value) => Object.prototype.toString.call(value).slice(8, -1);
|
|
247
|
+
/**
|
|
248
|
+
* Returns a descriptive tag string for a given value based on its constructor
|
|
249
|
+
* and `Symbol.toStringTag`.
|
|
250
|
+
*
|
|
251
|
+
* For objects, combine the constructor name with the `Symbol.toStringTag` if they differ
|
|
252
|
+
* (e.g., `"Map"` or `"CustomClass[Tag]"`). For non-objects, falls back to {@link shortObjectTag}.
|
|
253
|
+
*
|
|
254
|
+
* @param value - The value to extract the tag from.
|
|
255
|
+
* @returns A string describing the object's type and tag.
|
|
256
|
+
* @category Formatting
|
|
257
|
+
*/
|
|
258
|
+
export const objectTag = (value) => {
|
|
259
|
+
if (isObject(value)) {
|
|
260
|
+
const ctor = [
|
|
261
|
+
value.constructor,
|
|
262
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
263
|
+
Object.getPrototypeOf(value)?.constructor,
|
|
264
|
+
].find(isFunction);
|
|
265
|
+
const ctorName = isNotNil(ctor) && ctor.name ? ctor.name : 'Object';
|
|
266
|
+
const tag = shortObjectTag(value);
|
|
267
|
+
return `${ctorName}${tag !== ctorName ? `[${tag}]` : ''}`;
|
|
268
|
+
}
|
|
269
|
+
return shortObjectTag(value);
|
|
270
|
+
};
|
|
271
|
+
const defaultDescription = (predicate) => `to satisfy ${predicate.name || '(anonymous)'} predicate`;
|
|
272
|
+
const clamp = (str, maxLength) => str.length > maxLength ? `${str.slice(0, maxLength).trimEnd()}...` : str;
|
|
273
|
+
/**
|
|
274
|
+
* Joins an array of strings into a single formatted string using the specified conjunction.
|
|
275
|
+
*
|
|
276
|
+
* @param items - An array of strings to be joined. If the array is empty, an empty string is returned.
|
|
277
|
+
* @param conjunction - A word or phrase to be used as the conjunction between the last two items.
|
|
278
|
+
* @returns A formatted string where the items are separated by commas and the conjunction is added before the final item.
|
|
279
|
+
* @category Formatting
|
|
280
|
+
*/
|
|
281
|
+
export const joinWithConjunction = (items, conjunction) => {
|
|
282
|
+
if (items.length === 0)
|
|
283
|
+
return '';
|
|
284
|
+
if (items.length === 1)
|
|
285
|
+
return items[0];
|
|
286
|
+
if (items.length === 2)
|
|
287
|
+
return `${items[0]} ${conjunction} ${items[1]}`;
|
|
288
|
+
return `${items.slice(0, -1).join(', ')}, ${conjunction} ${items[items.length - 1]}`;
|
|
289
|
+
};
|
|
290
|
+
const defaultOptions = {
|
|
291
|
+
maxDepth: 2,
|
|
292
|
+
maxItems: 5,
|
|
293
|
+
maxLength: 30,
|
|
294
|
+
};
|
|
295
|
+
//# sourceMappingURL=format.js.map
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Due to their reliance on features from several other submodules,
|
|
3
|
+
* these functions are not located in more fitting areas, such as './assert.ts' or './check.ts'.
|
|
4
|
+
*
|
|
5
|
+
* @module high-level-functions
|
|
6
|
+
*/
|
|
7
|
+
import type { Constructor, IterableElement } from 'type-fest';
|
|
8
|
+
import type { ArrayItemsIntersection, NarrowedType, Predicate, TypePredicate } from '@budsbox/lib-types';
|
|
9
|
+
import type { PredicatesListArg, PredicatesListNarrowed } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Determines whether the provided key is a valid property key of the given source object.
|
|
12
|
+
*
|
|
13
|
+
* @param key - The property key to check for existence within the source object.
|
|
14
|
+
* @param source - The object in which to check for the property key.
|
|
15
|
+
* @param checkProto - Determines whether the prototype chain should also be checked. Defaults to `false`.
|
|
16
|
+
* @returns A boolean indicating whether the key is a valid property key of the source object.
|
|
17
|
+
* @typeParam TSource - The type of the source object to inspect.
|
|
18
|
+
*/
|
|
19
|
+
export declare const isKeyOf: <TSource>(key: PropertyKey, source: TSource, checkProto?: boolean) => key is keyof TSource;
|
|
20
|
+
/**
|
|
21
|
+
* Determines whether the provided value is iterable.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The value to be checked.
|
|
24
|
+
* @returns `true` if the value is iterable, otherwise `false`.
|
|
25
|
+
* @category Checks
|
|
26
|
+
*/
|
|
27
|
+
export declare function isIterable(value: unknown): value is Iterable<unknown>;
|
|
28
|
+
/**
|
|
29
|
+
* Asserts that the provided value is iterable.
|
|
30
|
+
*
|
|
31
|
+
* @param value - The value to be checked for iterable compatibility.
|
|
32
|
+
* @param valueName - The name of the value for the error message. Defaults to 'value'.
|
|
33
|
+
* @throws {@link !TypeError} If the provided value is not iterable.
|
|
34
|
+
* @category Assertions
|
|
35
|
+
*/
|
|
36
|
+
export declare function assertIterable(value: unknown, valueName?: string): asserts value is Iterable<unknown>;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a type predicate that checks whether a value is an instance of the provided constructor.
|
|
39
|
+
* The returned predicate acts as a type guard for narrowing the value to the constructor's type.
|
|
40
|
+
*
|
|
41
|
+
* @param ctor - A constructor function to check against.
|
|
42
|
+
* @returns A type predicate function that narrows the value type to an instance of the constructor.
|
|
43
|
+
* @throws {@link !TypeError} If `ctor` is not a function.
|
|
44
|
+
* @typeParam T - The type of instances produced by the constructor.
|
|
45
|
+
* @category Checks
|
|
46
|
+
*/
|
|
47
|
+
export declare const ofType: <T>(ctor: Constructor<T>) => TypePredicate<unknown, T>;
|
|
48
|
+
/**
|
|
49
|
+
* Asserts that a value is an instance of the provided constructor.
|
|
50
|
+
* If the assertion succeeds, the value is narrowed to the constructor's type.
|
|
51
|
+
*
|
|
52
|
+
* @param ctor - A constructor function to check against.
|
|
53
|
+
* @param value - The value to be verified as an instance of the constructor.
|
|
54
|
+
* @param name - The name of the value for the error message. Defaults to 'value'.
|
|
55
|
+
* @throws {@link !TypeError} If `ctor` is not a function or if the value is not an instance of the constructor.
|
|
56
|
+
* @typeParam T - The type of instances produced by the constructor.
|
|
57
|
+
* @category Assertions
|
|
58
|
+
*/
|
|
59
|
+
export declare function assertOfType<T>(ctor: Constructor<T>, value: unknown, name?: string): asserts value is T;
|
|
60
|
+
/** @ignore */
|
|
61
|
+
export declare function somePredicate<TGuards extends readonly TypePredicate[]>(...predicates: TGuards): TypePredicate<ArrayItemsIntersection<PredicatesListArg<TGuards>>, NarrowedType<IterableElement<TGuards>>>;
|
|
62
|
+
/**
|
|
63
|
+
* Creates a predicate that returns true if at least one of the provided predicates is satisfied.
|
|
64
|
+
*
|
|
65
|
+
* @param predicates - A list of predicate functions to evaluate.
|
|
66
|
+
* @returns A predicate function that checks if any predicate matches.
|
|
67
|
+
* @typeParam TPredicates - The type of the list of predicates.
|
|
68
|
+
* @category Checks
|
|
69
|
+
*/
|
|
70
|
+
export declare function somePredicate<TPredicates extends readonly Predicate[]>(...predicates: TPredicates): Predicate<ArrayItemsIntersection<PredicatesListArg<TPredicates>>>;
|
|
71
|
+
/**
|
|
72
|
+
* Asserts that a value satisfies at least one of the provided predicates.
|
|
73
|
+
*
|
|
74
|
+
* If none of the predicates are satisfied, a {@link !TypeError} is thrown **with default value name**.
|
|
75
|
+
* When type predicates are used, the value is narrowed to the union of their narrowed types.
|
|
76
|
+
*
|
|
77
|
+
* @param value - The value to validate against the predicates.
|
|
78
|
+
* @param predicates - One or more predicate functions to check. At least one must return true.
|
|
79
|
+
* @returns void.
|
|
80
|
+
* @throws {@link !TypeError} in the following cases:
|
|
81
|
+
* - If the value does not satisfy any of the provided predicates.
|
|
82
|
+
* - If any of the predicates is not a function
|
|
83
|
+
* - If `valueName` is not a string.
|
|
84
|
+
* @typeParam TGuards - The type of the list of type predicates.
|
|
85
|
+
* {@label DEFAULT_NAME}
|
|
86
|
+
* @category Assertions
|
|
87
|
+
*/
|
|
88
|
+
export declare function assertSome<TGuards extends readonly TypePredicate[]>(value: ArrayItemsIntersection<PredicatesListArg<TGuards>>, ...predicates: TGuards): asserts value is NarrowedType<IterableElement<TGuards>>;
|
|
89
|
+
/**
|
|
90
|
+
* Asserts that a value satisfies at least one of the provided predicates.
|
|
91
|
+
*
|
|
92
|
+
* If none of the predicates are satisfied, a {@link !TypeError} is thrown **with the specified value name**.
|
|
93
|
+
* When type predicates are used, the value is narrowed to the union of their narrowed types.
|
|
94
|
+
*
|
|
95
|
+
* @param value - The value to validate against the predicates.
|
|
96
|
+
* @param valueName - Optional name of the value for the error message.
|
|
97
|
+
* @param predicates - One or more predicate functions to check. At least one must return true.
|
|
98
|
+
* @returns void.
|
|
99
|
+
* @throws {@link !TypeError} in the following cases:
|
|
100
|
+
* - If the value does not satisfy any of the provided predicates.
|
|
101
|
+
* - If any of the predicates is not a function
|
|
102
|
+
* - If `valueName` is not a string.
|
|
103
|
+
* @typeParam TGuards - The type of the list of type predicates.
|
|
104
|
+
* {@label CUSTOM_NAME}
|
|
105
|
+
* @category Assertions
|
|
106
|
+
*/
|
|
107
|
+
export declare function assertSome<TGuards extends readonly TypePredicate[]>(value: ArrayItemsIntersection<PredicatesListArg<TGuards>>, valueName: string, ...predicates: TGuards): asserts value is NarrowedType<IterableElement<TGuards>>;
|
|
108
|
+
export declare function assertSome<TPredicates extends readonly Predicate[]>(value: ArrayItemsIntersection<PredicatesListArg<TPredicates>>, ...predicates: TPredicates): void;
|
|
109
|
+
export declare function assertSome<TPredicates extends readonly Predicate[]>(value: ArrayItemsIntersection<PredicatesListArg<TPredicates>>, valueName: string, ...predicates: TPredicates): void;
|
|
110
|
+
/** @ignore */
|
|
111
|
+
export declare function everyPredicate<TPredicates extends readonly TypePredicate[]>(...predicates: TPredicates): TypePredicate<ArrayItemsIntersection<PredicatesListArg<TPredicates>>, ArrayItemsIntersection<PredicatesListNarrowed<TPredicates>> & ArrayItemsIntersection<PredicatesListArg<TPredicates>>>;
|
|
112
|
+
/**
|
|
113
|
+
* Creates a predicate that returns true if all the provided predicates are satisfied.
|
|
114
|
+
*
|
|
115
|
+
* @param predicates - A list of predicate functions to evaluate.
|
|
116
|
+
* @returns A predicate function that checks if every predicate matches.
|
|
117
|
+
* @typeParam TPredicates - The type of the list of predicates.
|
|
118
|
+
* @category Checks
|
|
119
|
+
*/
|
|
120
|
+
export declare function everyPredicate<TPredicates extends readonly Predicate[]>(...predicates: readonly Predicate[]): Predicate<ArrayItemsIntersection<PredicatesListArg<TPredicates>>>;
|
|
121
|
+
/**
|
|
122
|
+
* Asserts that a value satisfies all the provided predicates.
|
|
123
|
+
*
|
|
124
|
+
* If any predicate fails, a {@link !TypeError} is thrown **with default name**.
|
|
125
|
+
* When type predicates are used, the value is narrowed to the intersection of their narrowed types.
|
|
126
|
+
*
|
|
127
|
+
* @param value - The value to validate against the predicates.
|
|
128
|
+
* @param rest - Either predicates only, or a value name followed by predicates. All predicates must return true.
|
|
129
|
+
* @returns void.
|
|
130
|
+
* @throws {@link !TypeError} If the value does not satisfy all of the provided predicates.
|
|
131
|
+
* @typeParam TPredicates - The type of the list of predicates.
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* assertEvery(value, isObject, hasName); // value must be object AND have name
|
|
135
|
+
* assertEvery(value, 'config', isObject, hasName); // with custom name
|
|
136
|
+
* ```
|
|
137
|
+
* @category Assertions
|
|
138
|
+
*/
|
|
139
|
+
export declare function assertEvery<TPredicates extends readonly Predicate[]>(value: ArrayItemsIntersection<PredicatesListArg<TPredicates>>, ...rest: TPredicates): asserts value is ArrayItemsIntersection<PredicatesListNarrowed<TPredicates>>;
|
|
140
|
+
/**
|
|
141
|
+
* Asserts that a value satisfies all the provided predicates.
|
|
142
|
+
*
|
|
143
|
+
* If any predicate fails, a {@link !TypeError} is thrown **with the specified value name**.
|
|
144
|
+
* When type predicates are used, the value is narrowed to the intersection of their narrowed types.
|
|
145
|
+
*
|
|
146
|
+
* @param value - The value to validate against the predicates.
|
|
147
|
+
* @param valueName - Optional name of the value to be used in error messages.
|
|
148
|
+
* @param rest - Either predicates only, or a value name followed by predicates. All predicates must return true.
|
|
149
|
+
* @returns void.
|
|
150
|
+
* @throws {@link !TypeError} If the value does not satisfy all of the provided predicates.
|
|
151
|
+
* @typeParam TPredicates - The type of the list of predicates.
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* assertEvery(value, isObject, hasName); // value must be object AND have name
|
|
155
|
+
* assertEvery(value, 'config', isObject, hasName); // with custom name
|
|
156
|
+
* ```
|
|
157
|
+
* @category Assertions
|
|
158
|
+
*/
|
|
159
|
+
export declare function assertEvery<TPredicates extends readonly Predicate[]>(value: ArrayItemsIntersection<PredicatesListArg<TPredicates>>, valueName: string, ...rest: TPredicates): asserts value is ArrayItemsIntersection<PredicatesListNarrowed<TPredicates>>;
|
|
160
|
+
/**
|
|
161
|
+
* Creates a type predicate that checks if a given value matches any of the specified values.
|
|
162
|
+
*
|
|
163
|
+
* This function accepts a set of possible values and returns a predicate function
|
|
164
|
+
* that evaluates if an input value exists within the set of those specified values.
|
|
165
|
+
*
|
|
166
|
+
* @param values - The set of values to match the input against.
|
|
167
|
+
* @returns A type predicate that verifies whether a value is one of the specified values.
|
|
168
|
+
* @typeParam TValues - A tuple type of all possible values to check against.
|
|
169
|
+
* @remarks Value equality is based on the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness#same-value-zero_equality SameValueZero algorithm}.
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const isColor = anyOf('red', 'green', 'blue');
|
|
173
|
+
* const test = 'red';
|
|
174
|
+
* if (isColor(test)) {
|
|
175
|
+
* console.log("It\'s a color!");
|
|
176
|
+
* }
|
|
177
|
+
* ```
|
|
178
|
+
* @category Checks
|
|
179
|
+
*/
|
|
180
|
+
export declare const anyOf: <TValues extends readonly unknown[]>(...values: TValues) => Predicate<unknown, IterableElement<TValues>>;
|
|
181
|
+
/**
|
|
182
|
+
* Asserts that the given value matches any of the values within the specified iterable.
|
|
183
|
+
*
|
|
184
|
+
* @param values - An iterable containing the values to match against.
|
|
185
|
+
* @param value - The value to assert is one of the specified values.
|
|
186
|
+
* @param valueName - An optional name for the value being asserted, typically used for error messages.
|
|
187
|
+
* @returns This function does not return a value. It throws an error if the assertion fails.
|
|
188
|
+
* @typeParam TValues - A tuple of allowed values to assert against.
|
|
189
|
+
* @remarks This function accepts values as iterable argument, not as `...rest` argument,
|
|
190
|
+
* because there's no way to distinguish between `valueName` and `values` when using `...rest`.
|
|
191
|
+
* @category Assertions
|
|
192
|
+
*/
|
|
193
|
+
export declare function assertAnyOf<TValues extends Iterable<unknown>>(values: TValues, value: unknown, valueName?: string): asserts value is IterableElement<TValues>;
|
|
194
|
+
/**
|
|
195
|
+
* Creates a type predicate that checks whether a value is a tuple matching the provided predicates.
|
|
196
|
+
*
|
|
197
|
+
* The value must be an {@link !Array array} with exactly the same length as the number of predicates,
|
|
198
|
+
* and each element must satisfy the corresponding predicate.
|
|
199
|
+
*
|
|
200
|
+
* @param predicates - A list of predicates, one per expected tuple element.
|
|
201
|
+
* @returns A type predicate that narrows the value to a tuple type inferred from the predicates.
|
|
202
|
+
* @throws {@link !TypeError} If `predicates` contain non-predicate elements.
|
|
203
|
+
* @typeParam TPredicates - The tuple type of the provided predicates.
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const isPoint = isTuple(isNumber, isNumber);
|
|
207
|
+
* isPoint([1, 2]); // true
|
|
208
|
+
* isPoint([1, 'x']); // false
|
|
209
|
+
* isPoint([1]); // false
|
|
210
|
+
* ```
|
|
211
|
+
* @category Checks
|
|
212
|
+
*/
|
|
213
|
+
export declare function isTuple<TPredicates extends readonly Predicate[]>(...predicates: TPredicates): TypePredicate<unknown, PredicatesListNarrowed<TPredicates>>;
|
|
214
|
+
/**
|
|
215
|
+
* Asserts that the value is a tuple matching the provided predicates, using a custom value name in error messages.
|
|
216
|
+
*
|
|
217
|
+
* The value must be an {@link !Array} with exactly the same length as the number of predicates,
|
|
218
|
+
* and each element must satisfy the corresponding predicate.
|
|
219
|
+
*
|
|
220
|
+
* @param value - The value to assert as a tuple.
|
|
221
|
+
* @param valueName - The name of the value to use in the error message.
|
|
222
|
+
* @param predicates - A tuple of predicates, one per expected element.
|
|
223
|
+
* @returns void.
|
|
224
|
+
* @throws {@link !TypeError} If the value does not match the expected tuple shape.
|
|
225
|
+
* @typeParam TPredicates - The tuple type of the provided predicates.
|
|
226
|
+
* {@label CUSTOM_NAME}
|
|
227
|
+
* @example
|
|
228
|
+
* ```typescript
|
|
229
|
+
* assertTuple(payload, 'payload', [isString, isNumber]);
|
|
230
|
+
* // payload is narrowed to [string, number]
|
|
231
|
+
* ```
|
|
232
|
+
* @category Assertions
|
|
233
|
+
*/
|
|
234
|
+
export declare function assertTuple<TPredicates extends readonly Predicate[]>(value: unknown, valueName: string, ...predicates: TPredicates): asserts value is PredicatesListNarrowed<TPredicates>;
|
|
235
|
+
/**
|
|
236
|
+
* Asserts that the value is a tuple matching the provided predicates, using the default value name.
|
|
237
|
+
*
|
|
238
|
+
* The value must be an {@link !Array} with exactly the same length as the number of predicates,
|
|
239
|
+
* and each element must satisfy the corresponding predicate.
|
|
240
|
+
*
|
|
241
|
+
* @param value - The value to assert as a tuple.
|
|
242
|
+
* @param predicates - A tuple of predicates, one per expected element.
|
|
243
|
+
* @returns void.
|
|
244
|
+
* @throws {@link !TypeError} In the following cases:
|
|
245
|
+
* - If the `value` does not match the expected tuple shape.
|
|
246
|
+
* - If `predicates` contain non-predicate elements.
|
|
247
|
+
* @typeParam TPredicates - The tuple type of the provided predicates.
|
|
248
|
+
* {@label DEFAULT_NAME}
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* assertTuple(value, [isString, isNumber]);
|
|
252
|
+
* // value is narrowed to [string, number]
|
|
253
|
+
* ```
|
|
254
|
+
* @category Assertions
|
|
255
|
+
*/
|
|
256
|
+
export declare function assertTuple<TPredicates extends readonly Predicate[]>(value: unknown, ...predicates: TPredicates): asserts value is PredicatesListNarrowed<TPredicates>;
|
|
257
|
+
//# sourceMappingURL=hlf.d.ts.map
|