shelving 1.82.0 → 1.83.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/constraint/Constraints.js +3 -3
- package/constraint/FilterConstraint.d.ts +5 -5
- package/constraint/SortConstraint.d.ts +2 -2
- package/db/Change.d.ts +12 -12
- package/db/Collection.d.ts +4 -4
- package/db/Database.d.ts +29 -29
- package/db/Item.d.ts +4 -4
- package/db/Query.d.ts +4 -4
- package/feedback/Feedback.d.ts +2 -2
- package/feedback/Feedback.js +3 -2
- package/firestore/client/FirestoreClientProvider.js +2 -2
- package/firestore/lite/FirestoreLiteProvider.js +2 -2
- package/firestore/server/FirestoreServerProvider.js +2 -2
- package/package.json +1 -1
- package/provider/CacheProvider.d.ts +12 -12
- package/provider/DebugProvider.d.ts +21 -21
- package/provider/MemoryProvider.d.ts +16 -16
- package/provider/MemoryProvider.js +3 -3
- package/provider/Provider.d.ts +30 -30
- package/provider/ThroughProvider.d.ts +23 -23
- package/provider/ValidationProvider.d.ts +22 -22
- package/provider/ValidationProvider.js +2 -2
- package/react/useItem.d.ts +4 -4
- package/react/useQuery.d.ts +4 -4
- package/schema/AllowSchema.d.ts +25 -24
- package/schema/AllowSchema.js +25 -29
- package/schema/ArraySchema.d.ts +2 -2
- package/schema/BooleanSchema.d.ts +2 -2
- package/schema/DataSchema.d.ts +2 -2
- package/schema/DataSchema.js +2 -2
- package/schema/DateSchema.d.ts +2 -2
- package/schema/DictionarySchema.d.ts +19 -0
- package/schema/{ObjectSchema.js → DictionarySchema.js} +6 -6
- package/schema/LinkSchema.d.ts +2 -2
- package/schema/NumberSchema.d.ts +2 -2
- package/schema/Schema.d.ts +10 -8
- package/schema/Schema.js +1 -1
- package/schema/StringSchema.d.ts +12 -10
- package/schema/index.d.ts +1 -1
- package/schema/index.js +1 -1
- package/state/ArrayState.js +4 -4
- package/state/DataState.js +3 -3
- package/state/DictionaryState.d.ts +15 -0
- package/state/{ObjectState.js → DictionaryState.js} +6 -6
- package/state/index.d.ts +2 -2
- package/state/index.js +2 -2
- package/test/basics.js +1 -1
- package/update/ArrayUpdate.js +2 -2
- package/update/DataUpdate.d.ts +10 -9
- package/update/DataUpdate.js +6 -4
- package/update/DictionaryUpdate.d.ts +29 -0
- package/update/{ObjectUpdate.js → DictionaryUpdate.js} +14 -14
- package/update/hydrations.js +2 -2
- package/update/index.d.ts +1 -1
- package/update/index.js +1 -1
- package/util/array.d.ts +40 -111
- package/util/array.js +50 -114
- package/util/clone.d.ts +2 -2
- package/util/clone.js +5 -5
- package/util/data.d.ts +20 -53
- package/util/data.js +11 -1
- package/util/dictionary.d.ts +39 -0
- package/util/dictionary.js +33 -0
- package/util/diff.d.ts +3 -3
- package/util/entry.d.ts +7 -5
- package/util/entry.js +8 -6
- package/util/filter.d.ts +0 -5
- package/util/filter.js +0 -3
- package/util/hydrate.d.ts +2 -2
- package/util/hydrate.js +7 -8
- package/util/index.d.ts +1 -0
- package/util/index.js +1 -0
- package/util/map.d.ts +28 -6
- package/util/map.js +22 -6
- package/util/merge.d.ts +3 -6
- package/util/merge.js +4 -6
- package/util/object.d.ts +77 -90
- package/util/object.js +23 -76
- package/util/set.d.ts +10 -0
- package/util/set.js +17 -0
- package/util/template.d.ts +3 -3
- package/util/template.js +3 -3
- package/util/transform.d.ts +22 -45
- package/util/transform.js +20 -37
- package/util/units.d.ts +6 -12
- package/util/units.js +8 -4
- package/util/validate.d.ts +6 -5
- package/util/validate.js +5 -4
- package/schema/ObjectSchema.d.ts +0 -19
- package/state/ObjectState.d.ts +0 -16
- package/update/ObjectUpdate.d.ts +0 -29
package/schema/Schema.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import type { Validatable } from "../util/validate.js";
|
|
2
|
+
/** Options allowed by a `Schema` instance. */
|
|
3
|
+
export declare type SchemaOptions = {
|
|
4
|
+
/** Title of the schema, e.g. for using as the title of a corresponding field. */
|
|
5
|
+
readonly title?: string;
|
|
6
|
+
/** Description of the schema, e.g. for using as a description in a corresponding field. */
|
|
7
|
+
readonly description?: string;
|
|
8
|
+
/** Placeholder of the schema, e.g. for using as a placeholder in a corresponding field. */
|
|
9
|
+
readonly placeholder?: string;
|
|
10
|
+
};
|
|
2
11
|
/**
|
|
3
12
|
* Schema is an object instance with a `validate()` method.
|
|
4
13
|
* - Type `T` represents the type of value `validate()` returns.
|
|
@@ -13,14 +22,7 @@ export declare abstract class Schema<T extends unknown = unknown> implements Val
|
|
|
13
22
|
readonly placeholder: string;
|
|
14
23
|
/** Default value. */
|
|
15
24
|
readonly value: unknown;
|
|
16
|
-
constructor({ title, description, placeholder
|
|
17
|
-
/** Title of the schema, e.g. for using as the title of a corresponding field. */
|
|
18
|
-
readonly title?: string;
|
|
19
|
-
/** Description of the schema, e.g. for using as a description in a corresponding field. */
|
|
20
|
-
readonly description?: string;
|
|
21
|
-
/** Placeholder of the schema, e.g. for using as a placeholder in a corresponding field. */
|
|
22
|
-
readonly placeholder?: string;
|
|
23
|
-
});
|
|
25
|
+
constructor({ title, description, placeholder }: SchemaOptions);
|
|
24
26
|
/** Every schema must implement a `validate()` method. */
|
|
25
27
|
abstract validate(unsafeValue: unknown): T;
|
|
26
28
|
}
|
package/schema/Schema.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* - `validate()` returns `Invalid` if value was not valid.
|
|
5
5
|
*/
|
|
6
6
|
export class Schema {
|
|
7
|
-
constructor({ title = "", description = "", placeholder = ""
|
|
7
|
+
constructor({ title = "", description = "", placeholder = "" }) {
|
|
8
8
|
this.title = title;
|
|
9
9
|
this.description = description;
|
|
10
10
|
this.placeholder = placeholder;
|
package/schema/StringSchema.d.ts
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
import { Schema } from "./Schema.js";
|
|
1
|
+
import { Schema, SchemaOptions } from "./Schema.js";
|
|
2
2
|
/** `type=""` prop for HTML `<input />` tags that are relevant for strings. */
|
|
3
3
|
export declare type HtmlInputType = "text" | "password" | "color" | "date" | "email" | "number" | "tel" | "search" | "url";
|
|
4
4
|
/** Function that sanitizes a string. */
|
|
5
5
|
export declare type Sanitizer = (str: string) => string;
|
|
6
|
+
/** Options for `StringSchema` */
|
|
7
|
+
export declare type StringSchemaOptions = SchemaOptions & {
|
|
8
|
+
readonly value?: string;
|
|
9
|
+
readonly type?: HtmlInputType;
|
|
10
|
+
readonly min?: number;
|
|
11
|
+
readonly max?: number | null;
|
|
12
|
+
readonly match?: RegExp | null;
|
|
13
|
+
readonly sanitizer?: Sanitizer | null;
|
|
14
|
+
readonly multiline?: boolean;
|
|
15
|
+
};
|
|
6
16
|
/**
|
|
7
17
|
* Schema that defines a valid string.
|
|
8
18
|
*
|
|
@@ -31,15 +41,7 @@ export declare class StringSchema extends Schema<string> {
|
|
|
31
41
|
readonly match: RegExp | null;
|
|
32
42
|
readonly sanitizer: Sanitizer | null;
|
|
33
43
|
readonly multiline: boolean;
|
|
34
|
-
constructor({ value, type, min, max, match, sanitizer, multiline, ...rest }:
|
|
35
|
-
readonly value?: string;
|
|
36
|
-
readonly type?: HtmlInputType;
|
|
37
|
-
readonly min?: number;
|
|
38
|
-
readonly max?: number | null;
|
|
39
|
-
readonly match?: RegExp | null;
|
|
40
|
-
readonly sanitizer?: Sanitizer | null;
|
|
41
|
-
readonly multiline?: boolean;
|
|
42
|
-
});
|
|
44
|
+
constructor({ value, type, min, max, match, sanitizer, multiline, ...rest }: StringSchemaOptions);
|
|
43
45
|
validate(unsafeValue?: unknown): string;
|
|
44
46
|
/**
|
|
45
47
|
* Clean a string by removing characters that aren't digits.
|
package/schema/index.d.ts
CHANGED
|
@@ -5,11 +5,11 @@ export * from "./BooleanSchema.js";
|
|
|
5
5
|
export * from "./ColorSchema.js";
|
|
6
6
|
export * from "./DataSchema.js";
|
|
7
7
|
export * from "./DateSchema.js";
|
|
8
|
+
export * from "./DictionarySchema.js";
|
|
8
9
|
export * from "./EmailSchema.js";
|
|
9
10
|
export * from "./KeySchema.js";
|
|
10
11
|
export * from "./LinkSchema.js";
|
|
11
12
|
export * from "./NumberSchema.js";
|
|
12
|
-
export * from "./ObjectSchema.js";
|
|
13
13
|
export * from "./OptionalSchema.js";
|
|
14
14
|
export * from "./PhoneSchema.js";
|
|
15
15
|
export * from "./RequiredSchema.js";
|
package/schema/index.js
CHANGED
|
@@ -5,11 +5,11 @@ export * from "./BooleanSchema.js";
|
|
|
5
5
|
export * from "./ColorSchema.js";
|
|
6
6
|
export * from "./DataSchema.js";
|
|
7
7
|
export * from "./DateSchema.js";
|
|
8
|
+
export * from "./DictionarySchema.js";
|
|
8
9
|
export * from "./EmailSchema.js";
|
|
9
10
|
export * from "./KeySchema.js";
|
|
10
11
|
export * from "./LinkSchema.js";
|
|
11
12
|
export * from "./NumberSchema.js";
|
|
12
|
-
export * from "./ObjectSchema.js";
|
|
13
13
|
export * from "./OptionalSchema.js";
|
|
14
14
|
export * from "./PhoneSchema.js";
|
|
15
15
|
export * from "./RequiredSchema.js";
|
package/state/ArrayState.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { toggleArrayItems, withArrayItems, withoutArrayItems } 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 {
|
|
@@ -11,15 +11,15 @@ export class ArrayState extends State {
|
|
|
11
11
|
}
|
|
12
12
|
/** Add items to this array. */
|
|
13
13
|
add(...items) {
|
|
14
|
-
this.set(
|
|
14
|
+
this.set(withArrayItems(this.value, ...items));
|
|
15
15
|
}
|
|
16
16
|
/** Remove items from this array. */
|
|
17
17
|
delete(...items) {
|
|
18
|
-
this.set(
|
|
18
|
+
this.set(withoutArrayItems(this.value, ...items));
|
|
19
19
|
}
|
|
20
20
|
/** Toggle items in this array. */
|
|
21
21
|
toggle(...items) {
|
|
22
|
-
this.set(
|
|
22
|
+
this.set(toggleArrayItems(this.value, ...items));
|
|
23
23
|
}
|
|
24
24
|
/** Iterate over the items. */
|
|
25
25
|
[Symbol.iterator]() {
|
package/state/DataState.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getData } from "../util/data.js";
|
|
2
|
-
import {
|
|
2
|
+
import { transformObject } from "../util/transform.js";
|
|
3
3
|
import { State } from "./State.js";
|
|
4
4
|
/** State that stores a data object and has additional methods to help with that. */
|
|
5
5
|
export class DataState extends State {
|
|
@@ -9,7 +9,7 @@ export class DataState extends State {
|
|
|
9
9
|
}
|
|
10
10
|
/** Update several props in this data. */
|
|
11
11
|
update(updates) {
|
|
12
|
-
this.set(
|
|
12
|
+
this.set(transformObject(this.data, updates));
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
/** State that stores an optional data object and has additional methods to help with that. */
|
|
@@ -24,7 +24,7 @@ export class OptionalDataState extends State {
|
|
|
24
24
|
}
|
|
25
25
|
/** Update several props in this data. */
|
|
26
26
|
update(updates) {
|
|
27
|
-
this.set(
|
|
27
|
+
this.set(transformObject(this.data, updates));
|
|
28
28
|
}
|
|
29
29
|
/** Set the data to `null`. */
|
|
30
30
|
unset() {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DictionaryItem, ImmutableDictionary } from "../util/dictionary.js";
|
|
2
|
+
import { Transformers } from "../util/transform.js";
|
|
3
|
+
import { State } from "./State.js";
|
|
4
|
+
/** State that stores a dictionary object and has additional methods to help with that. */
|
|
5
|
+
export declare class DictionaryState<T> extends State<ImmutableDictionary<T>> implements Iterable<DictionaryItem<T>> {
|
|
6
|
+
constructor(initial?: ImmutableDictionary<T>);
|
|
7
|
+
/** Get the length of the current value of this state. */
|
|
8
|
+
get count(): number;
|
|
9
|
+
/** Set a named entry in this object with a different value. */
|
|
10
|
+
update(updates: Transformers<ImmutableDictionary<T>>): void;
|
|
11
|
+
/** Remove a named entry from this object. */
|
|
12
|
+
delete(...keys: string[]): void;
|
|
13
|
+
/** Iterate over the entries of the object. */
|
|
14
|
+
[Symbol.iterator](): Iterator<DictionaryItem<T>>;
|
|
15
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { withoutDictionaryItems } from "../util/dictionary.js";
|
|
2
|
+
import { transformDictionary } from "../util/transform.js";
|
|
3
3
|
import { State } from "./State.js";
|
|
4
|
-
/** State that stores a
|
|
5
|
-
export class
|
|
4
|
+
/** State that stores a dictionary object and has additional methods to help with that. */
|
|
5
|
+
export class DictionaryState extends State {
|
|
6
6
|
constructor(initial = {}) {
|
|
7
7
|
super(initial);
|
|
8
8
|
}
|
|
@@ -12,11 +12,11 @@ export class ObjectState extends State {
|
|
|
12
12
|
}
|
|
13
13
|
/** Set a named entry in this object with a different value. */
|
|
14
14
|
update(updates) {
|
|
15
|
-
this.set(
|
|
15
|
+
this.set(transformDictionary(this.value, updates));
|
|
16
16
|
}
|
|
17
17
|
/** Remove a named entry from this object. */
|
|
18
18
|
delete(...keys) {
|
|
19
|
-
this.set(
|
|
19
|
+
this.set(withoutDictionaryItems(this.value, ...keys));
|
|
20
20
|
}
|
|
21
21
|
/** Iterate over the entries of the object. */
|
|
22
22
|
[Symbol.iterator]() {
|
package/state/index.d.ts
CHANGED
package/state/index.js
CHANGED
package/test/basics.js
CHANGED
|
@@ -6,7 +6,7 @@ import { STRING } from "../schema/StringSchema.js";
|
|
|
6
6
|
export const BASIC_SCHEMA = DATA({
|
|
7
7
|
str: STRING,
|
|
8
8
|
num: NUMBER,
|
|
9
|
-
group: ALLOW_STRING(
|
|
9
|
+
group: ALLOW_STRING({ a: "A", b: "B", c: "C" }),
|
|
10
10
|
tags: ARRAY(STRING),
|
|
11
11
|
});
|
|
12
12
|
export const basic1 = { id: "basic1", str: "aaa", num: 100, group: "a", tags: ["odd", "prime"] };
|
package/update/ArrayUpdate.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ArraySchema } from "../schema/ArraySchema.js";
|
|
2
|
-
import {
|
|
2
|
+
import { withArrayItems, withoutArrayItems } from "../util/array.js";
|
|
3
3
|
import { validateArray } from "../util/validate.js";
|
|
4
4
|
import { Update } from "./Update.js";
|
|
5
5
|
/** Update that can be applied to an array to add/remove items. */
|
|
@@ -18,7 +18,7 @@ export class ArrayUpdate extends Update {
|
|
|
18
18
|
return new ArrayUpdate([], deletes);
|
|
19
19
|
}
|
|
20
20
|
transform(arr = []) {
|
|
21
|
-
return
|
|
21
|
+
return withoutArrayItems(withArrayItems(arr, ...this.adds), ...this.deletes);
|
|
22
22
|
}
|
|
23
23
|
validate(validator) {
|
|
24
24
|
if (!(validator instanceof ArraySchema))
|
package/update/DataUpdate.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { Data,
|
|
1
|
+
import { Data, DataKey, DataProp } from "../util/data.js";
|
|
2
2
|
import { Nullish } from "../util/null.js";
|
|
3
3
|
import { Transformable } from "../util/transform.js";
|
|
4
4
|
import { Validator, Validators } from "../util/validate.js";
|
|
5
5
|
import { Update } from "./Update.js";
|
|
6
6
|
/**
|
|
7
|
-
* Set of named
|
|
8
|
-
* -
|
|
7
|
+
* Set of named updates for the props of a data object.
|
|
8
|
+
* - Similar to `Transformers` but only allows `Update` instances.
|
|
9
9
|
* - If a prop contains a new value, the prop is set to that new value.
|
|
10
|
-
* - If a prop contains
|
|
11
|
-
* - This is a subset of `Dispatchers`
|
|
10
|
+
* - If a prop contains an `Update` instance, the existing value is updated.
|
|
12
11
|
*/
|
|
13
12
|
export declare type Updates<T extends Data = Data> = {
|
|
14
13
|
readonly [K in keyof T]?: T[K] | Update<T[K]>;
|
|
@@ -17,18 +16,20 @@ export declare type Updates<T extends Data = Data> = {
|
|
|
17
16
|
* Validate a set of updates against a set of validators.
|
|
18
17
|
*/
|
|
19
18
|
export declare function validateUpdates<T extends Data>(unsafeUpdates: Updates<T>, validators: Validators<T>): Updates<T>;
|
|
19
|
+
/** Update data using a set of updates. */
|
|
20
|
+
export declare const updateData: <T extends Data>(data: T, updates: Updates<T>) => T;
|
|
20
21
|
/**
|
|
21
22
|
* Update that can be applied to a data object to update its props.
|
|
22
23
|
*/
|
|
23
|
-
export declare class DataUpdate<T extends Data = Data> extends Update<T> implements Iterable<
|
|
24
|
+
export declare class DataUpdate<T extends Data = Data> extends Update<T> implements Iterable<DataProp<Updates<T>>>, Transformable<T, T> {
|
|
24
25
|
/** Return a data update with a specific prop marked for update. */
|
|
25
|
-
static with<X extends Data, K extends
|
|
26
|
+
static with<X extends Data, K extends DataKey<X>>(key: Nullish<K>, value: X[K] | Update<X[K]>): DataUpdate<X>;
|
|
26
27
|
readonly updates: Updates<T>;
|
|
27
28
|
constructor(props: Updates<T>);
|
|
28
29
|
transform(data: T): T;
|
|
29
30
|
validate(validator: Validator<T>): this;
|
|
30
31
|
/** Return a data update with a specific prop marked for update. */
|
|
31
|
-
with<K extends
|
|
32
|
+
with<K extends DataKey<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
|
|
32
33
|
/** Iterate over the transforms in this object. */
|
|
33
|
-
[Symbol.iterator](): Iterator<
|
|
34
|
+
[Symbol.iterator](): Iterator<DataProp<Updates<T>>, void>;
|
|
34
35
|
}
|
package/update/DataUpdate.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
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 {
|
|
4
|
+
import { getDataProps } from "../util/data.js";
|
|
5
5
|
import { isNullish } from "../util/null.js";
|
|
6
|
-
import {
|
|
6
|
+
import { transformObject } from "../util/transform.js";
|
|
7
7
|
import { validate } from "../util/validate.js";
|
|
8
8
|
import { Update } from "./Update.js";
|
|
9
9
|
/**
|
|
@@ -14,7 +14,7 @@ export function validateUpdates(unsafeUpdates, validators) {
|
|
|
14
14
|
}
|
|
15
15
|
function* _validateUpdates(unsafeUpdates, validators) {
|
|
16
16
|
const feedbacks = new Map();
|
|
17
|
-
for (const [key, validator] of
|
|
17
|
+
for (const [key, validator] of getDataProps(validators)) {
|
|
18
18
|
const unsafeUpdate = unsafeUpdates[key];
|
|
19
19
|
if (unsafeUpdate !== undefined) {
|
|
20
20
|
try {
|
|
@@ -30,6 +30,8 @@ function* _validateUpdates(unsafeUpdates, validators) {
|
|
|
30
30
|
if (feedbacks.size)
|
|
31
31
|
throw new InvalidFeedback("Invalid updates", feedbacks);
|
|
32
32
|
}
|
|
33
|
+
/** Update data using a set of updates. */
|
|
34
|
+
export const updateData = transformObject;
|
|
33
35
|
/**
|
|
34
36
|
* Update that can be applied to a data object to update its props.
|
|
35
37
|
*/
|
|
@@ -43,7 +45,7 @@ export class DataUpdate extends Update {
|
|
|
43
45
|
return new DataUpdate(!isNullish(key) ? { [key]: value } : {});
|
|
44
46
|
}
|
|
45
47
|
transform(data) {
|
|
46
|
-
return
|
|
48
|
+
return updateData(data, this.updates);
|
|
47
49
|
}
|
|
48
50
|
validate(validator) {
|
|
49
51
|
if (!(validator instanceof DataSchema))
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Entry } from "../util/entry.js";
|
|
2
|
+
import { Nullish } from "../util/null.js";
|
|
3
|
+
import { ImmutableDictionary } from "../util/dictionary.js";
|
|
4
|
+
import { Validator } from "../util/validate.js";
|
|
5
|
+
import { Update } from "./Update.js";
|
|
6
|
+
import { Delete } from "./Delete.js";
|
|
7
|
+
/** Set of named transforms for the entries of a dictionary object. */
|
|
8
|
+
export declare type DictionaryUpdates<T> = ImmutableDictionary<T | Update<T> | Delete>;
|
|
9
|
+
/** Update that can be applied to a dictionary object to add/remove/update its entries. */
|
|
10
|
+
export declare class DictionaryUpdate<T> extends Update<ImmutableDictionary<T>> implements Iterable<Entry<string, T | Update<T> | Delete>> {
|
|
11
|
+
/** Return a dictionary update with a specific entry marked for update. */
|
|
12
|
+
static with<X>(key: Nullish<string>, value: X | Update<X> | Delete): DictionaryUpdate<X>;
|
|
13
|
+
/** Return a dictionary update with a specific entry marked for deletion. */
|
|
14
|
+
static without<X>(key: Nullish<string>): DictionaryUpdate<X>;
|
|
15
|
+
readonly updates: DictionaryUpdates<T>;
|
|
16
|
+
constructor(updates?: DictionaryUpdates<T>);
|
|
17
|
+
transform(obj?: ImmutableDictionary<T>): ImmutableDictionary<T>;
|
|
18
|
+
validate(validator: Validator<ImmutableDictionary<T>>): this;
|
|
19
|
+
/** Return a dictionary update with a specific entry marked for update. */
|
|
20
|
+
with(key: Nullish<string>, value: T | Update<T>): this;
|
|
21
|
+
/** Return a dictionary update with a specific entry marked for deletion. */
|
|
22
|
+
without(key: Nullish<string>): this;
|
|
23
|
+
/**
|
|
24
|
+
* Iterate over the changes in this object.
|
|
25
|
+
* - Updates are yielded first, then deletes.
|
|
26
|
+
* - Entries whose value is `undefined` indicate deletion.
|
|
27
|
+
*/
|
|
28
|
+
[Symbol.iterator](): Iterator<Entry<string, T | Update<T> | Delete>, void>;
|
|
29
|
+
}
|
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
import { isNullish } from "../util/null.js";
|
|
2
2
|
import { transform } from "../util/transform.js";
|
|
3
|
-
import {
|
|
3
|
+
import { DictionarySchema } from "../schema/DictionarySchema.js";
|
|
4
4
|
import { validate } from "../util/validate.js";
|
|
5
5
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
6
6
|
import { isFeedback } from "../feedback/Feedback.js";
|
|
7
7
|
import { Update } from "./Update.js";
|
|
8
8
|
import { Delete, DELETE } from "./Delete.js";
|
|
9
|
-
/** Update that can be applied to a
|
|
10
|
-
export class
|
|
9
|
+
/** Update that can be applied to a dictionary object to add/remove/update its entries. */
|
|
10
|
+
export class DictionaryUpdate extends Update {
|
|
11
11
|
constructor(updates = {}) {
|
|
12
12
|
super();
|
|
13
13
|
this.updates = updates;
|
|
14
14
|
}
|
|
15
|
-
/** Return
|
|
15
|
+
/** Return a dictionary update with a specific entry marked for update. */
|
|
16
16
|
static with(key, value) {
|
|
17
|
-
return new
|
|
17
|
+
return new DictionaryUpdate(isNullish(key) ? {} : { [key]: value });
|
|
18
18
|
}
|
|
19
|
-
/** Return
|
|
19
|
+
/** Return a dictionary update with a specific entry marked for deletion. */
|
|
20
20
|
static without(key) {
|
|
21
|
-
return new
|
|
21
|
+
return new DictionaryUpdate(isNullish(key) ? {} : { [key]: DELETE });
|
|
22
22
|
}
|
|
23
23
|
transform(obj = {}) {
|
|
24
|
-
return Object.fromEntries(
|
|
24
|
+
return Object.fromEntries(_transformDictionaryEntries(obj, this.updates));
|
|
25
25
|
}
|
|
26
26
|
validate(validator) {
|
|
27
|
-
if (!(validator instanceof
|
|
27
|
+
if (!(validator instanceof DictionarySchema))
|
|
28
28
|
return super.validate(validator);
|
|
29
29
|
return {
|
|
30
30
|
__proto__: Object.getPrototypeOf(this),
|
|
31
31
|
...this,
|
|
32
|
-
updates: Object.fromEntries(
|
|
32
|
+
updates: Object.fromEntries(_validateDictionaryUpdates(this.updates, validator.items)),
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
-
/** Return
|
|
35
|
+
/** Return a dictionary update with a specific entry marked for update. */
|
|
36
36
|
with(key, value) {
|
|
37
37
|
if (isNullish(key))
|
|
38
38
|
return this;
|
|
@@ -42,7 +42,7 @@ export class ObjectUpdate extends Update {
|
|
|
42
42
|
updates: { ...this.updates, [key]: value },
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
|
-
/** Return
|
|
45
|
+
/** Return a dictionary update with a specific entry marked for deletion. */
|
|
46
46
|
without(key) {
|
|
47
47
|
if (isNullish(key))
|
|
48
48
|
return this;
|
|
@@ -62,14 +62,14 @@ export class ObjectUpdate extends Update {
|
|
|
62
62
|
yield entry;
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
-
function*
|
|
65
|
+
function* _transformDictionaryEntries(obj, updates) {
|
|
66
66
|
for (const [k, v] of Object.entries({ ...obj, ...updates })) {
|
|
67
67
|
if (v instanceof Delete)
|
|
68
68
|
continue;
|
|
69
69
|
yield [k, transform(obj[k], v)];
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
|
-
function*
|
|
72
|
+
function* _validateDictionaryUpdates(updates, validator) {
|
|
73
73
|
const feedbacks = new Map();
|
|
74
74
|
for (const [key, value] of Object.entries(updates)) {
|
|
75
75
|
try {
|
package/update/hydrations.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Delete } from "./Delete.js";
|
|
2
2
|
import { Increment } from "./Increment.js";
|
|
3
3
|
import { ArrayUpdate } from "./ArrayUpdate.js";
|
|
4
|
-
import {
|
|
4
|
+
import { DictionaryUpdate } from "./DictionaryUpdate.js";
|
|
5
5
|
import { DataUpdate } from "./DataUpdate.js";
|
|
6
6
|
/** Set of hydrations for all update classes. */
|
|
7
7
|
export const UPDATE_HYDRATIONS = {
|
|
8
8
|
Increment,
|
|
9
9
|
Delete,
|
|
10
10
|
ArrayUpdate,
|
|
11
|
-
|
|
11
|
+
DictionaryUpdate,
|
|
12
12
|
DataUpdate,
|
|
13
13
|
};
|
package/update/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ export * from "./Update.js";
|
|
|
2
2
|
export * from "./Delete.js";
|
|
3
3
|
export * from "./Increment.js";
|
|
4
4
|
export * from "./ArrayUpdate.js";
|
|
5
|
-
export * from "./ObjectUpdate.js";
|
|
6
5
|
export * from "./DataUpdate.js";
|
|
6
|
+
export * from "./DictionaryUpdate.js";
|
|
7
7
|
export * from "./hydrations.js";
|
package/update/index.js
CHANGED
|
@@ -2,6 +2,6 @@ export * from "./Update.js";
|
|
|
2
2
|
export * from "./Delete.js";
|
|
3
3
|
export * from "./Increment.js";
|
|
4
4
|
export * from "./ArrayUpdate.js";
|
|
5
|
-
export * from "./ObjectUpdate.js";
|
|
6
5
|
export * from "./DataUpdate.js";
|
|
6
|
+
export * from "./DictionaryUpdate.js";
|
|
7
7
|
export * from "./hydrations.js";
|