shelving 1.157.2 → 1.157.3

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/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.157.2",
14
+ "version": "1.157.3",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -1,6 +1,6 @@
1
1
  import { ValueFeedback } from "../feedback/Feedback.js";
2
2
  import { isData } from "../util/data.js";
3
- import { mapObject } from "../util/transform.js";
3
+ import { mapProps } from "../util/transform.js";
4
4
  import { validateData } from "../util/validate.js";
5
5
  import { NULLABLE } from "./NullableSchema.js";
6
6
  import { OPTIONAL } from "./OptionalSchema.js";
@@ -22,13 +22,15 @@ export class DataSchema extends Schema {
22
22
  export const DATA = (props) => new DataSchema({ props });
23
23
  /** Valid data object with specifed properties, or `null` */
24
24
  export const NULLABLE_DATA = (props) => NULLABLE(new DataSchema({ props }));
25
- /** Create a `DataSchema` that validates partially, i.e. properties can be their value, or `undefined` */
26
25
  export function PARTIAL(source) {
27
26
  const props = source instanceof DataSchema ? source.props : source;
28
27
  return new DataSchema({
29
- props: mapObject(props, OPTIONAL),
28
+ props: mapProps(props, _optionalProp),
30
29
  });
31
30
  }
31
+ function _optionalProp([, v]) {
32
+ return OPTIONAL(v);
33
+ }
32
34
  /** Create a `DataSchema` that validates a data item, i.e. it has a string or number `.id` identifier property. */
33
35
  export function ITEM(id, validators) {
34
36
  const props = validators instanceof DataSchema ? validators.props : validators;
@@ -1,4 +1,4 @@
1
- import type { Schema } from "./Schema.js";
1
+ import type { Validator } from "../index.js";
2
2
  import { ThroughSchema, type ThroughSchemaOptions } from "./ThroughSchema.js";
3
3
  /** Allowed options for `NullableSchema` */
4
4
  export interface NullableSchemaOptions<T> extends ThroughSchemaOptions<T | null> {
@@ -12,4 +12,4 @@ export declare class NullableSchema<T> extends ThroughSchema<T | null> {
12
12
  validate(unsafeValue?: unknown): T | null;
13
13
  }
14
14
  /** Create a new nullable schema from a source schema. */
15
- export declare const NULLABLE: <T>(source: Schema<T>) => NullableSchema<T>;
15
+ export declare const NULLABLE: <T>(source: Validator<T>) => NullableSchema<T>;
@@ -1,4 +1,4 @@
1
- import type { Schema } from "./Schema.js";
1
+ import type { Validator } from "../util/validate.js";
2
2
  import { ThroughSchema, type ThroughSchemaOptions } from "./ThroughSchema.js";
3
3
  export interface OptionalSchemaOptions<T> extends ThroughSchemaOptions<T | undefined> {
4
4
  /** Default value for an `OptionalSchema` can always `undefined` */
@@ -16,4 +16,4 @@ export declare class OptionalSchema<T> extends ThroughSchema<T | undefined> {
16
16
  validate(unsafeValue: unknown): T | undefined;
17
17
  }
18
18
  /** Make a property of a set of data optional, i.e. it can be the value or `undefined` */
19
- export declare const OPTIONAL: <T>(source: Schema<T>) => OptionalSchema<T>;
19
+ export declare const OPTIONAL: <T>(source: Validator<T>) => OptionalSchema<T>;
package/util/hydrate.js CHANGED
@@ -5,7 +5,7 @@ import { isMap } from "./map.js";
5
5
  import { getProps, getPrototype, isObject, isPlainObject } from "./object.js";
6
6
  import { isSet } from "./set.js";
7
7
  import { isString } from "./string.js";
8
- import { mapArray, mapObject } from "./transform.js";
8
+ import { mapArray, mapProps } from "./transform.js";
9
9
  /** Is an unknown value a dehydrated object with a `$type` key. */
10
10
  function _isDehydrated(value) {
11
11
  return isString(value.$type);
@@ -22,7 +22,7 @@ export function hydrate(value, hydrations) {
22
22
  return mapArray(value, hydrate, hydrations);
23
23
  if (isPlainObject(value)) {
24
24
  if (!_isDehydrated(value))
25
- return mapObject(value, hydrate, hydrations);
25
+ return mapProps(value, _hydrateProp, hydrations);
26
26
  const { $type, $value } = value;
27
27
  if ($type === "Map")
28
28
  return new Map($value);
@@ -33,11 +33,14 @@ export function hydrate(value, hydrations) {
33
33
  // Complex object, check the hydrations list.
34
34
  const hydration = hydrations[$type];
35
35
  if (hydration)
36
- return { __proto__: hydration.prototype, ...mapObject($value, hydrate, hydrations) };
36
+ return { __proto__: hydration.prototype, ...mapProps($value, _hydrateProp, hydrations) };
37
37
  throw new ValueError(`Cannot hydrate object "${$type}"`, { type: $type, received: $value, caller: hydrate });
38
38
  }
39
39
  return value;
40
40
  }
41
+ function _hydrateProp([, v], hydrations) {
42
+ return hydrate(v, hydrations);
43
+ }
41
44
  /**
42
45
  * Deeply dehydrate a class instance based on a set of `Hydrations`
43
46
  * - Dehydration allows you to pass class instances from a server back to a client.
@@ -59,13 +62,16 @@ export function dehydrate(value, hydrations) {
59
62
  if (isDate(value))
60
63
  return { $type: "Date", $value: value.getTime() };
61
64
  if (isPlainObject(value))
62
- return mapObject(value, dehydrate, hydrations);
65
+ return mapProps(value, _dehydrateProp, hydrations);
63
66
  // Complex object, check the hydrations list.
64
67
  const proto = getPrototype(value);
65
68
  for (const [$type, hydration] of getProps(hydrations))
66
69
  if (proto === hydration.prototype)
67
- return { $type, $value: mapObject(value, dehydrate, hydrations) };
70
+ return { $type, $value: mapProps(value, _dehydrateProp, hydrations) };
68
71
  throw new ValueError("Cannot dehydrate object", { received: value, caller: dehydrate });
69
72
  }
70
73
  return value;
71
74
  }
75
+ function _dehydrateProp([, v], hydrations) {
76
+ return dehydrate(v, hydrations);
77
+ }
@@ -1,32 +1,22 @@
1
- import type { ArrayItem, ImmutableArray } from "./array.js";
2
- import type { ImmutableDictionary } from "./dictionary.js";
1
+ import type { ImmutableArray } from "./array.js";
2
+ import type { DictionaryItem, ImmutableDictionary } from "./dictionary.js";
3
3
  import type { Entry } from "./entry.js";
4
4
  import type { Arguments } from "./function.js";
5
- import type { ImmutableObject, Value } from "./object.js";
6
- /** Function that can transform an input value into an output value. */
7
- export type Transform<I, O, A extends Arguments = []> = (input: I, ...args: A) => O;
8
- /** Function that can transform an input value into an output value. */
9
- export type AsyncTransform<I, O, A extends Arguments = []> = (input: I, ...args: A) => O | PromiseLike<O>;
5
+ import type { ImmutableObject, Prop, Value } from "./object.js";
10
6
  /** Set of named transforms for a data object (or `undefined` to skip the transform). */
11
7
  export type Transforms<I extends ImmutableObject, O extends ImmutableObject, A extends Arguments = []> = {
12
- readonly [K in keyof I]?: Transform<I[K], O[K], A>;
8
+ readonly [K in keyof I]?: (input: I[K], ...args: A) => O[K];
13
9
  };
14
10
  /** Modify a set of items using a transform. */
15
11
  export declare function mapItems<I, O, A extends Arguments = []>(items: Iterable<I>, transform: (v: I, ...args: A) => O, ...args: A): Iterable<O>;
16
- export declare function mapItems<I, O, A extends Arguments = []>(items: Iterable<I>, transform: Transform<I, O, A>, ...args: A): Iterable<O>;
17
12
  /** Modify the items of an array using a transform. */
18
- export declare function mapArray<T extends ImmutableArray>(arr: T, transform: Transform<ArrayItem<T>, ArrayItem<T>>): T;
19
13
  export declare function mapArray<I, O, A extends Arguments = []>(arr: Iterable<I>, transform: (v: I, ...args: A) => O, ...args: A): ImmutableArray<O>;
20
- export declare function mapArray<I, O, A extends Arguments = []>(arr: Iterable<I>, transform: Transform<I, O, A>, ...args: A): ImmutableArray<O>;
21
- /** Modify the values of the props of an object using a transform. */
22
- export declare function mapObject<T extends ImmutableObject>(obj: T, transform: Transform<Value<T>, Value<T>>): T;
23
- export declare function mapObject<I extends ImmutableObject, O extends ImmutableObject, A extends Arguments = []>(obj: I, transform: (v: Value<I>, ...args: A) => Value<O>, ...args: A): O;
24
- export declare function mapObject<I extends ImmutableObject, O extends ImmutableObject, A extends Arguments = []>(obj: I, transform: Transform<Value<I>, Value<O>, A>, ...args: A): O;
25
- /** Modify the values of a dictionary using a transform. */
26
- export declare function mapDictionary<I, O, A extends Arguments = []>(dictionary: ImmutableDictionary<I>, transform: (v: I, ...args: A) => O, ...args: A): ImmutableDictionary<O>;
27
- export declare function mapDictionary<I, O, A extends Arguments = []>(dictionary: ImmutableDictionary<I>, transform: Transform<I, O, A>, ...args: A): ImmutableDictionary<O>;
28
- /** Modify the values of a set of entries using a transform. */
29
- export declare function mapEntries<K, I, O, A extends Arguments = []>(entries: Iterable<Entry<K, I>>, transform: Transform<I, O, A>, ...args: A): Iterable<Entry<K, O>>;
14
+ /** Modify the _values_ of the props of an object using a transform. */
15
+ export declare function mapProps<I extends ImmutableObject, O extends ImmutableObject, A extends Arguments = []>(obj: I, transform: (prop: Prop<I>, ...args: A) => Value<O>, ...args: A): O;
16
+ /** Modify the _values_ of a dictionary using a transform. */
17
+ export declare function mapDictionary<I, O, A extends Arguments = []>(dictionary: ImmutableDictionary<I>, transform: (item: DictionaryItem<I>, ...args: A) => O, ...args: A): ImmutableDictionary<O>;
18
+ /** Modify the _values_ of a set of entries using a transform. */
19
+ export declare function mapEntries<K, I, O, A extends Arguments = []>(entries: Iterable<Entry<K, I>>, transform: (entry: Entry<K, I>, ...args: A) => O, ...args: A): Iterable<Entry<K, O>>;
30
20
  /**
31
21
  * Transform an object using a set of named transforms.
32
22
  *
@@ -38,4 +28,3 @@ export declare function transformObject<I extends ImmutableObject, O extends Imm
38
28
  export declare function transformObject<I extends ImmutableObject, O extends ImmutableObject, A extends Arguments = []>(obj: I | Partial<I>, transforms: Transforms<I, O | Partial<O>, A>, ...args: A): Partial<O>;
39
29
  /** Transform items in a sequence as they are yielded using a (potentially async) transform. */
40
30
  export declare function mapSequence<I, O, A extends Arguments = []>(sequence: AsyncIterable<I>, transform: (input: I, ...args: A) => O | PromiseLike<O>, ...args: A): AsyncIterable<O>;
41
- export declare function mapSequence<I, O, A extends Arguments = []>(sequence: AsyncIterable<I>, transform: AsyncTransform<I, O, A>, ...args: A): AsyncIterable<O>;
package/util/transform.js CHANGED
@@ -1,22 +1,26 @@
1
1
  import { getDictionaryItems } from "./dictionary.js";
2
2
  import { getProps } from "./object.js";
3
+ /** Modify a set of items using a transform. */
3
4
  export function* mapItems(items, transform, ...args) {
4
5
  for (const item of items)
5
6
  yield transform(item, ...args);
6
7
  }
8
+ /** Modify the items of an array using a transform. */
7
9
  export function mapArray(arr, transform, ...args) {
8
10
  return Array.from(mapItems(arr, transform, ...args));
9
11
  }
10
- export function mapObject(obj, transform, ...args) {
12
+ /** Modify the _values_ of the props of an object using a transform. */
13
+ export function mapProps(obj, transform, ...args) {
11
14
  return Object.fromEntries(mapEntries(getProps(obj), transform, ...args));
12
15
  }
16
+ /** Modify the _values_ of a dictionary using a transform. */
13
17
  export function mapDictionary(dictionary, transform, ...args) {
14
18
  return Object.fromEntries(mapEntries(getDictionaryItems(dictionary), transform, ...args));
15
19
  }
16
- /** Modify the values of a set of entries using a transform. */
20
+ /** Modify the _values_ of a set of entries using a transform. */
17
21
  export function* mapEntries(entries, transform, ...args) {
18
- for (const [k, v] of entries)
19
- yield [k, transform(v, ...args)];
22
+ for (const e of entries)
23
+ yield [e[0], transform(e, ...args)];
20
24
  }
21
25
  export function transformObject(input, transforms, ...args) {
22
26
  let changed = false;
@@ -37,6 +41,7 @@ export function transformObject(input, transforms, ...args) {
37
41
  }
38
42
  return changed ? output : input;
39
43
  }
44
+ /** Transform items in a sequence as they are yielded using a (potentially async) transform. */
40
45
  export async function* mapSequence(sequence, transform, ...args) {
41
46
  for await (const item of sequence)
42
47
  yield transform(item, ...args);