shelving 1.154.2 → 1.156.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/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.154.2",
14
+ "version": "1.156.0",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -7,12 +7,14 @@ export type ChoiceOptions<K extends string> = {
7
7
  export interface ChoiceSchemaOptions<K extends string> extends Omit<SchemaOptions, "value"> {
8
8
  /** Specify correct options using a dictionary of entries. */
9
9
  options: ChoiceOptions<K>;
10
+ /** Default option for the value. */
11
+ value?: K;
10
12
  }
11
13
  /** Choose from an allowed set of values. */
12
14
  export declare class ChoiceSchema<K extends string> extends Schema<K> {
13
15
  readonly value: string;
14
16
  readonly options: ChoiceOptions<K>;
15
- constructor({ options, ...rest }: ChoiceSchemaOptions<K>);
17
+ constructor({ options, value, ...rest }: ChoiceSchemaOptions<K>);
16
18
  validate(unsafeValue?: unknown): K;
17
19
  }
18
20
  /** Choose from an allowed set of values. */
@@ -5,8 +5,8 @@ import { Schema } from "./Schema.js";
5
5
  /** Choose from an allowed set of values. */
6
6
  export class ChoiceSchema extends Schema {
7
7
  options;
8
- constructor({ options, ...rest }) {
9
- super({ value: requireFirst(getKeys(options)), ...rest });
8
+ constructor({ options, value = requireFirst(getKeys(options)), ...rest }) {
9
+ super({ value, ...rest });
10
10
  this.options = options;
11
11
  }
12
12
  validate(unsafeValue = this.value) {
@@ -10,13 +10,20 @@ export interface DateSchemaOptions extends SchemaOptions {
10
10
  readonly min?: Nullish<PossibleDate>;
11
11
  readonly max?: Nullish<PossibleDate>;
12
12
  readonly input?: DateInputType | undefined;
13
+ /**
14
+ * Rounding step (in milliseconds, because that's the base unit for time).
15
+ * - E.g. `1000 * 60` will round to the nearest minute.
16
+ * - Note: HTML `<input>` `step` attributes are in _seconds_, so you may need to convert units.
17
+ */
18
+ readonly step?: number | undefined;
13
19
  }
14
20
  export declare class DateSchema extends Schema<string> {
15
21
  readonly value: PossibleDate;
16
22
  readonly min: Date | undefined;
17
23
  readonly max: Date | undefined;
18
24
  readonly input: DateInputType;
19
- constructor({ min, max, value, input, ...options }: DateSchemaOptions);
25
+ readonly step: number | undefined;
26
+ constructor({ min, max, value, input, step, ...options }: DateSchemaOptions);
20
27
  validate(value?: unknown): string;
21
28
  stringify(value: Date): string;
22
29
  format(value: Date): string;
@@ -1,27 +1,31 @@
1
1
  import { ValueFeedback } from "../feedback/Feedback.js";
2
2
  import { getDate, requireDateString } from "../util/date.js";
3
3
  import { formatDate } from "../util/format.js";
4
+ import { roundStep } from "../util/number.js";
4
5
  import { NULLABLE } from "./NullableSchema.js";
5
6
  import { Schema } from "./Schema.js";
6
7
  export class DateSchema extends Schema {
7
8
  min;
8
9
  max;
9
10
  input;
10
- constructor({ min, max, value = "now", input = "date", ...options }) {
11
+ step;
12
+ constructor({ min, max, value = "now", input = "date", step, ...options }) {
11
13
  super({ title: "Date", value, ...options });
12
14
  this.min = getDate(min);
13
15
  this.max = getDate(max);
14
16
  this.input = input;
17
+ this.step = step;
15
18
  }
16
19
  validate(value = this.value) {
17
20
  const date = getDate(value);
18
21
  if (!date)
19
22
  throw new ValueFeedback(value ? "Invalid date" : "Required", value);
20
- if (this.min && date < this.min)
21
- throw new ValueFeedback(`Minimum ${this.format(this.min)}`, date);
22
- if (this.max && date > this.max)
23
- throw new ValueFeedback(`Maximum ${this.format(this.max)}`, date);
24
- return this.stringify(date);
23
+ const rounded = typeof this.step === "number" ? new Date(roundStep(date.getTime(), this.step)) : date;
24
+ if (this.min && rounded < this.min)
25
+ throw new ValueFeedback(`Minimum ${this.format(this.min)}`, rounded);
26
+ if (this.max && rounded > this.max)
27
+ throw new ValueFeedback(`Maximum ${this.format(this.max)}`, rounded);
28
+ return this.stringify(rounded);
25
29
  }
26
30
  stringify(value) {
27
31
  return requireDateString(value);
@@ -1,19 +1,9 @@
1
- import { type PossibleDate } from "../util/date.js";
2
1
  import { DateSchema, type DateSchemaOptions } from "./DateSchema.js";
3
- /** Allowed options for `TimeSchama` */
4
- export interface TimeSchemaOptions extends DateSchemaOptions {
5
- step?: number | undefined;
6
- }
7
2
  /** Define a valid time in 24h hh:mm:ss.fff format, e.g. `23:59` or `24:00 */
8
3
  export declare class TimeSchema extends DateSchema {
9
- readonly value: PossibleDate;
10
- /**
11
- * Rounding step (in milliseconds, because that's the base unit for time), e.g. `60000` will round to the nearest second.
12
- * - Note: `<input type="time">` elements expect `step=""` to be in _seconds_ so you need to multiply this by `1000`
13
- */
14
- readonly step: number | undefined;
15
- constructor({ title, input, step, ...options }: TimeSchemaOptions);
16
- validate(unsafeValue?: unknown): string;
4
+ constructor({ title, input, ...options }: DateSchemaOptions);
5
+ stringify(value: Date): string;
6
+ format(value: Date): string;
17
7
  }
18
8
  /** Valid time, e.g. `2005-09-12` (required because falsy values are invalid). */
19
9
  export declare const TIME: TimeSchema;
@@ -1,30 +1,17 @@
1
- import { ValueFeedback } from "../feedback/Feedback.js";
2
- import { getDate, requireTimeString } from "../util/date.js";
1
+ import { requireTimeString } from "../util/date.js";
3
2
  import { formatTime } from "../util/format.js";
4
- import { roundStep } from "../util/number.js";
5
3
  import { DateSchema } from "./DateSchema.js";
6
4
  import { NULLABLE } from "./NullableSchema.js";
7
5
  /** Define a valid time in 24h hh:mm:ss.fff format, e.g. `23:59` or `24:00 */
8
6
  export class TimeSchema extends DateSchema {
9
- /**
10
- * Rounding step (in milliseconds, because that's the base unit for time), e.g. `60000` will round to the nearest second.
11
- * - Note: `<input type="time">` elements expect `step=""` to be in _seconds_ so you need to multiply this by `1000`
12
- */
13
- step;
14
- constructor({ title = "Time", input = "time", step, ...options }) {
7
+ constructor({ title = "Time", input = "time", ...options }) {
15
8
  super({ title, input, ...options });
16
- this.step = step;
17
9
  }
18
- validate(unsafeValue = this.value) {
19
- const date = getDate(unsafeValue);
20
- if (!date)
21
- throw new ValueFeedback(unsafeValue ? "Invalid time" : "Required", unsafeValue);
22
- const rounded = typeof this.step === "number" ? new Date(roundStep(date.getTime(), this.step)) : date;
23
- if (this.max && rounded > this.max)
24
- throw new ValueFeedback(`Maximum ${formatTime(this.max)}`, rounded);
25
- if (this.min && rounded < this.min)
26
- throw new ValueFeedback(`Minimum ${formatTime(this.min)}`, rounded);
27
- return requireTimeString(rounded);
10
+ stringify(value) {
11
+ return requireTimeString(value);
12
+ }
13
+ format(value) {
14
+ return formatTime(value);
28
15
  }
29
16
  }
30
17
  /** Valid time, e.g. `2005-09-12` (required because falsy values are invalid). */