shelving 1.22.2 → 1.23.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/db/Database.d.ts +13 -27
- package/db/Database.js +23 -29
- package/db/Pagination.js +1 -1
- package/db/Write.d.ts +28 -0
- package/db/Write.js +32 -0
- package/db/index.d.ts +1 -1
- package/db/index.js +1 -1
- package/package.json +1 -1
- package/provider/BatchProvider.js +2 -2
- package/provider/CacheProvider.js +3 -3
- package/provider/MemoryProvider.js +6 -6
- package/query/Filter.d.ts +1 -1
- package/query/Filter.js +1 -1
- package/query/Filters.d.ts +1 -1
- package/query/Filters.js +1 -1
- package/query/Query.d.ts +1 -1
- package/query/Query.js +2 -2
- package/query/Rule.d.ts +3 -3
- package/query/Sort.d.ts +1 -1
- package/query/Sort.js +1 -1
- package/query/Sorts.d.ts +1 -1
- package/query/Sorts.js +1 -1
- package/react/useQuery.js +2 -2
- package/stream/DataState.d.ts +2 -2
- package/stream/DataState.js +2 -2
- package/stream/State.d.ts +4 -4
- package/stream/State.js +2 -2
- package/stream/Stream.d.ts +7 -7
- package/stream/Stream.js +5 -5
- package/transform/AddEntriesTransform.d.ts +1 -1
- package/transform/AddEntriesTransform.js +1 -1
- package/transform/AddItemsTransform.d.ts +1 -1
- package/transform/AddItemsTransform.js +1 -1
- package/transform/DataTransform.d.ts +1 -1
- package/transform/DataTransform.js +3 -3
- package/transform/IncrementTransform.d.ts +1 -1
- package/transform/IncrementTransform.js +1 -1
- package/transform/RemoveEntriesTransform.d.ts +1 -1
- package/transform/RemoveEntriesTransform.js +1 -1
- package/transform/RemoveItemsTransform.d.ts +1 -1
- package/transform/RemoveItemsTransform.js +1 -1
- package/transform/Transform.d.ts +3 -3
- package/transform/hydrations.d.ts +1 -1
- package/transform/hydrations.js +1 -1
- package/transform/util.js +2 -2
- package/util/clone.js +3 -3
- package/util/filter.d.ts +4 -4
- package/util/filter.js +5 -5
- package/util/hydrate.d.ts +7 -7
- package/util/hydrate.js +13 -13
- package/util/index.d.ts +2 -2
- package/util/index.js +2 -2
- package/util/observable.d.ts +5 -5
- package/util/observable.js +6 -6
- package/util/sort.d.ts +4 -4
- package/util/sort.js +5 -6
- package/util/transform.d.ts +88 -0
- package/util/transform.js +48 -0
- package/db/Change.d.ts +0 -37
- package/db/Change.js +0 -60
- package/util/derive.d.ts +0 -88
- package/util/derive.js +0 -48
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { transformData } from "../util/index.js";
|
|
2
2
|
import { Transform } from "./Transform.js";
|
|
3
3
|
/** Set of transforms that can be appled to an object's properties. */
|
|
4
4
|
export class DataTransform extends Transform {
|
|
@@ -6,8 +6,8 @@ export class DataTransform extends Transform {
|
|
|
6
6
|
super();
|
|
7
7
|
this.transforms = transforms;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
return
|
|
9
|
+
transform(existing) {
|
|
10
|
+
return transformData(existing, this.transforms);
|
|
11
11
|
}
|
|
12
12
|
/** Return a new object with the specified additional transform. */
|
|
13
13
|
prop(key, transform) {
|
|
@@ -4,7 +4,7 @@ import { Transform } from "./Transform.js";
|
|
|
4
4
|
export declare class RemoveEntriesTransform<T> extends Transform<ImmutableObject<T>> implements Iterable<string> {
|
|
5
5
|
readonly props: ImmutableArray<string>;
|
|
6
6
|
constructor(...props: ImmutableArray<string>);
|
|
7
|
-
|
|
7
|
+
transform(existing?: unknown): ImmutableObject<T>;
|
|
8
8
|
/** Iterate over the entry keys. */
|
|
9
9
|
[Symbol.iterator](): Iterator<string, void>;
|
|
10
10
|
}
|
|
@@ -4,7 +4,7 @@ import { Transform } from "./Transform.js";
|
|
|
4
4
|
export declare class RemoveItemsTransform<T> extends Transform<ImmutableArray<T>> implements Iterable<T> {
|
|
5
5
|
readonly items: ImmutableArray<T>;
|
|
6
6
|
constructor(...items: ImmutableArray<T>);
|
|
7
|
-
|
|
7
|
+
transform(existing?: ImmutableArray<T> | unknown): ImmutableArray<T>;
|
|
8
8
|
/** Iterate over the items. */
|
|
9
9
|
[Symbol.iterator](): Iterator<T, void>;
|
|
10
10
|
}
|
package/transform/Transform.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Transformable } from "../util/index.js";
|
|
2
2
|
/**
|
|
3
3
|
* An object that transforms an existing value value into a new value with its `transform()` method.
|
|
4
4
|
* - Probably has a configuration that accompanies it.
|
|
5
5
|
*/
|
|
6
|
-
export declare abstract class Transform<T> implements
|
|
6
|
+
export declare abstract class Transform<T> implements Transformable<T, T> {
|
|
7
7
|
/** Apply this transform to a value. */
|
|
8
|
-
abstract
|
|
8
|
+
abstract transform(existing?: unknown): T;
|
|
9
9
|
}
|
package/transform/hydrations.js
CHANGED
package/transform/util.js
CHANGED
|
@@ -2,7 +2,7 @@ import { Feedback } from "../feedback/Feedback.js";
|
|
|
2
2
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
3
3
|
import { DataSchema } from "../schema/DataSchema.js";
|
|
4
4
|
import { DataTransform, Transform } from "../transform/index.js";
|
|
5
|
-
import {
|
|
5
|
+
import { transform, validate, toProps } from "../util/index.js";
|
|
6
6
|
/** Validate a transform against a validator. */
|
|
7
7
|
export function validateTransform(unsafeTransform, validator) {
|
|
8
8
|
if (validator instanceof DataSchema && unsafeTransform instanceof DataTransform) {
|
|
@@ -11,7 +11,7 @@ export function validateTransform(unsafeTransform, validator) {
|
|
|
11
11
|
return safeTransforms === unsafeTransforms ? unsafeTransform : new DataTransform(safeTransforms);
|
|
12
12
|
}
|
|
13
13
|
else {
|
|
14
|
-
validate(
|
|
14
|
+
validate(transform(undefined, unsafeTransform), validator);
|
|
15
15
|
return unsafeTransform;
|
|
16
16
|
}
|
|
17
17
|
}
|
package/util/clone.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isArray } from "./array.js";
|
|
2
2
|
import { isObject } from "./object.js";
|
|
3
|
-
import {
|
|
3
|
+
import { transformArray, transformObject } from "./transform.js";
|
|
4
4
|
/** Does an object implement `Cloneable` */
|
|
5
5
|
export const isCloneable = (v) => isObject(v) && typeof v.cloneable === "function";
|
|
6
6
|
/** Shallow clone a value. */
|
|
@@ -19,7 +19,7 @@ export function deepClone(value, recursor = deepClone) {
|
|
|
19
19
|
export function cloneArray(input, recursor = shallowClone) {
|
|
20
20
|
if (isCloneable(input))
|
|
21
21
|
return input.clone();
|
|
22
|
-
const output =
|
|
22
|
+
const output = transformArray(input, recursor);
|
|
23
23
|
Object.setPrototypeOf(output, Object.getPrototypeOf(input));
|
|
24
24
|
return output;
|
|
25
25
|
}
|
|
@@ -27,7 +27,7 @@ export function cloneArray(input, recursor = shallowClone) {
|
|
|
27
27
|
export function cloneObject(input, recursor = shallowClone) {
|
|
28
28
|
if (isCloneable(input))
|
|
29
29
|
return input.clone();
|
|
30
|
-
const output =
|
|
30
|
+
const output = transformObject(input, recursor);
|
|
31
31
|
Object.setPrototypeOf(input, Object.getPrototypeOf(input));
|
|
32
32
|
return output;
|
|
33
33
|
}
|
package/util/filter.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ImmutableArray } from "./array.js";
|
|
|
2
2
|
import type { ImmutableMap } from "./map.js";
|
|
3
3
|
import { Entry } from "./entry.js";
|
|
4
4
|
import { ImmutableObject } from "./object.js";
|
|
5
|
-
import {
|
|
5
|
+
import { Transformer } from "./transform.js";
|
|
6
6
|
/** Object that can match an item against a target with its `match()` function. */
|
|
7
7
|
export interface Matchable<L, R> {
|
|
8
8
|
match(item: L, target: R): boolean;
|
|
@@ -52,9 +52,9 @@ export declare function filterObject<L, R>(object: ImmutableObject<L>, matcher:
|
|
|
52
52
|
export declare function filterMap<L>(input: ImmutableMap<L>, matcher: Matcher<Entry<L>, void>): ImmutableMap<L>;
|
|
53
53
|
export declare function filterMap<L, R>(input: ImmutableMap<L>, matcher: Matcher<Entry<L>, R>, target: R): ImmutableMap<L>;
|
|
54
54
|
/** Derive a value and match it against a target value. */
|
|
55
|
-
export declare class
|
|
56
|
-
private
|
|
55
|
+
export declare class TransformMatcher<L, LL, R> implements Matchable<L, R> {
|
|
56
|
+
private _transformer;
|
|
57
57
|
private _matcher;
|
|
58
|
-
constructor(
|
|
58
|
+
constructor(transformer: Transformer<L, LL>, matcher?: Matcher<LL, R>);
|
|
59
59
|
match(item: L, target: R): boolean;
|
|
60
60
|
}
|
package/util/filter.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ASC } from "./sort.js";
|
|
2
|
-
import {
|
|
2
|
+
import { transform } from "./transform.js";
|
|
3
3
|
export function match(item, matcher, target) {
|
|
4
4
|
return typeof matcher === "function" ? matcher(item, target) : matcher.match(item, target);
|
|
5
5
|
}
|
|
@@ -51,12 +51,12 @@ export function filterMap(input, matcher, target) {
|
|
|
51
51
|
return output.size === input.size ? input : output;
|
|
52
52
|
}
|
|
53
53
|
/** Derive a value and match it against a target value. */
|
|
54
|
-
export class
|
|
55
|
-
constructor(
|
|
56
|
-
this.
|
|
54
|
+
export class TransformMatcher {
|
|
55
|
+
constructor(transformer, matcher = IS) {
|
|
56
|
+
this._transformer = transformer;
|
|
57
57
|
this._matcher = matcher;
|
|
58
58
|
}
|
|
59
59
|
match(item, target) {
|
|
60
|
-
return match(
|
|
60
|
+
return match(transform(item, this._transformer), this._matcher, target);
|
|
61
61
|
}
|
|
62
62
|
}
|
package/util/hydrate.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Class } from "./class.js";
|
|
2
2
|
import { Data } from "./data.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Transformable } from "./transform.js";
|
|
4
4
|
import { ImmutableObject } from "./object.js";
|
|
5
5
|
/**
|
|
6
6
|
* A set of hydrations describes a set of string keys and the class constructor to be dehydrated and rehydrated.
|
|
@@ -30,15 +30,15 @@ export declare function hydrate(value: unknown, hydrations: Hydrations): unknown
|
|
|
30
30
|
export declare type DehydratedObject<T extends Data> = T & {
|
|
31
31
|
readonly _type: string;
|
|
32
32
|
};
|
|
33
|
-
/**
|
|
34
|
-
export declare class Hydrator implements
|
|
33
|
+
/** Hydrates a value with a set of hydrations. */
|
|
34
|
+
export declare class Hydrator implements Transformable<unknown, unknown> {
|
|
35
35
|
private _hydrations;
|
|
36
36
|
constructor(hydrations: Hydrations);
|
|
37
|
-
|
|
37
|
+
transform(value: unknown): unknown;
|
|
38
38
|
}
|
|
39
|
-
/**
|
|
40
|
-
export declare class Dehydrator implements
|
|
39
|
+
/** Dehydrates a value with a set of hydrations. */
|
|
40
|
+
export declare class Dehydrator implements Transformable<unknown, unknown> {
|
|
41
41
|
private _hydrations;
|
|
42
42
|
constructor(hydrations: Hydrations);
|
|
43
|
-
|
|
43
|
+
transform(value: unknown): unknown;
|
|
44
44
|
}
|
package/util/hydrate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AssertionError } from "../error/index.js";
|
|
2
2
|
import { isData } from "./data.js";
|
|
3
|
-
import {
|
|
3
|
+
import { transformArray, transformObject } from "./transform.js";
|
|
4
4
|
import { isPlainObject } from "./object.js";
|
|
5
5
|
/**
|
|
6
6
|
* Deeply dehydrate a class instance based on a set of `Hydrations`
|
|
@@ -13,7 +13,7 @@ import { isPlainObject } from "./object.js";
|
|
|
13
13
|
* @throws `Error` if the value is a class instance that cannot be dehydrated (i.e. is not matched by any constructor in `hydrations`).
|
|
14
14
|
*/
|
|
15
15
|
export function dehydrate(value, hydrations) {
|
|
16
|
-
return new Dehydrator(hydrations).
|
|
16
|
+
return new Dehydrator(hydrations).transform(value);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* Deeply hydrate a class instance based on a set of `Hydrations`
|
|
@@ -23,44 +23,44 @@ export function dehydrate(value, hydrations) {
|
|
|
23
23
|
* - Note: the recursion in this function does not currently protect against infinite loops.
|
|
24
24
|
*/
|
|
25
25
|
export function hydrate(value, hydrations) {
|
|
26
|
-
return new Hydrator(hydrations).
|
|
26
|
+
return new Hydrator(hydrations).transform(value);
|
|
27
27
|
}
|
|
28
28
|
/** Is an unknown value a dehydrated object with a `_type` key. */
|
|
29
29
|
const isDehydrated = (v) => typeof v._type === "string";
|
|
30
|
-
/**
|
|
30
|
+
/** Hydrates a value with a set of hydrations. */
|
|
31
31
|
export class Hydrator {
|
|
32
32
|
constructor(hydrations) {
|
|
33
33
|
this._hydrations = hydrations;
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
transform(value) {
|
|
36
36
|
if (value instanceof Array)
|
|
37
|
-
return
|
|
37
|
+
return transformArray(value, this);
|
|
38
38
|
if (isPlainObject(value)) {
|
|
39
39
|
if (!isDehydrated(value))
|
|
40
|
-
return
|
|
40
|
+
return transformObject(value, this);
|
|
41
41
|
const { _type, ...props } = value;
|
|
42
42
|
const hydration = this._hydrations[_type];
|
|
43
43
|
if (hydration)
|
|
44
|
-
return { __proto__: hydration.prototype, ...
|
|
44
|
+
return { __proto__: hydration.prototype, ...transformObject(props, this) };
|
|
45
45
|
throw new AssertionError(`Cannot hydrate "${_type}" object`, value);
|
|
46
46
|
}
|
|
47
47
|
return value;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
/**
|
|
50
|
+
/** Dehydrates a value with a set of hydrations. */
|
|
51
51
|
export class Dehydrator {
|
|
52
52
|
constructor(hydrations) {
|
|
53
53
|
this._hydrations = hydrations;
|
|
54
54
|
}
|
|
55
|
-
|
|
55
|
+
transform(value) {
|
|
56
56
|
if (value instanceof Array)
|
|
57
|
-
return
|
|
57
|
+
return transformArray(value, this);
|
|
58
58
|
if (isPlainObject(value))
|
|
59
|
-
return
|
|
59
|
+
return transformObject(value, this);
|
|
60
60
|
if (isData(value)) {
|
|
61
61
|
for (const [_type, hydration] of Object.entries(this._hydrations))
|
|
62
62
|
if (value instanceof hydration)
|
|
63
|
-
return { _type, ...
|
|
63
|
+
return { _type, ...transformObject(value, this) };
|
|
64
64
|
throw new AssertionError(`Cannot dehydrate object`, value);
|
|
65
65
|
}
|
|
66
66
|
return value;
|
package/util/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from "./array.js";
|
|
2
2
|
export * from "./assert.js";
|
|
3
|
+
export * from "./async.js";
|
|
3
4
|
export * from "./boolean.js";
|
|
4
5
|
export * from "./class.js";
|
|
5
6
|
export * from "./clone.js";
|
|
@@ -7,7 +8,6 @@ export * from "./constants.js";
|
|
|
7
8
|
export * from "./data.js";
|
|
8
9
|
export * from "./date.js";
|
|
9
10
|
export * from "./debug.js";
|
|
10
|
-
export * from "./derive.js";
|
|
11
11
|
export * from "./diff.js";
|
|
12
12
|
export * from "./entry.js";
|
|
13
13
|
export * from "./equal.js";
|
|
@@ -23,13 +23,13 @@ export * from "./null.js";
|
|
|
23
23
|
export * from "./number.js";
|
|
24
24
|
export * from "./object.js";
|
|
25
25
|
export * from "./observable.js";
|
|
26
|
-
export * from "./async.js";
|
|
27
26
|
export * from "./random.js";
|
|
28
27
|
export * from "./search.js";
|
|
29
28
|
export * from "./serialise.js";
|
|
30
29
|
export * from "./sort.js";
|
|
31
30
|
export * from "./string.js";
|
|
32
31
|
export * from "./template.js";
|
|
32
|
+
export * from "./transform.js";
|
|
33
33
|
export * from "./undefined.js";
|
|
34
34
|
export * from "./units.js";
|
|
35
35
|
export * from "./url.js";
|
package/util/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from "./array.js";
|
|
2
2
|
export * from "./assert.js";
|
|
3
|
+
export * from "./async.js";
|
|
3
4
|
export * from "./boolean.js";
|
|
4
5
|
export * from "./class.js";
|
|
5
6
|
export * from "./clone.js";
|
|
@@ -7,7 +8,6 @@ export * from "./constants.js";
|
|
|
7
8
|
export * from "./data.js";
|
|
8
9
|
export * from "./date.js";
|
|
9
10
|
export * from "./debug.js";
|
|
10
|
-
export * from "./derive.js";
|
|
11
11
|
export * from "./diff.js";
|
|
12
12
|
export * from "./entry.js";
|
|
13
13
|
export * from "./equal.js";
|
|
@@ -23,13 +23,13 @@ export * from "./null.js";
|
|
|
23
23
|
export * from "./number.js";
|
|
24
24
|
export * from "./object.js";
|
|
25
25
|
export * from "./observable.js";
|
|
26
|
-
export * from "./async.js";
|
|
27
26
|
export * from "./random.js";
|
|
28
27
|
export * from "./search.js";
|
|
29
28
|
export * from "./serialise.js";
|
|
30
29
|
export * from "./sort.js";
|
|
31
30
|
export * from "./string.js";
|
|
32
31
|
export * from "./template.js";
|
|
32
|
+
export * from "./transform.js";
|
|
33
33
|
export * from "./undefined.js";
|
|
34
34
|
export * from "./units.js";
|
|
35
35
|
export * from "./url.js";
|
package/util/observable.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Dispatcher } from "./function.js";
|
|
2
2
|
import { Handler } from "./error.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Transformer } from "./transform.js";
|
|
4
4
|
import { Validator } from "./validate.js";
|
|
5
5
|
/** Function that ends a subscription. */
|
|
6
6
|
export declare type Unsubscriber = () => void;
|
|
@@ -78,10 +78,10 @@ export declare class ThroughObserver<T> extends AbstractObserver<T, T> {
|
|
|
78
78
|
export declare class OnceObserver<T> extends ThroughObserver<T> {
|
|
79
79
|
next(value: T): void;
|
|
80
80
|
}
|
|
81
|
-
/** Oserver that
|
|
82
|
-
export declare class
|
|
83
|
-
protected
|
|
84
|
-
constructor(
|
|
81
|
+
/** Oserver that transforms its next values with a transformer. */
|
|
82
|
+
export declare class TransformObserver<I, O> extends AbstractObserver<I, O> {
|
|
83
|
+
protected _transformer: Transformer<I, O>;
|
|
84
|
+
constructor(transformer: Transformer<I, O>, target: Observer<O>);
|
|
85
85
|
next(value: I): void;
|
|
86
86
|
}
|
|
87
87
|
/** Observer that validates its next values with a validator. */
|
package/util/observable.js
CHANGED
|
@@ -2,7 +2,7 @@ import { ConditionError } from "../error/index.js";
|
|
|
2
2
|
import { BLACKHOLE, dispatch } from "./function.js";
|
|
3
3
|
import { logError } from "./error.js";
|
|
4
4
|
import { isData } from "./data.js";
|
|
5
|
-
import {
|
|
5
|
+
import { transform } from "./transform.js";
|
|
6
6
|
import { validate } from "./validate.js";
|
|
7
7
|
/** Is an unknown object an object implementing `Observable` */
|
|
8
8
|
export const isObservable = (v) => isData(v) && typeof v.subscribe === "function";
|
|
@@ -103,17 +103,17 @@ export class OnceObserver extends ThroughObserver {
|
|
|
103
103
|
this.complete();
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
-
/** Oserver that
|
|
107
|
-
export class
|
|
108
|
-
constructor(
|
|
106
|
+
/** Oserver that transforms its next values with a transformer. */
|
|
107
|
+
export class TransformObserver extends AbstractObserver {
|
|
108
|
+
constructor(transformer, target) {
|
|
109
109
|
super(target);
|
|
110
|
-
this.
|
|
110
|
+
this._transformer = transformer;
|
|
111
111
|
}
|
|
112
112
|
next(value) {
|
|
113
113
|
const target = this._target;
|
|
114
114
|
if (!target)
|
|
115
115
|
throw new ConditionError("Observer is closed");
|
|
116
|
-
dispatchNext(target,
|
|
116
|
+
dispatchNext(target, transform(value, this._transformer));
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
/** Observer that validates its next values with a validator. */
|
package/util/sort.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ImmutableObject } from "./object.js";
|
|
|
2
2
|
import { ImmutableMap } from "./map.js";
|
|
3
3
|
import type { Entry } from "./entry.js";
|
|
4
4
|
import { ImmutableArray } from "./array.js";
|
|
5
|
-
import {
|
|
5
|
+
import { Transformer } from "./transform.js";
|
|
6
6
|
/** Object that can rank two values using its `rank()` function. */
|
|
7
7
|
export interface Rankable<T> {
|
|
8
8
|
rank(left: T, right: T): number;
|
|
@@ -53,9 +53,9 @@ export declare function sortObject<T>(input: ImmutableObject<T>, ranker?: Ranker
|
|
|
53
53
|
/** Sort a map using a ranker (defaults to sorting by key in ascending order). */
|
|
54
54
|
export declare function sortMap<T>(input: ImmutableMap<T>, ranker?: Ranker<Entry<T>>): ImmutableMap<T>;
|
|
55
55
|
/** Derive a value and match it against a target value. */
|
|
56
|
-
export declare class
|
|
57
|
-
private
|
|
56
|
+
export declare class TransformRanker<T, TT> implements Rankable<T> {
|
|
57
|
+
private _transformer;
|
|
58
58
|
private _ranker;
|
|
59
|
-
constructor(
|
|
59
|
+
constructor(transformer: Transformer<T, TT>, ranker?: Ranker<TT>);
|
|
60
60
|
rank(left: T, right: T): number;
|
|
61
61
|
}
|
package/util/sort.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { transform } from "./transform.js";
|
|
2
2
|
/** Rank two values with a `Ranker`. */
|
|
3
3
|
export function rank(left, ranker, right) {
|
|
4
4
|
return typeof ranker === "function" ? ranker(left, right) : ranker.rank(left, right);
|
|
@@ -90,7 +90,6 @@ export const VALUE_DESC = ([, l], [, r]) => DESC(l, r);
|
|
|
90
90
|
*
|
|
91
91
|
* @param items The actual list of items that's sorted in place.
|
|
92
92
|
* @param ranker A rank function that takes a left/right value and returns 1/0/-1 (like `Array.prototype.sort()`).
|
|
93
|
-
* @param deriver A deriving function that picks the specific value to sort from out of the full value.
|
|
94
93
|
* @param leftPointer Index in the set of items to start sorting on.
|
|
95
94
|
* @param rightPointer Index in the set of items to stop sorting on.
|
|
96
95
|
*
|
|
@@ -161,12 +160,12 @@ export function sortMap(input, ranker = KEY_ASC) {
|
|
|
161
160
|
return _quicksort(array, ranker) ? new Map(array) : input;
|
|
162
161
|
}
|
|
163
162
|
/** Derive a value and match it against a target value. */
|
|
164
|
-
export class
|
|
165
|
-
constructor(
|
|
166
|
-
this.
|
|
163
|
+
export class TransformRanker {
|
|
164
|
+
constructor(transformer, ranker = ASC) {
|
|
165
|
+
this._transformer = transformer;
|
|
167
166
|
this._ranker = ranker;
|
|
168
167
|
}
|
|
169
168
|
rank(left, right) {
|
|
170
|
-
return rank(
|
|
169
|
+
return rank(transform(left, this._transformer), this._ranker, transform(right, this._transformer));
|
|
171
170
|
}
|
|
172
171
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Entry } from "./entry.js";
|
|
2
|
+
import type { ArrayType, ImmutableArray } from "./array.js";
|
|
3
|
+
import type { ImmutableMap } from "./map.js";
|
|
4
|
+
import { ImmutableObject } from "./object.js";
|
|
5
|
+
import { Data, Value, Prop } from "./data.js";
|
|
6
|
+
/** Object that can be applied to an input to generate an output with its `apply()` method. */
|
|
7
|
+
export interface Transformable<I, O> {
|
|
8
|
+
transform(input: I): O;
|
|
9
|
+
}
|
|
10
|
+
/** Any applier (useful for `extends AnyTransformr` clauses). */
|
|
11
|
+
export declare type AnyTransformable = Transformable<any, any>;
|
|
12
|
+
/** Function that takes an input value and returns a value transformed from it, or an applier with matching arguments. */
|
|
13
|
+
export declare type Transformer<I, O> = Transformable<I, O> | ((input: I) => O) | O;
|
|
14
|
+
/** Any transformer (useful for `extends AnyTransformr` clauses). */
|
|
15
|
+
export declare type AnyTransformer = Transformer<any, any>;
|
|
16
|
+
/** Extract the transformed type from a transformer. */
|
|
17
|
+
export declare type TransformedType<T extends AnyTransformer> = T extends Transformer<unknown, infer TT> ? TT : never;
|
|
18
|
+
/** Is an unknown value a transformer. */
|
|
19
|
+
export declare const isTransformer: <T extends unknown>(v: unknown) => v is T;
|
|
20
|
+
/** Is an unknown value a derivable. */
|
|
21
|
+
export declare const isTransformable: <T extends AnyTransformable>(v: unknown) => v is T;
|
|
22
|
+
/** Transform a value using a transformer. */
|
|
23
|
+
export declare function transform<I, O>(input: I, transformer: (v: I) => O): O;
|
|
24
|
+
export declare function transform<I, O>(input: I, transformer: Transformer<I, O>): O;
|
|
25
|
+
/**
|
|
26
|
+
* Apply a transformer to each item in a set of items and yield the transformed item.
|
|
27
|
+
*
|
|
28
|
+
* @yield Transformed items after calling `transformer()` on each.
|
|
29
|
+
* @returns Number of items that changed.
|
|
30
|
+
*/
|
|
31
|
+
export declare function transformItems<I, O>(items: Iterable<I>, transformer: (input: I) => O): Iterable<O>;
|
|
32
|
+
export declare function transformItems<I, O>(items: Iterable<I>, transformer: Transformer<I, O>): Iterable<O>;
|
|
33
|
+
/**
|
|
34
|
+
* Apply a transformer to each item in an array and return the transformed array.
|
|
35
|
+
*
|
|
36
|
+
* @param arr The input array or map-like object or iterable to iterate over.
|
|
37
|
+
* @param mapper Mapping function that receives the value and key and returns the corresponding value.
|
|
38
|
+
* - Mapper can return a promise. If it does will return a Promise that resolves once every value has resolved.
|
|
39
|
+
* - Return the `SKIP` symbol from the mapper to skip that property and not include it in the output object.
|
|
40
|
+
* - `SKIP` is useful because using `filter(Boolean)` doesn't currently filter in TypeScript (and requires another loop anyway).
|
|
41
|
+
* - Mapper can be a non-function static value and all the values will be set to that value.
|
|
42
|
+
*
|
|
43
|
+
* @return The mapped array.
|
|
44
|
+
* - Immutable so if the values don't change then the same instance will be returned.
|
|
45
|
+
*/
|
|
46
|
+
export declare function transformArray<T extends ImmutableArray>(arr: T, transformer: Transformer<ArrayType<T>, ArrayType<T>>): T;
|
|
47
|
+
export declare function transformArray<I, O>(arr: Iterable<I>, transformer: (v: I) => O): ImmutableArray<O>;
|
|
48
|
+
export declare function transformArray<I, O>(arr: Iterable<I>, transformer: Transformer<I, O>): ImmutableArray<O>;
|
|
49
|
+
/**
|
|
50
|
+
* Transform the _values_ of a set of entries using a transformer.
|
|
51
|
+
* @yield Transformed entry after calling transforming the new value for each entry.
|
|
52
|
+
*/
|
|
53
|
+
export declare function transformValues<I, O>(entries: Iterable<Entry<I>>, transformer: (v: I) => O): Iterable<Entry<O>>;
|
|
54
|
+
export declare function transformValues<I, O>(entries: Iterable<Entry<I>>, transformer: Transformer<I, O>): Iterable<Entry<O>>;
|
|
55
|
+
/**
|
|
56
|
+
* Transform the _values_ of a map using a transformer.
|
|
57
|
+
* @return New map after transforming its values.
|
|
58
|
+
*/
|
|
59
|
+
export declare function transformMap<I, O>(map: ImmutableMap<I>, transformer: (v: I) => O): ImmutableMap<O>;
|
|
60
|
+
export declare function transformMap<I, O>(map: ImmutableMap<I>, transformer: Transformer<I, O>): ImmutableMap<O>;
|
|
61
|
+
/**
|
|
62
|
+
* Transform the _values_ of an object using a transformer.
|
|
63
|
+
* @return New object after transforming its entries.
|
|
64
|
+
*/
|
|
65
|
+
export declare function transformObject<T extends Data>(obj: T, transformer: Transformer<Value<T>, Value<T>>): T;
|
|
66
|
+
export declare function transformObject<I extends Data, O extends {
|
|
67
|
+
[K in keyof I]: unknown;
|
|
68
|
+
}>(obj: I, transformer: Transformer<Value<I>, Value<O>>): O;
|
|
69
|
+
export declare function transformObject<I, O>(obj: ImmutableObject<I>, transformer: (v: I) => O): ImmutableObject<O>;
|
|
70
|
+
export declare function transformObject<I, O>(obj: ImmutableObject<I>, transformer: Transformer<I, O>): ImmutableObject<O>;
|
|
71
|
+
/** Set of named transformers for a data object. */
|
|
72
|
+
export declare type Transformers<T extends Data> = {
|
|
73
|
+
readonly [K in keyof T]?: Transformer<T[K], T[K]>;
|
|
74
|
+
};
|
|
75
|
+
/** Complete set of named transformers for a data object (i.e. every prop has a transformer specified). */
|
|
76
|
+
export declare type RequiredTransformers<T extends Data> = {
|
|
77
|
+
readonly [K in keyof T]: Transformer<T[K], T[K]>;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Transform the props of a data object using a set of transforms.
|
|
81
|
+
* @yields Valid new prop entries for the data object (only changed values are yielded).
|
|
82
|
+
*/
|
|
83
|
+
export declare function transformProps<T extends Data>(existing: T, transformers: Transformers<T>): Generator<Prop<T>, void>;
|
|
84
|
+
/**
|
|
85
|
+
* Transform a new data object using a set of transformers for its props.
|
|
86
|
+
* @returns New object with changed props (or the same object if no changes were made).
|
|
87
|
+
*/
|
|
88
|
+
export declare function transformData<T extends Data>(existing: T, transformers: Transformers<T>): T;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isFunction } from "./function.js";
|
|
2
|
+
import { toProps, isData } from "./data.js";
|
|
3
|
+
import { WatchIterator } from "./iterate.js";
|
|
4
|
+
/** Is an unknown value a transformer. */
|
|
5
|
+
export const isTransformer = (v) => typeof v === "function" || isTransformable(v);
|
|
6
|
+
/** Is an unknown value a derivable. */
|
|
7
|
+
export const isTransformable = (v) => isData(v) && typeof v.transform === "function";
|
|
8
|
+
export function transform(input, transformer) {
|
|
9
|
+
return isFunction(transformer) ? transformer(input) : isTransformable(transformer) ? transformer.transform(input) : transformer;
|
|
10
|
+
}
|
|
11
|
+
export function* transformItems(items, transformer) {
|
|
12
|
+
for (const item of items)
|
|
13
|
+
yield transform(item, transformer);
|
|
14
|
+
}
|
|
15
|
+
export function transformArray(arr, transformer) {
|
|
16
|
+
return Array.from(transformItems(arr, transformer));
|
|
17
|
+
}
|
|
18
|
+
export function* transformValues(entries, transformer) {
|
|
19
|
+
for (const [k, v] of entries)
|
|
20
|
+
yield [k, transform(v, transformer)];
|
|
21
|
+
}
|
|
22
|
+
export function transformMap(map, transformer) {
|
|
23
|
+
return new Map(transformValues(map, transformer));
|
|
24
|
+
}
|
|
25
|
+
export function transformObject(obj, transformer) {
|
|
26
|
+
return Object.fromEntries(transformValues(Object.entries(obj), transformer));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Transform the props of a data object using a set of transforms.
|
|
30
|
+
* @yields Valid new prop entries for the data object (only changed values are yielded).
|
|
31
|
+
*/
|
|
32
|
+
export function* transformProps(existing, transformers) {
|
|
33
|
+
for (const [k, v] of toProps(transformers)) {
|
|
34
|
+
const current = existing[k];
|
|
35
|
+
const transformed = transform(current, v);
|
|
36
|
+
if (transformed !== current)
|
|
37
|
+
yield [k, transformed];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Transform a new data object using a set of transformers for its props.
|
|
42
|
+
* @returns New object with changed props (or the same object if no changes were made).
|
|
43
|
+
*/
|
|
44
|
+
export function transformData(existing, transformers) {
|
|
45
|
+
const watcher = new WatchIterator(transformProps(existing, transformers));
|
|
46
|
+
const transformed = Object.fromEntries(watcher);
|
|
47
|
+
return watcher.count ? { ...existing, ...transformed } : existing;
|
|
48
|
+
}
|