@oscarpalmer/atoms 0.141.2 → 0.143.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/atoms.full.js +185 -72
- package/dist/color/misc/is.js +6 -6
- package/dist/function/memoize.js +1 -1
- package/dist/index.js +7 -6
- package/dist/internal/math/aggregate.js +12 -8
- package/dist/internal/number.js +1 -1
- package/dist/internal/value/partial.js +14 -0
- package/dist/is.js +11 -3
- package/dist/math.js +21 -5
- package/dist/promise.js +107 -36
- package/dist/result.js +7 -5
- package/dist/sized/map.js +1 -1
- package/dist/sized/set.js +2 -2
- package/dist/value/misc.js +3 -2
- package/dist/value/omit.js +11 -0
- package/dist/value/pick.js +11 -0
- package/package.json +5 -5
- package/src/color/misc/is.ts +6 -6
- package/src/function/debounce.ts +2 -2
- package/src/function/memoize.ts +1 -1
- package/src/function/throttle.ts +2 -2
- package/src/index.ts +2 -1
- package/src/internal/function/limiter.ts +3 -3
- package/src/internal/math/aggregate.ts +19 -10
- package/src/internal/number.ts +1 -1
- package/src/internal/value/partial.ts +46 -0
- package/src/is.ts +11 -2
- package/src/math.ts +67 -9
- package/src/models.ts +2 -2
- package/src/promise.ts +249 -57
- package/src/random.ts +2 -2
- package/src/result.ts +31 -11
- package/src/sized/map.ts +1 -1
- package/src/sized/set.ts +2 -2
- package/src/value/get-set.ts +1 -1
- package/src/value/merge.ts +1 -1
- package/src/value/misc.ts +2 -1
- package/src/value/omit.ts +19 -0
- package/src/value/pick.ts +19 -0
- package/types/color/misc/is.d.ts +6 -6
- package/types/function/debounce.d.ts +2 -2
- package/types/function/memoize.d.ts +1 -1
- package/types/function/throttle.d.ts +2 -2
- package/types/index.d.ts +2 -1
- package/types/internal/function/limiter.d.ts +2 -2
- package/types/internal/math/aggregate.d.ts +1 -0
- package/types/internal/number.d.ts +1 -1
- package/types/internal/value/partial.d.ts +2 -0
- package/types/is.d.ts +8 -2
- package/types/math.d.ts +20 -0
- package/types/models.d.ts +2 -2
- package/types/promise.d.ts +68 -11
- package/types/random.d.ts +2 -2
- package/types/result.d.ts +21 -6
- package/types/sized/map.d.ts +1 -1
- package/types/sized/set.d.ts +2 -2
- package/types/value/misc.d.ts +2 -1
- package/types/value/omit.d.ts +8 -0
- package/types/value/{partial.d.ts → pick.d.ts} +1 -1
- package/dist/value/partial.js +0 -17
- package/src/value/partial.ts +0 -39
package/src/result.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {isPlainObject} from './internal/is';
|
|
2
|
+
import {attemptPromise} from './promise';
|
|
2
3
|
|
|
3
4
|
// #region Types
|
|
4
5
|
|
|
@@ -158,16 +159,16 @@ export function ok<Value>(value: Value): Ok<Value> {
|
|
|
158
159
|
* @param error Error value
|
|
159
160
|
* @returns Callback result
|
|
160
161
|
*/
|
|
161
|
-
export function
|
|
162
|
+
export function attempt<Value, E>(callback: () => Value, error: E): ExtendedErr<E> | Ok<Value>;
|
|
162
163
|
|
|
163
164
|
/**
|
|
164
165
|
* Executes a callback, catching any errors, and returns a result
|
|
165
166
|
* @param callback Callback to execute
|
|
166
167
|
* @returns Callback result
|
|
167
168
|
*/
|
|
168
|
-
export function
|
|
169
|
+
export function attempt<Value>(callback: () => Value): Result<Value, Error>;
|
|
169
170
|
|
|
170
|
-
export function
|
|
171
|
+
export function attempt<Value, E>(
|
|
171
172
|
callback: () => Value,
|
|
172
173
|
err?: E,
|
|
173
174
|
): ExtendedErr<E> | Result<Value, E> {
|
|
@@ -180,7 +181,19 @@ export function result<Value, E>(
|
|
|
180
181
|
}
|
|
181
182
|
}
|
|
182
183
|
|
|
183
|
-
|
|
184
|
+
attempt.async = asyncAttempt;
|
|
185
|
+
attempt.promise = attemptPromise;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Executes a promise, catching any errors, and returns a result
|
|
189
|
+
* @param promise Promise to execute
|
|
190
|
+
* @param error Error value
|
|
191
|
+
* @returns Callback result
|
|
192
|
+
*/
|
|
193
|
+
async function asyncAttempt<Value, E>(
|
|
194
|
+
promise: Promise<Value>,
|
|
195
|
+
error: E,
|
|
196
|
+
): Promise<ExtendedErr<E> | Ok<Awaited<Value>>>;
|
|
184
197
|
|
|
185
198
|
/**
|
|
186
199
|
* Executes a callback asynchronously, catching any errors, and returns a result
|
|
@@ -188,26 +201,33 @@ result.async = asyncResult;
|
|
|
188
201
|
* @param error Error value
|
|
189
202
|
* @returns Callback result
|
|
190
203
|
*/
|
|
191
|
-
async function
|
|
204
|
+
async function asyncAttempt<Value, E>(
|
|
192
205
|
callback: () => Promise<Value>,
|
|
193
206
|
error: E,
|
|
194
207
|
): Promise<ExtendedErr<E> | Ok<Value>>;
|
|
195
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Executes a promise, catching any errors, and returns a result
|
|
211
|
+
* @param promise Promise to execute
|
|
212
|
+
* @returns Callback result
|
|
213
|
+
*/
|
|
214
|
+
async function asyncAttempt<Value>(promise: Promise<Value>): Promise<Awaited<Value>>;
|
|
215
|
+
|
|
196
216
|
/**
|
|
197
217
|
* Executes a callback asynchronously, catching any errors, and returns a result
|
|
198
218
|
* @param callback Callback to execute
|
|
199
219
|
* @returns Callback result
|
|
200
220
|
*/
|
|
201
|
-
async function
|
|
221
|
+
async function asyncAttempt<Value>(callback: () => Promise<Value>): Promise<Result<Value>>;
|
|
202
222
|
|
|
203
|
-
async function
|
|
204
|
-
|
|
223
|
+
async function asyncAttempt<Value, E>(
|
|
224
|
+
value: Promise<Value> | (() => Promise<Value>),
|
|
205
225
|
err?: E,
|
|
206
|
-
): Promise<
|
|
226
|
+
): Promise<unknown> {
|
|
207
227
|
try {
|
|
208
|
-
const
|
|
228
|
+
const result = await (typeof value === 'function' ? value() : value);
|
|
209
229
|
|
|
210
|
-
return ok(
|
|
230
|
+
return ok(result);
|
|
211
231
|
} catch (thrown) {
|
|
212
232
|
return getError((err ?? thrown) as E, err == null ? undefined : (thrown as Error));
|
|
213
233
|
}
|
package/src/sized/map.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {getSizedMaximum} from '../internal/sized';
|
|
|
5
5
|
/**
|
|
6
6
|
* A Map with a maximum size
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* Behavior is similar to a _LRU_-cache, where the least recently used entries are removed
|
|
9
9
|
*/
|
|
10
10
|
export class SizedMap<Key = unknown, Value = unknown> extends Map<Key, Value> {
|
|
11
11
|
/**
|
package/src/sized/set.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {getSizedMaximum} from '../internal/sized';
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* - A Set with a maximum size
|
|
7
|
-
* -
|
|
7
|
+
* - Behavior is similar to a _LRU_-cache, where the oldest values are removed
|
|
8
8
|
*/
|
|
9
9
|
export class SizedSet<Value = unknown> extends Set<Value> {
|
|
10
10
|
/**
|
|
@@ -84,7 +84,7 @@ export class SizedSet<Value = unknown> extends Set<Value> {
|
|
|
84
84
|
* Get a value from the SizedSet, if it exists _(and move it to the end)_
|
|
85
85
|
* @param value Value to get from the SizedSet
|
|
86
86
|
* @param update Update the value's position in the SizedSet? _(defaults to `false`)_
|
|
87
|
-
* @returns
|
|
87
|
+
* @returns Found value if it exists, otherwise `undefined`
|
|
88
88
|
*/
|
|
89
89
|
get(value: Value, update?: boolean): Value | undefined {
|
|
90
90
|
if (this.has(value)) {
|
package/src/value/get-set.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {getValue} from '../internal/value/get';
|
|
2
|
-
export {setValue} from '../internal/value/set';
|
|
2
|
+
export {setValue} from '../internal/value/set';
|
package/src/value/merge.ts
CHANGED
|
@@ -101,7 +101,7 @@ function initializeMerger(options?: Partial<MergeOptions>): Merger {
|
|
|
101
101
|
|
|
102
102
|
return <Model extends ArrayOrPlainObject>(values: NestedPartial<Model>[]): Model =>
|
|
103
103
|
handleMerge(values, actual) as Model;
|
|
104
|
-
}
|
|
104
|
+
}
|
|
105
105
|
|
|
106
106
|
function mergeObjects(
|
|
107
107
|
values: ArrayOrPlainObject[],
|
package/src/value/misc.ts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {partial} from '../internal/value/partial';
|
|
2
|
+
import type {PlainObject} from '../models';
|
|
3
|
+
|
|
4
|
+
// #region Functions
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a new object without the specified keys
|
|
8
|
+
* @param value Original object
|
|
9
|
+
* @param keys Keys to omit
|
|
10
|
+
* @returns Partial object without the specified keys
|
|
11
|
+
*/
|
|
12
|
+
export function omit<Value extends PlainObject, Key extends keyof Value>(
|
|
13
|
+
value: Value,
|
|
14
|
+
keys: Key[],
|
|
15
|
+
): Omit<Value, Key> {
|
|
16
|
+
return partial(value, keys, true);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// #endregion
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {partial} from '../internal/value/partial';
|
|
2
|
+
import type {PlainObject} from '../models';
|
|
3
|
+
|
|
4
|
+
// #region Functions
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a new object with only the specified keys
|
|
8
|
+
* @param value Original object
|
|
9
|
+
* @param keys Keys to use
|
|
10
|
+
* @returns Partial object with only the specified keys
|
|
11
|
+
*/
|
|
12
|
+
export function pick<Value extends PlainObject, Key extends keyof Value>(
|
|
13
|
+
value: Value,
|
|
14
|
+
keys: Key[],
|
|
15
|
+
): Pick<Value, Key> {
|
|
16
|
+
return partial(value, keys, false);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// #endregion
|
package/types/color/misc/is.d.ts
CHANGED
|
@@ -2,39 +2,39 @@ import type { Color } from '../index';
|
|
|
2
2
|
import type { HSLAColor, HSLColor, RGBAColor, RGBColor } from '../models';
|
|
3
3
|
/**
|
|
4
4
|
* Is the value a Color?
|
|
5
|
-
* @param value
|
|
5
|
+
* @param value Value to check
|
|
6
6
|
* @returns `true` if the value is a Color, otherwise `false`
|
|
7
7
|
*/
|
|
8
8
|
export declare function isColor(value: unknown): value is Color;
|
|
9
9
|
/**
|
|
10
10
|
* Is the value a hex color?
|
|
11
|
-
* @param value
|
|
11
|
+
* @param value Value to check
|
|
12
12
|
* @param alpha Allow alpha channel? _(defaults to `true`)_
|
|
13
13
|
* @returns `true` if the value is a hex color, otherwise `false`
|
|
14
14
|
*/
|
|
15
15
|
export declare function isHexColor(value: unknown, alpha?: boolean): value is string;
|
|
16
16
|
/**
|
|
17
17
|
* Is the value an HSLA color?
|
|
18
|
-
* @param value
|
|
18
|
+
* @param value Value to check
|
|
19
19
|
* @returns `true` if the value is an HSLA color, otherwise `false`
|
|
20
20
|
*/
|
|
21
21
|
export declare function isHslaColor(value: unknown): value is HSLAColor;
|
|
22
22
|
/**
|
|
23
23
|
* Is the value an HSL color?
|
|
24
|
-
* @param value
|
|
24
|
+
* @param value Value to check
|
|
25
25
|
* @returns `true` if the value is an HSL color, otherwise `false`
|
|
26
26
|
*/
|
|
27
27
|
export declare function isHslColor(value: unknown): value is HSLColor;
|
|
28
28
|
export declare function isHslLike(value: unknown): value is Record<keyof HSLColor, unknown>;
|
|
29
29
|
/**
|
|
30
30
|
* Is the value an RGBA color?
|
|
31
|
-
* @param value
|
|
31
|
+
* @param value Value to check
|
|
32
32
|
* @returns `true` if the value is an RGBA color, otherwise `false`
|
|
33
33
|
*/
|
|
34
34
|
export declare function isRgbaColor(value: unknown): value is RGBAColor;
|
|
35
35
|
/**
|
|
36
36
|
* Is the value an RGB color?
|
|
37
|
-
* @param value
|
|
37
|
+
* @param value Value to check
|
|
38
38
|
* @returns `true` if the value is an RGB color, otherwise `false`
|
|
39
39
|
*/
|
|
40
40
|
export declare function isRgbColor(value: unknown): value is RGBColor;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CancelableCallback, GenericCallback } from '../models';
|
|
2
2
|
/**
|
|
3
3
|
* Debounce a function, ensuring it is only called after `time` milliseconds have passed
|
|
4
4
|
*
|
|
@@ -7,4 +7,4 @@ import type { CancellableCallback, GenericCallback } from '../models';
|
|
|
7
7
|
* @param time Time in milliseconds to wait before calling the callback _(defaults to match frame rate)_
|
|
8
8
|
* @returns Debounced callback with a `cancel` method
|
|
9
9
|
*/
|
|
10
|
-
export declare function debounce<Callback extends GenericCallback>(callback: Callback, time?: number):
|
|
10
|
+
export declare function debounce<Callback extends GenericCallback>(callback: Callback, time?: number): CancelableCallback<Callback>;
|
|
@@ -27,7 +27,7 @@ declare class Memoized<Callback extends GenericCallback> {
|
|
|
27
27
|
/**
|
|
28
28
|
* Get a result from the cache
|
|
29
29
|
* @param key Key to get
|
|
30
|
-
* @returns
|
|
30
|
+
* @returns Cached result or `undefined` if it does not exist
|
|
31
31
|
*/
|
|
32
32
|
get(key: unknown): ReturnType<Callback> | undefined;
|
|
33
33
|
/**
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CancelableCallback, GenericCallback } from '../models';
|
|
2
2
|
/**
|
|
3
3
|
* Throttle a function, ensuring it is only called once every `time` milliseconds
|
|
4
4
|
* @param callback Callback to throttle
|
|
5
5
|
* @param time Time in milliseconds to wait before calling the callback again _(defaults to match frame rate)_
|
|
6
6
|
* @returns Throttled callback with a `cancel` method
|
|
7
7
|
*/
|
|
8
|
-
export declare function throttle<Callback extends GenericCallback>(callback: Callback, time?: number):
|
|
8
|
+
export declare function throttle<Callback extends GenericCallback>(callback: Callback, time?: number): CancelableCallback<Callback>;
|
package/types/index.d.ts
CHANGED
|
@@ -33,7 +33,8 @@ export * from './string/template';
|
|
|
33
33
|
export * from './value/clone';
|
|
34
34
|
export * from './value/diff';
|
|
35
35
|
export * from './value/merge';
|
|
36
|
-
export * from './value/
|
|
36
|
+
export * from './value/omit';
|
|
37
|
+
export * from './value/pick';
|
|
37
38
|
export * from './value/smush';
|
|
38
39
|
export * from './value/unsmush';
|
|
39
40
|
export * from './beacon';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CancelableCallback } from '../../models';
|
|
2
2
|
import type { GenericCallback } from '../../models';
|
|
3
|
-
export declare function getLimiter<Callback extends GenericCallback>(callback: Callback, throttler: boolean, time?: number):
|
|
3
|
+
export declare function getLimiter<Callback extends GenericCallback>(callback: Callback, throttler: boolean, time?: number): CancelableCallback<Callback>;
|
|
@@ -5,6 +5,7 @@ type Aggregation = {
|
|
|
5
5
|
};
|
|
6
6
|
export type AggregationType = 'average' | 'max' | 'min' | 'sum';
|
|
7
7
|
export declare function aggregate(type: AggregationType, array: unknown[], key: unknown): Aggregation;
|
|
8
|
+
export declare function getAggregateCallback(key: unknown): Function | undefined;
|
|
8
9
|
/**
|
|
9
10
|
* Get the maximum value from a list of items
|
|
10
11
|
* @param items List of items
|
|
@@ -18,6 +18,6 @@ export declare function clamp(value: number, minimum: number, maximum: number, l
|
|
|
18
18
|
/**
|
|
19
19
|
* Get the number value from an unknown value _(based on Lodash)_
|
|
20
20
|
* @param value Original value
|
|
21
|
-
* @returns
|
|
21
|
+
* @returns Original value as a number, or `NaN` if the value is unable to be parsed
|
|
22
22
|
*/
|
|
23
23
|
export declare function getNumber(value: unknown): number;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare function partial<Value extends object, Key extends keyof Value>(value: unknown, keys: Key[], omit: true): Omit<Value, Key>;
|
|
2
|
+
export declare function partial<Value extends object, Key extends keyof Value>(value: unknown, keys: Key[], omit: false): Pick<Value, Key>;
|
package/types/is.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import type { Primitive } from './models';
|
|
2
2
|
/**
|
|
3
3
|
* Is the value empty, or only containing `null` or `undefined` values?
|
|
4
|
-
* @param value
|
|
5
|
-
* @returns `true` if the
|
|
4
|
+
* @param value Value to check
|
|
5
|
+
* @returns `true` if the value is considered empty, otherwise `false`
|
|
6
6
|
*/
|
|
7
7
|
export declare function isEmpty(value: unknown): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Is the value not `undefined` or `null`?
|
|
10
|
+
* @param value Value to check
|
|
11
|
+
* @returns `true` if the value is not `undefined` or `null`, otherwise `false`
|
|
12
|
+
*/
|
|
13
|
+
export declare function isNonNullable(value: unknown): value is Exclude<unknown, undefined | null>;
|
|
8
14
|
/**
|
|
9
15
|
* Is the value `undefined` or `null`?
|
|
10
16
|
* @param value Value to check
|
package/types/math.d.ts
CHANGED
|
@@ -41,6 +41,26 @@ export declare function count<Item extends PlainObject>(array: Item[], key: keyo
|
|
|
41
41
|
* @return Number of items, or `NaN` if no count can be calculated
|
|
42
42
|
*/
|
|
43
43
|
export declare function count(values: unknown[]): number;
|
|
44
|
+
/**
|
|
45
|
+
* Get the median value from a list of items
|
|
46
|
+
* @param array List of items
|
|
47
|
+
* @param callback Callback to get an item's value
|
|
48
|
+
* @returns Median value, or `NaN` if no median can be calculated
|
|
49
|
+
*/
|
|
50
|
+
export declare function median<Item>(array: Item[], callback: (item: Item, index: number, array: Item[]) => number): number;
|
|
51
|
+
/**
|
|
52
|
+
* Get the median value from a list of items
|
|
53
|
+
* @param array List of items
|
|
54
|
+
* @param key Key to use for value
|
|
55
|
+
* @returns Median value, or `NaN` if no median can be calculated
|
|
56
|
+
*/
|
|
57
|
+
export declare function median<Item extends PlainObject>(array: Item[], key: keyof NumericalValues<Item>): number;
|
|
58
|
+
/**
|
|
59
|
+
* Get the median value from a list of numbers
|
|
60
|
+
* @param array List of numbers
|
|
61
|
+
* @returns Median value, or `NaN` if no median can be calculated
|
|
62
|
+
*/
|
|
63
|
+
export declare function median(array: number[]): number;
|
|
44
64
|
/**
|
|
45
65
|
* Get the minimum value from a list of items
|
|
46
66
|
* @param items List of items
|
package/types/models.d.ts
CHANGED
|
@@ -9,9 +9,9 @@ export type ArrayOrPlainObject = unknown[] | Record<PropertyKey, unknown>;
|
|
|
9
9
|
*/
|
|
10
10
|
export type BuiltIns = void | Date | Primitive | RegExp;
|
|
11
11
|
/**
|
|
12
|
-
* An extend callback that can be
|
|
12
|
+
* An extend callback that can be canceled
|
|
13
13
|
*/
|
|
14
|
-
export type
|
|
14
|
+
export type CancelableCallback<Callback extends GenericCallback> = Callback & {
|
|
15
15
|
/**
|
|
16
16
|
* Cancel the callback
|
|
17
17
|
*/
|
package/types/promise.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import type { RequiredKeys } from './models';
|
|
2
|
+
export declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void);
|
|
5
|
+
/**
|
|
6
|
+
* Cancel the promise, rejecting it with an optional reason
|
|
7
|
+
* @param reason Optional reason for canceling the promise
|
|
8
|
+
*/
|
|
9
|
+
cancel(reason?: unknown): void;
|
|
10
|
+
}
|
|
1
11
|
type FulfilledPromiseResult<Value> = {
|
|
2
12
|
status: typeof TYPE_FULFILLED;
|
|
3
13
|
value: Value;
|
|
@@ -12,6 +22,10 @@ type PromiseOptions = {
|
|
|
12
22
|
*/
|
|
13
23
|
time?: number;
|
|
14
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Promise handling strategy
|
|
27
|
+
*/
|
|
28
|
+
export type PromiseStrategy = 'complete' | 'first';
|
|
15
29
|
export declare class PromiseTimeoutError extends Error {
|
|
16
30
|
constructor();
|
|
17
31
|
}
|
|
@@ -19,27 +33,33 @@ type Promises<Items extends unknown[]> = {
|
|
|
19
33
|
[K in keyof Items]: Promise<Items[K]>;
|
|
20
34
|
};
|
|
21
35
|
type PromisesOptions = {
|
|
22
|
-
eager?: boolean;
|
|
23
36
|
signal?: AbortSignal;
|
|
37
|
+
strategy?: PromiseStrategy;
|
|
24
38
|
};
|
|
25
39
|
type PromisesResult<Items extends unknown[]> = {
|
|
26
|
-
[K in keyof Items]:
|
|
40
|
+
[K in keyof Items]: Items[K] extends Promise<infer Value> ? PromisesResultItem<Value> : never;
|
|
27
41
|
};
|
|
28
42
|
type PromisesResultItem<Value> = FulfilledPromiseResult<Value> | RejectedPromiseResult;
|
|
29
43
|
type RejectedPromiseResult = {
|
|
30
44
|
status: typeof TYPE_REJECTED;
|
|
31
45
|
reason: unknown;
|
|
32
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Create a cancelable promise
|
|
49
|
+
* @param executor Executor function for the promise
|
|
50
|
+
* @returns Cancelable promise
|
|
51
|
+
*/
|
|
52
|
+
export declare function cancelable<Value>(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void): CancelablePromise<Value>;
|
|
33
53
|
/**
|
|
34
54
|
* Create a delayed promise that resolves after a certain amount of time, or rejects if aborted
|
|
35
55
|
* @param options Options for the delay
|
|
36
|
-
* @returns
|
|
56
|
+
* @returns Delayed promise
|
|
37
57
|
*/
|
|
38
58
|
export declare function delay(options?: PromiseOptions): Promise<void>;
|
|
39
59
|
/**
|
|
40
60
|
* Create a delayed promise that resolves after a certain amount of time
|
|
41
61
|
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
42
|
-
* @returns
|
|
62
|
+
* @returns Delayed promise
|
|
43
63
|
*/
|
|
44
64
|
export declare function delay(time?: number): Promise<void>;
|
|
45
65
|
/**
|
|
@@ -55,19 +75,23 @@ export declare function isFulfilled<Value>(value: unknown): value is FulfilledPr
|
|
|
55
75
|
*/
|
|
56
76
|
export declare function isRejected(value: unknown): value is RejectedPromiseResult;
|
|
57
77
|
/**
|
|
58
|
-
*
|
|
78
|
+
* Handle a list of promises, returning their results in an ordered array.
|
|
79
|
+
*
|
|
80
|
+
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
59
81
|
* @param items List of promises
|
|
60
82
|
* @param options Options for handling the promises
|
|
61
83
|
* @return List of results
|
|
62
84
|
*/
|
|
63
|
-
export declare function promises<Items extends unknown[], Options extends PromisesOptions>(items: Promises<Items>, options?: Options): Promise<Options['
|
|
85
|
+
export declare function promises<Items extends unknown[], Options extends PromisesOptions>(items: Promises<Items>, options?: Options): Promise<Options['strategy'] extends 'first' ? Items : PromisesResult<Items>>;
|
|
64
86
|
/**
|
|
65
|
-
* Handle a list of promises, returning their results in an ordered array.
|
|
87
|
+
* Handle a list of promises, returning their results in an ordered array.
|
|
88
|
+
*
|
|
89
|
+
* If any promise in the list is rejected, the whole function will reject
|
|
66
90
|
* @param items List of promises
|
|
67
|
-
* @param
|
|
91
|
+
* @param strategy Strategy for handling the promises; rejects on the first error encountered
|
|
68
92
|
* @return List of results
|
|
69
93
|
*/
|
|
70
|
-
export declare function promises<Items extends unknown[]>(items: Promises<Items>,
|
|
94
|
+
export declare function promises<Items extends unknown[]>(items: Promises<Items>, strategy: 'first'): Promise<Items>;
|
|
71
95
|
/**
|
|
72
96
|
* Handle a list of promises, returning their results in an ordered array of rejected and resolved results
|
|
73
97
|
* @param items List of promises
|
|
@@ -75,8 +99,41 @@ export declare function promises<Items extends unknown[]>(items: Promises<Items>
|
|
|
75
99
|
* @return List of results
|
|
76
100
|
*/
|
|
77
101
|
export declare function promises<Items extends unknown[]>(items: Promises<Items>, signal?: AbortSignal): Promise<PromisesResult<Items>>;
|
|
78
|
-
|
|
79
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Create a promise that should be settled within a certain amount of time
|
|
104
|
+
* @param promise Promise to settle
|
|
105
|
+
* @param options Timed options
|
|
106
|
+
* @returns Timed promise
|
|
107
|
+
*/
|
|
108
|
+
export declare function timed<Value>(promise: Promise<Value>, options: RequiredKeys<PromiseOptions, 'time'>): Promise<Value>;
|
|
109
|
+
/**
|
|
110
|
+
* Create a promise that should be settled within a certain amount of time
|
|
111
|
+
* @param promise Promise to settle
|
|
112
|
+
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
113
|
+
* @returns Timed promise
|
|
114
|
+
*/
|
|
115
|
+
export declare function timed<Value>(promise: Promise<Value>, time: number): Promise<Value>;
|
|
116
|
+
/**
|
|
117
|
+
* Wrap a promise with safety handlers, with optional abort capabilities and timeout
|
|
118
|
+
* @param promise Promise to wrap
|
|
119
|
+
* @param options Options for the promise
|
|
120
|
+
* @returns Wrapped promise
|
|
121
|
+
*/
|
|
122
|
+
export declare function attemptPromise<Value>(promise: Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
123
|
+
/**
|
|
124
|
+
* Wrap a promise-returning callback with safety handlers, with optional abort capabilities and timeout
|
|
125
|
+
* @param callback Callback to wrap
|
|
126
|
+
* @param options Options for the promise
|
|
127
|
+
* @returns Promise-wrapped callback
|
|
128
|
+
*/
|
|
129
|
+
export declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
130
|
+
/**
|
|
131
|
+
* Wrap a callback with a promise and safety handlers, with optional abort capabilities and timeout
|
|
132
|
+
* @param callback Callback to wrap
|
|
133
|
+
* @param options Options for the promise
|
|
134
|
+
* @returns Promise-wrapped callback
|
|
135
|
+
*/
|
|
136
|
+
export declare function attemptPromise<Value>(callback: () => Value, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
80
137
|
declare const TYPE_FULFILLED = "fulfilled";
|
|
81
138
|
declare const TYPE_REJECTED = "rejected";
|
|
82
139
|
export {};
|
package/types/random.d.ts
CHANGED
|
@@ -35,9 +35,9 @@ export declare function getRandomItem<Value>(array: Value[]): Value | undefined;
|
|
|
35
35
|
*/
|
|
36
36
|
export declare function getRandomItems<Value>(array: Value[], amount: number): Value[];
|
|
37
37
|
/**
|
|
38
|
-
* Get
|
|
38
|
+
* Get a shuffled array
|
|
39
39
|
* @param array Array to get random items from
|
|
40
|
-
* @returns
|
|
40
|
+
* @returns Shuffled version of the original array
|
|
41
41
|
*/
|
|
42
42
|
export declare function getRandomItems<Value>(array: Value[]): Value[];
|
|
43
43
|
export { getRandomFloat, getRandomInteger } from './internal/random';
|
package/types/result.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { attemptPromise } from './promise';
|
|
1
2
|
/**
|
|
2
3
|
* An error result
|
|
3
4
|
*/
|
|
@@ -83,29 +84,43 @@ export declare function ok<Value>(value: Value): Ok<Value>;
|
|
|
83
84
|
* @param error Error value
|
|
84
85
|
* @returns Callback result
|
|
85
86
|
*/
|
|
86
|
-
export declare function
|
|
87
|
+
export declare function attempt<Value, E>(callback: () => Value, error: E): ExtendedErr<E> | Ok<Value>;
|
|
87
88
|
/**
|
|
88
89
|
* Executes a callback, catching any errors, and returns a result
|
|
89
90
|
* @param callback Callback to execute
|
|
90
91
|
* @returns Callback result
|
|
91
92
|
*/
|
|
92
|
-
export declare function
|
|
93
|
-
export declare namespace
|
|
94
|
-
var async: typeof
|
|
93
|
+
export declare function attempt<Value>(callback: () => Value): Result<Value, Error>;
|
|
94
|
+
export declare namespace attempt {
|
|
95
|
+
var async: typeof asyncAttempt;
|
|
96
|
+
var promise: typeof attemptPromise;
|
|
95
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Executes a promise, catching any errors, and returns a result
|
|
100
|
+
* @param promise Promise to execute
|
|
101
|
+
* @param error Error value
|
|
102
|
+
* @returns Callback result
|
|
103
|
+
*/
|
|
104
|
+
declare function asyncAttempt<Value, E>(promise: Promise<Value>, error: E): Promise<ExtendedErr<E> | Ok<Awaited<Value>>>;
|
|
96
105
|
/**
|
|
97
106
|
* Executes a callback asynchronously, catching any errors, and returns a result
|
|
98
107
|
* @param callback Callback to execute
|
|
99
108
|
* @param error Error value
|
|
100
109
|
* @returns Callback result
|
|
101
110
|
*/
|
|
102
|
-
declare function
|
|
111
|
+
declare function asyncAttempt<Value, E>(callback: () => Promise<Value>, error: E): Promise<ExtendedErr<E> | Ok<Value>>;
|
|
112
|
+
/**
|
|
113
|
+
* Executes a promise, catching any errors, and returns a result
|
|
114
|
+
* @param promise Promise to execute
|
|
115
|
+
* @returns Callback result
|
|
116
|
+
*/
|
|
117
|
+
declare function asyncAttempt<Value>(promise: Promise<Value>): Promise<Awaited<Value>>;
|
|
103
118
|
/**
|
|
104
119
|
* Executes a callback asynchronously, catching any errors, and returns a result
|
|
105
120
|
* @param callback Callback to execute
|
|
106
121
|
* @returns Callback result
|
|
107
122
|
*/
|
|
108
|
-
declare function
|
|
123
|
+
declare function asyncAttempt<Value>(callback: () => Promise<Value>): Promise<Result<Value>>;
|
|
109
124
|
/**
|
|
110
125
|
* Gets the value of an ok result _(or a default value)_
|
|
111
126
|
* @param value Result to unwrap
|
package/types/sized/map.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A Map with a maximum size
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Behavior is similar to a _LRU_-cache, where the least recently used entries are removed
|
|
5
5
|
*/
|
|
6
6
|
export declare class SizedMap<Key = unknown, Value = unknown> extends Map<Key, Value> {
|
|
7
7
|
#private;
|
package/types/sized/set.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* - A Set with a maximum size
|
|
3
|
-
* -
|
|
3
|
+
* - Behavior is similar to a _LRU_-cache, where the oldest values are removed
|
|
4
4
|
*/
|
|
5
5
|
export declare class SizedSet<Value = unknown> extends Set<Value> {
|
|
6
6
|
#private;
|
|
@@ -36,7 +36,7 @@ export declare class SizedSet<Value = unknown> extends Set<Value> {
|
|
|
36
36
|
* Get a value from the SizedSet, if it exists _(and move it to the end)_
|
|
37
37
|
* @param value Value to get from the SizedSet
|
|
38
38
|
* @param update Update the value's position in the SizedSet? _(defaults to `false`)_
|
|
39
|
-
* @returns
|
|
39
|
+
* @returns Found value if it exists, otherwise `undefined`
|
|
40
40
|
*/
|
|
41
41
|
get(value: Value, update?: boolean): Value | undefined;
|
|
42
42
|
}
|
package/types/value/misc.d.ts
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PlainObject } from '../models';
|
|
2
|
+
/**
|
|
3
|
+
* Create a new object without the specified keys
|
|
4
|
+
* @param value Original object
|
|
5
|
+
* @param keys Keys to omit
|
|
6
|
+
* @returns Partial object without the specified keys
|
|
7
|
+
*/
|
|
8
|
+
export declare function omit<Value extends PlainObject, Key extends keyof Value>(value: Value, keys: Key[]): Omit<Value, Key>;
|
|
@@ -5,4 +5,4 @@ import type { PlainObject } from '../models';
|
|
|
5
5
|
* @param keys Keys to use
|
|
6
6
|
* @returns Partial object with only the specified keys
|
|
7
7
|
*/
|
|
8
|
-
export declare function
|
|
8
|
+
export declare function pick<Value extends PlainObject, Key extends keyof Value>(value: Value, keys: Key[]): Pick<Value, Key>;
|
package/dist/value/partial.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Create a new object with only the specified keys
|
|
3
|
-
* @param value Original object
|
|
4
|
-
* @param keys Keys to use
|
|
5
|
-
* @returns Partial object with only the specified keys
|
|
6
|
-
*/
|
|
7
|
-
function partial(value, keys) {
|
|
8
|
-
if (typeof value !== "object" || value === null || Object.keys(value).length === 0 || !Array.isArray(keys) || keys.length === 0) return {};
|
|
9
|
-
const { length } = keys;
|
|
10
|
-
const partials = {};
|
|
11
|
-
for (let index = 0; index < length; index += 1) {
|
|
12
|
-
const key = keys[index];
|
|
13
|
-
if (key in value) partials[key] = value[key];
|
|
14
|
-
}
|
|
15
|
-
return partials;
|
|
16
|
-
}
|
|
17
|
-
export { partial };
|