shelving 1.79.0 → 1.80.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/modules/constraint/Constraints.js +2 -2
- package/modules/constraint/QueryConstraints.d.ts +1 -1
- package/modules/constraint/QueryConstraints.js +1 -1
- package/modules/schema/AllowSchema.js +2 -2
- package/modules/state/ArrayState.d.ts +6 -8
- package/modules/state/ArrayState.js +10 -14
- package/modules/state/DataState.js +2 -1
- package/modules/state/ObjectState.d.ts +1 -1
- package/modules/state/ObjectState.js +4 -4
- package/modules/update/ArrayUpdate.js +1 -1
- package/modules/update/DataUpdate.d.ts +1 -3
- package/modules/update/DataUpdate.js +1 -1
- package/modules/update/hydrations.d.ts +2 -2
- package/modules/update/hydrations.js +2 -2
- package/modules/util/array.d.ts +6 -52
- package/modules/util/array.js +15 -89
- package/modules/util/data.d.ts +8 -60
- package/modules/util/data.js +0 -51
- package/modules/util/entry.d.ts +2 -2
- package/modules/util/object.d.ts +49 -41
- package/modules/util/object.js +56 -53
- package/modules/util/transform.d.ts +8 -12
- package/modules/util/transform.js +20 -8
- package/modules/util/validate.d.ts +2 -2
- package/modules/util/validate.js +1 -1
- package/package.json +1 -1
|
@@ -20,12 +20,12 @@ export class Constraints extends Constraint {
|
|
|
20
20
|
}
|
|
21
21
|
/** Clone this set of constraints but add additional constraints. */
|
|
22
22
|
with(...constraints) {
|
|
23
|
-
const _constraints = withItems(this._constraints, constraints);
|
|
23
|
+
const _constraints = withItems(this._constraints, ...constraints);
|
|
24
24
|
return _constraints !== this._constraints ? { __proto__: Object.getPrototypeOf(this), ...this, _constraints: _constraints } : this;
|
|
25
25
|
}
|
|
26
26
|
/** Clone this set of constraints but remove specific constraints. */
|
|
27
27
|
without(...constraints) {
|
|
28
|
-
const _constraints = withoutItems(this._constraints, constraints);
|
|
28
|
+
const _constraints = withoutItems(this._constraints, ...constraints);
|
|
29
29
|
return _constraints !== this._constraints ? { __proto__: Object.getPrototypeOf(this), ...this, _constraints: _constraints } : this;
|
|
30
30
|
}
|
|
31
31
|
/** Iterate over the constraints. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Data } from "../util/data.js";
|
|
1
|
+
import type { Data } from "../util/data.js";
|
|
2
2
|
import { Filterable, FilterConstraints } from "./FilterConstraints.js";
|
|
3
3
|
import { Sortable, SortConstraints } from "./SortConstraints.js";
|
|
4
4
|
import { Constraint } from "./Constraint.js";
|
|
@@ -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 });
|
|
@@ -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
|
}
|
|
@@ -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]() {
|
|
@@ -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. */
|
|
@@ -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. */
|
|
@@ -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]() {
|
|
@@ -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))
|
|
@@ -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
|
*/
|
|
@@ -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";
|
|
@@ -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;
|
|
@@ -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/modules/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/modules/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/modules/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/modules/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/modules/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. */
|
package/modules/util/object.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ImmutableArray } from "./array.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Data, Prop } from "./data.js";
|
|
3
|
+
import type { Entry } from "./entry.js";
|
|
3
4
|
/** Readonly object with string keys. */
|
|
4
5
|
export declare type ImmutableObject<T = unknown> = {
|
|
5
6
|
readonly [key: string | number]: T;
|
|
@@ -8,8 +9,6 @@ export declare type ImmutableObject<T = unknown> = {
|
|
|
8
9
|
export declare type MutableObject<T = unknown> = {
|
|
9
10
|
[key: string | number]: T;
|
|
10
11
|
};
|
|
11
|
-
/** Extract the type for the values of an object. */
|
|
12
|
-
export declare type ObjectType<T extends ImmutableObject> = T[keyof T];
|
|
13
12
|
/**
|
|
14
13
|
* Is a value an unknown object?
|
|
15
14
|
* - This is a TypeScript assertion object that asserts the value extends `ImmutableObject`
|
|
@@ -23,75 +22,84 @@ export declare function isPlainObject<T extends ImmutableObject>(value: T | unkn
|
|
|
23
22
|
/** Assert that a value is a plain object */
|
|
24
23
|
export declare function assertPlainObject<T extends ImmutableObject>(value: T | unknown): asserts value is T;
|
|
25
24
|
/** Is an unknown string an own prop of an object. */
|
|
26
|
-
export declare const
|
|
25
|
+
export declare const isProp: <T extends ImmutableObject<unknown>>(obj: T, key: unknown) => key is keyof T;
|
|
26
|
+
/** Turn a data object into an array of props. */
|
|
27
|
+
export declare function getProps<T extends Data>(data: T): ImmutableArray<Prop<T>>;
|
|
28
|
+
export declare function getProps<T extends Data>(data: T | Partial<T>): ImmutableArray<Prop<T>>;
|
|
29
|
+
export declare function getProps<T extends ImmutableObject>(data: T): ImmutableArray<Entry<keyof T, T[keyof T]>>;
|
|
30
|
+
export declare function getProps<T extends ImmutableObject>(data: T | Partial<T>): ImmutableArray<Entry<keyof T, T[keyof T]>>;
|
|
27
31
|
/**
|
|
28
|
-
*
|
|
32
|
+
* Extract the value of a named prop from a data object.
|
|
33
|
+
* - Extraction is possibly deep if deeper keys are specified.
|
|
29
34
|
*
|
|
30
|
-
* @param
|
|
35
|
+
* @param obj The target object to get from.
|
|
36
|
+
* @param k1 The key of the prop in the object to get.
|
|
37
|
+
* @param k2 The sub-key of the prop in the object to get.
|
|
38
|
+
* @param k3 The sub-sub-key of the prop in the object to get.
|
|
39
|
+
* @param k4 The sub-sub-sub-key of the prop in the object to get.
|
|
40
|
+
*/
|
|
41
|
+
export declare function getProp<T extends ImmutableObject, 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];
|
|
42
|
+
export declare function getProp<T extends ImmutableObject, 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];
|
|
43
|
+
export declare function getProp<T extends ImmutableObject, K1 extends keyof T, K2 extends keyof T[K1]>(obj: T, k1: K1, k2: K2): T[K1][K2];
|
|
44
|
+
export declare function getProp<T extends ImmutableObject, K1 extends keyof T>(obj: T, k1: K1): T[K1];
|
|
45
|
+
/**
|
|
46
|
+
* Set a prop on an object (immutably).
|
|
47
|
+
*
|
|
48
|
+
* @param input The input data object.
|
|
31
49
|
* @param key The key of the entry to add.
|
|
32
50
|
* @param value The value of the entry to add. If set, the entry will only be added if its current value is not `value`
|
|
33
51
|
*
|
|
34
|
-
* @return New object without the specified
|
|
52
|
+
* @return New object without the specified prop (or same object if prop value didn't change).
|
|
35
53
|
*/
|
|
36
|
-
export declare
|
|
54
|
+
export declare function withProp<T extends ImmutableObject, K extends keyof T>(input: T, key: K, value: T[K]): T;
|
|
37
55
|
/**
|
|
38
|
-
*
|
|
56
|
+
* Set several props on an object (immutably).
|
|
39
57
|
*
|
|
40
|
-
* @param input The input object.
|
|
41
|
-
* @
|
|
58
|
+
* @param input The input data object.
|
|
59
|
+
* @param props Set of props to add to the object.
|
|
60
|
+
* @return New object with the specified prop added (or same object if no props changed).
|
|
42
61
|
*/
|
|
43
|
-
export declare
|
|
62
|
+
export declare function withProps<T extends ImmutableObject>(input: T, props: T | Partial<T>): T;
|
|
44
63
|
/**
|
|
45
|
-
* Remove
|
|
64
|
+
* Remove several key/value entries from an object (immutably).
|
|
46
65
|
*
|
|
47
66
|
* @param input The input object.
|
|
48
|
-
* @param
|
|
49
|
-
* @param value The value of the entry to remove. If not undefined, the entry will only be removed if its current value is exactly `value`
|
|
67
|
+
* @param keys Set of keys for props to remove.
|
|
50
68
|
*
|
|
51
|
-
* @return New object without the specified entries (or same object if the
|
|
52
|
-
* - If `key` doesn't already exist in `obj` then the exact same input object will be returned.
|
|
69
|
+
* @return New object without the specified entries (or same object if none of the entries existed).
|
|
53
70
|
*/
|
|
54
|
-
export declare function
|
|
71
|
+
export declare function withoutProps<T extends ImmutableObject, K extends keyof T>(input: T, ...keys: K[]): Omit<T, K>;
|
|
55
72
|
/**
|
|
56
|
-
*
|
|
73
|
+
* Pick several props from an object (immutably).
|
|
57
74
|
*
|
|
58
75
|
* @param input The input object.
|
|
59
|
-
* @param keys Set of keys
|
|
76
|
+
* @param keys Set of keys for props to pick.
|
|
60
77
|
*
|
|
61
|
-
* @return New object
|
|
62
|
-
* - If every entry already exists and has the exact same input object will be returned.
|
|
78
|
+
* @return New object with only the specified props.
|
|
63
79
|
*/
|
|
64
|
-
export declare function
|
|
80
|
+
export declare function pickProps<T extends ImmutableObject, K extends keyof T>(input: T, ...keys: (keyof T)[]): Pick<T, K>;
|
|
65
81
|
/**
|
|
66
|
-
* Set a
|
|
82
|
+
* Set a single named prop on an object (by reference).
|
|
67
83
|
*
|
|
68
|
-
* @param
|
|
84
|
+
* @param data The target data object to modify.
|
|
69
85
|
* @param key The key of the prop in the object to set.
|
|
70
86
|
* @param value The value to set the prop to.
|
|
71
87
|
*/
|
|
72
|
-
export declare
|
|
73
|
-
/**
|
|
74
|
-
* Set several key/value entries on a map-like object (by reference).
|
|
75
|
-
*
|
|
76
|
-
* @param obj The target object to modify.
|
|
77
|
-
* @param entries An object containing new props to set on the object.
|
|
78
|
-
*/
|
|
79
|
-
export declare const setEntries: <T>(obj: MutableObject<T>, entries: ImmutableObject<T>) => void;
|
|
88
|
+
export declare function setProp<T extends MutableObject, K extends keyof T>(data: T, key: K, value: T[K]): T[K];
|
|
80
89
|
/**
|
|
81
|
-
*
|
|
90
|
+
* Set several named props on an object (by reference).
|
|
82
91
|
*
|
|
83
92
|
* @param obj The target object to modify.
|
|
84
|
-
* @param
|
|
85
|
-
* @param value The value of the entry to remove. If set, the entry will only be removed if its current value is exactly `value`
|
|
93
|
+
* @param props An object containing new props to set on the object.
|
|
86
94
|
*/
|
|
87
|
-
export declare function
|
|
95
|
+
export declare function setProps<T extends MutableObject>(obj: T, props: Partial<T>): void;
|
|
88
96
|
/**
|
|
89
|
-
* Remove several key/value entries from
|
|
97
|
+
* Remove several key/value entries from an object (by reference).
|
|
90
98
|
*
|
|
91
99
|
* @param obj The target object to modify.
|
|
92
|
-
* @param
|
|
100
|
+
* @param keys Set of keys or keys to remove.
|
|
93
101
|
*/
|
|
94
|
-
export declare function
|
|
102
|
+
export declare function deleteProps<T extends MutableObject>(obj: T, ...keys: (keyof T)[]): void;
|
|
95
103
|
/** Type that represents an empty object. */
|
|
96
104
|
export declare type EmptyObject = {
|
|
97
105
|
readonly [K in never]: never;
|
|
@@ -99,4 +107,4 @@ export declare type EmptyObject = {
|
|
|
99
107
|
/** An empty object. */
|
|
100
108
|
export declare const EMPTY_OBJECT: EmptyObject;
|
|
101
109
|
/** Function that returns an an empty object. */
|
|
102
|
-
export declare const
|
|
110
|
+
export declare const getEmptyObject: () => EmptyObject;
|
package/modules/util/object.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { AssertionError } from "../error/AssertionError.js";
|
|
2
|
-
import { setProp, setProps, withProp, withProps } from "./data.js";
|
|
3
2
|
/**
|
|
4
3
|
* Is a value an unknown object?
|
|
5
4
|
* - This is a TypeScript assertion object that asserts the value extends `ImmutableObject`
|
|
@@ -25,97 +24,101 @@ export function assertPlainObject(value) {
|
|
|
25
24
|
throw new AssertionError(`Must be plain object`, value);
|
|
26
25
|
}
|
|
27
26
|
/** Is an unknown string an own prop of an object. */
|
|
28
|
-
export const
|
|
27
|
+
export const isProp = (obj, key) => (typeof key === "string" || typeof key === "number" || typeof key === "symbol") && Object.prototype.hasOwnProperty.call(obj, key);
|
|
28
|
+
export function getProps(data) {
|
|
29
|
+
return Object.entries(data);
|
|
30
|
+
}
|
|
31
|
+
export function getProp(data, k1, k2, k3, k4) {
|
|
32
|
+
return k2 === undefined ? data[k1] : k3 === undefined ? data[k1][k2] : k4 === undefined ? data[k1][k2][k3] : data[k1][k2][k3][k4];
|
|
33
|
+
}
|
|
29
34
|
/**
|
|
30
|
-
*
|
|
35
|
+
* Set a prop on an object (immutably).
|
|
31
36
|
*
|
|
32
|
-
* @param input The input object.
|
|
37
|
+
* @param input The input data object.
|
|
33
38
|
* @param key The key of the entry to add.
|
|
34
39
|
* @param value The value of the entry to add. If set, the entry will only be added if its current value is not `value`
|
|
35
40
|
*
|
|
36
|
-
* @return New object without the specified
|
|
41
|
+
* @return New object without the specified prop (or same object if prop value didn't change).
|
|
37
42
|
*/
|
|
38
|
-
export
|
|
43
|
+
export function withProp(input, key, value) {
|
|
44
|
+
return input[key] === value ? input : { ...input, [key]: value };
|
|
45
|
+
}
|
|
39
46
|
/**
|
|
40
|
-
*
|
|
47
|
+
* Set several props on an object (immutably).
|
|
41
48
|
*
|
|
42
|
-
* @param input The input object.
|
|
43
|
-
* @
|
|
49
|
+
* @param input The input data object.
|
|
50
|
+
* @param props Set of props to add to the object.
|
|
51
|
+
* @return New object with the specified prop added (or same object if no props changed).
|
|
44
52
|
*/
|
|
45
|
-
export
|
|
53
|
+
export function withProps(input, props) {
|
|
54
|
+
for (const [k, v] of Object.entries(props))
|
|
55
|
+
if (input[k] !== v)
|
|
56
|
+
return { ...input, ...props };
|
|
57
|
+
return input;
|
|
58
|
+
}
|
|
46
59
|
/**
|
|
47
|
-
* Remove
|
|
60
|
+
* Remove several key/value entries from an object (immutably).
|
|
48
61
|
*
|
|
49
62
|
* @param input The input object.
|
|
50
|
-
* @param
|
|
51
|
-
* @param value The value of the entry to remove. If not undefined, the entry will only be removed if its current value is exactly `value`
|
|
63
|
+
* @param keys Set of keys for props to remove.
|
|
52
64
|
*
|
|
53
|
-
* @return New object without the specified entries (or same object if the
|
|
54
|
-
* - If `key` doesn't already exist in `obj` then the exact same input object will be returned.
|
|
65
|
+
* @return New object without the specified entries (or same object if none of the entries existed).
|
|
55
66
|
*/
|
|
56
|
-
export function
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
67
|
+
export function withoutProps(input, ...keys) {
|
|
68
|
+
for (const key of keys)
|
|
69
|
+
if (key in input)
|
|
70
|
+
return Object.fromEntries(Object.entries(input).filter(_doesntHaveKey, keys));
|
|
61
71
|
return input;
|
|
62
72
|
}
|
|
73
|
+
function _doesntHaveKey([key]) {
|
|
74
|
+
return !this.includes(key);
|
|
75
|
+
}
|
|
63
76
|
/**
|
|
64
|
-
*
|
|
77
|
+
* Pick several props from an object (immutably).
|
|
65
78
|
*
|
|
66
79
|
* @param input The input object.
|
|
67
|
-
* @param keys Set of keys
|
|
80
|
+
* @param keys Set of keys for props to pick.
|
|
68
81
|
*
|
|
69
|
-
* @return New object
|
|
70
|
-
* - If every entry already exists and has the exact same input object will be returned.
|
|
82
|
+
* @return New object with only the specified props.
|
|
71
83
|
*/
|
|
72
|
-
export function
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
delete output[key];
|
|
78
|
-
changed = true;
|
|
79
|
-
}
|
|
80
|
-
return changed ? output : input;
|
|
84
|
+
export function pickProps(input, ...keys) {
|
|
85
|
+
return Object.fromEntries(Object.entries(input).filter(_doesHaveKey, keys));
|
|
86
|
+
}
|
|
87
|
+
function _doesHaveKey([key]) {
|
|
88
|
+
return this.includes(key);
|
|
81
89
|
}
|
|
82
90
|
/**
|
|
83
|
-
* Set a
|
|
91
|
+
* Set a single named prop on an object (by reference).
|
|
84
92
|
*
|
|
85
|
-
* @param
|
|
93
|
+
* @param data The target data object to modify.
|
|
86
94
|
* @param key The key of the prop in the object to set.
|
|
87
95
|
* @param value The value to set the prop to.
|
|
88
96
|
*/
|
|
89
|
-
export
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
* @param obj The target object to modify.
|
|
94
|
-
* @param entries An object containing new props to set on the object.
|
|
95
|
-
*/
|
|
96
|
-
export const setEntries = setProps;
|
|
97
|
+
export function setProp(data, key, value) {
|
|
98
|
+
data[key] = value;
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
97
101
|
/**
|
|
98
|
-
*
|
|
102
|
+
* Set several named props on an object (by reference).
|
|
99
103
|
*
|
|
100
104
|
* @param obj The target object to modify.
|
|
101
|
-
* @param
|
|
102
|
-
* @param value The value of the entry to remove. If set, the entry will only be removed if its current value is exactly `value`
|
|
105
|
+
* @param props An object containing new props to set on the object.
|
|
103
106
|
*/
|
|
104
|
-
export function
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
export function setProps(obj, props) {
|
|
108
|
+
for (const [k, v] of getProps(props))
|
|
109
|
+
obj[k] = v;
|
|
107
110
|
}
|
|
108
111
|
/**
|
|
109
|
-
* Remove several key/value entries from
|
|
112
|
+
* Remove several key/value entries from an object (by reference).
|
|
110
113
|
*
|
|
111
114
|
* @param obj The target object to modify.
|
|
112
|
-
* @param
|
|
115
|
+
* @param keys Set of keys or keys to remove.
|
|
113
116
|
*/
|
|
114
|
-
export function
|
|
117
|
+
export function deleteProps(obj, ...keys) {
|
|
115
118
|
for (const key of keys)
|
|
116
119
|
delete obj[key];
|
|
117
120
|
}
|
|
118
121
|
/** An empty object. */
|
|
119
122
|
export const EMPTY_OBJECT = {};
|
|
120
123
|
/** Function that returns an an empty object. */
|
|
121
|
-
export const
|
|
124
|
+
export const getEmptyObject = () => EMPTY_OBJECT;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Data, Value, Prop } from "./data.js";
|
|
2
|
-
import { Entry } from "./entry.js";
|
|
3
|
-
import type { ImmutableObject } from "./object.js";
|
|
4
1
|
import type { ArrayType, ImmutableArray } from "./array.js";
|
|
2
|
+
import type { Data, Prop, Value } from "./data.js";
|
|
3
|
+
import { Entry } from "./entry.js";
|
|
4
|
+
import { ImmutableObject } from "./object.js";
|
|
5
5
|
/** Object that transforms an input value into an output value with its `transform()` method. */
|
|
6
6
|
export interface Transformable<I, O> {
|
|
7
7
|
transform(input: I): O;
|
|
@@ -20,26 +20,24 @@ export declare type Transformers<T extends Data> = {
|
|
|
20
20
|
readonly [K in keyof T]?: Transformer<T[K], T[K]>;
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
|
-
* Transform the props of a data object using a set of transformers
|
|
24
|
-
* @returns New object with changed props
|
|
23
|
+
* Transform the props of a data object using a set of named transformers.
|
|
24
|
+
* @returns New object with changed props.
|
|
25
25
|
*/
|
|
26
26
|
export declare function transformData<T extends Data>(data: T, transforms: Transformers<T>): T;
|
|
27
27
|
/**
|
|
28
|
-
* Transform the props of a data object using a set of
|
|
29
|
-
* @yield Transformed prop
|
|
28
|
+
* Transform the props of a data object using a set of named transformers.
|
|
29
|
+
* @yield Transformed prop entries after calling the corresponding transformer.
|
|
30
30
|
*/
|
|
31
|
-
export declare function transformProps<T extends Data>(
|
|
31
|
+
export declare function transformProps<T extends Data>(obj: T, transforms: Transformers<T>): Iterable<Prop<T>>;
|
|
32
32
|
/**
|
|
33
33
|
* Transform the _values_ of an iterable set of entries using a transformer.
|
|
34
34
|
* @yield Transformed entry after calling transforming the new value for each entry.
|
|
35
35
|
*/
|
|
36
|
-
export declare function mapEntries<K, I, O>(entries: Iterable<Entry<K, I>>, transformer: (v: I) => O): Iterable<Entry<K, O>>;
|
|
37
36
|
export declare function mapEntries<K, I, O>(entries: Iterable<Entry<K, I>>, transformer: Transformer<I, O>): Iterable<Entry<K, O>>;
|
|
38
37
|
/**
|
|
39
38
|
* Transform the _values_ of a map-like object using a transformer.
|
|
40
39
|
* @return New object after transforming its entries.
|
|
41
40
|
*/
|
|
42
|
-
export declare function mapObject<I, O>(obj: ImmutableObject<I>, transformer: (v: I) => O): ImmutableObject<O>;
|
|
43
41
|
export declare function mapObject<I, O>(obj: ImmutableObject<I>, transformer: Transformer<I, O>): ImmutableObject<O>;
|
|
44
42
|
/**
|
|
45
43
|
* Transform the _values_ of an data object using a transformer.
|
|
@@ -53,12 +51,10 @@ export declare function mapData<I extends Data, O extends {
|
|
|
53
51
|
* Map an iterable set of items using a transformer.
|
|
54
52
|
* @yield Transformed items after calling `transformer()` on each.
|
|
55
53
|
*/
|
|
56
|
-
export declare function mapItems<I, O>(items: Iterable<I>, transformer: (input: I) => O): Iterable<O>;
|
|
57
54
|
export declare function mapItems<I, O>(items: Iterable<I>, transformer: Transformer<I, O>): Iterable<O>;
|
|
58
55
|
/**
|
|
59
56
|
* Apply a transformer to each item in an array and return the transformed array.
|
|
60
57
|
* @return The transformed array.
|
|
61
58
|
*/
|
|
62
59
|
export declare function mapArray<T extends ImmutableArray>(arr: T, transformer: Transformer<ArrayType<T>, ArrayType<T>>): T;
|
|
63
|
-
export declare function mapArray<I, O>(arr: Iterable<I>, transformer: (v: I) => O): ImmutableArray<O>;
|
|
64
60
|
export declare function mapArray<I, O>(arr: Iterable<I>, transformer: Transformer<I, O>): ImmutableArray<O>;
|
|
@@ -1,36 +1,48 @@
|
|
|
1
1
|
import { isFunction } from "./function.js";
|
|
2
|
-
import { getProps, isData } from "./data.js";
|
|
3
2
|
import { getEntries } from "./entry.js";
|
|
3
|
+
import { getProps, isObject } from "./object.js";
|
|
4
4
|
/** Is an unknown value a transformable. */
|
|
5
|
-
export const isTransformable = (v) =>
|
|
5
|
+
export const isTransformable = (v) => isObject(v) && typeof v.transform === "function";
|
|
6
6
|
export function transform(input, transformer) {
|
|
7
7
|
return isFunction(transformer) ? transformer(input) : isTransformable(transformer) ? transformer.transform(input) : transformer;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
|
-
* Transform the props of a data object using a set of transformers
|
|
11
|
-
* @returns New object with changed props
|
|
10
|
+
* Transform the props of a data object using a set of named transformers.
|
|
11
|
+
* @returns New object with changed props.
|
|
12
12
|
*/
|
|
13
13
|
export function transformData(data, transforms) {
|
|
14
14
|
return { ...data, ...Object.fromEntries(transformProps(data, transforms)) };
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* Transform the props of a data object using a set of
|
|
18
|
-
* @yield Transformed prop
|
|
17
|
+
* Transform the props of a data object using a set of named transformers.
|
|
18
|
+
* @yield Transformed prop entries after calling the corresponding transformer.
|
|
19
19
|
*/
|
|
20
|
-
export function* transformProps(
|
|
20
|
+
export function* transformProps(obj, transforms) {
|
|
21
21
|
for (const [k, v] of getProps(transforms))
|
|
22
|
-
yield [k, transform(
|
|
22
|
+
yield [k, transform(obj[k], v)];
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Transform the _values_ of an iterable set of entries using a transformer.
|
|
26
|
+
* @yield Transformed entry after calling transforming the new value for each entry.
|
|
27
|
+
*/
|
|
24
28
|
export function* mapEntries(entries, transformer) {
|
|
25
29
|
for (const [k, v] of entries)
|
|
26
30
|
yield [k, transform(v, transformer)];
|
|
27
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Transform the _values_ of a map-like object using a transformer.
|
|
34
|
+
* @return New object after transforming its entries.
|
|
35
|
+
*/
|
|
28
36
|
export function mapObject(obj, transformer) {
|
|
29
37
|
return Object.fromEntries(mapEntries(getEntries(obj), transformer));
|
|
30
38
|
}
|
|
31
39
|
export function mapData(data, transformer) {
|
|
32
40
|
return mapObject(data, transformer);
|
|
33
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Map an iterable set of items using a transformer.
|
|
44
|
+
* @yield Transformed items after calling `transformer()` on each.
|
|
45
|
+
*/
|
|
34
46
|
export function* mapItems(items, transformer) {
|
|
35
47
|
for (const item of items)
|
|
36
48
|
yield transform(item, transformer);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Entry } from "./entry.js";
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
2
|
+
import type { Data, Prop } from "./data.js";
|
|
3
|
+
import { ImmutableObject } from "./object.js";
|
|
4
4
|
import { ImmutableArray } from "./array.js";
|
|
5
5
|
/** Object that can validate an unknown value with its `validate()` method. */
|
|
6
6
|
export interface Validatable<T> {
|
package/modules/util/validate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isFeedback } from "../feedback/Feedback.js";
|
|
2
2
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
3
|
-
import { getProps } from "./
|
|
3
|
+
import { getProps } from "./object.js";
|
|
4
4
|
import { getArray } from "./array.js";
|
|
5
5
|
/** Validate an unknown value with a validator. */
|
|
6
6
|
export function validate(unsafeValue, validator) {
|