shelving 1.22.2 → 1.24.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.
Files changed (62) hide show
  1. package/db/Database.d.ts +17 -27
  2. package/db/Database.js +34 -29
  3. package/db/Pagination.js +1 -1
  4. package/db/Write.d.ts +30 -0
  5. package/db/Write.js +38 -0
  6. package/db/index.d.ts +1 -1
  7. package/db/index.js +1 -1
  8. package/package.json +1 -1
  9. package/provider/BatchProvider.js +2 -2
  10. package/provider/CacheProvider.js +3 -3
  11. package/provider/MemoryProvider.js +6 -6
  12. package/query/Filter.d.ts +1 -1
  13. package/query/Filter.js +1 -1
  14. package/query/Filters.d.ts +1 -1
  15. package/query/Filters.js +1 -1
  16. package/query/Query.d.ts +1 -1
  17. package/query/Query.js +2 -2
  18. package/query/Rule.d.ts +3 -3
  19. package/query/Sort.d.ts +1 -1
  20. package/query/Sort.js +1 -1
  21. package/query/Sorts.d.ts +1 -1
  22. package/query/Sorts.js +1 -1
  23. package/react/useQuery.js +2 -2
  24. package/stream/DataState.d.ts +2 -2
  25. package/stream/DataState.js +2 -2
  26. package/stream/State.d.ts +4 -4
  27. package/stream/State.js +2 -2
  28. package/stream/Stream.d.ts +7 -7
  29. package/stream/Stream.js +5 -5
  30. package/transform/AddEntriesTransform.d.ts +1 -1
  31. package/transform/AddEntriesTransform.js +1 -1
  32. package/transform/AddItemsTransform.d.ts +1 -1
  33. package/transform/AddItemsTransform.js +1 -1
  34. package/transform/DataTransform.d.ts +1 -1
  35. package/transform/DataTransform.js +3 -3
  36. package/transform/IncrementTransform.d.ts +1 -1
  37. package/transform/IncrementTransform.js +1 -1
  38. package/transform/RemoveEntriesTransform.d.ts +1 -1
  39. package/transform/RemoveEntriesTransform.js +1 -1
  40. package/transform/RemoveItemsTransform.d.ts +1 -1
  41. package/transform/RemoveItemsTransform.js +1 -1
  42. package/transform/Transform.d.ts +3 -3
  43. package/transform/hydrations.d.ts +1 -1
  44. package/transform/hydrations.js +1 -1
  45. package/transform/util.js +2 -2
  46. package/util/clone.js +3 -3
  47. package/util/filter.d.ts +4 -4
  48. package/util/filter.js +5 -5
  49. package/util/hydrate.d.ts +7 -7
  50. package/util/hydrate.js +13 -13
  51. package/util/index.d.ts +2 -2
  52. package/util/index.js +2 -2
  53. package/util/observable.d.ts +5 -5
  54. package/util/observable.js +6 -6
  55. package/util/sort.d.ts +4 -4
  56. package/util/sort.js +5 -6
  57. package/util/transform.d.ts +77 -0
  58. package/util/transform.js +48 -0
  59. package/db/Change.d.ts +0 -37
  60. package/db/Change.js +0 -60
  61. package/util/derive.d.ts +0 -88
  62. package/util/derive.js +0 -48
@@ -6,7 +6,7 @@ export class AddItemsTransform extends Transform {
6
6
  super();
7
7
  this.items = items;
8
8
  }
9
- derive(existing) {
9
+ transform(existing) {
10
10
  return isArray(existing) ? withItems(existing, this.items) : this.items;
11
11
  }
12
12
  /** Iterate over the items. */
@@ -14,7 +14,7 @@ export declare type Transforms<T extends Data> = {
14
14
  export declare class DataTransform<T extends Data> extends Transform<T> implements Iterable<Prop<Transforms<T>>> {
15
15
  readonly transforms: Transforms<T>;
16
16
  constructor(transforms: Transforms<T>);
17
- derive(existing: T): T;
17
+ transform(existing: T): T;
18
18
  /** Return a new object with the specified additional transform. */
19
19
  prop<K extends Key<T>>(key: K, transform: T[K] | Transform<T[K]>): this;
20
20
  /** Return a new object with the specified additional transforms. */
@@ -1,4 +1,4 @@
1
- import { deriveData } from "../util/index.js";
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
- derive(existing) {
10
- return deriveData(existing, this.transforms);
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) {
@@ -8,5 +8,5 @@ export declare class IncrementTransform extends Transform<number> {
8
8
  static DECREMENT_ONE: IncrementTransform;
9
9
  readonly amount: number;
10
10
  constructor(amount: number);
11
- derive(existing?: unknown): number;
11
+ transform(existing?: unknown): number;
12
12
  }
@@ -8,7 +8,7 @@ export class IncrementTransform extends Transform {
8
8
  super();
9
9
  this.amount = amount;
10
10
  }
11
- derive(existing) {
11
+ transform(existing) {
12
12
  return typeof existing === "number" ? existing + this.amount : this.amount;
13
13
  }
14
14
  }
@@ -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
- derive(existing?: unknown): ImmutableObject<T>;
7
+ transform(existing?: unknown): ImmutableObject<T>;
8
8
  /** Iterate over the entry keys. */
9
9
  [Symbol.iterator](): Iterator<string, void>;
10
10
  }
@@ -6,7 +6,7 @@ export class RemoveEntriesTransform extends Transform {
6
6
  super();
7
7
  this.props = props;
8
8
  }
9
- derive(existing) {
9
+ transform(existing) {
10
10
  return isObject(existing) ? withoutEntries(existing, this.props) : {};
11
11
  }
12
12
  /** Iterate over the entry keys. */
@@ -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
- derive(existing?: ImmutableArray<T> | unknown): ImmutableArray<T>;
7
+ transform(existing?: ImmutableArray<T> | unknown): ImmutableArray<T>;
8
8
  /** Iterate over the items. */
9
9
  [Symbol.iterator](): Iterator<T, void>;
10
10
  }
@@ -6,7 +6,7 @@ export class RemoveItemsTransform extends Transform {
6
6
  super();
7
7
  this.items = items;
8
8
  }
9
- derive(existing) {
9
+ transform(existing) {
10
10
  return isArray(existing) ? withoutItems(existing, this.items) : this.items;
11
11
  }
12
12
  /** Iterate over the items. */
@@ -1,9 +1,9 @@
1
- import { Derivable } from "../util/index.js";
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 Derivable<T, T> {
6
+ export declare abstract class Transform<T> implements Transformable<T, T> {
7
7
  /** Apply this transform to a value. */
8
- abstract derive(existing?: unknown): T;
8
+ abstract transform(existing?: unknown): T;
9
9
  }
@@ -11,5 +11,5 @@ export declare const TRANSFORM_HYDRATIONS: {
11
11
  removeItems: typeof RemoveItemsTransform;
12
12
  addEntries: typeof AddEntriesTransform;
13
13
  removeEntries: typeof RemoveEntriesTransform;
14
- transforms: typeof DataTransform;
14
+ transformData: typeof DataTransform;
15
15
  };
@@ -11,6 +11,6 @@ export const TRANSFORM_HYDRATIONS = {
11
11
  removeItems: RemoveItemsTransform,
12
12
  addEntries: AddEntriesTransform,
13
13
  removeEntries: RemoveEntriesTransform,
14
- transforms: DataTransform,
14
+ transformData: DataTransform,
15
15
  };
16
16
  TRANSFORM_HYDRATIONS;
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 { derive, validate, toProps } from "../util/index.js";
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(derive(undefined, unsafeTransform), validator);
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 { deriveArray, deriveObject } from "./derive.js";
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 = deriveArray(input, recursor);
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 = deriveObject(input, recursor);
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 { Deriver } from "./derive.js";
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 MatchDerived<L, LL, R> implements Matchable<L, R> {
56
- private _deriver;
55
+ export declare class TransformMatcher<L, LL, R> implements Matchable<L, R> {
56
+ private _transformer;
57
57
  private _matcher;
58
- constructor(deriver: Deriver<L, LL>, matcher?: Matcher<LL, R>);
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 { derive } from "./derive.js";
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 MatchDerived {
55
- constructor(deriver, matcher = IS) {
56
- this._deriver = deriver;
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(derive(item, this._deriver), this._matcher, target);
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 { Derivable } from "./derive.js";
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
- /** Deriver that hydrates a value with a set of hydrations. */
34
- export declare class Hydrator implements Derivable<unknown, unknown> {
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
- derive(value: unknown): unknown;
37
+ transform(value: unknown): unknown;
38
38
  }
39
- /** Deriver that dehydrates a value with a set of hydrations. */
40
- export declare class Dehydrator implements Derivable<unknown, unknown> {
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
- derive(value: unknown): unknown;
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 { deriveArray, deriveObject } from "./derive.js";
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).derive(value);
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).derive(value);
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
- /** Deriver that hydrates a value with a set of hydrations. */
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
- derive(value) {
35
+ transform(value) {
36
36
  if (value instanceof Array)
37
- return deriveArray(value, this);
37
+ return transformArray(value, this);
38
38
  if (isPlainObject(value)) {
39
39
  if (!isDehydrated(value))
40
- return deriveObject(value, this);
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, ...deriveObject(props, this) };
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
- /** Deriver that dehydrates a value with a set of hydrations. */
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
- derive(value) {
55
+ transform(value) {
56
56
  if (value instanceof Array)
57
- return deriveArray(value, this);
57
+ return transformArray(value, this);
58
58
  if (isPlainObject(value))
59
- return deriveObject(value, this);
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, ...deriveObject(value, this) };
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";
@@ -1,6 +1,6 @@
1
1
  import { Dispatcher } from "./function.js";
2
2
  import { Handler } from "./error.js";
3
- import { Deriver } from "./derive.js";
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 derives its next values with a deriver. */
82
- export declare class DeriveObserver<I, O> extends AbstractObserver<I, O> {
83
- protected _deriver: Deriver<I, O>;
84
- constructor(deriver: Deriver<I, O>, target: Observer<O>);
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. */
@@ -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 { derive } from "./derive.js";
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 derives its next values with a deriver. */
107
- export class DeriveObserver extends AbstractObserver {
108
- constructor(deriver, target) {
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._deriver = deriver;
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, derive(value, this._deriver));
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 { Deriver } from "./derive.js";
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 RankDerived<T, TT> implements Rankable<T> {
57
- private _deriver;
56
+ export declare class TransformRanker<T, TT> implements Rankable<T> {
57
+ private _transformer;
58
58
  private _ranker;
59
- constructor(deriver: Deriver<T, TT>, ranker?: Ranker<TT>);
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 { derive } from "./derive.js";
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 RankDerived {
165
- constructor(deriver, ranker = ASC) {
166
- this._deriver = deriver;
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(derive(left, this._deriver), this._ranker, derive(right, this._deriver));
169
+ return rank(transform(left, this._transformer), this._ranker, transform(right, this._transformer));
171
170
  }
172
171
  }
@@ -0,0 +1,77 @@
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
+ /** Is an unknown value a derivable. */
13
+ export declare const isApplier: <T extends AnyTransformable>(v: unknown) => v is T;
14
+ /** Function that takes an input value and returns a value transformed from it, or an applier with matching arguments. */
15
+ export declare type Transformer<I, O> = Transformable<I, O> | ((input: I) => O) | O;
16
+ /** Any transformer (useful for `extends AnyTransformr` clauses). */
17
+ export declare type AnyTransformer = Transformer<any, any>;
18
+ /** Extract the transformed type from a transformer. */
19
+ export declare type TransformedType<T extends AnyTransformer> = T extends Transformer<unknown, infer TT> ? TT : never;
20
+ /** Is an unknown value a transformer. */
21
+ export declare const isTransformer: <T extends unknown>(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
+ * @yield Transformed items after calling `transformer()` on each.
28
+ */
29
+ export declare function transformItems<I, O>(items: Iterable<I>, transformer: (input: I) => O): Iterable<O>;
30
+ export declare function transformItems<I, O>(items: Iterable<I>, transformer: Transformer<I, O>): Iterable<O>;
31
+ /**
32
+ * Apply a transformer to each item in an array and return the transformed array.
33
+ * @return The transformed array.
34
+ */
35
+ export declare function transformArray<T extends ImmutableArray>(arr: T, transformer: Transformer<ArrayType<T>, ArrayType<T>>): T;
36
+ export declare function transformArray<I, O>(arr: Iterable<I>, transformer: (v: I) => O): ImmutableArray<O>;
37
+ export declare function transformArray<I, O>(arr: Iterable<I>, transformer: Transformer<I, O>): ImmutableArray<O>;
38
+ /**
39
+ * Transform the _values_ of a set of entries using a transformer.
40
+ * @yield Transformed entry after calling transforming the new value for each entry.
41
+ */
42
+ export declare function transformValues<I, O>(entries: Iterable<Entry<I>>, transformer: (v: I) => O): Iterable<Entry<O>>;
43
+ export declare function transformValues<I, O>(entries: Iterable<Entry<I>>, transformer: Transformer<I, O>): Iterable<Entry<O>>;
44
+ /**
45
+ * Transform the _values_ of a map using a transformer.
46
+ * @return New map after transforming its values.
47
+ */
48
+ export declare function transformMap<I, O>(map: ImmutableMap<I>, transformer: (v: I) => O): ImmutableMap<O>;
49
+ export declare function transformMap<I, O>(map: ImmutableMap<I>, transformer: Transformer<I, O>): ImmutableMap<O>;
50
+ /**
51
+ * Transform the _values_ of an object using a transformer.
52
+ * @return New object after transforming its entries.
53
+ */
54
+ export declare function transformObject<T extends Data>(obj: T, transformer: Transformer<Value<T>, Value<T>>): T;
55
+ export declare function transformObject<I extends Data, O extends {
56
+ [K in keyof I]: unknown;
57
+ }>(obj: I, transformer: Transformer<Value<I>, Value<O>>): O;
58
+ export declare function transformObject<I, O>(obj: ImmutableObject<I>, transformer: (v: I) => O): ImmutableObject<O>;
59
+ export declare function transformObject<I, O>(obj: ImmutableObject<I>, transformer: Transformer<I, O>): ImmutableObject<O>;
60
+ /** Set of named transformers for a data object. */
61
+ export declare type Transformers<T extends Data> = {
62
+ readonly [K in keyof T]?: Transformer<T[K], T[K]>;
63
+ };
64
+ /** Complete set of named transformers for a data object (i.e. every prop has a transformer specified). */
65
+ export declare type RequiredTransformers<T extends Data> = {
66
+ readonly [K in keyof T]: Transformer<T[K], T[K]>;
67
+ };
68
+ /**
69
+ * Transform the props of a data object using a set of transforms.
70
+ * @yields Valid new prop entries for the data object (only changed values are yielded).
71
+ */
72
+ export declare function transformProps<T extends Data>(existing: T, transformers: Transformers<T>): Generator<Prop<T>, void>;
73
+ /**
74
+ * Transform a new data object using a set of transformers for its props.
75
+ * @returns New object with changed props (or the same object if no changes were made).
76
+ */
77
+ 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 derivable. */
5
+ export const isApplier = (v) => isData(v) && typeof v.transform === "function";
6
+ /** Is an unknown value a transformer. */
7
+ export const isTransformer = (v) => typeof v === "function" || isApplier(v);
8
+ export function transform(input, transformer) {
9
+ return isFunction(transformer) ? transformer(input) : isApplier(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
+ }