@sincpro/web-framework 1.1.0 → 2.1.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/README.md +188 -87
- package/dist/core/cache.d.ts +1 -2
- package/dist/core/cache.js +8 -5
- package/dist/core/explain.d.ts +15 -0
- package/dist/core/{transport.js → explain.js} +4 -26
- package/dist/core/form.d.ts +1 -1
- package/dist/core/form.js +1 -0
- package/dist/core/index.d.ts +9 -15
- package/dist/core/index.js +8 -14
- package/dist/core/list.d.ts +5 -4
- package/dist/core/list.js +1 -0
- package/dist/core/lookup.d.ts +1 -1
- package/dist/core/lookup.js +1 -0
- package/dist/core/present.d.ts +1 -0
- package/dist/core/present.js +54 -6
- package/dist/core/relation.d.ts +1 -1
- package/dist/core/relation.js +1 -1
- package/dist/core/summary.d.ts +1 -6
- package/dist/core/trail.d.ts +1 -1
- package/dist/core/trail.js +1 -0
- package/dist/errors/index.d.ts +48 -0
- package/dist/errors/index.js +56 -0
- package/dist/react/context.d.ts +6 -4
- package/dist/react/context.js +11 -3
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +2 -0
- package/dist/react/scope.d.ts +1 -1
- package/dist/react/scope.js +1 -1
- package/dist/react/useCommand.d.ts +1 -1
- package/dist/react/useCommand.js +1 -2
- package/dist/react/useContextValues.d.ts +15 -0
- package/dist/react/useContextValues.js +19 -0
- package/dist/react/useGroups.js +2 -2
- package/dist/react/useHistory.d.ts +1 -1
- package/dist/react/useHistory.js +3 -2
- package/dist/react/useListing.d.ts +4 -4
- package/dist/react/useListing.js +16 -7
- package/dist/react/useLookup.d.ts +1 -1
- package/dist/react/useLookup.js +3 -2
- package/dist/react/useNameOf.d.ts +1 -1
- package/dist/react/useNameOf.js +3 -3
- package/dist/react/usePivot.js +2 -2
- package/dist/react/useRecord.d.ts +1 -2
- package/dist/react/useRecord.js +18 -6
- package/dist/react/useRelated.d.ts +1 -1
- package/dist/react/useRelated.js +1 -0
- package/dist/react/useService.d.ts +38 -0
- package/dist/react/useService.js +66 -0
- package/dist/react/useSummary.d.ts +1 -1
- package/dist/react/useSummary.js +1 -0
- package/dist/telemetry/index.d.ts +45 -0
- package/dist/telemetry/index.js +55 -0
- package/dist/web/Refused.js +1 -1
- package/dist/web/index.d.ts +1 -1
- package/dist/web/index.js +1 -1
- package/dist/web/list/EntityList.d.ts +1 -1
- package/dist/web/list/EntityList.js +3 -3
- package/dist/web/list/EntitySearchBar.d.ts +2 -1
- package/dist/web/list/EntitySearchBar.js +17 -11
- package/dist/web/list/presentation.d.ts +1 -1
- package/dist/web/list/presentation.js +1 -0
- package/dist/web/list/rowCommands.d.ts +1 -1
- package/dist/web/list/rowCommands.js +1 -2
- package/dist/web/record/CommandButton.d.ts +1 -1
- package/dist/web/record/CommandButton.js +1 -0
- package/dist/web/record/EntityPicker.d.ts +8 -5
- package/dist/web/record/EntityPicker.js +6 -5
- package/dist/web/record/EntityTags.d.ts +1 -1
- package/dist/web/record/EntityTags.js +1 -0
- package/dist/web/record/RelatedList.d.ts +1 -1
- package/dist/web/record/RelatedList.js +1 -0
- package/dist/web/record/control.d.ts +4 -2
- package/dist/web/record/control.js +8 -5
- package/dist/web/record/edit.js +21 -1
- package/dist/web/record/lines.js +13 -2
- package/dist/web/record/opening.d.ts +1 -1
- package/dist/web/record/opening.js +1 -0
- package/dist/web/record/record.d.ts +12 -2
- package/dist/web/record/record.js +10 -2
- package/dist/web/workspace/SincproApp.d.ts +9 -20
- package/dist/web/workspace/SincproApp.js +17 -4
- package/dist/web/workspace/composer.d.ts +1 -2
- package/dist/web/workspace/composer.js +2 -2
- package/dist/web/workspace/open.d.ts +1 -1
- package/dist/web/workspace/open.js +1 -0
- package/dist/web/workspace/record.d.ts +60 -13
- package/dist/web/workspace/record.js +89 -28
- package/dist/web/workspace/workspace.d.ts +1 -1
- package/dist/web/workspace/workspace.js +1 -0
- package/package.json +45 -15
- package/dist/codegen/cli.d.ts +0 -2
- package/dist/codegen/cli.js +0 -82
- package/dist/codegen/client.d.ts +0 -14
- package/dist/codegen/client.js +0 -178
- package/dist/codegen/domain.d.ts +0 -14
- package/dist/codegen/domain.js +0 -195
- package/dist/codegen/entities.d.ts +0 -14
- package/dist/codegen/entities.js +0 -158
- package/dist/codegen/index.d.ts +0 -11
- package/dist/codegen/index.js +0 -9
- package/dist/codegen/metamodel.d.ts +0 -89
- package/dist/codegen/metamodel.js +0 -1
- package/dist/codegen/names.d.ts +0 -40
- package/dist/codegen/names.js +0 -114
- package/dist/codegen/rest.d.ts +0 -31
- package/dist/codegen/rest.js +0 -27
- package/dist/core/contract.d.ts +0 -34
- package/dist/core/contract.js +0 -41
- package/dist/core/discovery.d.ts +0 -47
- package/dist/core/discovery.js +0 -68
- package/dist/core/entity.d.ts +0 -227
- package/dist/core/entity.js +0 -187
- package/dist/core/ids.d.ts +0 -17
- package/dist/core/ids.js +0 -52
- package/dist/core/memory.d.ts +0 -46
- package/dist/core/memory.js +0 -108
- package/dist/core/meta.d.ts +0 -121
- package/dist/core/meta.js +0 -130
- package/dist/core/metas.d.ts +0 -17
- package/dist/core/metas.js +0 -7
- package/dist/core/rest.d.ts +0 -49
- package/dist/core/rest.js +0 -143
- package/dist/core/retry.d.ts +0 -24
- package/dist/core/retry.js +0 -25
- package/dist/core/schema.d.ts +0 -39
- package/dist/core/schema.js +0 -128
- package/dist/core/transport.d.ts +0 -90
- package/dist/core/zone.d.ts +0 -27
- package/dist/core/zone.js +0 -75
package/dist/core/schema.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { field } from "./meta.js";
|
|
2
|
-
const UNKNOWN = {
|
|
3
|
-
type: "unknown",
|
|
4
|
-
kind: "scalar",
|
|
5
|
-
nullable: true,
|
|
6
|
-
sortable: false,
|
|
7
|
-
ops: [],
|
|
8
|
-
};
|
|
9
|
-
function nameOfRef(ref) {
|
|
10
|
-
return ref.slice(ref.lastIndexOf("/") + 1);
|
|
11
|
-
}
|
|
12
|
-
/** `anyOf: [X, {type: "null"}]` is `X`, nullable — how pydantic writes `X | None`. */
|
|
13
|
-
function unwrap(schema) {
|
|
14
|
-
const options = schema.anyOf ?? schema.oneOf;
|
|
15
|
-
if (options === undefined)
|
|
16
|
-
return { schema, nullable: false };
|
|
17
|
-
const real = options.filter((one) => one.type !== "null");
|
|
18
|
-
const [only] = real;
|
|
19
|
-
return real.length === 1 && only !== undefined
|
|
20
|
-
? { schema: { ...only, ...pick(schema) }, nullable: real.length < options.length }
|
|
21
|
-
: { schema, nullable: real.length < options.length };
|
|
22
|
-
}
|
|
23
|
-
/** The words of the outer schema win over the ones of what it wraps. */
|
|
24
|
-
function pick(schema) {
|
|
25
|
-
return {
|
|
26
|
-
...(schema.title === undefined ? {} : { title: schema.title }),
|
|
27
|
-
...(schema.description === undefined ? {} : { description: schema.description }),
|
|
28
|
-
...(schema.default === undefined ? {} : { default: schema.default }),
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
function fieldFrom(raw, required, components, seen) {
|
|
32
|
-
const { schema: unwrapped, nullable } = unwrap(raw);
|
|
33
|
-
const target = unwrapped.$ref !== undefined
|
|
34
|
-
? { ...components[nameOfRef(unwrapped.$ref)], ...pick(unwrapped) }
|
|
35
|
-
: unwrapped;
|
|
36
|
-
const options = {
|
|
37
|
-
...(target.title === undefined ? {} : { label: target.title }),
|
|
38
|
-
...(target.description === undefined ? {} : { help: target.description }),
|
|
39
|
-
...(target.default === undefined ? {} : { default: target.default }),
|
|
40
|
-
...(nullable ? { nullable } : {}),
|
|
41
|
-
required: required && target.default === undefined && !nullable,
|
|
42
|
-
};
|
|
43
|
-
if (unwrapped.$ref !== undefined) {
|
|
44
|
-
const name = nameOfRef(unwrapped.$ref);
|
|
45
|
-
if (components[name] === undefined || seen.has(name))
|
|
46
|
-
return UNKNOWN;
|
|
47
|
-
if (target.properties === undefined)
|
|
48
|
-
return target.enum ? field.choice(target.enum.map(String), options) : UNKNOWN;
|
|
49
|
-
return field.embedded(metaOf(name, target, components, new Set([...seen, name])), options);
|
|
50
|
-
}
|
|
51
|
-
if (target.enum)
|
|
52
|
-
return field.choice(target.enum.map(String), options);
|
|
53
|
-
const type = Array.isArray(target.type)
|
|
54
|
-
? target.type.find((t) => t !== "null")
|
|
55
|
-
: target.type;
|
|
56
|
-
switch (type) {
|
|
57
|
-
case "string":
|
|
58
|
-
if (target.format === "date")
|
|
59
|
-
return field.date(options);
|
|
60
|
-
if (target.format === "date-time")
|
|
61
|
-
return field.datetime(options);
|
|
62
|
-
return field.text(options);
|
|
63
|
-
case "integer":
|
|
64
|
-
return field.integer(options);
|
|
65
|
-
case "number":
|
|
66
|
-
return field.number(options);
|
|
67
|
-
case "boolean":
|
|
68
|
-
return field.boolean(options);
|
|
69
|
-
case "array": {
|
|
70
|
-
const item = target.items === undefined ? undefined : unwrap(target.items).schema;
|
|
71
|
-
if (item?.$ref !== undefined) {
|
|
72
|
-
const inner = fieldFrom(item, false, components, seen);
|
|
73
|
-
return inner.kind === "embedded" && inner.definition
|
|
74
|
-
? field.embedded(inner.definition, { ...options, many: true })
|
|
75
|
-
: field.tags(options);
|
|
76
|
-
}
|
|
77
|
-
return item?.type === "string" || item?.enum ? field.tags(options) : UNKNOWN;
|
|
78
|
-
}
|
|
79
|
-
default:
|
|
80
|
-
return UNKNOWN;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
function metaOf(name, schema, components, seen) {
|
|
84
|
-
const required = new Set(schema.required ?? []);
|
|
85
|
-
const fields = Object.fromEntries(Object.entries(schema.properties ?? {}).map(([key, property]) => [
|
|
86
|
-
key,
|
|
87
|
-
fieldFrom(property, required.has(key), components, seen),
|
|
88
|
-
]));
|
|
89
|
-
return {
|
|
90
|
-
aggregate: name,
|
|
91
|
-
identity: "",
|
|
92
|
-
default_order: "",
|
|
93
|
-
fields,
|
|
94
|
-
...(schema.title === undefined ? {} : { name: { default: schema.title } }),
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
/** The definition a component schema describes. `identity` empty: a value object has none. */
|
|
98
|
-
export function metaFromSchema(name, components, options = {}) {
|
|
99
|
-
const schema = components[name];
|
|
100
|
-
if (schema === undefined)
|
|
101
|
-
throw new Error(`no schema named ${name}`);
|
|
102
|
-
const meta = metaOf(name, schema, components, new Set([name]));
|
|
103
|
-
return options.identity
|
|
104
|
-
? { ...meta, identity: options.identity, get_id: options.identity }
|
|
105
|
-
: meta;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* The server's definition, with what it left `unknown` taken from the schema — recursively
|
|
109
|
-
* into value objects. What the server DID say always wins: its labels, its operators, its
|
|
110
|
-
* hints. A field the schema does not know is left as the server sent it.
|
|
111
|
-
*/
|
|
112
|
-
export function completeMeta(meta, schema) {
|
|
113
|
-
const fields = Object.fromEntries(Object.entries(meta.fields).map(([name, own]) => {
|
|
114
|
-
const known = schema.fields[name];
|
|
115
|
-
if (known === undefined)
|
|
116
|
-
return [name, own];
|
|
117
|
-
if (own.type === "unknown")
|
|
118
|
-
return [name, { ...known, ...labelsOf(own) }];
|
|
119
|
-
if (own.kind === "embedded" && own.definition && known.definition)
|
|
120
|
-
return [name, { ...own, definition: completeMeta(own.definition, known.definition) }];
|
|
121
|
-
return [name, own];
|
|
122
|
-
}));
|
|
123
|
-
return { ...meta, fields };
|
|
124
|
-
}
|
|
125
|
-
/** A label the server declared is kept over the schema's generated title. */
|
|
126
|
-
function labelsOf(own) {
|
|
127
|
-
return own.label && Object.keys(own.label).length > 0 ? { label: own.label } : {};
|
|
128
|
-
}
|
package/dist/core/transport.d.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import type { Criteria, Dropped, Meta, Page, Problem } from "@sincpro/criteria";
|
|
2
|
-
/**
|
|
3
|
-
* The one seam between this package and a server.
|
|
4
|
-
*
|
|
5
|
-
* Context: a list and a record are the same two questions whatever the wire is — «a page of
|
|
6
|
-
* these, under this criteria» and «this one». REST today; JSON-RPC and gRPC are the other
|
|
7
|
-
* wires sincpro_framework serves. Each of those is
|
|
8
|
-
* one adapter implementing this, and nothing above it changes. A route is a string the adapter
|
|
9
|
-
* understands: a path for REST, a method for RPC.
|
|
10
|
-
*
|
|
11
|
-
* Every adapter throws `TransportError` for a refusal, so a screen explains a failure once
|
|
12
|
-
* whichever wire it came over.
|
|
13
|
-
*/
|
|
14
|
-
export interface Transport {
|
|
15
|
-
/** A page of records under the criteria. */
|
|
16
|
-
list<T>(route: string, criteria: Criteria<T>, signal?: AbortSignal): Promise<Page<T>>;
|
|
17
|
-
/** One record, with the definition of its model. */
|
|
18
|
-
one<T>(route: string, signal?: AbortSignal): Promise<One<T>>;
|
|
19
|
-
/** A Command — a write, an action, a preview: `method` and a JSON body, its answer back. */
|
|
20
|
-
send<T>(method: Method, route: string, body: unknown, signal?: AbortSignal, options?: SendOptions): Promise<T>;
|
|
21
|
-
/**
|
|
22
|
-
* A read that is neither a page nor one record — a Query of the context's own, `GET /me` —:
|
|
23
|
-
* the route with its parameters already in it (`listRoute`), its answer as it came. Optional,
|
|
24
|
-
* so a transport written before it keeps compiling; call it through `readRoute`.
|
|
25
|
-
*/
|
|
26
|
-
read?<T>(route: string, signal?: AbortSignal): Promise<T>;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* A read through `transport.read`, or the reason it cannot be: a transport that has no `read`
|
|
30
|
-
* is said so by name, never a `TypeError` from somewhere inside a screen.
|
|
31
|
-
*/
|
|
32
|
-
export declare function readRoute<T>(transport: Transport, route: string, signal?: AbortSignal): Promise<T>;
|
|
33
|
-
export type Method = "POST" | "PUT" | "PATCH" | "DELETE";
|
|
34
|
-
/**
|
|
35
|
-
* How a Command travels besides its body.
|
|
36
|
-
*
|
|
37
|
-
* Context: sincpro_framework runs a Command declared `@idempotency` once per key (REST: the
|
|
38
|
-
* `Idempotency-Key` header; 400 `IDEMPOTENCY_KEY_MISSING` without it) and answers a repeat with
|
|
39
|
-
* the first answer. That is what makes «retry» safe after a request that got no answer: the
|
|
40
|
-
* server may have created the invoice, and the same key cannot create it twice.
|
|
41
|
-
*/
|
|
42
|
-
export type SendOptions = {
|
|
43
|
-
/** One per gesture, kept while it is retried — `retryKeys()`. */
|
|
44
|
-
idempotencyKey?: string;
|
|
45
|
-
};
|
|
46
|
-
/** One record as sincpro_framework answers it (`ResponseRecord`): the record, its model. */
|
|
47
|
-
export type One<T> = {
|
|
48
|
-
record: T;
|
|
49
|
-
meta: Meta | null;
|
|
50
|
-
dropped: Dropped[];
|
|
51
|
-
/** What this caller kept of an edit between requests (sincpro_framework `Drafts`). */
|
|
52
|
-
draft: Draft | null;
|
|
53
|
-
};
|
|
54
|
-
/** A kept edit: the values typed, the version of the draft, the record version it started on. */
|
|
55
|
-
/**
|
|
56
|
-
* The caller's draft of a record (sincpro_framework's `DraftState`): the version the next edit
|
|
57
|
-
* names, the stored version it started from, and whether the record moved on since (`stale`).
|
|
58
|
-
* `values` only when a server answers them apart from the record — sincpro_framework answers the
|
|
59
|
-
* record FROM the draft instead.
|
|
60
|
-
*/
|
|
61
|
-
export type Draft = {
|
|
62
|
-
values?: Record<string, unknown>;
|
|
63
|
-
version: number;
|
|
64
|
-
origin_version?: number;
|
|
65
|
-
kept_at?: string;
|
|
66
|
-
stale?: boolean;
|
|
67
|
-
};
|
|
68
|
-
/**
|
|
69
|
-
* A refusal, from any wire. `problem` is what the server said about it (RFC 9457 plus the
|
|
70
|
-
* engine's `kind`, `next_step`, `guidance`) or `null` when it said nothing readable — a proxy's
|
|
71
|
-
* HTML page. `status` is `null` when the request never got an answer.
|
|
72
|
-
*/
|
|
73
|
-
export declare class TransportError extends Error {
|
|
74
|
-
readonly route: string;
|
|
75
|
-
readonly status: number | null;
|
|
76
|
-
readonly problem: Problem | null;
|
|
77
|
-
constructor(route: string, status: number | null, problem: Problem | null, message?: string);
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* What a person reads about a failure: a heading, and a sentence with what happened and what
|
|
81
|
-
* to do. The server's own words first; the copy table's when it sent none.
|
|
82
|
-
*
|
|
83
|
-
* explain(new TransportError("/x", 403, { kind: "permission_denied", guidance: "Pedí acceso.", … }))
|
|
84
|
-
* -> { heading: "No tenés acceso", sentence: "Pedí acceso.", retryable: false }
|
|
85
|
-
*/
|
|
86
|
-
export declare function explain(error: unknown): {
|
|
87
|
-
heading: string;
|
|
88
|
-
sentence: string;
|
|
89
|
-
retryable: boolean;
|
|
90
|
-
};
|
package/dist/core/zone.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The time zone a date and time is read and written in — and the two conversions a form needs:
|
|
3
|
-
* the server's instant to the wall clock of that zone, and back.
|
|
4
|
-
*
|
|
5
|
-
* Context: sincpro_framework sends a `datetime` as an instant (`2026-03-12T14:30:00Z`); a person
|
|
6
|
-
* reads and types the wall clock of where the business is (10:30 in La Paz). Which zone that is
|
|
7
|
-
* belongs to whoever is above the screens — the application, the company the user works in —
|
|
8
|
-
* so it is installed once, like the framework's words (`FrameworkProvider timeZone`), and every
|
|
9
|
-
* date and time follows it. Without it, the runtime's. A date with no time (`date`) is a day of
|
|
10
|
-
* the calendar, not an instant: it is never converted.
|
|
11
|
-
*/
|
|
12
|
-
/** Install the time zone dates and times are read and written in (IANA: `"America/La_Paz"`);
|
|
13
|
-
* `undefined` goes back to the runtime's. */
|
|
14
|
-
export declare function installTimeZone(timeZone: string | undefined): void;
|
|
15
|
-
/** The time zone in use. */
|
|
16
|
-
export declare function timeZoneNow(): string;
|
|
17
|
-
/** `"2026-03-12T14:30:00Z"` → `"2026-03-12T10:30"` in `America/La_Paz`: the wall clock there,
|
|
18
|
-
* as a date-and-time control reads it. `null` when it is not an instant. */
|
|
19
|
-
export declare function wallTimeOf(instant: string, timeZone: string): string | null;
|
|
20
|
-
/**
|
|
21
|
-
* `"2026-03-12T10:30"` on the wall clock of `timeZone` → the instant, `"2026-03-12T14:30:00.000Z"`.
|
|
22
|
-
* `null` when it is not a wall time.
|
|
23
|
-
*
|
|
24
|
-
* Context: the offset depends on the instant (daylight saving), so it is asked twice: at the
|
|
25
|
-
* first guess, and again at the answer when the guess fell on the other side of a change.
|
|
26
|
-
*/
|
|
27
|
-
export declare function instantOf(wall: string, timeZone: string): string | null;
|
package/dist/core/zone.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The time zone a date and time is read and written in — and the two conversions a form needs:
|
|
3
|
-
* the server's instant to the wall clock of that zone, and back.
|
|
4
|
-
*
|
|
5
|
-
* Context: sincpro_framework sends a `datetime` as an instant (`2026-03-12T14:30:00Z`); a person
|
|
6
|
-
* reads and types the wall clock of where the business is (10:30 in La Paz). Which zone that is
|
|
7
|
-
* belongs to whoever is above the screens — the application, the company the user works in —
|
|
8
|
-
* so it is installed once, like the framework's words (`FrameworkProvider timeZone`), and every
|
|
9
|
-
* date and time follows it. Without it, the runtime's. A date with no time (`date`) is a day of
|
|
10
|
-
* the calendar, not an instant: it is never converted.
|
|
11
|
-
*/
|
|
12
|
-
let installed;
|
|
13
|
-
/** Install the time zone dates and times are read and written in (IANA: `"America/La_Paz"`);
|
|
14
|
-
* `undefined` goes back to the runtime's. */
|
|
15
|
-
export function installTimeZone(timeZone) {
|
|
16
|
-
installed = timeZone;
|
|
17
|
-
}
|
|
18
|
-
/** The time zone in use. */
|
|
19
|
-
export function timeZoneNow() {
|
|
20
|
-
return installed ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
21
|
-
}
|
|
22
|
-
const clocks = new Map();
|
|
23
|
-
/** One formatter per zone, kept: building one costs far more than using it. */
|
|
24
|
-
function clockOf(timeZone) {
|
|
25
|
-
let clock = clocks.get(timeZone);
|
|
26
|
-
if (clock === undefined) {
|
|
27
|
-
clock = new Intl.DateTimeFormat("en-US", {
|
|
28
|
-
timeZone,
|
|
29
|
-
hourCycle: "h23",
|
|
30
|
-
year: "numeric",
|
|
31
|
-
month: "2-digit",
|
|
32
|
-
day: "2-digit",
|
|
33
|
-
hour: "2-digit",
|
|
34
|
-
minute: "2-digit",
|
|
35
|
-
second: "2-digit",
|
|
36
|
-
});
|
|
37
|
-
clocks.set(timeZone, clock);
|
|
38
|
-
}
|
|
39
|
-
return clock;
|
|
40
|
-
}
|
|
41
|
-
/** The wall clock of `timeZone` at `at` (ms), as the fields of a UTC date: what to compare. */
|
|
42
|
-
function wallAt(at, timeZone) {
|
|
43
|
-
const parts = Object.fromEntries(clockOf(timeZone)
|
|
44
|
-
.formatToParts(new Date(at))
|
|
45
|
-
.map((part) => [part.type, part.value]));
|
|
46
|
-
return Date.UTC(Number(parts["year"]), Number(parts["month"]) - 1, Number(parts["day"]), Number(parts["hour"]), Number(parts["minute"]), Number(parts["second"]));
|
|
47
|
-
}
|
|
48
|
-
const pad = (n) => String(n).padStart(2, "0");
|
|
49
|
-
/** `"2026-03-12T14:30:00Z"` → `"2026-03-12T10:30"` in `America/La_Paz`: the wall clock there,
|
|
50
|
-
* as a date-and-time control reads it. `null` when it is not an instant. */
|
|
51
|
-
export function wallTimeOf(instant, timeZone) {
|
|
52
|
-
const at = Date.parse(instant);
|
|
53
|
-
if (Number.isNaN(at))
|
|
54
|
-
return null;
|
|
55
|
-
const wall = new Date(wallAt(at, timeZone));
|
|
56
|
-
return (`${wall.getUTCFullYear()}-${pad(wall.getUTCMonth() + 1)}-${pad(wall.getUTCDate())}` +
|
|
57
|
-
`T${pad(wall.getUTCHours())}:${pad(wall.getUTCMinutes())}`);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* `"2026-03-12T10:30"` on the wall clock of `timeZone` → the instant, `"2026-03-12T14:30:00.000Z"`.
|
|
61
|
-
* `null` when it is not a wall time.
|
|
62
|
-
*
|
|
63
|
-
* Context: the offset depends on the instant (daylight saving), so it is asked twice: at the
|
|
64
|
-
* first guess, and again at the answer when the guess fell on the other side of a change.
|
|
65
|
-
*/
|
|
66
|
-
export function instantOf(wall, timeZone) {
|
|
67
|
-
const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/.exec(wall);
|
|
68
|
-
if (match === null)
|
|
69
|
-
return null;
|
|
70
|
-
const [, year, month, day, hour, minute] = match.map(Number);
|
|
71
|
-
const guess = Date.UTC(year, month - 1, day, hour, minute);
|
|
72
|
-
const first = guess - (wallAt(guess, timeZone) - guess);
|
|
73
|
-
const second = guess - (wallAt(first, timeZone) - first);
|
|
74
|
-
return new Date(second).toISOString();
|
|
75
|
-
}
|