@zsherman/canvas 0.0.0-stage → 0.1.1

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 (196) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +373 -0
  3. package/README.md +15 -2
  4. package/THIRD_PARTY_NOTICES.md +117 -0
  5. package/dist/dom/canvas/assets.js +89 -0
  6. package/dist/dom/canvas/board-store.js +56 -0
  7. package/dist/dom/canvas/canvas-actions.js +78 -0
  8. package/dist/dom/canvas/canvas-board.js +557 -0
  9. package/dist/dom/canvas/canvas-board.styles.js +110 -0
  10. package/dist/dom/canvas/canvas-chrome.js +1167 -0
  11. package/dist/dom/canvas/canvas-chrome.styles.js +220 -0
  12. package/dist/dom/canvas/canvas-comments.js +950 -0
  13. package/dist/dom/canvas/canvas-comments.styles.js +231 -0
  14. package/dist/dom/canvas/canvas-context.js +196 -0
  15. package/dist/dom/canvas/canvas-controller.js +697 -0
  16. package/dist/dom/canvas/canvas-cursor-chat.js +120 -0
  17. package/dist/dom/canvas/canvas-cursor-chat.styles.js +55 -0
  18. package/dist/dom/canvas/canvas-menus.js +276 -0
  19. package/dist/dom/canvas/canvas-menus.styles.js +7 -0
  20. package/dist/dom/canvas/canvas-style-bar.js +930 -0
  21. package/dist/dom/canvas/canvas-style-bar.styles.js +52 -0
  22. package/dist/dom/canvas/canvas-surface.js +1354 -0
  23. package/dist/dom/canvas/canvas-surface.styles.js +345 -0
  24. package/dist/dom/canvas/canvas-tool-rail.js +406 -0
  25. package/dist/dom/canvas/canvas-tool-rail.styles.js +20 -0
  26. package/dist/dom/canvas/comments.js +58 -0
  27. package/dist/dom/canvas/equality.js +13 -0
  28. package/dist/dom/canvas/export-png.js +496 -0
  29. package/dist/dom/canvas/gesture-machine.js +91 -0
  30. package/dist/dom/canvas/handlers.js +18 -0
  31. package/dist/dom/canvas/index.js +9 -0
  32. package/dist/dom/canvas/input.js +85 -0
  33. package/dist/dom/canvas/instant.js +28 -0
  34. package/dist/dom/canvas/kernel.js +39 -0
  35. package/dist/dom/canvas/motion.js +686 -0
  36. package/dist/dom/canvas/palette.js +165 -0
  37. package/dist/dom/canvas/random-id.js +15 -0
  38. package/dist/dom/canvas/scene-renderer.js +559 -0
  39. package/dist/dom/canvas/scene.styles.js +7 -0
  40. package/dist/dom/canvas/surface-model.js +324 -0
  41. package/dist/dom/canvas/uploads.js +294 -0
  42. package/dist/dom/index.js +9 -0
  43. package/dist/dom/kernel/actions.js +455 -0
  44. package/dist/dom/kernel/board.js +856 -0
  45. package/dist/dom/kernel/claims.js +40 -0
  46. package/dist/dom/kernel/clipboard.js +30 -0
  47. package/dist/dom/kernel/constants.js +210 -0
  48. package/dist/dom/kernel/contract.js +0 -0
  49. package/dist/dom/kernel/decode.js +85 -0
  50. package/dist/dom/kernel/editor.js +2077 -0
  51. package/dist/dom/kernel/errors.js +82 -0
  52. package/dist/dom/kernel/fonts/inter.js +7924 -0
  53. package/dist/dom/kernel/fractional-index.js +54 -0
  54. package/dist/dom/kernel/geometry.js +123 -0
  55. package/dist/dom/kernel/hit-test.js +156 -0
  56. package/dist/dom/kernel/index.js +22 -0
  57. package/dist/dom/kernel/inspect.js +116 -0
  58. package/dist/dom/kernel/kernel.js +251 -0
  59. package/dist/dom/kernel/outline.js +64 -0
  60. package/dist/dom/kernel/plan.js +1370 -0
  61. package/dist/dom/kernel/rev.js +173 -0
  62. package/dist/dom/kernel/scene.js +580 -0
  63. package/dist/dom/kernel/schema.js +1109 -0
  64. package/dist/dom/kernel/snap.js +258 -0
  65. package/dist/dom/kernel/svg.js +148 -0
  66. package/dist/dom/kernel/text.js +142 -0
  67. package/dist/solid/canvas/assets.jsx +89 -0
  68. package/dist/solid/canvas/board-store.jsx +56 -0
  69. package/dist/solid/canvas/canvas-actions.jsx +78 -0
  70. package/dist/solid/canvas/canvas-board.jsx +326 -0
  71. package/dist/solid/canvas/canvas-board.styles.jsx +110 -0
  72. package/dist/solid/canvas/canvas-chrome.jsx +612 -0
  73. package/dist/solid/canvas/canvas-chrome.styles.jsx +220 -0
  74. package/dist/solid/canvas/canvas-comments.jsx +435 -0
  75. package/dist/solid/canvas/canvas-comments.styles.jsx +231 -0
  76. package/dist/solid/canvas/canvas-context.jsx +174 -0
  77. package/dist/solid/canvas/canvas-controller.jsx +697 -0
  78. package/dist/solid/canvas/canvas-cursor-chat.jsx +87 -0
  79. package/dist/solid/canvas/canvas-cursor-chat.styles.jsx +55 -0
  80. package/dist/solid/canvas/canvas-menus.jsx +223 -0
  81. package/dist/solid/canvas/canvas-menus.styles.jsx +7 -0
  82. package/dist/solid/canvas/canvas-style-bar.jsx +486 -0
  83. package/dist/solid/canvas/canvas-style-bar.styles.jsx +52 -0
  84. package/dist/solid/canvas/canvas-surface.jsx +681 -0
  85. package/dist/solid/canvas/canvas-surface.styles.jsx +345 -0
  86. package/dist/solid/canvas/canvas-tool-rail.jsx +195 -0
  87. package/dist/solid/canvas/canvas-tool-rail.styles.jsx +20 -0
  88. package/dist/solid/canvas/comments.jsx +58 -0
  89. package/dist/solid/canvas/equality.jsx +13 -0
  90. package/dist/solid/canvas/export-png.jsx +496 -0
  91. package/dist/solid/canvas/gesture-machine.jsx +91 -0
  92. package/dist/solid/canvas/handlers.jsx +18 -0
  93. package/dist/solid/canvas/index.jsx +9 -0
  94. package/dist/solid/canvas/input.jsx +85 -0
  95. package/dist/solid/canvas/instant.jsx +28 -0
  96. package/dist/solid/canvas/kernel.jsx +39 -0
  97. package/dist/solid/canvas/motion.jsx +686 -0
  98. package/dist/solid/canvas/palette.jsx +165 -0
  99. package/dist/solid/canvas/random-id.jsx +15 -0
  100. package/dist/solid/canvas/scene-renderer.jsx +559 -0
  101. package/dist/solid/canvas/scene.styles.jsx +7 -0
  102. package/dist/solid/canvas/surface-model.jsx +324 -0
  103. package/dist/solid/canvas/uploads.jsx +294 -0
  104. package/dist/solid/index.jsx +9 -0
  105. package/dist/solid/kernel/actions.jsx +455 -0
  106. package/dist/solid/kernel/board.jsx +856 -0
  107. package/dist/solid/kernel/claims.jsx +40 -0
  108. package/dist/solid/kernel/clipboard.jsx +30 -0
  109. package/dist/solid/kernel/constants.jsx +210 -0
  110. package/dist/solid/kernel/contract.jsx +0 -0
  111. package/dist/solid/kernel/decode.jsx +85 -0
  112. package/dist/solid/kernel/editor.jsx +2077 -0
  113. package/dist/solid/kernel/errors.jsx +82 -0
  114. package/dist/solid/kernel/fonts/inter.jsx +7924 -0
  115. package/dist/solid/kernel/fractional-index.jsx +54 -0
  116. package/dist/solid/kernel/geometry.jsx +123 -0
  117. package/dist/solid/kernel/hit-test.jsx +156 -0
  118. package/dist/solid/kernel/index.jsx +22 -0
  119. package/dist/solid/kernel/inspect.jsx +116 -0
  120. package/dist/solid/kernel/kernel.jsx +251 -0
  121. package/dist/solid/kernel/outline.jsx +64 -0
  122. package/dist/solid/kernel/plan.jsx +1370 -0
  123. package/dist/solid/kernel/rev.jsx +173 -0
  124. package/dist/solid/kernel/scene.jsx +580 -0
  125. package/dist/solid/kernel/schema.jsx +1109 -0
  126. package/dist/solid/kernel/snap.jsx +258 -0
  127. package/dist/solid/kernel/svg.jsx +148 -0
  128. package/dist/solid/kernel/text.jsx +142 -0
  129. package/dist/styles.css +1 -0
  130. package/dist/types/canvas/assets.d.ts +111 -0
  131. package/dist/types/canvas/board-store.d.ts +86 -0
  132. package/dist/types/canvas/canvas-actions.d.ts +21 -0
  133. package/dist/types/canvas/canvas-board.d.ts +53 -0
  134. package/dist/types/canvas/canvas-board.styles.d.ts +94 -0
  135. package/dist/types/canvas/canvas-chrome.d.ts +109 -0
  136. package/dist/types/canvas/canvas-chrome.styles.d.ts +193 -0
  137. package/dist/types/canvas/canvas-comments.d.ts +20 -0
  138. package/dist/types/canvas/canvas-comments.styles.d.ts +208 -0
  139. package/dist/types/canvas/canvas-context.d.ts +104 -0
  140. package/dist/types/canvas/canvas-controller.d.ts +159 -0
  141. package/dist/types/canvas/canvas-cursor-chat.d.ts +13 -0
  142. package/dist/types/canvas/canvas-cursor-chat.styles.d.ts +54 -0
  143. package/dist/types/canvas/canvas-menus.d.ts +21 -0
  144. package/dist/types/canvas/canvas-menus.styles.d.ts +6 -0
  145. package/dist/types/canvas/canvas-style-bar.d.ts +19 -0
  146. package/dist/types/canvas/canvas-style-bar.styles.d.ts +43 -0
  147. package/dist/types/canvas/canvas-surface.d.ts +15 -0
  148. package/dist/types/canvas/canvas-surface.styles.d.ts +313 -0
  149. package/dist/types/canvas/canvas-tool-rail.d.ts +41 -0
  150. package/dist/types/canvas/canvas-tool-rail.styles.d.ts +15 -0
  151. package/dist/types/canvas/comments.d.ts +106 -0
  152. package/dist/types/canvas/equality.d.ts +4 -0
  153. package/dist/types/canvas/export-png.d.ts +77 -0
  154. package/dist/types/canvas/gesture-machine.d.ts +46 -0
  155. package/dist/types/canvas/handlers.d.ts +7 -0
  156. package/dist/types/canvas/index.d.ts +18 -0
  157. package/dist/types/canvas/input.d.ts +63 -0
  158. package/dist/types/canvas/instant.d.ts +13 -0
  159. package/dist/types/canvas/kernel.d.ts +27 -0
  160. package/dist/types/canvas/motion.d.ts +72 -0
  161. package/dist/types/canvas/palette.d.ts +46 -0
  162. package/dist/types/canvas/random-id.d.ts +6 -0
  163. package/dist/types/canvas/scene-renderer.d.ts +27 -0
  164. package/dist/types/canvas/scene.styles.d.ts +4 -0
  165. package/dist/types/canvas/surface-model.d.ts +113 -0
  166. package/dist/types/canvas/uploads.d.ts +58 -0
  167. package/dist/types/index.d.ts +1 -0
  168. package/dist/types/kernel/actions.d.ts +19 -0
  169. package/dist/types/kernel/board.d.ts +137 -0
  170. package/dist/types/kernel/claims.d.ts +29 -0
  171. package/dist/types/kernel/clipboard.d.ts +10 -0
  172. package/dist/types/kernel/constants.d.ts +195 -0
  173. package/dist/types/kernel/contract.d.ts +1533 -0
  174. package/dist/types/kernel/decode.d.ts +60 -0
  175. package/dist/types/kernel/editor.d.ts +12 -0
  176. package/dist/types/kernel/errors.d.ts +11 -0
  177. package/dist/types/kernel/fractional-index.d.ts +7 -0
  178. package/dist/types/kernel/geometry.d.ts +42 -0
  179. package/dist/types/kernel/hit-test.d.ts +16 -0
  180. package/dist/types/kernel/index.d.ts +8 -0
  181. package/dist/types/kernel/inspect.d.ts +6 -0
  182. package/dist/types/kernel/kernel.d.ts +6 -0
  183. package/dist/types/kernel/outline.d.ts +4 -0
  184. package/dist/types/kernel/plan.d.ts +20 -0
  185. package/dist/types/kernel/rev.d.ts +21 -0
  186. package/dist/types/kernel/scene.d.ts +16 -0
  187. package/dist/types/kernel/schema.d.ts +3436 -0
  188. package/dist/types/kernel/snap.d.ts +41 -0
  189. package/dist/types/kernel/svg.d.ts +3 -0
  190. package/dist/types/kernel/text.d.ts +21 -0
  191. package/docs/api.json +207 -0
  192. package/docs/canvas.md +444 -0
  193. package/docs/kernel.md +108 -0
  194. package/docs/llms.txt +41 -0
  195. package/docs/search-index.json +52 -0
  196. package/package.json +60 -4
@@ -0,0 +1,40 @@
1
+ // src/kernel/claims.ts
2
+ const sameConnection = (a, b) => b !== null && a.actor === b.actor && a.connection === b.connection;
3
+ const order = (a, b) => a.actor < b.actor ? -1 : a.actor > b.actor ? 1 : a.connection < b.connection ? -1 : a.connection > b.connection ? 1 : 0;
4
+ /** One entry per connection: the last one sent, at its first's position. */
5
+ const connections = (presence) => [...new Map(presence.map((entry) => [`${entry.actor} ${entry.connection}`, entry])).values()];
6
+ /** The shapes an editor mode claims. */
7
+ const claimsOf = (mode) => {
8
+ switch (mode._tag) {
9
+ case "EditingText": return [mode.id];
10
+ case "Dragging": return mode.copy ? [] : mode.ids;
11
+ case "Resizing": return mode.ids;
12
+ case "Connecting": return mode.arrow === null ? [] : [mode.arrow];
13
+ default: return [];
14
+ }
15
+ };
16
+ /**
17
+ * Each claimed shape's claimants, holder first. `mine`, when given, stands
18
+ * for `self`'s claims in place of its presence entry (the editor's mode is
19
+ * newer than what it published).
20
+ */
21
+ const claimants = (presence, self, mine) => {
22
+ const out = /* @__PURE__ */ new Map();
23
+ const add = (id, claimant) => {
24
+ const list = out.get(id);
25
+ if (list === void 0) out.set(id, [claimant]);
26
+ else if (!list.some((c) => sameConnection(c, claimant))) list.push(claimant);
27
+ };
28
+ const entries = connections(presence);
29
+ for (const entry of entries) {
30
+ if (mine !== null && sameConnection(entry, self)) continue;
31
+ for (const id of entry.claims) add(id, entry);
32
+ }
33
+ if (self !== null && mine !== null) for (const id of mine) add(id, {
34
+ ...self,
35
+ name: ""
36
+ });
37
+ for (const list of out.values()) list.sort(order);
38
+ return out;
39
+ };
40
+ export { claimants, claimsOf, connections, sameConnection };
@@ -0,0 +1,30 @@
1
+ import { run } from "./decode.jsx";
2
+ import { invalid } from "./errors.jsx";
3
+ import { clipboard } from "./schema.jsx";
4
+ // src/kernel/clipboard.ts
5
+ const NOT_OURS = Object.freeze({ _tag: "NotOurs" });
6
+ /**
7
+ * The board clipboard in `text`, `NotOurs` for any other text, or `Invalid`
8
+ * for a board clipboard this kernel cannot read. Never throws.
9
+ */
10
+ const decodeClipboard = (text) => {
11
+ if (typeof text !== "string") return NOT_OURS;
12
+ let value;
13
+ try {
14
+ value = JSON.parse(text);
15
+ } catch {
16
+ return NOT_OURS;
17
+ }
18
+ if (typeof value !== "object" || value === null || !("kind" in value)) return NOT_OURS;
19
+ if (value.kind !== "hyper-canvas") return NOT_OURS;
20
+ const result = run(clipboard, value, ["clipboard"]);
21
+ if ("issue" in result) return {
22
+ _tag: "Invalid",
23
+ error: invalid(result.issue.path, result.issue.expected, "Copy the shapes again from a board running this version.")
24
+ };
25
+ return {
26
+ _tag: "Shapes",
27
+ clipboard: result.ok
28
+ };
29
+ };
30
+ export { decodeClipboard };
@@ -0,0 +1,210 @@
1
+ // src/kernel/constants.ts
2
+ const LIMITS = {
3
+ /** Largest absolute coordinate. */
4
+ coordinate: 1e9,
5
+ /** Largest width or height. */
6
+ size: 1e6,
7
+ shapes: 1e4,
8
+ commands: 500,
9
+ ids: 1e4,
10
+ /**
11
+ * Ids, pasted shapes and ink points one transaction's commands name in all:
12
+ * bounds a plan's work (README, "Cost"). Twice `shapes`, so one paste of a
13
+ * full clipboard with fresh ids fits.
14
+ */
15
+ named: 2e4,
16
+ points: 1e4,
17
+ text: 1e4,
18
+ reason: 1e3,
19
+ label: 200,
20
+ name: 100,
21
+ presence: 1e3,
22
+ /** Characters in a fractional index a board may hold. */
23
+ z: 1e3,
24
+ /** Characters of an image's description. */
25
+ alt: 1e3,
26
+ /** Largest natural width or height of an image, in pixels. */
27
+ natural: 1e5,
28
+ /** Uploads one actor shows at once, and images one `InsertImage` inserts. */
29
+ uploads: 50,
30
+ /** UTF-16 code units of a cursor chat message. */
31
+ chat: 200,
32
+ /** Latest instant a timestamp holds, in epoch milliseconds (the ECMAScript date range). */
33
+ instant: 864e13
34
+ };
35
+ /**
36
+ * Longest z a committed plan leaves on the board. A longer key re-keys its
37
+ * neighbourhood to keys at most half as long (see `rekey` in plan.ts).
38
+ */
39
+ const Z_KEY = 32;
40
+ /** Smallest width or height of a shape a person or command can make. */
41
+ const MIN_SIZE = 16;
42
+ const DEFAULTS = {
43
+ gap: 40,
44
+ sticky: {
45
+ w: 200,
46
+ h: 200,
47
+ color: "yellow",
48
+ size: "m",
49
+ align: "start"
50
+ },
51
+ box: {
52
+ w: 160,
53
+ h: 100,
54
+ color: "blue",
55
+ size: "m",
56
+ form: "rectangle",
57
+ align: "center",
58
+ fill: true,
59
+ dash: "solid"
60
+ },
61
+ text: {
62
+ color: "ink",
63
+ size: "m",
64
+ align: "start"
65
+ },
66
+ arrow: {
67
+ color: "gray",
68
+ start: "none",
69
+ end: "arrow",
70
+ route: "straight",
71
+ dash: "solid"
72
+ },
73
+ ink: {
74
+ color: "ink",
75
+ size: "m",
76
+ brush: "pen"
77
+ },
78
+ /** Offset of a duplicate. */
79
+ duplicate: 16,
80
+ /** Longest side of an inserted image, in board units; never above its natural size. */
81
+ image: 480,
82
+ /** A timestamp's size: a small badge that fits a time like "12:04 PM" at 12 board units. */
83
+ timestamp: {
84
+ w: 80,
85
+ h: 22
86
+ }
87
+ };
88
+ /** A timestamp badge's label size in board units. */
89
+ const TIMESTAMP_FONT_SIZE = 12;
90
+ /** Font size in board units for each text size. */
91
+ const FONT_SIZE = {
92
+ s: 16,
93
+ m: 20,
94
+ l: 28,
95
+ xl: 40
96
+ };
97
+ /** Stroke width in board units of ink for each size. */
98
+ const INK_WIDTH = {
99
+ s: 2,
100
+ m: 4,
101
+ l: 8,
102
+ xl: 12
103
+ };
104
+ /** Ink width multiplier and opacity for each brush. */
105
+ const BRUSH = {
106
+ pen: {
107
+ width: 1,
108
+ opacity: 1
109
+ },
110
+ marker: {
111
+ width: 2.5,
112
+ opacity: 1
113
+ },
114
+ highlighter: {
115
+ width: 4,
116
+ opacity: .35
117
+ }
118
+ };
119
+ /** CSS font weight of all board text. */
120
+ const FONT_WEIGHT = 400;
121
+ /** Ink points closer than this to the simplified line are dropped (Ramer–Douglas–Peucker), in board units. */
122
+ const INK_TOLERANCE = .5;
123
+ /** Line height as a multiple of the font size. Engines must use the same (CSS `line-height: 1.3`). */
124
+ const LINE_HEIGHT = 1.3;
125
+ /** Inner padding around text in a sticky and in a box. */
126
+ const PADDING = {
127
+ sticky: 16,
128
+ box: 12
129
+ };
130
+ const STROKE_WIDTH = {
131
+ box: 2,
132
+ arrow: 2
133
+ };
134
+ /** How far an elbow route runs straight out of a shape before it turns. */
135
+ const ELBOW_OFFSET = 24;
136
+ const RADIUS = {
137
+ sticky: 4,
138
+ box: 8
139
+ };
140
+ /** Interaction distances, in screen pixels. */
141
+ const INTERACTION = {
142
+ /** Movement before a press becomes a drag. */
143
+ dragThreshold: 4,
144
+ /** How far outside a shape or line a press still hits it, by pointer. */
145
+ hitTolerance: {
146
+ mouse: 4,
147
+ pen: 6,
148
+ touch: 12
149
+ },
150
+ /** Drawn size of a handle. */
151
+ handleSize: 8,
152
+ /** How far outside a handle a press still hits it, by pointer. */
153
+ handleTolerance: {
154
+ mouse: 4,
155
+ pen: 6,
156
+ touch: 12
157
+ },
158
+ /** Scene items are kept this far outside the viewport. */
159
+ cullMargin: 256,
160
+ /** Points of the eraser trail kept. */
161
+ eraserTrail: 12,
162
+ /** Arrow-key nudge, and with shift, in board units. */
163
+ nudge: 1,
164
+ nudgeLarge: 10,
165
+ /** Snapping distance. */
166
+ snap: 6,
167
+ /** Ink points closer than this to the previous one are not recorded. */
168
+ inkSpacing: 1,
169
+ /** Margin around shapes for zoom to fit. */
170
+ fitMargin: 64
171
+ };
172
+ const ZOOM = {
173
+ min: .1,
174
+ max: 8,
175
+ /** Zoom in and out step through these. */
176
+ steps: [
177
+ .1,
178
+ .25,
179
+ .5,
180
+ .75,
181
+ 1,
182
+ 1.25,
183
+ 1.5,
184
+ 2,
185
+ 3,
186
+ 4,
187
+ 6,
188
+ 8
189
+ ],
190
+ /** Zoom change per wheel pixel: factor = 1 - dy * step, clamped to [0.5, 2] per event. */
191
+ step: .01
192
+ };
193
+ /**
194
+ * The bound on a board's history (see `BoardHistoryJson`). A pending proposal
195
+ * is never dropped, nor is the newest record, so `proposalBytes +
196
+ * recordBytes` must fit `bytes` and `proposals + 1` must fit `records`; then
197
+ * `apply` can always trim a board back within the bound.
198
+ */
199
+ const HISTORY = Object.freeze({
200
+ records: 200,
201
+ bytes: 786432,
202
+ recordBytes: 524288,
203
+ proposals: 50,
204
+ proposalBytes: 262144
205
+ });
206
+ /** Grid spacing when the grid is turned on, in board units. */
207
+ const GRID = 16;
208
+ /** How many transactions an editor keeps for undo. */
209
+ const UNDO_DEPTH = 100;
210
+ export { BRUSH, DEFAULTS, ELBOW_OFFSET, FONT_SIZE, FONT_WEIGHT, GRID, HISTORY, INK_TOLERANCE, INK_WIDTH, INTERACTION, LIMITS, LINE_HEIGHT, MIN_SIZE, PADDING, RADIUS, STROKE_WIDTH, TIMESTAMP_FONT_SIZE, UNDO_DEPTH, ZOOM, Z_KEY };
File without changes
@@ -0,0 +1,85 @@
1
+ // src/kernel/decode.ts
2
+ const fail = (checker, path, expected) => {
3
+ if (checker.issue === null) checker.issue = {
4
+ path,
5
+ expected
6
+ };
7
+ return false;
8
+ };
9
+ const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
10
+ const isPlainObject = (value) => {
11
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
12
+ const proto = Object.getPrototypeOf(value);
13
+ return proto === Object.prototype || proto === null;
14
+ };
15
+ /** A well-formed string of at most `max` UTF-16 code units. */
16
+ const string = (max, expected = `a string of at most ${max} characters`) => (value, checker, path) => typeof value === "string" && value.length <= max && !LONE_SURROGATE.test(value) ? true : fail(checker, path, expected);
17
+ const pattern = (regex, expected) => (value, checker, path) => typeof value === "string" && regex.test(value) ? true : fail(checker, path, expected);
18
+ /** Shape, transaction and actor ids. */
19
+ const id = pattern(/^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/, "an id: 1 to 64 letters, digits, '.', '_', ':' or '-', starting with a letter or digit");
20
+ const boolean = (value, checker, path) => typeof value === "boolean" ? true : fail(checker, path, "true or false");
21
+ /** A finite number within `[min, max]`. */
22
+ const number = (min, max, expected = `a number from ${min} to ${max}`) => (value, checker, path) => typeof value === "number" && Number.isFinite(value) && value >= min && value <= max ? true : fail(checker, path, expected);
23
+ const integer = (min, max) => (value, checker, path) => typeof value === "number" && Number.isInteger(value) && value >= min && value <= max ? true : fail(checker, path, `a whole number from ${min} to ${max}`);
24
+ const literal = (...values) => (value, checker, path) => values.some((candidate) => candidate === value) ? true : fail(checker, path, `one of ${values.map((v) => JSON.stringify(v)).join(", ")}`);
25
+ const nullable = (guard) => (value, checker, path) => {
26
+ if (value === null) return true;
27
+ if (guard(value, checker, path)) return true;
28
+ if (checker.issue !== null && checker.issue.path === path) checker.issue = {
29
+ path,
30
+ expected: `${checker.issue.expected}, or null`
31
+ };
32
+ return false;
33
+ };
34
+ const array = (guard, max) => (value, checker, path) => {
35
+ if (!Array.isArray(value)) return fail(checker, path, "an array");
36
+ if (value.length > max) return fail(checker, path, `an array of at most ${max} items`);
37
+ for (let index = 0; index < value.length; index++) if (!guard(value[index], checker, [...path, index])) return false;
38
+ return true;
39
+ };
40
+ /** A plain object of at most `max` entries whose keys and values pass the guards. */
41
+ const dictionary = (key, guard, max) => (value, checker, path) => {
42
+ if (!isPlainObject(value)) return fail(checker, path, "an object");
43
+ const entries = Object.entries(value);
44
+ if (entries.length > max) return fail(checker, path, `an object of at most ${max} entries`);
45
+ for (const [k, v] of entries) if (!key(k, checker, [...path, k]) || !guard(v, checker, [...path, k])) return false;
46
+ return true;
47
+ };
48
+ const hasOwn = (fields, key) => Object.prototype.hasOwnProperty.call(fields, key);
49
+ /** A plain object with exactly the `required` keys and any of the `optional` keys. */
50
+ const struct = (required, optional) => (value, checker, path) => {
51
+ if (!isPlainObject(value)) return fail(checker, path, "an object");
52
+ const accepted = [...Object.keys(required), ...Object.keys(optional ?? {})];
53
+ for (const key of Object.keys(value)) if (!hasOwn(required, key) && (optional === void 0 || !hasOwn(optional, key))) return fail(checker, [...path, key], `no such field; the fields are ${accepted.join(", ")}`);
54
+ for (const key of Object.keys(required)) {
55
+ const guard = required[key];
56
+ if (guard === void 0) continue;
57
+ if (!Object.prototype.hasOwnProperty.call(value, key)) return fail(checker, [...path, key], "a value; this field is required");
58
+ if (!guard(value[key], checker, [...path, key])) return false;
59
+ }
60
+ if (optional !== void 0) for (const key of Object.keys(optional)) {
61
+ const guard = optional[key];
62
+ if (guard === void 0 || !Object.prototype.hasOwnProperty.call(value, key)) continue;
63
+ if (!guard(value[key], checker, [...path, key])) return false;
64
+ }
65
+ return true;
66
+ };
67
+ /** A union whose members are told apart by the string at `key`. */
68
+ const tagged = (key, members) => (value, checker, path) => {
69
+ if (!isPlainObject(value)) return fail(checker, path, "an object");
70
+ const tag = value[key];
71
+ const tags = Object.keys(members);
72
+ if (typeof tag !== "string" || !Object.prototype.hasOwnProperty.call(members, tag)) return fail(checker, [...path, key], `one of ${tags.map((t) => JSON.stringify(t)).join(", ")}`);
73
+ const guard = members[tag];
74
+ return guard !== void 0 && guard(value, checker, path);
75
+ };
76
+ /** Runs a guard: the value, typed, or the first issue it found. */
77
+ const run = (guard, value, path) => {
78
+ const checker = { issue: null };
79
+ if (guard(value, checker, path)) return { ok: value };
80
+ return { issue: checker.issue ?? {
81
+ path,
82
+ expected: "a valid value"
83
+ } };
84
+ };
85
+ export { LONE_SURROGATE, array, boolean, dictionary, id, integer, literal, nullable, number, pattern, run, string, struct, tagged };