@webdeveric/utils 0.19.0 → 0.21.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.
Files changed (48) hide show
  1. package/dist/cjs/type-assertion.js +57 -2
  2. package/dist/cjs/type-assertion.js.map +1 -1
  3. package/dist/cjs/type-predicate-factory.js +21 -0
  4. package/dist/cjs/type-predicate-factory.js.map +1 -0
  5. package/dist/cjs/type-predicate.js +23 -33
  6. package/dist/cjs/type-predicate.js.map +1 -1
  7. package/dist/mjs/type-assertion.js +48 -2
  8. package/dist/mjs/type-assertion.js.map +1 -1
  9. package/dist/mjs/type-predicate-factory.js +12 -0
  10. package/dist/mjs/type-predicate-factory.js.map +1 -0
  11. package/dist/mjs/type-predicate.js +21 -20
  12. package/dist/mjs/type-predicate.js.map +1 -1
  13. package/dist/types/type-assertion.d.ts +11 -0
  14. package/dist/types/type-assertion.d.ts.map +1 -1
  15. package/dist/types/type-predicate-factory.d.ts +8 -0
  16. package/dist/types/type-predicate-factory.d.ts.map +1 -0
  17. package/dist/types/type-predicate.d.ts +22 -14
  18. package/dist/types/type-predicate.d.ts.map +1 -1
  19. package/package.json +22 -21
  20. package/src/asArray.ts +0 -3
  21. package/src/capitalize.ts +0 -1
  22. package/src/clamp.ts +0 -7
  23. package/src/compose.ts +0 -13
  24. package/src/convert.ts +0 -30
  25. package/src/delay.ts +0 -23
  26. package/src/delayAnimationFrame.ts +0 -35
  27. package/src/getMaxValue.ts +0 -16
  28. package/src/getMinValue.ts +0 -16
  29. package/src/getOwnKeys.ts +0 -3
  30. package/src/getOwnProperties.ts +0 -3
  31. package/src/getType.ts +0 -3
  32. package/src/index.ts +0 -24
  33. package/src/isEmpty.ts +0 -21
  34. package/src/isObject.ts +0 -3
  35. package/src/joinStrings.ts +0 -6
  36. package/src/node/fcrdns.ts +0 -37
  37. package/src/node/index.ts +0 -1
  38. package/src/normalize.ts +0 -102
  39. package/src/secToString.ts +0 -45
  40. package/src/toPascalCase.ts +0 -29
  41. package/src/trimEnd.ts +0 -5
  42. package/src/trimIndentation.ts +0 -18
  43. package/src/trimStart.ts +0 -5
  44. package/src/type-assertion.ts +0 -319
  45. package/src/type-predicate.ts +0 -101
  46. package/src/types.ts +0 -48
  47. package/src/uniqueItems.ts +0 -3
  48. package/src/until.ts +0 -130
@@ -1,319 +0,0 @@
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';
33
-
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);
73
- }
74
- }
75
-
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);
82
- }
83
- }
84
-
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[] {
131
- if (!isNumericValueArray(input)) {
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);
187
- }
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
- }
@@ -1,101 +0,0 @@
1
- import type { NumericString, NumericValue, Primitive, StringRecord, TypePredicateFn, UnknownRecord } from './types.js';
2
-
3
- export const createIsEnumPredicate = <E extends Record<string, unknown>, T = E>(enumObject: E): TypePredicateFn<T> => {
4
- const values = Object.values(enumObject);
5
-
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;
57
-
58
- export const isStringMatching = (input: unknown, pattern: RegExp): input is string =>
59
- typeof input === 'string' && pattern.test(input);
60
-
61
- export const isNumericString = (input: unknown): input is NumericString =>
62
- isStringMatching(input, /^[-+]?\d+(\.\d+)?$/);
63
-
64
- export const isNumericValue = (input: unknown): input is NumericValue =>
65
- typeof input === 'number' || typeof input === 'bigint' || isNumericString(input);
66
-
67
- export const isNumericValueArray = (input: unknown): input is NumericValue[] =>
68
- Array.isArray(input) && input.every(isNumericValue);
69
-
70
- export const isIntString = (input: unknown): input is NumericString => isStringMatching(input, /^[-+]?\d+$/);
71
-
72
- export const isFiniteNumber = (input: unknown): input is number => Number.isFinite(input);
73
-
74
- export const isInteger = (input: unknown): input is number => Number.isInteger(input);
75
-
76
- export const isPositiveInteger = (input: unknown): input is number => isInteger(input) && input >= 0;
77
-
78
- export const isPositiveFiniteNumber = (input: unknown): input is number => isFiniteNumber(input) && input >= 0;
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
-
88
- export const isLengthAware = (input: unknown): input is { length: number } =>
89
- input !== null && typeof input === 'object' && 'length' in input && typeof input.length === 'number';
90
-
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 DELETED
@@ -1,48 +0,0 @@
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;
5
-
6
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
- export type AnyRecord = Record<PropertyKey, any>;
8
-
9
- export type StringRecord = Record<string, string>;
10
-
11
- export type UnknownRecord = Record<PropertyKey, unknown>;
12
-
13
- export type NumericString<T extends string | number = number> = `${T}` extends `${number}` ? `${T}` : never;
14
-
15
- export type NumericValue = number | bigint | NumericString;
16
-
17
- export type MaybePlural<T extends string> = T | `${T}s`;
18
-
19
- export type StringKeys<T> = keyof T & string;
20
-
21
- export type IfDefined<T, D, U> = undefined extends T ? U : D;
22
-
23
- export type IfNever<T, N, O> = [T] extends [never] ? N : O;
24
-
25
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- export type ReturnTypeDefault<F, D = undefined> = F extends (...args: any) => infer R ? R : D;
27
-
28
- export type AnyNewable = {
29
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
- new (...args: any[]): any;
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;
@@ -1,3 +0,0 @@
1
- export function uniqueItems<T>(data: T[]): T[] {
2
- return [...new Set<T>(data)];
3
- }
package/src/until.ts DELETED
@@ -1,130 +0,0 @@
1
- import type { AnyRecord } from './types.js';
2
- import { getType } from './getType.js';
3
-
4
- export interface UntilContext {
5
- callCount: number;
6
- lastCall: number | undefined;
7
- data: AnyRecord;
8
- options: UntilOptions;
9
- }
10
-
11
- export type DelayFn = (context: Readonly<UntilContext>) => number;
12
-
13
- export type Delay = DelayFn | number;
14
-
15
- export type UntilOptions = {
16
- delay: Delay;
17
- timeout?: Delay;
18
- callLimit?: number;
19
- };
20
-
21
- export type ResolverValue<T> = T | PromiseLike<T>;
22
-
23
- export type ResolveFn<T> = (value: ResolverValue<T>) => void;
24
-
25
- export type RejectFn = (error?: Error) => void;
26
-
27
- export type UntilCallback<T> = (resolve: ResolveFn<T>, reject: RejectFn, context: Readonly<UntilContext>) => void;
28
-
29
- const defaultOptions: Readonly<UntilOptions> = {
30
- delay: 10,
31
- };
32
-
33
- export const isDelay = (input: unknown): input is Delay => Number.isInteger(input) || typeof input === 'function';
34
-
35
- /**
36
- * Return a Promise that delegates resolving/rejecting to the passed in function.
37
- */
38
- export function until<T>(fn: UntilCallback<T>, options: UntilOptions = defaultOptions): Promise<T> {
39
- if (typeof fn !== 'function') {
40
- return Promise.reject(new Error('fn must be a function'));
41
- }
42
-
43
- if (!options || typeof options !== 'object') {
44
- return Promise.reject(new TypeError(`options must be an object. ${getType(options)} provided.`));
45
- }
46
-
47
- const { delay, timeout, callLimit } = Object.assign({}, defaultOptions, options);
48
-
49
- const getDelay = (delay: Delay, context: UntilContext): number =>
50
- typeof delay === 'function' ? delay(context) : delay;
51
-
52
- if (!isDelay(delay)) {
53
- return Promise.reject(new TypeError('invalid delay value'));
54
- }
55
-
56
- if (timeout !== undefined && !isDelay(timeout)) {
57
- return Promise.reject(new TypeError('invalid timeout value'));
58
- }
59
-
60
- if (callLimit !== undefined && !Number.isInteger(callLimit)) {
61
- return Promise.reject(new TypeError('invalid callLimit value'));
62
- }
63
-
64
- return new Promise<T>((resolutionFn: ResolveFn<T>, rejectionFn: RejectFn) => {
65
- let callCount = 0;
66
- let lastCall: number | undefined;
67
- let done = false;
68
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
- let timer: any;
70
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
71
- let timeoutTimer: any;
72
-
73
- const finish = (): void => {
74
- done = true;
75
- clearTimeout(timer);
76
- clearTimeout(timeoutTimer);
77
- };
78
-
79
- const resolve: ResolveFn<T> = (value: ResolverValue<T>): void => {
80
- resolutionFn(value);
81
- finish();
82
- };
83
-
84
- const reject: RejectFn = (error?: Error): void => {
85
- rejectionFn(error);
86
- finish();
87
- };
88
-
89
- const context: Readonly<UntilContext> = Object.freeze({
90
- get callCount() {
91
- return callCount;
92
- },
93
- get lastCall() {
94
- return lastCall;
95
- },
96
- get options() {
97
- return options;
98
- },
99
- data: {},
100
- });
101
-
102
- const callUntilDone = (fn: UntilCallback<T>, delay: Delay, resolve: ResolveFn<T>, reject: RejectFn): void => {
103
- try {
104
- if (callLimit && callCount >= callLimit) {
105
- throw new Error(`until callLimit reached: ${callLimit}`);
106
- }
107
-
108
- ++callCount;
109
-
110
- fn(resolve, reject, context);
111
-
112
- lastCall = Date.now();
113
-
114
- if (!done) {
115
- timer = setTimeout(callUntilDone, getDelay(delay, context), fn, delay, resolve, reject);
116
- }
117
- } catch (error) {
118
- reject(error instanceof Error ? error : new Error(`${error}`));
119
- }
120
- };
121
-
122
- callUntilDone(fn, delay, resolve, reject);
123
-
124
- if (!done && timeout !== undefined) {
125
- timeoutTimer = setTimeout(() => {
126
- reject(new Error('until: timed out'));
127
- }, getDelay(timeout, context));
128
- }
129
- });
130
- }