@oscarpalmer/atoms 0.184.2 → 0.186.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/array/difference.d.mts +29 -0
- package/dist/array/exists.d.mts +35 -0
- package/dist/array/filter.d.mts +72 -2
- package/dist/array/find.d.mts +70 -0
- package/dist/array/first.d.mts +77 -2
- package/dist/array/flatten.d.mts +6 -0
- package/dist/array/flatten.mjs +6 -0
- package/dist/array/from.d.mts +36 -0
- package/dist/array/get.d.mts +21 -13
- package/dist/array/group-by.d.mts +142 -0
- package/dist/array/index.d.mts +2 -2
- package/dist/array/index.mjs +2 -2
- package/dist/array/insert.d.mts +16 -0
- package/dist/array/intersection.d.mts +29 -0
- package/dist/array/last.d.mts +75 -2
- package/dist/array/{position.d.mts → match.d.mts} +168 -36
- package/dist/array/{position.mjs → match.mjs} +16 -16
- package/dist/array/move.d.mts +78 -8
- package/dist/array/move.mjs +11 -1
- package/dist/array/partition.d.mts +35 -0
- package/dist/array/push.d.mts +8 -0
- package/dist/array/push.mjs +8 -0
- package/dist/array/reverse.d.mts +1 -0
- package/dist/array/reverse.mjs +1 -0
- package/dist/array/select.d.mts +94 -8
- package/dist/array/single.d.mts +29 -0
- package/dist/array/slice.d.mts +106 -16
- package/dist/array/sort.d.mts +30 -4
- package/dist/array/sort.mjs +1 -1
- package/dist/array/splice.d.mts +48 -0
- package/dist/array/splice.mjs +2 -1
- package/dist/array/swap.d.mts +113 -8
- package/dist/array/swap.mjs +2 -1
- package/dist/array/to-map.d.mts +124 -0
- package/dist/array/to-record.d.mts +124 -0
- package/dist/array/to-set.d.mts +24 -0
- package/dist/array/toggle.d.mts +38 -3
- package/dist/array/union.d.mts +29 -0
- package/dist/array/unique.d.mts +24 -0
- package/dist/array/update.d.mts +38 -3
- package/dist/beacon.d.mts +12 -0
- package/dist/beacon.mjs +9 -0
- package/dist/color/instance.d.mts +8 -0
- package/dist/color/instance.mjs +3 -0
- package/dist/color/models.d.mts +30 -0
- package/dist/function/assert.d.mts +29 -8
- package/dist/function/assert.mjs +29 -8
- package/dist/function/memoize.d.mts +3 -0
- package/dist/function/memoize.mjs +3 -0
- package/dist/function/retry.d.mts +3 -0
- package/dist/function/retry.mjs +3 -0
- package/dist/function/work.mjs +1 -1
- package/dist/index.d.mts +2158 -288
- package/dist/index.mjs +294 -181
- package/dist/internal/array/chunk.d.mts +6 -0
- package/dist/internal/array/chunk.mjs +6 -0
- package/dist/internal/array/compact.d.mts +12 -0
- package/dist/internal/array/index-of.d.mts +70 -0
- package/dist/internal/math/aggregate.d.mts +29 -0
- package/dist/internal/value/compare.d.mts +2 -1
- package/dist/internal/value/equal.d.mts +5 -0
- package/dist/internal/value/get.d.mts +27 -5
- package/dist/internal/value/has.d.mts +7 -7
- package/dist/internal/value/has.mjs +1 -1
- package/dist/internal/value/misc.d.mts +2 -2
- package/dist/internal/value/misc.mjs +10 -4
- package/dist/logger.d.mts +11 -0
- package/dist/logger.mjs +11 -0
- package/dist/models.d.mts +14 -1
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.d.mts +0 -6
- package/dist/promise/models.d.mts +36 -0
- package/dist/promise/models.mjs +6 -0
- package/dist/queue.d.mts +13 -1
- package/dist/queue.mjs +9 -0
- package/dist/result/index.d.mts +0 -8
- package/dist/result/index.mjs +0 -8
- package/dist/result/match.d.mts +4 -4
- package/dist/result/work/flow.d.mts +12 -36
- package/dist/result/work/pipe.d.mts +11 -33
- package/dist/sized/set.d.mts +3 -2
- package/dist/sized/set.mjs +3 -2
- package/dist/value/collection.d.mts +1 -1
- package/dist/value/handle.mjs +1 -1
- package/dist/value/merge.d.mts +28 -25
- package/dist/value/merge.mjs +29 -18
- package/dist/value/shake.d.mts +3 -0
- package/dist/value/smush.d.mts +3 -0
- package/dist/value/transform.d.mts +10 -1
- package/dist/value/unsmush.d.mts +2 -3
- package/package.json +5 -5
- package/src/array/difference.ts +33 -0
- package/src/array/exists.ts +35 -0
- package/src/array/filter.ts +72 -2
- package/src/array/find.ts +70 -0
- package/src/array/first.ts +77 -3
- package/src/array/flatten.ts +6 -0
- package/src/array/from.ts +40 -0
- package/src/array/get.ts +21 -15
- package/src/array/group-by.ts +142 -0
- package/src/array/index.ts +1 -1
- package/src/array/insert.ts +16 -2
- package/src/array/intersection.ts +33 -0
- package/src/array/last.ts +75 -2
- package/src/array/{position.ts → match.ts} +197 -65
- package/src/array/move.ts +87 -13
- package/src/array/partition.ts +35 -0
- package/src/array/push.ts +8 -2
- package/src/array/reverse.ts +5 -0
- package/src/array/select.ts +96 -13
- package/src/array/single.ts +29 -0
- package/src/array/slice.ts +114 -24
- package/src/array/sort.ts +30 -4
- package/src/array/splice.ts +52 -4
- package/src/array/swap.ts +122 -13
- package/src/array/to-map.ts +124 -0
- package/src/array/to-record.ts +124 -0
- package/src/array/to-set.ts +24 -0
- package/src/array/toggle.ts +42 -3
- package/src/array/union.ts +33 -0
- package/src/array/unique.ts +24 -0
- package/src/array/update.ts +38 -3
- package/src/beacon.ts +12 -0
- package/src/color/index.ts +0 -3
- package/src/color/instance.ts +9 -1
- package/src/color/models.ts +30 -0
- package/src/function/assert.ts +66 -7
- package/src/function/memoize.ts +3 -0
- package/src/function/once.ts +5 -1
- package/src/function/retry.ts +3 -0
- package/src/internal/array/chunk.ts +6 -0
- package/src/internal/array/compact.ts +12 -0
- package/src/internal/array/index-of.ts +70 -0
- package/src/internal/math/aggregate.ts +29 -0
- package/src/internal/string.ts +0 -2
- package/src/internal/value/compare.ts +2 -1
- package/src/internal/value/equal.ts +5 -0
- package/src/internal/value/get.ts +27 -5
- package/src/internal/value/has.ts +10 -10
- package/src/internal/value/misc.ts +24 -13
- package/src/logger.ts +11 -0
- package/src/models.ts +18 -0
- package/src/promise/index.ts +0 -6
- package/src/promise/models.ts +36 -0
- package/src/queue.ts +13 -1
- package/src/result/index.ts +0 -8
- package/src/result/match.ts +4 -4
- package/src/result/work/flow.ts +12 -36
- package/src/result/work/pipe.ts +11 -33
- package/src/sized/set.ts +4 -3
- package/src/value/collection.ts +1 -1
- package/src/value/merge.ts +88 -66
- package/src/value/shake.ts +3 -0
- package/src/value/smush.ts +3 -0
- package/src/value/transform.ts +10 -1
- package/src/value/unsmush.ts +2 -8
package/src/array/unique.ts
CHANGED
|
@@ -5,9 +5,18 @@ import type {PlainObject} from '../models';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Get an array of unique items
|
|
8
|
+
*
|
|
8
9
|
* @param array Original array
|
|
9
10
|
* @param callback Callback to get an item's value
|
|
10
11
|
* @returns Array of unique items
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* unique(
|
|
16
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
17
|
+
* item => item.id,
|
|
18
|
+
* ); // => [{id: 1}, {id: 2}, {id: 3}]
|
|
19
|
+
* ```
|
|
11
20
|
*/
|
|
12
21
|
export function unique<Item>(
|
|
13
22
|
array: Item[],
|
|
@@ -16,9 +25,18 @@ export function unique<Item>(
|
|
|
16
25
|
|
|
17
26
|
/**
|
|
18
27
|
* Get an array of unique items
|
|
28
|
+
*
|
|
19
29
|
* @param array Original array
|
|
20
30
|
* @param key Key to use for unique value
|
|
21
31
|
* @returns Array of unique items
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* unique(
|
|
36
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
37
|
+
* 'id',
|
|
38
|
+
* ); // => [{id: 1}, {id: 2}, {id: 3}]
|
|
39
|
+
* ```
|
|
22
40
|
*/
|
|
23
41
|
export function unique<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
24
42
|
array: Item[],
|
|
@@ -27,8 +45,14 @@ export function unique<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
27
45
|
|
|
28
46
|
/**
|
|
29
47
|
* Get an array of unique items
|
|
48
|
+
*
|
|
30
49
|
* @param array Original array
|
|
31
50
|
* @returns Array of unique items
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* unique([1, 2, 3, 2]); // => [1, 2, 3]
|
|
55
|
+
* ```
|
|
32
56
|
*/
|
|
33
57
|
export function unique<Item>(array: Item[]): Item[];
|
|
34
58
|
|
package/src/array/update.ts
CHANGED
|
@@ -2,11 +2,23 @@ import {updateInArray} from '../internal/array/update';
|
|
|
2
2
|
import type {PlainObject} from '../models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Update an item in an array
|
|
5
|
+
* Update an item in an array
|
|
6
|
+
*
|
|
7
|
+
* If the item exists, it will be updated; if it doesn't, it will be added
|
|
8
|
+
*
|
|
6
9
|
* @param destination Array to update within
|
|
7
10
|
* @param updated Updated items
|
|
8
11
|
* @param callback Callback to find existing item
|
|
9
12
|
* @returns Original array
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* update(
|
|
17
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
18
|
+
* [{id: 2, name: 'Updated'}, {id: 4, name: 'New'}],
|
|
19
|
+
* item => item.id,
|
|
20
|
+
* ); // => [{id: 1}, {id: 2, name: 'Updated'}, {id: 3}, {id: 4, name: 'New'}]
|
|
21
|
+
* ```
|
|
10
22
|
*/
|
|
11
23
|
export function update<Item>(
|
|
12
24
|
destination: Item[],
|
|
@@ -15,11 +27,23 @@ export function update<Item>(
|
|
|
15
27
|
): Item[];
|
|
16
28
|
|
|
17
29
|
/**
|
|
18
|
-
* Update an item in an array
|
|
30
|
+
* Update an item in an array
|
|
31
|
+
*
|
|
32
|
+
* If the item exists, it will be updated; if it doesn't, it will be added
|
|
33
|
+
*
|
|
19
34
|
* @param destination Array to update within
|
|
20
35
|
* @param updated Updated items
|
|
21
36
|
* @param key Key to find existing item
|
|
22
37
|
* @returns Original array
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* update(
|
|
42
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
43
|
+
* [{id: 2, name: 'Updated'}, {id: 4, name: 'New'}],
|
|
44
|
+
* 'id',
|
|
45
|
+
* ); // => [{id: 1}, {id: 2, name: 'Updated'}, {id: 3}, {id: 4, name: 'New'}]
|
|
46
|
+
* ```
|
|
23
47
|
*/
|
|
24
48
|
export function update<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
25
49
|
destination: Item[],
|
|
@@ -28,10 +52,21 @@ export function update<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
28
52
|
): Item[];
|
|
29
53
|
|
|
30
54
|
/**
|
|
31
|
-
* Update an item in an array
|
|
55
|
+
* Update an item in an array
|
|
56
|
+
*
|
|
57
|
+
* If the item exists, it will be updated; if it doesn't, it will be added
|
|
58
|
+
*
|
|
32
59
|
* @param destination Array to update within
|
|
33
60
|
* @param updated Updated items
|
|
34
61
|
* @returns Original array
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* update(
|
|
66
|
+
* [1, 2, 3],
|
|
67
|
+
* [2, 4],
|
|
68
|
+
* ); // => [1, 2, 3, 4]
|
|
69
|
+
* ```
|
|
35
70
|
*/
|
|
36
71
|
export function update<Item>(destination: Item[], updated: Item[]): Item[];
|
|
37
72
|
|
package/src/beacon.ts
CHANGED
|
@@ -4,6 +4,9 @@ import type {PlainObject} from './models';
|
|
|
4
4
|
|
|
5
5
|
// #region Types
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* A beacon is a lighthouse, holding an observable value that can be subscribed to and emitted from
|
|
9
|
+
*/
|
|
7
10
|
class Beacon<Value> {
|
|
8
11
|
readonly #options: Options;
|
|
9
12
|
readonly #state: BeaconState<Value>;
|
|
@@ -122,6 +125,9 @@ type BeaconState<Value> = {
|
|
|
122
125
|
value: Value;
|
|
123
126
|
};
|
|
124
127
|
|
|
128
|
+
/**
|
|
129
|
+
* An observable holds a value and allows observers to subscribe to changes in that value
|
|
130
|
+
*/
|
|
125
131
|
class Observable<Value> {
|
|
126
132
|
readonly #state: ObservableState<Value>;
|
|
127
133
|
|
|
@@ -186,6 +192,9 @@ type ObservableState<Value> = {
|
|
|
186
192
|
observers: Map<Subscription<Value>, Observer<Value>>;
|
|
187
193
|
};
|
|
188
194
|
|
|
195
|
+
/**
|
|
196
|
+
* An observer receives notifications from an observable
|
|
197
|
+
*/
|
|
189
198
|
type Observer<Value> = {
|
|
190
199
|
/**
|
|
191
200
|
* Callback for when the observable is completed
|
|
@@ -203,6 +212,9 @@ type Observer<Value> = {
|
|
|
203
212
|
|
|
204
213
|
type Options = Required<BeaconOptions<unknown>>;
|
|
205
214
|
|
|
215
|
+
/**
|
|
216
|
+
* A subscription represents an active subscription to an observable, holding its state and allowing it to be destroyed or unsubscribed from
|
|
217
|
+
*/
|
|
206
218
|
class Subscription<Value> {
|
|
207
219
|
readonly #state: SubscriptionState<Value>;
|
|
208
220
|
|
package/src/color/index.ts
CHANGED
|
@@ -39,11 +39,8 @@ export {
|
|
|
39
39
|
} from './misc/is';
|
|
40
40
|
|
|
41
41
|
export {getNormalizedHex, hexToHsl, hexToHsla, hexToRgb, hexToRgba} from './space/hex';
|
|
42
|
-
|
|
43
42
|
export {hslToHex, hslToRgb, hslToRgba} from './space/hsl';
|
|
44
|
-
|
|
45
43
|
export {rgbToHex, rgbToHsl, rgbToHsla} from './space/rgb';
|
|
46
|
-
|
|
47
44
|
export type {Color, HSLAColor, HSLColor, RGBAColor, RGBColor};
|
|
48
45
|
|
|
49
46
|
// #endregion
|
package/src/color/instance.ts
CHANGED
|
@@ -4,9 +4,17 @@ import {getAlpha} from './misc/alpha';
|
|
|
4
4
|
import {getColorState, setHexColor, setHSLColor, setRGBColor} from './misc/state';
|
|
5
5
|
import type {ColorState, HSLAColor, HSLColor, RGBAColor, RGBColor} from './models';
|
|
6
6
|
|
|
7
|
-
// #region
|
|
7
|
+
// #region Types
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* A color that is represented in multiple color formats
|
|
11
|
+
*/
|
|
9
12
|
export class Color {
|
|
13
|
+
/**
|
|
14
|
+
* A property to identify this as a Color instance, used for type checking
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
10
18
|
declare private readonly $color: boolean;
|
|
11
19
|
|
|
12
20
|
readonly #state: ColorState;
|
package/src/color/models.ts
CHANGED
|
@@ -9,19 +9,49 @@ type ColorWithAlpha = {
|
|
|
9
9
|
alpha: number;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* An _HSL_-color with an alpha channel
|
|
14
|
+
*/
|
|
12
15
|
export type HSLAColor = HSLColor & ColorWithAlpha;
|
|
13
16
|
|
|
17
|
+
/**
|
|
18
|
+
* An _HSL_-color
|
|
19
|
+
*/
|
|
14
20
|
export type HSLColor = {
|
|
21
|
+
/**
|
|
22
|
+
* Hue of the color _(in degrees; 0-360)_
|
|
23
|
+
*/
|
|
15
24
|
hue: number;
|
|
25
|
+
/**
|
|
26
|
+
* Lightness of the color _(in percentage; 0-100)_
|
|
27
|
+
*/
|
|
16
28
|
lightness: number;
|
|
29
|
+
/**
|
|
30
|
+
* Saturation of the color _(in percentage; 0-100)_
|
|
31
|
+
*/
|
|
17
32
|
saturation: number;
|
|
18
33
|
};
|
|
19
34
|
|
|
35
|
+
/**
|
|
36
|
+
* An _RGB_-color with an alpha channel
|
|
37
|
+
*/
|
|
20
38
|
export type RGBAColor = RGBColor & ColorWithAlpha;
|
|
21
39
|
|
|
40
|
+
/**
|
|
41
|
+
* An _RGB_-color
|
|
42
|
+
*/
|
|
22
43
|
export type RGBColor = {
|
|
44
|
+
/**
|
|
45
|
+
* Blue channel of the color _(in hexadecimal; 0-255)_
|
|
46
|
+
*/
|
|
23
47
|
blue: number;
|
|
48
|
+
/**
|
|
49
|
+
* Green channel of the color _(in hexadecimal; 0-255)_
|
|
50
|
+
*/
|
|
24
51
|
green: number;
|
|
52
|
+
/**
|
|
53
|
+
* Red channel of the color _(in hexadecimal; 0-255)_
|
|
54
|
+
*/
|
|
25
55
|
red: number;
|
|
26
56
|
};
|
|
27
57
|
|
package/src/function/assert.ts
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {hasValueResult} from '../internal/value/has';
|
|
2
|
+
import type {Constructor, NestedKeys, NestedValue, PlainObject} from '../models';
|
|
2
3
|
|
|
3
4
|
// #region Types
|
|
4
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Asserter for a nested property of a value
|
|
8
|
+
*/
|
|
9
|
+
export type AssertProperty<
|
|
10
|
+
Value extends PlainObject,
|
|
11
|
+
Path extends NestedKeys<Value>,
|
|
12
|
+
Asserted extends NestedPick<Value, Path> = NestedPick<Value, Path>,
|
|
13
|
+
> = Asserter<Asserted>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A function that asserts a value is of a specific type, throwing an error if it is not
|
|
17
|
+
*/
|
|
5
18
|
export type Asserter<Value> = (value: unknown) => asserts value is Value;
|
|
6
19
|
|
|
20
|
+
type NestedPick<Value, Path extends string> = Value extends PlainObject
|
|
21
|
+
? Path extends `${infer Head}.${infer Rest}`
|
|
22
|
+
? Head extends keyof Value
|
|
23
|
+
? {[Key in Head]: NestedPick<Value[Key], Rest>}
|
|
24
|
+
: never
|
|
25
|
+
: Path extends keyof Value
|
|
26
|
+
? {[Key in Path]: Value[Key]}
|
|
27
|
+
: never
|
|
28
|
+
: never;
|
|
29
|
+
|
|
7
30
|
// #endregion
|
|
8
31
|
|
|
9
32
|
// #region Functions
|
|
@@ -12,7 +35,7 @@ export type Asserter<Value> = (value: unknown) => asserts value is Value;
|
|
|
12
35
|
* Asserts that a condition is true, throwing an error if it is not
|
|
13
36
|
* @param condition Condition to assert
|
|
14
37
|
* @param message Error message
|
|
15
|
-
* @param error Error constructor
|
|
38
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
16
39
|
*/
|
|
17
40
|
export function assert<Condition extends () => boolean>(
|
|
18
41
|
condition: Condition,
|
|
@@ -28,6 +51,7 @@ assert.condition = assertCondition;
|
|
|
28
51
|
assert.defined = assertDefined;
|
|
29
52
|
assert.instanceOf = assertInstanceOf;
|
|
30
53
|
assert.is = assertIs;
|
|
54
|
+
assert.property = assertProperty;
|
|
31
55
|
|
|
32
56
|
/**
|
|
33
57
|
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
@@ -35,7 +59,7 @@ assert.is = assertIs;
|
|
|
35
59
|
* Available as `assertCondition` and `assert.condition`
|
|
36
60
|
* @param condition Condition to assert
|
|
37
61
|
* @param message Error message
|
|
38
|
-
* @param error Error constructor
|
|
62
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
39
63
|
* @returns Asserter
|
|
40
64
|
*/
|
|
41
65
|
export function assertCondition<Value>(
|
|
@@ -49,17 +73,19 @@ export function assertCondition<Value>(
|
|
|
49
73
|
}
|
|
50
74
|
|
|
51
75
|
/**
|
|
52
|
-
* Asserts that a value is defined throwing an error if it is not
|
|
76
|
+
* Asserts that a value is defined, throwing an error if it is not
|
|
53
77
|
*
|
|
54
78
|
* Available as `assertDefined` and `assert.defined`
|
|
55
79
|
* @param value Value to assert
|
|
56
80
|
* @param message Error message
|
|
81
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
57
82
|
*/
|
|
58
83
|
export function assertDefined<Value>(
|
|
59
84
|
value: unknown,
|
|
60
85
|
message?: string,
|
|
86
|
+
error?: ErrorConstructor,
|
|
61
87
|
): asserts value is Exclude<Value, null | undefined> {
|
|
62
|
-
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
|
|
88
|
+
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED, error);
|
|
63
89
|
}
|
|
64
90
|
|
|
65
91
|
/**
|
|
@@ -68,7 +94,7 @@ export function assertDefined<Value>(
|
|
|
68
94
|
* Available as `assertInstanceOf` and `assert.instanceOf`
|
|
69
95
|
* @param constructor Constructor to check against
|
|
70
96
|
* @param message Error message
|
|
71
|
-
* @param error Error constructor
|
|
97
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
72
98
|
* @returns Asserter
|
|
73
99
|
*/
|
|
74
100
|
export function assertInstanceOf<Value>(
|
|
@@ -87,7 +113,7 @@ export function assertInstanceOf<Value>(
|
|
|
87
113
|
* Available as `assertIs` and `assert.is`
|
|
88
114
|
* @param condition Type guard function to check the value
|
|
89
115
|
* @param message Error message
|
|
90
|
-
* @param error Error constructor
|
|
116
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
91
117
|
* @returns Asserter
|
|
92
118
|
*/
|
|
93
119
|
export function assertIs<Value>(
|
|
@@ -100,6 +126,39 @@ export function assertIs<Value>(
|
|
|
100
126
|
};
|
|
101
127
|
}
|
|
102
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Creates an asserter that asserts a property of a value exists and satisfies a condition, throwing an error if it does not
|
|
131
|
+
*
|
|
132
|
+
* Available as `assertProperty` and `assert.property`
|
|
133
|
+
* @param path Path to the property to check, e.g., `foo.bar.baz` for a nested property
|
|
134
|
+
* @param condition Condition to assert for the property
|
|
135
|
+
* @param message Error message
|
|
136
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
137
|
+
* @returns Asserter
|
|
138
|
+
*/
|
|
139
|
+
export function assertProperty<
|
|
140
|
+
Value extends PlainObject,
|
|
141
|
+
Path extends NestedKeys<Value>,
|
|
142
|
+
Asserted = NestedPick<Value, Path>,
|
|
143
|
+
>(
|
|
144
|
+
path: Path,
|
|
145
|
+
condition: (value: NestedValue<Value, Path>) => boolean,
|
|
146
|
+
message: string,
|
|
147
|
+
error?: ErrorConstructor,
|
|
148
|
+
): Asserter<Asserted> {
|
|
149
|
+
return (value: unknown): asserts value is Asserted => {
|
|
150
|
+
assert(
|
|
151
|
+
() => {
|
|
152
|
+
const result = hasValueResult(value as never, path, false);
|
|
153
|
+
|
|
154
|
+
return result.ok && condition(result.value as never);
|
|
155
|
+
},
|
|
156
|
+
message,
|
|
157
|
+
error,
|
|
158
|
+
);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
103
162
|
// #endregion
|
|
104
163
|
|
|
105
164
|
// #region Variables
|
package/src/function/memoize.ts
CHANGED
|
@@ -5,6 +5,9 @@ import {SizedMap} from '../sized/map';
|
|
|
5
5
|
|
|
6
6
|
// #region Types
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* A memoized function, caching and retrieving results based on the its parameters _(or a custom cache key)_
|
|
10
|
+
*/
|
|
8
11
|
class Memoized<Callback extends GenericCallback> {
|
|
9
12
|
readonly #state: MemoizedState<Callback>;
|
|
10
13
|
|
package/src/function/once.ts
CHANGED
|
@@ -107,7 +107,11 @@ export function asyncOnce<Callback extends GenericAsyncCallback>(
|
|
|
107
107
|
return fn as OnceAsyncCallback<Callback>;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
function handleOnceResult<Value>(
|
|
110
|
+
function handleOnceResult<Value>(
|
|
111
|
+
state: OnceAsyncState<Value>,
|
|
112
|
+
value: unknown,
|
|
113
|
+
error: boolean,
|
|
114
|
+
): void {
|
|
111
115
|
state.error = error;
|
|
112
116
|
state.finished = true;
|
|
113
117
|
state.value = value as Value;
|
package/src/function/retry.ts
CHANGED
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Chunk an array into smaller arrays
|
|
5
|
+
*
|
|
5
6
|
* @param array Array to chunk
|
|
6
7
|
* @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
|
|
7
8
|
* @returns Array of arrays
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
|
|
13
|
+
* ```
|
|
8
14
|
*/
|
|
9
15
|
export function chunk<Item>(array: Item[], size?: number): Item[][] {
|
|
10
16
|
if (!Array.isArray(array)) {
|
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Compact an array _(removing all false-y values)_
|
|
5
|
+
*
|
|
5
6
|
* @param array Array to compact
|
|
6
7
|
* @param strict True to remove all false-y values
|
|
7
8
|
* @returns Compacted array
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* compact([0, 1, '', 'hello', false, true, null, undefined]); // => [1, 'hello', true]
|
|
13
|
+
* ```
|
|
8
14
|
*/
|
|
9
15
|
export function compact<Item>(
|
|
10
16
|
array: Item[],
|
|
@@ -13,8 +19,14 @@ export function compact<Item>(
|
|
|
13
19
|
|
|
14
20
|
/**
|
|
15
21
|
* Compact an array _(removing all `null` and `undefined` values)_
|
|
22
|
+
*
|
|
16
23
|
* @param array Array to compact
|
|
17
24
|
* @returns Compacted array
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* compact([0, 1, '', 'hello', false, true, null, undefined]); // => [0, 1, '', 'hello', false, true]
|
|
29
|
+
* ```
|
|
18
30
|
*/
|
|
19
31
|
export function compact<Item>(array: Item[]): Exclude<Item, null | undefined>[];
|
|
20
32
|
|
|
@@ -5,10 +5,20 @@ import type {PlainObject} from '../../models';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Get the index of the first matching item by callback
|
|
8
|
+
*
|
|
8
9
|
* @param array Array to search in
|
|
9
10
|
* @param callback Callback to get an item's value
|
|
10
11
|
* @param value Value to match against
|
|
11
12
|
* @returns Index of the first matching item, or `-1` if no match is found
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* indexOf(
|
|
17
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
18
|
+
* item => item.id,
|
|
19
|
+
* 2,
|
|
20
|
+
* ); // => 1
|
|
21
|
+
* ```
|
|
12
22
|
*/
|
|
13
23
|
export function indexOf<
|
|
14
24
|
Item,
|
|
@@ -17,10 +27,20 @@ export function indexOf<
|
|
|
17
27
|
|
|
18
28
|
/**
|
|
19
29
|
* Get the index of the first matching item by key
|
|
30
|
+
*
|
|
20
31
|
* @param array Array to search in
|
|
21
32
|
* @param key Key to match items by
|
|
22
33
|
* @param value Value to match against
|
|
23
34
|
* @returns Index of the first matching item, or `-1` if no match is found
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* indexOf(
|
|
39
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
40
|
+
* 'id',
|
|
41
|
+
* 2,
|
|
42
|
+
* ); // => 1
|
|
43
|
+
* ```
|
|
24
44
|
*/
|
|
25
45
|
export function indexOf<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
26
46
|
array: Item[],
|
|
@@ -30,9 +50,18 @@ export function indexOf<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
30
50
|
|
|
31
51
|
/**
|
|
32
52
|
* Get the index of the first item matching the filter
|
|
53
|
+
*
|
|
33
54
|
* @param array Array to search in
|
|
34
55
|
* @param filter Filter callback to match items
|
|
35
56
|
* @returns Index of the first matching item, or `-1` if no match is found
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* indexOf(
|
|
61
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
62
|
+
* item => item.id === 2,
|
|
63
|
+
* ); // => 1
|
|
64
|
+
* ```
|
|
36
65
|
*/
|
|
37
66
|
export function indexOf<Item>(
|
|
38
67
|
array: Item[],
|
|
@@ -41,9 +70,15 @@ export function indexOf<Item>(
|
|
|
41
70
|
|
|
42
71
|
/**
|
|
43
72
|
* Get the index of the first item matching the given item
|
|
73
|
+
*
|
|
44
74
|
* @param array Array to search in
|
|
45
75
|
* @param item Item to match against
|
|
46
76
|
* @returns Index of the first matching item, or `-1` if no match is found
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* indexOf([1, 2, 3, 4, 5], 3); // => 2
|
|
81
|
+
* ```
|
|
47
82
|
*/
|
|
48
83
|
export function indexOf<Item>(array: Item[], item: Item): number;
|
|
49
84
|
|
|
@@ -57,10 +92,20 @@ indexOf.last = lastIndexOf;
|
|
|
57
92
|
* Get the index of the last matching item by callback
|
|
58
93
|
*
|
|
59
94
|
* Available as `lastIndexOf` and `indexOf.last`
|
|
95
|
+
*
|
|
60
96
|
* @param array Array to search in
|
|
61
97
|
* @param callback Callback to get an item's value
|
|
62
98
|
* @param value Value to match against
|
|
63
99
|
* @returns Index of the last matching item, or `-1` if no match is found
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* lastIndexOf(
|
|
104
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
105
|
+
* item => item.id,
|
|
106
|
+
* 2,
|
|
107
|
+
* ); // => 3
|
|
108
|
+
* ```
|
|
64
109
|
*/
|
|
65
110
|
export function lastIndexOf<
|
|
66
111
|
Item,
|
|
@@ -71,10 +116,20 @@ export function lastIndexOf<
|
|
|
71
116
|
* Get the index of the last matching item by key
|
|
72
117
|
*
|
|
73
118
|
* Available as `lastIndexOf` and `indexOf.last`
|
|
119
|
+
*
|
|
74
120
|
* @param array Array to search in
|
|
75
121
|
* @param key Key to match items by
|
|
76
122
|
* @param value Value to match against
|
|
77
123
|
* @returns Index of the last matching item, or `-1` if no match is found
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* lastIndexOf(
|
|
128
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
129
|
+
* 'id',
|
|
130
|
+
* 2,
|
|
131
|
+
* ); // => 3
|
|
132
|
+
* ```
|
|
78
133
|
*/
|
|
79
134
|
export function lastIndexOf<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
80
135
|
array: Item[],
|
|
@@ -86,9 +141,18 @@ export function lastIndexOf<Item extends PlainObject, ItemKey extends keyof Item
|
|
|
86
141
|
* Get the index of the last item matching the filter
|
|
87
142
|
*
|
|
88
143
|
* Available as `lastIndexOf` and `indexOf.last`
|
|
144
|
+
*
|
|
89
145
|
* @param array Array to search in
|
|
90
146
|
* @param filter Filter callback to match items
|
|
91
147
|
* @returns Index of the last matching item, or `-1` if no match is found
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* lastIndexOf(
|
|
152
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
153
|
+
* item => item.id === 2,
|
|
154
|
+
* ); // => 3
|
|
155
|
+
* ```
|
|
92
156
|
*/
|
|
93
157
|
export function lastIndexOf<Item>(
|
|
94
158
|
array: Item[],
|
|
@@ -99,9 +163,15 @@ export function lastIndexOf<Item>(
|
|
|
99
163
|
* Get the index of the last item matching the given item
|
|
100
164
|
*
|
|
101
165
|
* Available as `lastIndexOf` and `indexOf.last`
|
|
166
|
+
*
|
|
102
167
|
* @param array Array to search in
|
|
103
168
|
* @param item Item to match against
|
|
104
169
|
* @returns Index of the last matching item, or `-1` if no match is found
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* lastIndexOf([1, 2, 3, 2, 1], 2); // => 3
|
|
174
|
+
* ```
|
|
105
175
|
*/
|
|
106
176
|
export function lastIndexOf<Item>(array: Item[], item: Item): number;
|
|
107
177
|
|
|
@@ -66,6 +66,17 @@ export function getAggregateCallback(key: unknown): Function | undefined {
|
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
68
|
* Get the maximum value from a list of items
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* max(
|
|
73
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}],
|
|
74
|
+
* item => item.value,
|
|
75
|
+
* ); // 20
|
|
76
|
+
*
|
|
77
|
+
* max([], item => item.value); // Number.NaN
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
69
80
|
* @param items List of items
|
|
70
81
|
* @param callback Callback to get an item's value
|
|
71
82
|
* @returns Maximum value, or `NaN` if no maximum can be found
|
|
@@ -77,6 +88,17 @@ export function max<Item>(
|
|
|
77
88
|
|
|
78
89
|
/**
|
|
79
90
|
* Get the maximum value from a list of items
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* max(
|
|
95
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}],
|
|
96
|
+
* 'value',
|
|
97
|
+
* ); // 20
|
|
98
|
+
*
|
|
99
|
+
* max([], 'value'); // Number.NaN
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
80
102
|
* @param items List of items
|
|
81
103
|
* @param key Key to use for value
|
|
82
104
|
* @returns Maximum value, or `NaN` if no maximum can be found
|
|
@@ -88,6 +110,13 @@ export function max<Item extends PlainObject, ItemKey extends keyof NumericalVal
|
|
|
88
110
|
|
|
89
111
|
/**
|
|
90
112
|
* Get the maximum value from a list of numbers
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* max([10, 20]); // 20
|
|
117
|
+
* max([]); // Number.NaN
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
91
120
|
* @param values List of numbers
|
|
92
121
|
* @returns Maximum value, or `NaN` if no maximum can be found
|
|
93
122
|
*/
|
package/src/internal/string.ts
CHANGED
|
@@ -19,11 +19,9 @@ export function getString(value: unknown): string {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
if (typeof value !== 'object') {
|
|
22
|
-
// oxlint-disable-next-line typescript/no-base-to-string: the whole point of this function is to get a string representation of any value, so calling `String` on it is fine
|
|
23
22
|
return String(value);
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
// oxlint-disable-next-line typescript/no-base-to-string: ditto
|
|
27
25
|
const asString = String(value.valueOf?.() ?? value);
|
|
28
26
|
|
|
29
27
|
return asString.startsWith('[object ') ? JSON.stringify(value) : asString;
|
|
@@ -45,6 +45,7 @@ export function compare(first: unknown, second: unknown): number {
|
|
|
45
45
|
const firstParts = getComparisonParts(first);
|
|
46
46
|
const secondParts = getComparisonParts(second);
|
|
47
47
|
const length = max([firstParts.length, secondParts.length]);
|
|
48
|
+
|
|
48
49
|
const lastIndex = length - 1;
|
|
49
50
|
|
|
50
51
|
for (let index = 0; index < length; index += 1) {
|
|
@@ -147,7 +148,7 @@ function getComparisonParts(value: unknown): unknown[] {
|
|
|
147
148
|
*/
|
|
148
149
|
export function registerComparator<Instance>(
|
|
149
150
|
constructor: Constructor<Instance>,
|
|
150
|
-
handler?: string |
|
|
151
|
+
handler?: string | Comparator<Instance>,
|
|
151
152
|
): void {
|
|
152
153
|
compare.handlers.register(constructor, handler);
|
|
153
154
|
}
|