|
1 | 1 | import sha1 from 'js-sha1';
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * Checks if a value is not null. |
| 5 | + * |
| 6 | + * @param value - The value to check. |
| 7 | + * @returns True if the value is not null, otherwise false. |
| 8 | + */ |
3 | 9 | export function isNotNull<T>(value: T | null): value is T {
|
4 | 10 | return value != null;
|
5 | 11 | }
|
6 | 12 |
|
| 13 | +/** |
| 14 | + * Checks if a value is not null, undefined, or void. |
| 15 | + * |
| 16 | + * @param value - The value to check. |
| 17 | + * @returns True if the value is not null, undefined, or void, otherwise false. |
| 18 | + */ |
7 | 19 | export function isSome<T>(
|
8 | 20 | value: T | null | undefined | void
|
9 | 21 | ): value is NonNullable<T> {
|
10 | 22 | return value != null;
|
11 | 23 | }
|
12 | 24 |
|
| 25 | +/** |
| 26 | + * Checks if a value is null, undefined, or void. |
| 27 | + * |
| 28 | + * @param value - The value to check. |
| 29 | + * @returns True if the value is null, undefined, or void, otherwise false. |
| 30 | + */ |
13 | 31 | export function isNone<T>(
|
14 | 32 | value: T | null | undefined | void
|
15 | 33 | ): value is null | undefined | void {
|
16 | 34 | return value == null;
|
17 | 35 | }
|
18 | 36 |
|
| 37 | +/** |
| 38 | + * Checks if a string is numeric. |
| 39 | + * |
| 40 | + * @param value - The string to check. |
| 41 | + * @returns True if the string is numeric, otherwise false. |
| 42 | + */ |
19 | 43 | export function isNumeric(value: string): boolean {
|
20 | 44 | return /-?\d+$/.test(value);
|
21 | 45 | }
|
22 | 46 |
|
| 47 | +/** |
| 48 | + * Generates a SHA-1 hash of the given text. |
| 49 | + * |
| 50 | + * @param text - The text to hash. |
| 51 | + * @returns The SHA-1 hash of the text. |
| 52 | + */ |
23 | 53 | export const hash = (text: string) => sha1.sha1(text);
|
| 54 | + |
| 55 | +/** |
| 56 | + * Executes an array of functions and returns the first result that satisfies the predicate. |
| 57 | + * |
| 58 | + * @param functions - The array of functions to execute. |
| 59 | + * @param predicate - The predicate to test the results. Defaults to checking if the result is not null. |
| 60 | + * @returns The first result that satisfies the predicate, or undefined if no result satisfies the predicate. |
| 61 | + */ |
| 62 | +export async function firstFrom<T>( |
| 63 | + functions: Array<() => T | Promise<T>>, |
| 64 | + predicate: (result: T) => boolean = result => result != null |
| 65 | +): Promise<T | undefined> { |
| 66 | + for (const fn of functions) { |
| 67 | + const result = await fn(); |
| 68 | + if (predicate(result)) { |
| 69 | + return result; |
| 70 | + } |
| 71 | + } |
| 72 | + return undefined; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Lazily executes an array of functions and yields their results. |
| 77 | + * |
| 78 | + * @param functions - The array of functions to execute. |
| 79 | + * @returns A generator yielding the results of the functions. |
| 80 | + */ |
| 81 | +function* lazyExecutor<T>(functions: Array<() => T>): Generator<T> { |
| 82 | + for (const fn of functions) { |
| 83 | + yield fn(); |
| 84 | + } |
| 85 | +} |
0 commit comments