@crombolo/core 0.51.0 → 0.52.2
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 +2 -2
- package/src/forms.ts +62 -0
- package/src/property-values.ts +21 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.2",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Domain model, entity keys and shared contracts for crombolo. No I/O.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@crombolo/common": "0.
|
|
31
|
+
"@crombolo/common": "0.52.2"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/forms.ts
CHANGED
|
@@ -358,6 +358,68 @@ export function withResolvedChoices(
|
|
|
358
358
|
}
|
|
359
359
|
}
|
|
360
360
|
|
|
361
|
+
/** Each word a form shows a visitor, passed through `word`. */
|
|
362
|
+
function mapFormWords(form: SchemaForm, word: (text: string) => string): SchemaForm {
|
|
363
|
+
const text = (value: string | undefined) => (value === undefined ? undefined : word(value))
|
|
364
|
+
const given = <T extends object>(record: T) =>
|
|
365
|
+
Object.fromEntries(Object.entries(record).filter(([, value]) => value !== undefined)) as T
|
|
366
|
+
return {
|
|
367
|
+
...form,
|
|
368
|
+
...given({
|
|
369
|
+
submitLabel: text(form.submitLabel),
|
|
370
|
+
nextLabel: text(form.nextLabel),
|
|
371
|
+
previousLabel: text(form.previousLabel),
|
|
372
|
+
messageOnSubmit: text(form.messageOnSubmit),
|
|
373
|
+
}),
|
|
374
|
+
pages: form.pages.map((page) => ({
|
|
375
|
+
...page,
|
|
376
|
+
...given({ caption: text(page.caption) }),
|
|
377
|
+
groups: page.groups.map((group) => ({
|
|
378
|
+
...group,
|
|
379
|
+
...given({ caption: text(group.caption) }),
|
|
380
|
+
fields: group.fields.map((field) => ({
|
|
381
|
+
...field,
|
|
382
|
+
caption: word(field.caption),
|
|
383
|
+
...given({
|
|
384
|
+
helpText: text(field.helpText),
|
|
385
|
+
placeholder: text(field.placeholder),
|
|
386
|
+
content: text(field.content),
|
|
387
|
+
mandatoryMessage: text(field.mandatoryMessage),
|
|
388
|
+
patternMessage: text(field.patternMessage),
|
|
389
|
+
}),
|
|
390
|
+
})),
|
|
391
|
+
})),
|
|
392
|
+
})),
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* The form with its words as `word` gives them, from the dictionary (story 293).
|
|
398
|
+
* The same form when none changes.
|
|
399
|
+
*/
|
|
400
|
+
export function withFormWords(form: SchemaForm, word: (text: string) => string): SchemaForm {
|
|
401
|
+
let changed = false
|
|
402
|
+
const next = mapFormWords(form, (text) => {
|
|
403
|
+
const out = word(text)
|
|
404
|
+
if (out !== text) changed = true
|
|
405
|
+
return out
|
|
406
|
+
})
|
|
407
|
+
return changed ? next : form
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* The dictionary items a form names: `#contact.name` names `contact.name`. A `#`
|
|
412
|
+
* followed by words, as in "#1 choice", is only text unless an item has that name.
|
|
413
|
+
*/
|
|
414
|
+
export function formWordNames(form: SchemaForm): string[] {
|
|
415
|
+
const names = new Set<string>()
|
|
416
|
+
mapFormWords(form, (text) => {
|
|
417
|
+
if (/^#\S+$/.test(text)) names.add(text.slice(1))
|
|
418
|
+
return text
|
|
419
|
+
})
|
|
420
|
+
return [...names]
|
|
421
|
+
}
|
|
422
|
+
|
|
361
423
|
export interface SchemaFormGroup {
|
|
362
424
|
caption?: string
|
|
363
425
|
/** A simple column count; the renderer lays the fields out across it. */
|
package/src/property-values.ts
CHANGED
|
@@ -94,11 +94,28 @@ function toUpload(stored: unknown): { src: string } | null {
|
|
|
94
94
|
return typeof src === 'string' && src ? { src } : null
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
const NO_ZONE = /^\d{4}-\d{2}-\d{2}([ T]\d{2}:\d{2}(:\d{2}(\.\d+)?)?)?$/
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A date's text as an instant, with no zone meaning UTC: a date picker's wall
|
|
101
|
+
* clock is kept that way, and `new Date` would read it in the server's zone, so
|
|
102
|
+
* the same text would mean a different moment on another machine. Undefined
|
|
103
|
+
* when it is not a date.
|
|
104
|
+
*/
|
|
105
|
+
export function dateFromText(text: string): Date | undefined {
|
|
106
|
+
const trimmed = text.trim()
|
|
107
|
+
const date = new Date(
|
|
108
|
+
NO_ZONE.test(trimmed)
|
|
109
|
+
? `${trimmed.replace(' ', 'T')}${trimmed.length === 10 ? 'T00:00:00' : ''}Z`
|
|
110
|
+
: trimmed,
|
|
111
|
+
)
|
|
112
|
+
return Number.isNaN(date.getTime()) ? undefined : date
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** A date as the date pickers show it: `2026-01-02 03:04:05`. Safe to apply twice. */
|
|
98
116
|
function toWallClock(stored: unknown): unknown {
|
|
99
|
-
const date =
|
|
100
|
-
|
|
101
|
-
return date.toISOString().slice(0, 19).replace('T', ' ')
|
|
117
|
+
const date = dateFromText(String(stored))
|
|
118
|
+
return date ? date.toISOString().slice(0, 19).replace('T', ' ') : stored
|
|
102
119
|
}
|
|
103
120
|
|
|
104
121
|
/** The value an editor receives for what is stored. */
|