@murky-web/typebuddy 1.1.1 → 1.3.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.
@@ -9,6 +9,28 @@ type Failed<T extends null = null> = {
9
9
  value: T;
10
10
  };
11
11
 
12
- type MaybePromise<T> = Promise<Success<T> | Failed>;
12
+ /**
13
+ * A settled outcome: the value, or the failure.
14
+ *
15
+ * This is what a `MaybePromise` resolves to, and it needed a name of its own.
16
+ * Without one, anything that holds an already-awaited outcome has to restate the
17
+ * union or reach for `Awaited<...>` to undo a wrapper that is no longer there —
18
+ * which describes the route taken rather than the thing being held.
19
+ */
20
+ type Result<T> = Success<T> | Failed;
13
21
 
14
- export type { Failed, MaybePromise, Success };
22
+ type MaybePromise<T> = Promise<Result<T>>;
23
+
24
+ /**
25
+ * `MaybePromise` under a name that says what it is.
26
+ *
27
+ * Elsewhere in the ecosystem `MaybePromise<T>` almost always means
28
+ * `T | Promise<T>` — a value that may or may not be a promise. Here it means a
29
+ * promise of a `Result`, which is a different thing entirely, so a reader who
30
+ * knows the other convention reads this one wrong. Prefer `AsyncResult` in new
31
+ * code. `MaybePromise` keeps working and is what the `prefer-maybe-promise` rule
32
+ * still suggests.
33
+ */
34
+ type AsyncResult<T> = MaybePromise<T>;
35
+
36
+ export type { AsyncResult, Failed, MaybePromise, Result, Success };
@@ -1,309 +0,0 @@
1
- //#region src/type_helper.ts
2
- const EMPTY_LENGTH = 0;
3
- const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
4
- const ulidRegex = /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/;
5
- function cloneDefaultArray(defaultValue) {
6
- if (!defaultValue) return defaultValue;
7
- return [...defaultValue];
8
- }
9
- /**
10
- * Checks if the provided result is a `Success` type.
11
- *
12
- * @template T The type of the value contained in the `Success` type.
13
- * @param {Readonly<Success<T> | Failed>} result The result to check, which can
14
- * be either a `Success<T>` or `Failed`.
15
- * @returns {boolean} `true` if the result is a `Success<T>`, `false` if it is a `Failed`.
16
- * This function acts as a type guard, narrowing the type of `result`
17
- * to `Success<T>` when the condition is met.
18
- * @example
19
- * ```typescript
20
- * const result = await someAsyncFunction();
21
- * if (isSuccess(result)) {
22
- * return result.value; // TypeScript knows `value` is of type `T`
23
- * }
24
- * ```
25
- */
26
- function isSuccess(result) {
27
- return !result.isError;
28
- }
29
- function ok(...args) {
30
- const [value] = args;
31
- return {
32
- isError: false,
33
- value
34
- };
35
- }
36
- function err(...args) {
37
- const [value] = args;
38
- return {
39
- isError: true,
40
- value: value ?? null
41
- };
42
- }
43
- function isString(value) {
44
- return typeof value === "string";
45
- }
46
- /**
47
- * Parses the input value as a boolean. Returns false if the value is no string.
48
- * @param {unknown} value - The value to check.
49
- * @returns {boolean} True if the value is a string.
50
- */
51
- function isEmptyString(value) {
52
- if (!isString(value)) return false;
53
- return value.trim() === "";
54
- }
55
- /**
56
- * Returns true if the value is null.
57
- * @param {unknown} value - The value to check.
58
- * @returns {boolean} True if the value is null.
59
- */
60
- function isNull(value) {
61
- return value === null;
62
- }
63
- /**
64
- * Returns true if the value is undefined.
65
- * @param {unknown} value - The value to check.
66
- * @returns {boolean} True if the value is undefined.
67
- */
68
- function isUndefined(value) {
69
- return typeof value === "undefined";
70
- }
71
- /**
72
- * Checks whether an Optional value is currently in its missing state.
73
- * @param {Optional<T>} value - The Optional value to check.
74
- * @returns {boolean} True when the value is undefined.
75
- */
76
- function isOptional(value) {
77
- return isUndefined(value);
78
- }
79
- /**
80
- * Checks whether a Maybe value is currently in its missing state.
81
- * @param {Maybe<T>} value - The Maybe value to check.
82
- * @returns {boolean} True when the value is null.
83
- */
84
- function isMaybe(value) {
85
- return isNull(value);
86
- }
87
- /**
88
- * Checks whether a Nullable value is currently in its missing state.
89
- * @param {Nullable<T>} value - The Nullable value to check.
90
- * @returns {boolean} True when the value is null or undefined.
91
- */
92
- function isNullable(value) {
93
- return isNull(value) || isUndefined(value);
94
- }
95
- function isArray(value) {
96
- return Array.isArray(value);
97
- }
98
- function isEmptyArray(value) {
99
- return Array.isArray(value) && value.length === EMPTY_LENGTH;
100
- }
101
- function fastIsArray(value) {
102
- return Object.prototype.toString.call(value) === "[object Array]";
103
- }
104
- function isNumber(value) {
105
- if (typeof value === "string" && value.trim() === "") return false;
106
- return typeof value === "number" && !Number.isNaN(value) && Number.isFinite(value);
107
- }
108
- function isObject(value) {
109
- if (typeof value !== "object" || value === null || isArray(value) || Object.prototype.toString.call(value) !== "[object Object]") return false;
110
- return Object.getPrototypeOf(value) === Object.prototype;
111
- }
112
- function isBoolean(value) {
113
- return typeof value === "boolean";
114
- }
115
- function isFunction(value) {
116
- return typeof value === "function";
117
- }
118
- function isPromise(value) {
119
- if (typeof value !== "object" || value === null) return false;
120
- return typeof Reflect.get(value, "then") === "function";
121
- }
122
- function isError(value) {
123
- return value instanceof Error;
124
- }
125
- function isDate(value) {
126
- return value instanceof Date;
127
- }
128
- function isRegExp(value) {
129
- return value instanceof RegExp;
130
- }
131
- function isSymbol(value) {
132
- return typeof value === "symbol";
133
- }
134
- function isEmptyObject(value) {
135
- return typeof value === "object" && !isNull(value) && !isUndefined(value) && !isEmptyArray(value) && Object.getPrototypeOf(value) === Object.prototype && Object.keys(value).length === EMPTY_LENGTH;
136
- }
137
- /**
138
- * Check if a value is an instance of a class.
139
- * @param {unknown} value - The value to check.
140
- * @param {unknown} constructor - The class constructor to check against.
141
- * @returns {boolean} True if the value is an instance of the class.
142
- */
143
- function isInstanceOf(value, constructor) {
144
- return value instanceof constructor;
145
- }
146
- /**
147
- * Get the keys of an object.
148
- * @param {unknown} object - The object to get the keys of.
149
- * @returns {Array} Keys of object.
150
- */
151
- function getKeys(object) {
152
- return Object.keys(object);
153
- }
154
- function parseInteger(value, defaultValue) {
155
- if (isNumber(value)) return Math.floor(value);
156
- if (isString(value)) {
157
- const parsed = Number(value.trim());
158
- if (Number.isInteger(parsed)) return parsed;
159
- }
160
- return defaultValue;
161
- }
162
- /**
163
- * Check if a value is an integer.
164
- * @param {unknown} value - The value to check.
165
- * @returns {boolean} True if the value is an integer.
166
- */
167
- function isInteger(value) {
168
- return typeof value === "number" && Number.isInteger(value);
169
- }
170
- /**
171
- * Check if a value is a float.
172
- * @param {unknown} value - The value to check.
173
- * @returns {boolean} True if the value is a float.
174
- */
175
- function isFloat(value) {
176
- return typeof value === "number" && !Number.isNaN(value) && !Number.isInteger(value);
177
- }
178
- function parseFloat(value, defaultValue) {
179
- if (isNumber(value)) return value;
180
- if (isString(value)) {
181
- const normalizedValue = value.trim().replace(",", ".");
182
- const parsed = Number.parseFloat(normalizedValue);
183
- if (!Number.isNaN(parsed)) return parsed;
184
- }
185
- return defaultValue;
186
- }
187
- function parseNumber(value, defaultValue) {
188
- if (isNumber(value)) return value;
189
- if (isString(value)) {
190
- const normalizedValue = value.trim().replace(",", ".");
191
- const parsed = Number(normalizedValue);
192
- if (Number.isFinite(parsed)) return parsed;
193
- }
194
- return defaultValue;
195
- }
196
- /**
197
- * Parses the input value as a string. Returns an empty string if the
198
- * value cannot be converted.
199
- * @param {unknown} value - The value to check.
200
- * @param {Optional<string>} defaultValue - Value gets returned if string could not
201
- * be parsed.
202
- * @returns {string} The parsed string.
203
- */
204
- function parseString(value, defaultValue = "") {
205
- if (isString(value)) return value;
206
- if (isNumber(value)) return value.toString();
207
- if (typeof value === "boolean") return value.toString();
208
- return defaultValue;
209
- }
210
- /**
211
- * Returns true for values that behave like "empty" application input.
212
- * @param {unknown} value - The value to check.
213
- * @returns {boolean} True if the value is nullish, empty string, empty array,
214
- * false, or a plain object whose values are all empty-like.
215
- */
216
- function isEmptyLike(value) {
217
- if (isNull(value) || isUndefined(value)) return true;
218
- if (isString(value)) return isEmptyString(value);
219
- if (isArray(value)) return value.every((entry) => {
220
- return isEmptyLike(entry);
221
- });
222
- if (isBoolean(value)) return !value;
223
- if (isObject(value)) {
224
- if (Object.getPrototypeOf(value) !== Object.prototype) return false;
225
- return Object.values(value).every((entry) => {
226
- return isEmptyLike(entry);
227
- });
228
- }
229
- return false;
230
- }
231
- /**
232
- * Checks if the provided value contains empty values.
233
- *
234
- * This function determines if the given value is either an empty string,
235
- * an empty object, or a string representation of an empty object.
236
- *
237
- * @param {unknown} value - The value to check for emptiness. It can be of any type.
238
- * @returns {boolean} `true` if the value is an empty string, an empty object, or a string
239
- * representation of an empty object; otherwise, `false`.
240
- */
241
- function hasEmptyValues(value) {
242
- if (isString(value)) {
243
- try {
244
- if (isEmptyObject(JSON.parse(value))) return true;
245
- } catch {
246
- return isEmptyString(value);
247
- }
248
- return isEmptyString(value);
249
- }
250
- return isEmptyObject(value);
251
- }
252
- function parseArray(value, defaultValue) {
253
- if (isArray(value)) return [...value];
254
- if (isString(value)) return value.split(/[,|;\n\t ]+/).map((entry) => {
255
- return entry.trim();
256
- }).filter((entry) => {
257
- return entry.length > EMPTY_LENGTH;
258
- });
259
- if (isNumber(value)) return [value];
260
- if (isEmptyObject(value)) return [value];
261
- if (isNull(value) || isUndefined(value)) return cloneDefaultArray(defaultValue);
262
- return cloneDefaultArray(defaultValue);
263
- }
264
- /**
265
- * Compares two arrays and returns true if they have at least one common value.
266
- * @param {readonly T[]} array1 - First array.
267
- * @param {readonly T[]} array2 - Second array.
268
- * @returns {boolean} True if the arrays have at least one common value.
269
- */
270
- function arrayContainsCommonValue(array1, array2) {
271
- if (!isArray(array1) || !isArray(array2)) return false;
272
- const valueOccurrences = new Set(array1);
273
- return array2.some((value) => {
274
- return valueOccurrences.has(value);
275
- });
276
- }
277
- /**
278
- * Returns true if the input is a UUID string.
279
- * @param {string} input - The input to check.
280
- * @returns {boolean} True if the input is a UUID string.
281
- */
282
- function isUuidString(input) {
283
- return isString(input) && uuidRegex.test(input);
284
- }
285
- /**
286
- * Returns true if the input is a ULID string.
287
- * @param {string }input - The input to check.
288
- * @returns {boolean} True if the input is a ULID string.
289
- */
290
- function isUlidString(input) {
291
- return typeof input === "string" && ulidRegex.test(input);
292
- }
293
- function parseDomainName(url, defaultValue) {
294
- const normalizedValue = url.trim();
295
- if (normalizedValue === "" || normalizedValue.startsWith("/")) return defaultValue;
296
- let urlCandidate = normalizedValue;
297
- if (!normalizedValue.includes("://")) urlCandidate = `https://${normalizedValue}`;
298
- let hostname = "";
299
- try {
300
- ({hostname} = new URL(urlCandidate));
301
- } catch {
302
- return defaultValue;
303
- }
304
- const [domainName] = hostname.replace(/^www\d?\./i, "").split(".");
305
- if (!domainName) return defaultValue;
306
- return domainName;
307
- }
308
- //#endregion
309
- export { arrayContainsCommonValue, err, fastIsArray, getKeys, hasEmptyValues, isArray, isBoolean, isDate, isEmptyArray, isEmptyLike, isEmptyObject, isEmptyString, isError, isFloat, isFunction, isInstanceOf, isInteger, isMaybe, isNull, isNullable, isNumber, isObject, isOptional, isPromise, isRegExp, isString, isSuccess, isSymbol, isUlidString, isUndefined, isUuidString, ok, parseArray, parseDomainName, parseFloat, parseInteger, parseNumber, parseString };