shelving 1.78.0 → 1.80.1
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/api/Resource.js +0 -1
- package/constraint/Constraints.js +2 -2
- package/constraint/QueryConstraints.d.ts +1 -1
- package/constraint/QueryConstraints.js +1 -1
- package/db/Collection.d.ts +34 -6
- package/db/Collection.js +47 -7
- package/db/Database.d.ts +41 -7
- package/db/Database.js +54 -2
- package/db/Item.d.ts +4 -4
- package/db/Item.js +19 -19
- package/feedback/ErrorFeedback.js +1 -5
- package/feedback/Feedback.js +1 -5
- package/feedback/InvalidFeedback.js +1 -5
- package/feedback/SuccessFeedback.js +1 -5
- package/feedback/WarningFeedback.js +1 -5
- package/package.json +10 -8
- package/schema/AllowSchema.js +2 -2
- package/state/ArrayState.d.ts +6 -8
- package/state/ArrayState.js +10 -14
- package/state/DataState.js +2 -1
- package/state/ObjectState.d.ts +1 -1
- package/state/ObjectState.js +4 -4
- package/update/ArrayUpdate.js +1 -1
- package/update/DataUpdate.d.ts +1 -3
- package/update/DataUpdate.js +1 -1
- package/update/hydrations.d.ts +2 -2
- package/update/hydrations.js +2 -2
- package/util/array.d.ts +6 -52
- package/util/array.js +15 -89
- package/util/data.d.ts +8 -60
- package/util/data.js +0 -51
- package/util/entry.d.ts +2 -2
- package/util/object.d.ts +49 -41
- package/util/object.js +56 -53
- package/util/transform.d.ts +8 -12
- package/util/transform.js +20 -8
- package/util/validate.d.ts +2 -2
- package/util/validate.js +1 -1
package/schema/AllowSchema.js
CHANGED
|
@@ -2,7 +2,7 @@ import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
|
2
2
|
import { getString } from "../util/string.js";
|
|
3
3
|
import { isArray, isItem } from "../util/array.js";
|
|
4
4
|
import { getOptionalNumber } from "../util/number.js";
|
|
5
|
-
import {
|
|
5
|
+
import { isProp } from "../util/object.js";
|
|
6
6
|
import { Schema } from "./Schema.js";
|
|
7
7
|
/** Validate a value against a specific set of allowed values. */
|
|
8
8
|
export function validateAllowed(value, allowed) {
|
|
@@ -11,7 +11,7 @@ export function validateAllowed(value, allowed) {
|
|
|
11
11
|
return value;
|
|
12
12
|
}
|
|
13
13
|
else {
|
|
14
|
-
if (
|
|
14
|
+
if (isProp(allowed, value))
|
|
15
15
|
return value;
|
|
16
16
|
}
|
|
17
17
|
throw new InvalidFeedback("Unknown value", { value });
|
package/state/ArrayState.d.ts
CHANGED
|
@@ -5,14 +5,12 @@ export declare class ArrayState<T> extends State<ImmutableArray<T>> implements I
|
|
|
5
5
|
constructor(initial?: ImmutableArray<T>);
|
|
6
6
|
/** Get the length of the current value of this state. */
|
|
7
7
|
get count(): number;
|
|
8
|
-
/** Add
|
|
9
|
-
add(
|
|
10
|
-
/** Remove
|
|
11
|
-
delete(
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
/** Toggle an item in this array. */
|
|
15
|
-
toggle(item: T): void;
|
|
8
|
+
/** Add items to this array. */
|
|
9
|
+
add(...items: T[]): void;
|
|
10
|
+
/** Remove items from this array. */
|
|
11
|
+
delete(...items: T[]): void;
|
|
12
|
+
/** Toggle items in this array. */
|
|
13
|
+
toggle(...items: T[]): void;
|
|
16
14
|
/** Iterate over the items. */
|
|
17
15
|
[Symbol.iterator](): Iterator<T, void>;
|
|
18
16
|
}
|
package/state/ArrayState.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { toggleItems, withItems, withoutItems } from "../util/array.js";
|
|
2
2
|
import { State } from "./State.js";
|
|
3
3
|
/** State that stores an array and has additional methods to help with that. */
|
|
4
4
|
export class ArrayState extends State {
|
|
@@ -9,21 +9,17 @@ export class ArrayState extends State {
|
|
|
9
9
|
get count() {
|
|
10
10
|
return this.value.length;
|
|
11
11
|
}
|
|
12
|
-
/** Add
|
|
13
|
-
add(
|
|
14
|
-
this.next(
|
|
12
|
+
/** Add items to this array. */
|
|
13
|
+
add(...items) {
|
|
14
|
+
this.next(withItems(this.value, ...items));
|
|
15
15
|
}
|
|
16
|
-
/** Remove
|
|
17
|
-
delete(
|
|
18
|
-
this.next(
|
|
16
|
+
/** Remove items from this array. */
|
|
17
|
+
delete(...items) {
|
|
18
|
+
this.next(withoutItems(this.value, ...items));
|
|
19
19
|
}
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
this.next(
|
|
23
|
-
}
|
|
24
|
-
/** Toggle an item in this array. */
|
|
25
|
-
toggle(item) {
|
|
26
|
-
this.next(toggleItem(this.value, item));
|
|
20
|
+
/** Toggle items in this array. */
|
|
21
|
+
toggle(...items) {
|
|
22
|
+
this.next(toggleItems(this.value, ...items));
|
|
27
23
|
}
|
|
28
24
|
/** Iterate over the items. */
|
|
29
25
|
[Symbol.iterator]() {
|
package/state/DataState.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getData
|
|
1
|
+
import { getData } from "../util/data.js";
|
|
2
|
+
import { withProp } from "../util/object.js";
|
|
2
3
|
import { transformData } from "../util/transform.js";
|
|
3
4
|
import { State } from "./State.js";
|
|
4
5
|
/** State that stores a data object and has additional methods to help with that. */
|
package/state/ObjectState.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare class ObjectState<T> extends State<ImmutableObject<T>> implements
|
|
|
7
7
|
/** Get the length of the current value of this state. */
|
|
8
8
|
get count(): number;
|
|
9
9
|
/** Remove a named entry from this object. */
|
|
10
|
-
delete(
|
|
10
|
+
delete(...keys: string[]): void;
|
|
11
11
|
/** Set a named entry in this object with a different value. */
|
|
12
12
|
set(key: string, value: T): void;
|
|
13
13
|
/** Iterate over the items. */
|
package/state/ObjectState.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { withProp, withoutProps } from "../util/object.js";
|
|
2
2
|
import { State } from "./State.js";
|
|
3
3
|
/** State that stores a map-like object and has additional methods to help with that. */
|
|
4
4
|
export class ObjectState extends State {
|
|
@@ -10,12 +10,12 @@ export class ObjectState extends State {
|
|
|
10
10
|
return Object.keys(this.value).length;
|
|
11
11
|
}
|
|
12
12
|
/** Remove a named entry from this object. */
|
|
13
|
-
delete(
|
|
14
|
-
this.next(
|
|
13
|
+
delete(...keys) {
|
|
14
|
+
this.next(withoutProps(this.value, ...keys));
|
|
15
15
|
}
|
|
16
16
|
/** Set a named entry in this object with a different value. */
|
|
17
17
|
set(key, value) {
|
|
18
|
-
this.next(
|
|
18
|
+
this.next(withProp(this.value, key, value));
|
|
19
19
|
}
|
|
20
20
|
/** Iterate over the items. */
|
|
21
21
|
[Symbol.iterator]() {
|
package/update/ArrayUpdate.js
CHANGED
|
@@ -18,7 +18,7 @@ export class ArrayUpdate extends Update {
|
|
|
18
18
|
return new ArrayUpdate([], deletes);
|
|
19
19
|
}
|
|
20
20
|
transform(arr = []) {
|
|
21
|
-
return withoutItems(withItems(arr, this.adds), this.deletes);
|
|
21
|
+
return withoutItems(withItems(arr, ...this.adds), ...this.deletes);
|
|
22
22
|
}
|
|
23
23
|
validate(validator) {
|
|
24
24
|
if (!(validator instanceof ArraySchema))
|
package/update/DataUpdate.d.ts
CHANGED
|
@@ -16,9 +16,7 @@ export declare type Updates<T extends Data = Data> = {
|
|
|
16
16
|
/**
|
|
17
17
|
* Validate a set of updates against a set of validators.
|
|
18
18
|
*/
|
|
19
|
-
export declare function validateUpdates<T extends Data>(unsafeUpdates: Updates<T>, validators: Validators<T>):
|
|
20
|
-
[k: string]: import("../util/data.js").Value<Updates<T>>;
|
|
21
|
-
};
|
|
19
|
+
export declare function validateUpdates<T extends Data>(unsafeUpdates: Updates<T>, validators: Validators<T>): Updates<T>;
|
|
22
20
|
/**
|
|
23
21
|
* Update that can be applied to a data object to update its props.
|
|
24
22
|
*/
|
package/update/DataUpdate.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isFeedback } from "../feedback/Feedback.js";
|
|
2
2
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
3
3
|
import { DataSchema } from "../schema/DataSchema.js";
|
|
4
|
-
import { getProps } from "../util/
|
|
4
|
+
import { getProps } from "../util/object.js";
|
|
5
5
|
import { isNullish } from "../util/null.js";
|
|
6
6
|
import { transformData } from "../util/transform.js";
|
|
7
7
|
import { validate } from "../util/validate.js";
|
package/update/hydrations.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Hydrations } from "../util/hydrate.js";
|
|
2
|
-
/** Set of hydrations for all
|
|
3
|
-
export declare const
|
|
2
|
+
/** Set of hydrations for all update classes. */
|
|
3
|
+
export declare const UPDATE_HYDRATIONS: Hydrations;
|
package/update/hydrations.js
CHANGED
|
@@ -3,8 +3,8 @@ import { Increment } from "./Increment.js";
|
|
|
3
3
|
import { ArrayUpdate } from "./ArrayUpdate.js";
|
|
4
4
|
import { ObjectUpdate } from "./ObjectUpdate.js";
|
|
5
5
|
import { DataUpdate } from "./DataUpdate.js";
|
|
6
|
-
/** Set of hydrations for all
|
|
7
|
-
export const
|
|
6
|
+
/** Set of hydrations for all update classes. */
|
|
7
|
+
export const UPDATE_HYDRATIONS = {
|
|
8
8
|
Increment,
|
|
9
9
|
Delete,
|
|
10
10
|
ArrayUpdate,
|
package/util/array.d.ts
CHANGED
|
@@ -23,17 +23,6 @@ export declare const isItem: <T>(arr: ImmutableArray<T>, item: unknown) => item
|
|
|
23
23
|
export declare type PossibleArray<T> = ImmutableArray<T> | Iterable<T>;
|
|
24
24
|
/** Convert an iterable to an array (if its not already an array). */
|
|
25
25
|
export declare function getArray<T>(items: PossibleArray<T>): ImmutableArray<T>;
|
|
26
|
-
/**
|
|
27
|
-
* Add an item to an array (immutably).
|
|
28
|
-
* - Returns an array that definitely contains the specified item.
|
|
29
|
-
*
|
|
30
|
-
* @param input The input array to add items to.
|
|
31
|
-
* @param item The item to add.
|
|
32
|
-
*
|
|
33
|
-
* @return New array with the specified item.
|
|
34
|
-
* - If the item already exists in the array (using `indexOf()`) then the item won't be added again and the exact same input array will be returned.
|
|
35
|
-
*/
|
|
36
|
-
export declare function withItem<T>(input: ImmutableArray<T>, item: T): ImmutableArray<T>;
|
|
37
26
|
/**
|
|
38
27
|
* Add multiple items to an array (immutably).
|
|
39
28
|
* - Returns an array that definitely contains the specified item.
|
|
@@ -41,21 +30,9 @@ export declare function withItem<T>(input: ImmutableArray<T>, item: T): Immutabl
|
|
|
41
30
|
* @param input The input array to add items to.
|
|
42
31
|
* @param items The array of items to add.
|
|
43
32
|
*
|
|
44
|
-
* @return New array with the specified item.
|
|
45
|
-
* - If all items already exist in the array (using `indexOf()`) then the items won't be added again and the exact same input array will be returned.
|
|
33
|
+
* @return New array with the specified item (or same array if no items were added).
|
|
46
34
|
*/
|
|
47
|
-
export declare function withItems<T>(input: ImmutableArray<T>, items:
|
|
48
|
-
/**
|
|
49
|
-
* Remove an item from an array (immutably).
|
|
50
|
-
* - Finds all instances of the item from the array and returns an array that definitely does not contain it.
|
|
51
|
-
*
|
|
52
|
-
* @param input The input array to remove items from.
|
|
53
|
-
* @param item The item to remove.
|
|
54
|
-
*
|
|
55
|
-
* @return New array without the specified item.
|
|
56
|
-
* - If the item does not already exist in the array (using `indexOf()`) then the exact same input array will be returned.
|
|
57
|
-
*/
|
|
58
|
-
export declare function withoutItem<T>(input: ImmutableArray<T>, item: T): ImmutableArray<T>;
|
|
35
|
+
export declare function withItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
|
|
59
36
|
/**
|
|
60
37
|
* Remove multiple items from an array (immutably).
|
|
61
38
|
* - Finds all instances of a number of items from an array and returns an array that definitely does not contain them.
|
|
@@ -63,20 +40,9 @@ export declare function withoutItem<T>(input: ImmutableArray<T>, item: T): Immut
|
|
|
63
40
|
* @param input The input array to remove items from.
|
|
64
41
|
* @param items The array of items to add.
|
|
65
42
|
*
|
|
66
|
-
* @return New array without the specified
|
|
67
|
-
* - If the items do not already exist in the array (using `indexOf()`) then the exact same input array will be returned.
|
|
43
|
+
* @return New array without the specified items (or same array if no items were removed).
|
|
68
44
|
*/
|
|
69
|
-
export declare function withoutItems<T>(input: ImmutableArray<T>, items:
|
|
70
|
-
/**
|
|
71
|
-
* Toggle an item in and out of an array (immutably).
|
|
72
|
-
* - If the item is being removed from the array ALL instances of that item in the array will be removed.
|
|
73
|
-
*
|
|
74
|
-
* @param input The input array to toggle items from.
|
|
75
|
-
* @param item The items to toggle.
|
|
76
|
-
*
|
|
77
|
-
* @return New array with or without the specified item.
|
|
78
|
-
*/
|
|
79
|
-
export declare function toggleItem<T>(input: ImmutableArray<T>, item: T): ImmutableArray<T>;
|
|
45
|
+
export declare function withoutItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
|
|
80
46
|
/**
|
|
81
47
|
* Toggle an item in and out of an array (immutably).
|
|
82
48
|
* - If an item is being removed from the array ALL instances of that item in the array will be removed.
|
|
@@ -84,21 +50,9 @@ export declare function toggleItem<T>(input: ImmutableArray<T>, item: T): Immuta
|
|
|
84
50
|
* @param input The input array to toggle items from.
|
|
85
51
|
* @param items The array of items to toggle.
|
|
86
52
|
*
|
|
87
|
-
* @return New array with or without the specified
|
|
88
|
-
*/
|
|
89
|
-
export declare function toggleItems<T>(input: ImmutableArray<T>, items: Iterable<T>): ImmutableArray<T>;
|
|
90
|
-
/**
|
|
91
|
-
* Swap an item in an array for a new item (immutably).
|
|
92
|
-
* - All instances of the item in the array will be swapped for the new item.
|
|
93
|
-
*
|
|
94
|
-
* @param input The input array to swap items in.
|
|
95
|
-
* @param oldItem The item to replace with `newItem`
|
|
96
|
-
* @param newItem The item to replace `oldItem` with.
|
|
97
|
-
*
|
|
98
|
-
* @return New array with or without the specified item.
|
|
99
|
-
* - If the item does not already exist in the array (using `indexOf()`) then the exact same input array will be returned.
|
|
53
|
+
* @return New array with or without the specified items (or same array if no items were toggled).
|
|
100
54
|
*/
|
|
101
|
-
export declare function
|
|
55
|
+
export declare function toggleItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
|
|
102
56
|
/** Get the first item from an array or iterable, or `null` if it didn't exist. */
|
|
103
57
|
export declare function getOptionalFirstItem<T>(items: ImmutableArray<T> | Iterable<T>): T | null;
|
|
104
58
|
/** Get the first item from an array or iterable. */
|
package/util/array.js
CHANGED
|
@@ -13,20 +13,6 @@ export const isItem = (arr, item) => arr.includes(item);
|
|
|
13
13
|
export function getArray(items) {
|
|
14
14
|
return isArray(items) ? items : Array.from(items);
|
|
15
15
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Add an item to an array (immutably).
|
|
18
|
-
* - Returns an array that definitely contains the specified item.
|
|
19
|
-
*
|
|
20
|
-
* @param input The input array to add items to.
|
|
21
|
-
* @param item The item to add.
|
|
22
|
-
*
|
|
23
|
-
* @return New array with the specified item.
|
|
24
|
-
* - If the item already exists in the array (using `indexOf()`) then the item won't be added again and the exact same input array will be returned.
|
|
25
|
-
*/
|
|
26
|
-
export function withItem(input, item) {
|
|
27
|
-
const i = input.indexOf(item);
|
|
28
|
-
return i >= 0 ? input : [...input, item];
|
|
29
|
-
}
|
|
30
16
|
/**
|
|
31
17
|
* Add multiple items to an array (immutably).
|
|
32
18
|
* - Returns an array that definitely contains the specified item.
|
|
@@ -34,35 +20,11 @@ export function withItem(input, item) {
|
|
|
34
20
|
* @param input The input array to add items to.
|
|
35
21
|
* @param items The array of items to add.
|
|
36
22
|
*
|
|
37
|
-
* @return New array with the specified item.
|
|
38
|
-
* - If all items already exist in the array (using `indexOf()`) then the items won't be added again and the exact same input array will be returned.
|
|
39
|
-
*/
|
|
40
|
-
export function withItems(input, items) {
|
|
41
|
-
let output = input;
|
|
42
|
-
for (const item of items)
|
|
43
|
-
output = withItem(output, item);
|
|
44
|
-
return output;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Remove an item from an array (immutably).
|
|
48
|
-
* - Finds all instances of the item from the array and returns an array that definitely does not contain it.
|
|
49
|
-
*
|
|
50
|
-
* @param input The input array to remove items from.
|
|
51
|
-
* @param item The item to remove.
|
|
52
|
-
*
|
|
53
|
-
* @return New array without the specified item.
|
|
54
|
-
* - If the item does not already exist in the array (using `indexOf()`) then the exact same input array will be returned.
|
|
23
|
+
* @return New array with the specified item (or same array if no items were added).
|
|
55
24
|
*/
|
|
56
|
-
export function
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return input;
|
|
60
|
-
const output = input.slice();
|
|
61
|
-
while (i >= 0) {
|
|
62
|
-
output.splice(i, 1);
|
|
63
|
-
i = output.indexOf(item, i);
|
|
64
|
-
}
|
|
65
|
-
return output;
|
|
25
|
+
export function withItems(input, ...items) {
|
|
26
|
+
const extras = items.filter(_doesNotInclude, input);
|
|
27
|
+
return extras.length ? [...input, ...extras] : input;
|
|
66
28
|
}
|
|
67
29
|
/**
|
|
68
30
|
* Remove multiple items from an array (immutably).
|
|
@@ -71,27 +33,14 @@ export function withoutItem(input, item) {
|
|
|
71
33
|
* @param input The input array to remove items from.
|
|
72
34
|
* @param items The array of items to add.
|
|
73
35
|
*
|
|
74
|
-
* @return New array without the specified
|
|
75
|
-
* - If the items do not already exist in the array (using `indexOf()`) then the exact same input array will be returned.
|
|
36
|
+
* @return New array without the specified items (or same array if no items were removed).
|
|
76
37
|
*/
|
|
77
|
-
export function withoutItems(input, items) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
output = withoutItem(output, item);
|
|
81
|
-
return output;
|
|
38
|
+
export function withoutItems(input, ...items) {
|
|
39
|
+
const output = input.filter(_doesNotInclude, items);
|
|
40
|
+
return output.length === input.length ? input : output;
|
|
82
41
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
* - If the item is being removed from the array ALL instances of that item in the array will be removed.
|
|
86
|
-
*
|
|
87
|
-
* @param input The input array to toggle items from.
|
|
88
|
-
* @param item The items to toggle.
|
|
89
|
-
*
|
|
90
|
-
* @return New array with or without the specified item.
|
|
91
|
-
*/
|
|
92
|
-
export function toggleItem(input, item) {
|
|
93
|
-
const i = input.indexOf(item);
|
|
94
|
-
return i >= 0 ? withoutItem(input, item) : [...input, item];
|
|
42
|
+
function _doesNotInclude(value) {
|
|
43
|
+
return !this.includes(value);
|
|
95
44
|
}
|
|
96
45
|
/**
|
|
97
46
|
* Toggle an item in and out of an array (immutably).
|
|
@@ -100,35 +49,12 @@ export function toggleItem(input, item) {
|
|
|
100
49
|
* @param input The input array to toggle items from.
|
|
101
50
|
* @param items The array of items to toggle.
|
|
102
51
|
*
|
|
103
|
-
* @return New array with or without the specified
|
|
52
|
+
* @return New array with or without the specified items (or same array if no items were toggled).
|
|
104
53
|
*/
|
|
105
|
-
export function toggleItems(input, items) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return output;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Swap an item in an array for a new item (immutably).
|
|
113
|
-
* - All instances of the item in the array will be swapped for the new item.
|
|
114
|
-
*
|
|
115
|
-
* @param input The input array to swap items in.
|
|
116
|
-
* @param oldItem The item to replace with `newItem`
|
|
117
|
-
* @param newItem The item to replace `oldItem` with.
|
|
118
|
-
*
|
|
119
|
-
* @return New array with or without the specified item.
|
|
120
|
-
* - If the item does not already exist in the array (using `indexOf()`) then the exact same input array will be returned.
|
|
121
|
-
*/
|
|
122
|
-
export function swapItem(input, oldItem, newItem) {
|
|
123
|
-
let i = input.indexOf(oldItem);
|
|
124
|
-
if (i < 0)
|
|
125
|
-
return input;
|
|
126
|
-
const output = input.slice();
|
|
127
|
-
while (i >= 0) {
|
|
128
|
-
output[i] = newItem;
|
|
129
|
-
i = output.indexOf(newItem, i + 1);
|
|
130
|
-
}
|
|
131
|
-
return output;
|
|
54
|
+
export function toggleItems(input, ...items) {
|
|
55
|
+
const extras = items.filter(_doesNotInclude, input);
|
|
56
|
+
const output = input.filter(_doesNotInclude, items);
|
|
57
|
+
return extras.length ? [...output, ...extras] : output.length !== input.length ? output : input;
|
|
132
58
|
}
|
|
133
59
|
/** Get the first item from an array or iterable, or `null` if it didn't exist. */
|
|
134
60
|
export function getOptionalFirstItem(items) {
|
package/util/data.d.ts
CHANGED
|
@@ -1,79 +1,27 @@
|
|
|
1
|
-
import type { ImmutableArray } from "./array.js";
|
|
2
1
|
/** Data object. */
|
|
3
2
|
export declare type Data = {
|
|
4
|
-
readonly [
|
|
3
|
+
readonly [K in string]: unknown;
|
|
5
4
|
};
|
|
5
|
+
/** Key from a data object. */
|
|
6
|
+
export declare type Key<T extends Data> = keyof T & string;
|
|
7
|
+
/** Value from a data object. */
|
|
8
|
+
export declare type Value<T extends Data> = T[keyof T & string];
|
|
9
|
+
/** Prop from a data object in entry format. */
|
|
10
|
+
export declare type Prop<T extends Data> = readonly [Key<T>, Value<T>];
|
|
6
11
|
/** Data or `null` to indicate the data doesn't exist. */
|
|
7
12
|
export declare type OptionalData<T extends Data> = T | null;
|
|
8
13
|
/** Empty data object. */
|
|
9
14
|
export declare type EmptyData = {
|
|
10
15
|
readonly [K in never]: never;
|
|
11
16
|
};
|
|
12
|
-
/** Key for a prop in a data object. */
|
|
13
|
-
export declare type Key<T extends Data> = keyof T & string;
|
|
14
|
-
/** Value for a prop in a data object. */
|
|
15
|
-
export declare type Value<T extends Data> = T[Key<T>];
|
|
16
|
-
/** Prop of a data object in entry format. */
|
|
17
|
-
export declare type Prop<T extends Data> = readonly [Key<T>, Value<T>];
|
|
18
17
|
/** Set of named data objects. */
|
|
19
18
|
export declare type Datas = {
|
|
20
|
-
readonly [
|
|
19
|
+
readonly [K in string]: Data;
|
|
21
20
|
};
|
|
22
21
|
/** Is an unknown value a data object? */
|
|
23
22
|
export declare const isData: <T extends Data>(value: unknown) => value is T;
|
|
24
|
-
/** Turn a data object into an array of props. */
|
|
25
|
-
export declare function getProps<T extends Data>(data: T): ImmutableArray<Prop<T>>;
|
|
26
|
-
export declare function getProps<T extends Data>(data: Partial<T>): ImmutableArray<Prop<T>>;
|
|
27
23
|
/** Get the data of a result (returns data or throws `RequiredError` if value is `null` or `undefined`). */
|
|
28
24
|
export declare function getData<T extends Data>(result: OptionalData<T>): T;
|
|
29
|
-
/**
|
|
30
|
-
* Extract the value of a named prop from a data object.
|
|
31
|
-
* - Extraction is possibly deep if deeper keys are specified.
|
|
32
|
-
*
|
|
33
|
-
* @param obj The target object to get from.
|
|
34
|
-
* @param k1 The key of the prop in the object to get.
|
|
35
|
-
* @param k2 The sub-key of the prop in the object to get.
|
|
36
|
-
* @param k3 The sub-sub-key of the prop in the object to get.
|
|
37
|
-
* @param k4 The sub-sub-sub-key of the prop in the object to get.
|
|
38
|
-
*/
|
|
39
|
-
export declare function getProp<T extends Data, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(obj: T, k1: K1, k2: K2, k3: K3, k4: K4): T[K1][K2][K3][K4];
|
|
40
|
-
export declare function getProp<T extends Data, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(obj: T, k1: K1, k2: K2, k3: K3): T[K1][K2][K3];
|
|
41
|
-
export declare function getProp<T extends Data, K1 extends keyof T, K2 extends keyof T[K1]>(obj: T, k1: K1, k2: K2): T[K1][K2];
|
|
42
|
-
export declare function getProp<T extends Data, K1 extends keyof T>(obj: T, k1: K1): T[K1];
|
|
43
|
-
/**
|
|
44
|
-
* Set a prop on a data object with known shape (immutably).
|
|
45
|
-
*
|
|
46
|
-
* @param data The input data object.
|
|
47
|
-
* @param key The key of the entry to add.
|
|
48
|
-
* @param value The value of the entry to add. If set, the entry will only be added if its current value is not `value`
|
|
49
|
-
*
|
|
50
|
-
* @return New object without the specified prop (or same object if prop value didn't change).
|
|
51
|
-
*/
|
|
52
|
-
export declare function withProp<T extends Data, K extends Key<T>>(data: T, key: K, value: T[K]): T;
|
|
53
|
-
/**
|
|
54
|
-
* Set several props on a data object with known shape (immutably).
|
|
55
|
-
*
|
|
56
|
-
* @param data The input data object.
|
|
57
|
-
* @return New object with the specified prop added (or same object if no props changed).
|
|
58
|
-
*/
|
|
59
|
-
export declare function withProps<T extends Data>(data: T, props: T | Partial<T>): T;
|
|
60
|
-
/**
|
|
61
|
-
* Set a single named prop on an object with a known shape (by reference).
|
|
62
|
-
*
|
|
63
|
-
* @param data The target data object to modify.
|
|
64
|
-
* @param key The key of the prop in the object to set.
|
|
65
|
-
* @param value The value to set the prop to.
|
|
66
|
-
*/
|
|
67
|
-
export declare function setProp<T extends Data, K extends keyof T>(data: T, key: K, value: T[K]): T[K];
|
|
68
|
-
/**
|
|
69
|
-
* Set several named props on a data object with a known shape (by reference).
|
|
70
|
-
*
|
|
71
|
-
* @param data The target data object to modify.
|
|
72
|
-
* @param props An object containing new props to set on the object.
|
|
73
|
-
*/
|
|
74
|
-
export declare function setProps<T extends Data>(data: T, props: {
|
|
75
|
-
[K in keyof T]?: T[K];
|
|
76
|
-
}): void;
|
|
77
25
|
/**
|
|
78
26
|
* Mutable type is the opposite of `Readonly<T>` helper type.
|
|
79
27
|
* - See https://github.com/microsoft/TypeScript/issues/24509
|
package/util/data.js
CHANGED
|
@@ -1,63 +1,12 @@
|
|
|
1
1
|
import { RequiredError } from "../error/RequiredError.js";
|
|
2
2
|
/** Is an unknown value a data object? */
|
|
3
3
|
export const isData = (value) => typeof value === "object" && value !== null;
|
|
4
|
-
export function getProps(data) {
|
|
5
|
-
return Object.entries(data);
|
|
6
|
-
}
|
|
7
4
|
/** Get the data of a result (returns data or throws `RequiredError` if value is `null` or `undefined`). */
|
|
8
5
|
export function getData(result) {
|
|
9
6
|
if (!result)
|
|
10
7
|
throw new RequiredError("Data is required");
|
|
11
8
|
return result;
|
|
12
9
|
}
|
|
13
|
-
export function getProp(data, k1, k2, k3, k4) {
|
|
14
|
-
return !k2 ? data[k1] : !k3 ? data[k1][k2] : !k4 ? data[k1][k2][k3] : data[k1][k2][k3][k4];
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Set a prop on a data object with known shape (immutably).
|
|
18
|
-
*
|
|
19
|
-
* @param data The input data object.
|
|
20
|
-
* @param key The key of the entry to add.
|
|
21
|
-
* @param value The value of the entry to add. If set, the entry will only be added if its current value is not `value`
|
|
22
|
-
*
|
|
23
|
-
* @return New object without the specified prop (or same object if prop value didn't change).
|
|
24
|
-
*/
|
|
25
|
-
export function withProp(data, key, value) {
|
|
26
|
-
return data[key] === value ? data : { ...data, [key]: value };
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Set several props on a data object with known shape (immutably).
|
|
30
|
-
*
|
|
31
|
-
* @param data The input data object.
|
|
32
|
-
* @return New object with the specified prop added (or same object if no props changed).
|
|
33
|
-
*/
|
|
34
|
-
export function withProps(data, props) {
|
|
35
|
-
for (const [k, v] of Object.entries(props))
|
|
36
|
-
if (data[k] !== v)
|
|
37
|
-
return { ...data, ...props };
|
|
38
|
-
return data;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Set a single named prop on an object with a known shape (by reference).
|
|
42
|
-
*
|
|
43
|
-
* @param data The target data object to modify.
|
|
44
|
-
* @param key The key of the prop in the object to set.
|
|
45
|
-
* @param value The value to set the prop to.
|
|
46
|
-
*/
|
|
47
|
-
export function setProp(data, key, value) {
|
|
48
|
-
data[key] = value;
|
|
49
|
-
return value;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Set several named props on a data object with a known shape (by reference).
|
|
53
|
-
*
|
|
54
|
-
* @param data The target data object to modify.
|
|
55
|
-
* @param props An object containing new props to set on the object.
|
|
56
|
-
*/
|
|
57
|
-
export function setProps(data, props) {
|
|
58
|
-
for (const [k, v] of getProps(props))
|
|
59
|
-
data[k] = v;
|
|
60
|
-
}
|
|
61
10
|
/**
|
|
62
11
|
* Format a data object as a string.
|
|
63
12
|
* - Use the custom `.toString()` function if it exists (don't use built in `Object.prototype.toString` because it's useless.
|
package/util/entry.d.ts
CHANGED
|
@@ -8,9 +8,9 @@ import { ImmutableMap } from "./map.js";
|
|
|
8
8
|
*/
|
|
9
9
|
export declare type Entry<K, T> = readonly [K, T];
|
|
10
10
|
/** Extract the type for the value of an entry. */
|
|
11
|
-
export declare type
|
|
11
|
+
export declare type EntryKey<X> = X extends Entry<infer Y, unknown> ? Y : never;
|
|
12
12
|
/** Extract the type for the value of an entry. */
|
|
13
|
-
export declare type
|
|
13
|
+
export declare type EntryValue<X> = X extends Entry<unknown, infer Y> ? Y : never;
|
|
14
14
|
/** Extract the key from an object entry. */
|
|
15
15
|
export declare const getEntryKey: <K, T>([k]: Entry<K, T>) => K;
|
|
16
16
|
/** Extract the value from an object entry. */
|