@webdeveric/utils 0.17.1 → 0.19.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/cjs/capitalize.js +6 -0
- package/dist/cjs/capitalize.js.map +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/normalize.js.map +1 -1
- package/dist/cjs/toPascalCase.js +9 -20
- package/dist/cjs/toPascalCase.js.map +1 -1
- package/dist/cjs/type-assertion.js +185 -10
- package/dist/cjs/type-assertion.js.map +1 -1
- package/dist/cjs/type-predicate.js +67 -18
- package/dist/cjs/type-predicate.js.map +1 -1
- package/dist/mjs/capitalize.js +2 -0
- package/dist/mjs/capitalize.js.map +1 -0
- package/dist/mjs/index.js +1 -0
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/normalize.js.map +1 -1
- package/dist/mjs/toPascalCase.js +9 -20
- package/dist/mjs/toPascalCase.js.map +1 -1
- package/dist/mjs/type-assertion.js +155 -9
- package/dist/mjs/type-assertion.js.map +1 -1
- package/dist/mjs/type-predicate.js +46 -14
- package/dist/mjs/type-predicate.js.map +1 -1
- package/dist/types/capitalize.d.ts +2 -0
- package/dist/types/capitalize.d.ts.map +1 -0
- package/dist/types/compose.d.ts +1 -1
- package/dist/types/compose.d.ts.map +1 -1
- package/dist/types/convert.d.ts +4 -4
- package/dist/types/convert.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/normalize.d.ts +6 -7
- package/dist/types/normalize.d.ts.map +1 -1
- package/dist/types/toPascalCase.d.ts +2 -1
- package/dist/types/toPascalCase.d.ts.map +1 -1
- package/dist/types/type-assertion.d.ts +35 -4
- package/dist/types/type-assertion.d.ts.map +1 -1
- package/dist/types/type-predicate.d.ts +21 -4
- package/dist/types/type-predicate.d.ts.map +1 -1
- package/dist/types/types.d.ts +20 -11
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/until.d.ts +7 -7
- package/dist/types/until.d.ts.map +1 -1
- package/package.json +4 -5
- package/src/capitalize.ts +1 -0
- package/src/convert.ts +1 -1
- package/src/index.ts +1 -0
- package/src/normalize.ts +1 -3
- package/src/toPascalCase.ts +14 -24
- package/src/type-assertion.ts +309 -10
- package/src/type-predicate.ts +75 -23
- package/src/types.ts +23 -1
package/src/convert.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getOwnProperties } from './getOwnProperties.js';
|
|
2
2
|
import { getType } from './getType.js';
|
|
3
3
|
|
|
4
|
-
import type { Builtin } from '
|
|
4
|
+
import type { Builtin } from './types.js';
|
|
5
5
|
|
|
6
6
|
export type ConvertFn<SourceType, ReturnType> = (source: Readonly<SourceType>) => ReturnType;
|
|
7
7
|
|
package/src/index.ts
CHANGED
package/src/normalize.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import cloneDeep from 'lodash.clonedeep';
|
|
2
2
|
|
|
3
|
-
import type { Builtin } from 'ts-essentials';
|
|
4
|
-
|
|
5
3
|
import { getOwnProperties } from './getOwnProperties.js';
|
|
6
4
|
import { isObject } from './isObject.js';
|
|
7
5
|
|
|
8
|
-
import type { AnyRecord } from './types.js';
|
|
6
|
+
import type { AnyRecord, Builtin } from './types.js';
|
|
9
7
|
|
|
10
8
|
export type NormalizeContext<OwnerRecordType, ContextData extends AnyRecord> = {
|
|
11
9
|
original: Readonly<OwnerRecordType>;
|
package/src/toPascalCase.ts
CHANGED
|
@@ -1,39 +1,29 @@
|
|
|
1
|
+
import { capitalize } from './capitalize.js';
|
|
1
2
|
import { getType } from './getType.js';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
let words = String(text).match(/[A-Z][a-z']+|\d+|[a-z']+/g);
|
|
4
|
+
import type { KeyValueTuple, StringRecord } from './types.js';
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
export function toPascalCase(text: string, customWords?: StringRecord): string {
|
|
7
|
+
const words = String(text).match(/[A-Z][a-z']+|\d+|[a-z']+/g);
|
|
8
|
+
|
|
9
|
+
if (words === null) {
|
|
7
10
|
return '';
|
|
8
11
|
}
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
words = words.map(word => {
|
|
12
|
-
const fixedWord = word.replace(/'/g, '');
|
|
13
|
-
|
|
14
|
-
return fixedWord.substring(0, 1).toUpperCase() + fixedWord.substring(1);
|
|
15
|
-
});
|
|
13
|
+
let fixedWords = words.map(word => capitalize(word.replace(/'/g, '')));
|
|
16
14
|
|
|
17
15
|
if (customWords && getType(customWords) === 'Object') {
|
|
18
|
-
const replacements =
|
|
16
|
+
const replacements = new Map(
|
|
17
|
+
Object.entries(customWords).map<KeyValueTuple<string, string>>(([key, value]) => [key.toLowerCase(), value]),
|
|
18
|
+
);
|
|
19
19
|
|
|
20
|
-
if (replacements.
|
|
20
|
+
if (replacements.size) {
|
|
21
21
|
// This takes a single word and returns the first matching replacement, if any.
|
|
22
|
-
const replaceCustomWords = (word: string): string =>
|
|
23
|
-
const lowerWord = word.toLowerCase();
|
|
24
|
-
|
|
25
|
-
for (const [key, value] of replacements) {
|
|
26
|
-
if (key === lowerWord && typeof value === 'string') {
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return word;
|
|
32
|
-
};
|
|
22
|
+
const replaceCustomWords = (word: string): string => replacements.get(word.toLowerCase()) ?? word;
|
|
33
23
|
|
|
34
|
-
|
|
24
|
+
fixedWords = fixedWords.map(replaceCustomWords);
|
|
35
25
|
}
|
|
36
26
|
}
|
|
37
27
|
|
|
38
|
-
return
|
|
28
|
+
return fixedWords.join('');
|
|
39
29
|
}
|
package/src/type-assertion.ts
CHANGED
|
@@ -1,20 +1,319 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
isBigInt,
|
|
3
|
+
isBoolean,
|
|
4
|
+
isFiniteNumber,
|
|
5
|
+
isInteger,
|
|
6
|
+
isIntString,
|
|
7
|
+
isLengthAware,
|
|
8
|
+
isNull,
|
|
9
|
+
isNumber,
|
|
10
|
+
isNumericString,
|
|
11
|
+
isNumericValue,
|
|
12
|
+
isNumericValueArray,
|
|
13
|
+
isObject,
|
|
14
|
+
isOptionalBigInt,
|
|
15
|
+
isOptionalBoolean,
|
|
16
|
+
isOptionalNull,
|
|
17
|
+
isOptionalNumber,
|
|
18
|
+
isOptionalString,
|
|
19
|
+
isOptionalSymbol,
|
|
20
|
+
isPositiveFiniteNumber,
|
|
21
|
+
isPositiveInteger,
|
|
22
|
+
isPrimitive,
|
|
23
|
+
isPromiseFulfilledResult,
|
|
24
|
+
isPromiseRejectedResult,
|
|
25
|
+
isPropertyKey,
|
|
26
|
+
isString,
|
|
27
|
+
isStringMatching,
|
|
28
|
+
isStringRecord,
|
|
29
|
+
isStringWithLength,
|
|
30
|
+
isSymbol,
|
|
31
|
+
isUndefined,
|
|
32
|
+
} from './type-predicate.js';
|
|
3
33
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
34
|
+
import type { NumericString, NumericValue, Primitive, StringRecord, UnknownRecord } from './types.js';
|
|
35
|
+
|
|
36
|
+
function getError(error: string | Error): Error {
|
|
37
|
+
return error instanceof Error ? error : new TypeError(error);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function assertIsBigInt(
|
|
41
|
+
input: unknown,
|
|
42
|
+
error: string | Error = 'input is not a bigint',
|
|
43
|
+
): asserts input is bigint {
|
|
44
|
+
if (!isBigInt(input)) {
|
|
45
|
+
throw getError(error);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function assertIsBoolean(
|
|
50
|
+
input: unknown,
|
|
51
|
+
error: string | Error = 'input is not a boolean',
|
|
52
|
+
): asserts input is boolean {
|
|
53
|
+
if (!isBoolean(input)) {
|
|
54
|
+
throw getError(error);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function assertIsFiniteNumber(
|
|
59
|
+
input: unknown,
|
|
60
|
+
error: string | Error = 'input is not a finite number',
|
|
61
|
+
): asserts input is number {
|
|
62
|
+
if (!isFiniteNumber(input)) {
|
|
63
|
+
throw getError(error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function assertIsInteger(
|
|
68
|
+
input: unknown,
|
|
69
|
+
error: string | Error = 'input is not an integer',
|
|
70
|
+
): asserts input is number {
|
|
71
|
+
if (!isInteger(input)) {
|
|
72
|
+
throw getError(error);
|
|
7
73
|
}
|
|
8
74
|
}
|
|
9
75
|
|
|
10
|
-
export function
|
|
11
|
-
|
|
12
|
-
|
|
76
|
+
export function assertIsIntString(
|
|
77
|
+
input: unknown,
|
|
78
|
+
error: string | Error = 'input is not an integer string',
|
|
79
|
+
): asserts input is NumericString {
|
|
80
|
+
if (!isIntString(input)) {
|
|
81
|
+
throw getError(error);
|
|
13
82
|
}
|
|
14
83
|
}
|
|
15
84
|
|
|
16
|
-
export function
|
|
85
|
+
export function assertIsLengthAware(
|
|
86
|
+
input: unknown,
|
|
87
|
+
error: string | Error = 'input is not length aware',
|
|
88
|
+
): asserts input is { length: number } {
|
|
89
|
+
if (!isLengthAware(input)) {
|
|
90
|
+
throw getError(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function assertIsNull(input: unknown, error: string | Error = 'input is not null'): asserts input is null {
|
|
95
|
+
if (!isNull(input)) {
|
|
96
|
+
throw getError(error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function assertIsNumber(
|
|
101
|
+
input: unknown,
|
|
102
|
+
error: string | Error = 'input is not a number',
|
|
103
|
+
): asserts input is number {
|
|
104
|
+
if (!isNumber(input)) {
|
|
105
|
+
throw getError(error);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function assertIsNumericString(
|
|
110
|
+
input: unknown,
|
|
111
|
+
error: string | Error = 'input is not a numeric string',
|
|
112
|
+
): asserts input is NumericString {
|
|
113
|
+
if (!isNumericString(input)) {
|
|
114
|
+
throw getError(error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function assertIsNumericValue(
|
|
119
|
+
input: unknown,
|
|
120
|
+
error: string | Error = 'input is not a numerical value',
|
|
121
|
+
): asserts input is NumericValue {
|
|
122
|
+
if (!isNumericValue(input)) {
|
|
123
|
+
throw getError(error);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function assertIsNumericValueArray(
|
|
128
|
+
input: unknown,
|
|
129
|
+
error: string | Error = 'input is not a numerical value array',
|
|
130
|
+
): asserts input is NumericValue[] {
|
|
17
131
|
if (!isNumericValueArray(input)) {
|
|
18
|
-
throw
|
|
132
|
+
throw getError(error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function assertIsObject<T extends UnknownRecord = UnknownRecord>(
|
|
137
|
+
input: unknown,
|
|
138
|
+
error: string | Error = 'input is not an object',
|
|
139
|
+
): asserts input is T {
|
|
140
|
+
if (!isObject<T>(input)) {
|
|
141
|
+
throw getError(error);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function assertIsOptionalBigInt(
|
|
146
|
+
input: unknown,
|
|
147
|
+
error: string | Error = 'input is not an optional bigint',
|
|
148
|
+
): asserts input is bigint | undefined {
|
|
149
|
+
if (!isOptionalBigInt(input)) {
|
|
150
|
+
throw getError(error);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function assertIsOptionalBoolean(
|
|
155
|
+
input: unknown,
|
|
156
|
+
error: string | Error = 'input is not an optional boolean',
|
|
157
|
+
): asserts input is boolean | undefined {
|
|
158
|
+
if (!isOptionalBoolean(input)) {
|
|
159
|
+
throw getError(error);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function assertIsOptionalNull(
|
|
164
|
+
input: unknown,
|
|
165
|
+
error: string | Error = 'input is not an optional null',
|
|
166
|
+
): asserts input is null | undefined {
|
|
167
|
+
if (!isOptionalNull(input)) {
|
|
168
|
+
throw getError(error);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function assertIsOptionalNumber(
|
|
173
|
+
input: unknown,
|
|
174
|
+
error: string | Error = 'input is not an optional number',
|
|
175
|
+
): asserts input is number | undefined {
|
|
176
|
+
if (!isOptionalNumber(input)) {
|
|
177
|
+
throw getError(error);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function assertIsOptionalString(
|
|
182
|
+
input: unknown,
|
|
183
|
+
error: string | Error = 'input is not an optional string',
|
|
184
|
+
): asserts input is string | undefined {
|
|
185
|
+
if (!isOptionalString(input)) {
|
|
186
|
+
throw getError(error);
|
|
19
187
|
}
|
|
20
188
|
}
|
|
189
|
+
|
|
190
|
+
export function assertIsOptionalSymbol(
|
|
191
|
+
input: unknown,
|
|
192
|
+
error: string | Error = 'input is not an optional symbol',
|
|
193
|
+
): asserts input is symbol | undefined {
|
|
194
|
+
if (!isOptionalSymbol(input)) {
|
|
195
|
+
throw getError(error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function assertIsPositiveFiniteNumber(
|
|
200
|
+
input: unknown,
|
|
201
|
+
error: string | Error = 'input is not a positive finite number',
|
|
202
|
+
): asserts input is number {
|
|
203
|
+
if (!isPositiveFiniteNumber(input)) {
|
|
204
|
+
throw getError(error);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function assertIsNumberGTEOne(
|
|
209
|
+
input: unknown,
|
|
210
|
+
error: string | Error = 'input is not a number >= 1',
|
|
211
|
+
): asserts input is number {
|
|
212
|
+
if (!isPositiveFiniteNumber(input) || input < 1) {
|
|
213
|
+
throw getError(error);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function assertIsPositiveInteger(
|
|
218
|
+
input: unknown,
|
|
219
|
+
error: string | Error = 'input is not a positive integer',
|
|
220
|
+
): asserts input is number {
|
|
221
|
+
if (!isPositiveInteger(input)) {
|
|
222
|
+
throw getError(error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function assertIsPrimitive(
|
|
227
|
+
input: unknown,
|
|
228
|
+
error: string | Error = 'input is not primitive',
|
|
229
|
+
): asserts input is Primitive {
|
|
230
|
+
if (!isPrimitive(input)) {
|
|
231
|
+
throw getError(error);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function assertIsPromiseFulfilledResult<T>(
|
|
236
|
+
input: unknown,
|
|
237
|
+
error: string | Error = 'input is not a promise fulfilled result',
|
|
238
|
+
): asserts input is PromiseFulfilledResult<T> {
|
|
239
|
+
if (!isPromiseFulfilledResult(input)) {
|
|
240
|
+
throw getError(error);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function assertIsPromiseRejectedResult(
|
|
245
|
+
input: unknown,
|
|
246
|
+
error: string | Error = 'input is not a promise rejected result',
|
|
247
|
+
): asserts input is PromiseRejectedResult {
|
|
248
|
+
if (!isPromiseRejectedResult(input)) {
|
|
249
|
+
throw getError(error);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function assertIsPropertyKey(
|
|
254
|
+
input: unknown,
|
|
255
|
+
error: string | Error = 'input is not a property key',
|
|
256
|
+
): asserts input is PropertyKey {
|
|
257
|
+
if (!isPropertyKey(input)) {
|
|
258
|
+
throw getError(error);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export function assertIsString(
|
|
263
|
+
input: unknown,
|
|
264
|
+
error: string | Error = 'input is not a string',
|
|
265
|
+
): asserts input is string {
|
|
266
|
+
if (!isString(input)) {
|
|
267
|
+
throw getError(error);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function assertIsStringMatching(
|
|
272
|
+
input: unknown,
|
|
273
|
+
pattern: RegExp,
|
|
274
|
+
error: string | Error = 'input is not a string that matches the pattern',
|
|
275
|
+
): asserts input is string {
|
|
276
|
+
if (!isStringMatching(input, pattern)) {
|
|
277
|
+
throw getError(error);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function assertIsStringRecord(
|
|
282
|
+
input: unknown,
|
|
283
|
+
error: string | Error = 'input is not a string record',
|
|
284
|
+
): asserts input is StringRecord {
|
|
285
|
+
if (!isStringRecord(input)) {
|
|
286
|
+
throw getError(error);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function assertIsStringWithLength(
|
|
291
|
+
input: unknown,
|
|
292
|
+
error: string | Error = 'input is not a string with length',
|
|
293
|
+
): asserts input is string {
|
|
294
|
+
if (!isStringWithLength(input)) {
|
|
295
|
+
throw getError(error);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function assertIsSymbol(
|
|
300
|
+
input: unknown,
|
|
301
|
+
error: string | Error = 'input is not a symbol',
|
|
302
|
+
): asserts input is symbol {
|
|
303
|
+
if (!isSymbol(input)) {
|
|
304
|
+
throw getError(error);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function assertIsUndefined(
|
|
309
|
+
input: unknown,
|
|
310
|
+
error: string | Error = 'input is not undefined',
|
|
311
|
+
): asserts input is undefined {
|
|
312
|
+
if (!isUndefined(input)) {
|
|
313
|
+
throw getError(error);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export function assertExhaustive(_: never, error: string | Error = 'Failed exhaustive check'): never {
|
|
318
|
+
throw getError(error);
|
|
319
|
+
}
|
package/src/type-predicate.ts
CHANGED
|
@@ -1,17 +1,63 @@
|
|
|
1
|
-
import type { NumericString, NumericValue, Primitive } from './types.js';
|
|
1
|
+
import type { NumericString, NumericValue, Primitive, StringRecord, TypePredicateFn, UnknownRecord } from './types.js';
|
|
2
2
|
|
|
3
|
-
export const
|
|
4
|
-
|
|
3
|
+
export const createIsEnumPredicate = <E extends Record<string, unknown>, T = E>(enumObject: E): TypePredicateFn<T> => {
|
|
4
|
+
const values = Object.values(enumObject);
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
return (input: unknown): input is T => {
|
|
7
|
+
return values.includes(input);
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const maybeUndefined = <T>(predicate: TypePredicateFn<T>, input: unknown): input is T | undefined => {
|
|
12
|
+
return typeof input === 'undefined' || predicate(input);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Primitives
|
|
16
|
+
|
|
17
|
+
export const isString = (input: unknown): input is string => typeof input === 'string';
|
|
18
|
+
|
|
19
|
+
export const isNumber = (input: unknown): input is number => typeof input === 'number' && !Number.isNaN(input);
|
|
20
|
+
|
|
21
|
+
export const isBigInt = (input: unknown): input is bigint => typeof input === 'bigint';
|
|
22
|
+
|
|
23
|
+
export const isBoolean = (input: unknown): input is boolean => typeof input === 'boolean';
|
|
24
|
+
|
|
25
|
+
export const isUndefined = (input: unknown): input is undefined => typeof input === 'undefined';
|
|
26
|
+
|
|
27
|
+
export const isSymbol = (input: unknown): input is symbol => typeof input === 'symbol';
|
|
28
|
+
|
|
29
|
+
export const isNull = (input: unknown): input is null => input === null;
|
|
30
|
+
|
|
31
|
+
export const isPrimitive = (input: unknown): input is Primitive =>
|
|
32
|
+
isString(input) ||
|
|
33
|
+
isNumber(input) ||
|
|
34
|
+
isBigInt(input) ||
|
|
35
|
+
isBoolean(input) ||
|
|
36
|
+
isUndefined(input) ||
|
|
37
|
+
isSymbol(input) ||
|
|
38
|
+
isNull(input);
|
|
39
|
+
|
|
40
|
+
// Optional primitives
|
|
41
|
+
|
|
42
|
+
export const isOptionalString = (input: unknown): input is string | undefined => maybeUndefined(isString, input);
|
|
43
|
+
|
|
44
|
+
export const isOptionalNumber = (input: unknown): input is number | undefined => maybeUndefined(isNumber, input);
|
|
45
|
+
|
|
46
|
+
export const isOptionalBigInt = (input: unknown): input is bigint | undefined => maybeUndefined(isBigInt, input);
|
|
47
|
+
|
|
48
|
+
export const isOptionalBoolean = (input: unknown): input is boolean | undefined => maybeUndefined(isBoolean, input);
|
|
49
|
+
|
|
50
|
+
export const isOptionalSymbol = (input: unknown): input is symbol | undefined => maybeUndefined(isSymbol, input);
|
|
51
|
+
|
|
52
|
+
export const isOptionalNull = (input: unknown): input is null | undefined => maybeUndefined(isNull, input);
|
|
53
|
+
|
|
54
|
+
// Primitives with extra checks
|
|
55
|
+
|
|
56
|
+
export const isStringWithLength = (input: unknown): input is string => typeof input === 'string' && input.length > 0;
|
|
8
57
|
|
|
9
58
|
export const isStringMatching = (input: unknown, pattern: RegExp): input is string =>
|
|
10
59
|
typeof input === 'string' && pattern.test(input);
|
|
11
60
|
|
|
12
|
-
export const isPropertyKey = (input: unknown): input is PropertyKey =>
|
|
13
|
-
typeof input === 'string' || typeof input === 'number' || typeof input === 'symbol';
|
|
14
|
-
|
|
15
61
|
export const isNumericString = (input: unknown): input is NumericString =>
|
|
16
62
|
isStringMatching(input, /^[-+]?\d+(\.\d+)?$/);
|
|
17
63
|
|
|
@@ -23,7 +69,7 @@ export const isNumericValueArray = (input: unknown): input is NumericValue[] =>
|
|
|
23
69
|
|
|
24
70
|
export const isIntString = (input: unknown): input is NumericString => isStringMatching(input, /^[-+]?\d+$/);
|
|
25
71
|
|
|
26
|
-
export const isFiniteNumber = (input: unknown): input is number =>
|
|
72
|
+
export const isFiniteNumber = (input: unknown): input is number => Number.isFinite(input);
|
|
27
73
|
|
|
28
74
|
export const isInteger = (input: unknown): input is number => Number.isInteger(input);
|
|
29
75
|
|
|
@@ -31,19 +77,25 @@ export const isPositiveInteger = (input: unknown): input is number => isInteger(
|
|
|
31
77
|
|
|
32
78
|
export const isPositiveFiniteNumber = (input: unknown): input is number => isFiniteNumber(input) && input >= 0;
|
|
33
79
|
|
|
80
|
+
// Common types
|
|
81
|
+
|
|
82
|
+
export const isPropertyKey = (input: unknown): input is PropertyKey =>
|
|
83
|
+
typeof input === 'string' || typeof input === 'number' || typeof input === 'symbol';
|
|
84
|
+
|
|
85
|
+
export const isObject = <T extends UnknownRecord = UnknownRecord>(input: unknown): input is T =>
|
|
86
|
+
input !== null && typeof input === 'object' && !Array.isArray(input);
|
|
87
|
+
|
|
34
88
|
export const isLengthAware = (input: unknown): input is { length: number } =>
|
|
35
|
-
|
|
36
|
-
typeof input === 'object' && input !== null && 'length' in input && typeof (input as any).length === 'number';
|
|
89
|
+
input !== null && typeof input === 'object' && 'length' in input && typeof input.length === 'number';
|
|
37
90
|
|
|
38
|
-
export const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
): input is O => input !== null && typeof input === 'object' && !Array.isArray(input);
|
|
91
|
+
export const isStringRecord = (input: unknown): input is StringRecord => {
|
|
92
|
+
return isObject(input) && Object.entries(input).every(entry => entry.every(isString));
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const isPromiseFulfilledResult = <T>(input: unknown): input is PromiseFulfilledResult<T> => {
|
|
96
|
+
return isObject(input) && input.status === 'fulfilled';
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export const isPromiseRejectedResult = (input: unknown): input is PromiseRejectedResult => {
|
|
100
|
+
return isObject(input) && input.status === 'rejected' && 'reason' in input;
|
|
101
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
export type Primitive = string | number |
|
|
1
|
+
export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
4
|
+
export type Builtin = Date | Error | Function | Primitive | RegExp;
|
|
2
5
|
|
|
3
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
7
|
export type AnyRecord = Record<PropertyKey, any>;
|
|
5
8
|
|
|
9
|
+
export type StringRecord = Record<string, string>;
|
|
10
|
+
|
|
6
11
|
export type UnknownRecord = Record<PropertyKey, unknown>;
|
|
7
12
|
|
|
8
13
|
export type NumericString<T extends string | number = number> = `${T}` extends `${number}` ? `${T}` : never;
|
|
@@ -24,3 +29,20 @@ export type AnyNewable = {
|
|
|
24
29
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
30
|
new (...args: any[]): any;
|
|
26
31
|
};
|
|
32
|
+
|
|
33
|
+
export type JoinTuples<A extends unknown[], B extends unknown[]> = [...A, ...B];
|
|
34
|
+
|
|
35
|
+
export type RangeTuple<T> = [min: T, max: T];
|
|
36
|
+
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
export type RangeTupleDistributive<T> = T extends any ? [min: T, max: T] : never;
|
|
39
|
+
|
|
40
|
+
export type NumberRangeTuple = RangeTuple<number>;
|
|
41
|
+
|
|
42
|
+
export type KeyValueTuple<K extends PropertyKey = PropertyKey, V = unknown> = [key: K, value: V];
|
|
43
|
+
|
|
44
|
+
export type Predicate<A, T extends A = A> =
|
|
45
|
+
| ((value: T, index: number, array: readonly A[]) => value is T)
|
|
46
|
+
| ((value: T, index: number, array: readonly A[]) => unknown);
|
|
47
|
+
|
|
48
|
+
export type TypePredicateFn<T> = (input: unknown) => input is T;
|