UNPKG

5.85 kBTypeScriptView Raw
1/**
2 * A class constructor accepting arbitrary arguments.
3 */
4export type Constructor<T> = new (...args: any[]) => T;
5export type BoundValue = any;
6/**
7 * Representing a value or promise. This type is used to represent results of
8 * synchronous/asynchronous resolution of values.
9 *
10 * Note that we are using PromiseLike instead of native Promise to describe
11 * the asynchronous variant. This allows producers of async values to use
12 * any Promise implementation (e.g. Bluebird) instead of native Promises
13 * provided by JavaScript runtime.
14 */
15export type ValueOrPromise<T> = T | PromiseLike<T>;
16export type MapObject<T> = Record<string, T>;
17/**
18 * Check whether a value is a Promise-like instance.
19 * Recognizes both native promises and third-party promise libraries.
20 *
21 * @param value - The value to check.
22 */
23export declare function isPromiseLike<T>(value: T | PromiseLike<T> | undefined): value is PromiseLike<T>;
24/**
25 * Get nested properties of an object by path
26 * @param value - Value of the source object
27 * @param path - Path to the property
28 */
29export declare function getDeepProperty<OUT = BoundValue, IN = BoundValue>(value: IN, path: string): OUT | undefined;
30/**
31 * Resolve entries of an object into a new object with the same keys. If one or
32 * more entries of the source object are resolved to a promise by the `resolver`
33 * function, this method returns a promise which will be resolved to the new
34 * object with fully resolved entries.
35 *
36 * @example
37 *
38 * - Example 1: resolve all entries synchronously
39 * ```ts
40 * const result = resolveMap({a: 'x', b: 'y'}, v => v.toUpperCase());
41 * ```
42 * The `result` will be `{a: 'X', b: 'Y'}`.
43 *
44 * - Example 2: resolve one or more entries asynchronously
45 * ```ts
46 * const result = resolveMap({a: 'x', b: 'y'}, v =>
47 * Promise.resolve(v.toUpperCase()),
48 * );
49 * ```
50 * The `result` will be a promise of `{a: 'X', b: 'Y'}`.
51 *
52 * @param map - The original object containing the source entries
53 * @param resolver - A function resolves an entry to a value or promise. It will
54 * be invoked with the property value, the property name, and the source object.
55 */
56export declare function resolveMap<T, V>(map: MapObject<T>, resolver: (val: T, key: string, values: MapObject<T>) => ValueOrPromise<V>): ValueOrPromise<MapObject<V>>;
57/**
58 * Resolve entries of an array into a new array with the same indexes. If one or
59 * more entries of the source array are resolved to a promise by the `resolver`
60 * function, this method returns a promise which will be resolved to the new
61 * array with fully resolved entries.
62 *
63 * @example
64 *
65 * - Example 1: resolve all entries synchronously
66 * ```ts
67 * const result = resolveList(['a', 'b'], v => v.toUpperCase());
68 * ```
69 * The `result` will be `['A', 'B']`.
70 *
71 * - Example 2: resolve one or more entries asynchronously
72 * ```ts
73 * const result = resolveList(['a', 'b'], v =>
74 * Promise.resolve(v.toUpperCase()),
75 * );
76 * ```
77 * The `result` will be a promise of `['A', 'B']`.
78 *
79 * @param list - The original array containing the source entries
80 * @param resolver - A function resolves an entry to a value or promise. It will
81 * be invoked with the property value, the property index, and the source array.
82 */
83export declare function resolveList<T, V>(list: T[], resolver: (val: T, index: number, values: T[]) => ValueOrPromise<V>): ValueOrPromise<V[]>;
84/**
85 * Try to run an action that returns a promise or a value
86 * @param action - A function that returns a promise or a value
87 * @param finalAction - A function to be called once the action
88 * is fulfilled or rejected (synchronously or asynchronously)
89 *
90 * @typeParam T - Type for the return value
91 */
92export declare function tryWithFinally<T>(action: () => ValueOrPromise<T>, finalAction: () => void): ValueOrPromise<T>;
93/**
94 * Try to run an action that returns a promise or a value with error and final
95 * actions to mimic `try {} catch(err) {} finally {}` for a value or promise.
96 *
97 * @param action - A function that returns a promise or a value
98 * @param errorAction - A function to be called once the action
99 * is rejected (synchronously or asynchronously). It must either return a new
100 * value or throw an error.
101 * @param finalAction - A function to be called once the action
102 * is fulfilled or rejected (synchronously or asynchronously)
103 *
104 * @typeParam T - Type for the return value
105 */
106export declare function tryCatchFinally<T>(action: () => ValueOrPromise<T>, errorAction?: (err: unknown) => T | never, finalAction?: () => void): ValueOrPromise<T>;
107/**
108 * Resolve an iterator of source values into a result until the evaluator
109 * returns `true`
110 * @param source - The iterator of source values
111 * @param resolver - The resolve function that maps the source value to a result
112 * @param evaluator - The evaluate function that decides when to stop
113 */
114export declare function resolveUntil<T, V>(source: Iterator<T>, resolver: (sourceVal: T) => ValueOrPromise<V | undefined>, evaluator: (sourceVal: T, targetVal: V | undefined) => boolean): ValueOrPromise<V | undefined>;
115/**
116 * Transform a value or promise with a function that produces a new value or
117 * promise
118 * @param valueOrPromise - The value or promise
119 * @param transformer - A function that maps the source value to a value or promise
120 */
121export declare function transformValueOrPromise<T, V>(valueOrPromise: ValueOrPromise<T>, transformer: (val: T) => ValueOrPromise<V>): ValueOrPromise<V>;
122/**
123 * A utility to generate uuid v4
124 *
125 * @deprecated Use `generateUniqueId`, [uuid](https://www.npmjs.com/package/uuid)
126 * or [hyperid](https://www.npmjs.com/package/hyperid) instead.
127 */
128export declare function uuid(): string;
129/**
130 * A regular expression for testing uuid v4 PATTERN
131 * @deprecated This pattern is an internal helper used by unit-tests, we are no
132 * longer using it.
133 */
134export declare const UUID_PATTERN: RegExp;