@crombolo/core 0.53.2 → 0.55.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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/core",
3
- "version": "0.53.2",
3
+ "version": "0.55.0",
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.53.2"
31
+ "@crombolo/common": "0.55.0"
32
32
  }
33
33
  }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * `{#key}`: a dictionary item named in a text, in a form or a page's value
3
+ * (story 297). It reads as the item's words in the reader's language, then its
4
+ * fallbacks, then as the item's name; a key that names no item shows as written.
5
+ * `docs/forms.md`, "Words from the dictionary".
6
+ */
7
+
8
+ const WORD = /\{#([^{}\s]+)\}/g
9
+ const ONLY_WORD = /^\{#([^{}\s]+)\}$/
10
+ /** The form texts story 293 wrote: a whole text of `#key`. Read for one release. */
11
+ const BARE_WORD = /^#(\S+)$/
12
+
13
+ /** The text with each `{#key}` as `word` gives it; a key it has nothing for stays as written. */
14
+ export function resolveDictionaryWords(
15
+ text: string,
16
+ word: (name: string) => string | undefined,
17
+ ): string {
18
+ if (!text.includes('{#')) return text
19
+ return text.replace(WORD, (whole, name: string) => word(name) ?? whole)
20
+ }
21
+
22
+ /** The item a text names when the whole of it is one `{#key}`, else undefined. */
23
+ export function onlyDictionaryWord(text: string): string | undefined {
24
+ return ONLY_WORD.exec(text.trim())?.[1]
25
+ }
26
+
27
+ /** Each item a text names with `{#key}`, in order, once each. */
28
+ export function dictionaryWordNames(text: string): string[] {
29
+ if (!text.includes('{#')) return []
30
+ return [...new Set([...text.matchAll(WORD)].map((match) => match[1] as string))]
31
+ }
32
+
33
+ /** The item a whole text of `#key` names, the way forms wrote it before `{#key}`. */
34
+ export function bareDictionaryWord(text: string): string | undefined {
35
+ return BARE_WORD.exec(text)?.[1]
36
+ }
37
+
38
+ /** `{#key}` for an item's name. */
39
+ export const dictionaryWord = (name: string): string => `{#${name}}`
40
+
41
+ /** As `resolveDictionaryWords`, for markup: rich text holds `{#key}` among its tags, so the words are escaped. */
42
+ export function resolveDictionaryWordsInMarkup(
43
+ markup: string,
44
+ word: (name: string) => string | undefined,
45
+ ): string {
46
+ return resolveDictionaryWords(markup, (name) => {
47
+ const found = word(name)
48
+ return found === undefined ? undefined : escapeMarkup(found)
49
+ })
50
+ }
51
+
52
+ const escapeMarkup = (text: string): string =>
53
+ text.replace(
54
+ /[&<>"']/g,
55
+ (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c] as string,
56
+ )
package/src/forms.ts CHANGED
@@ -11,6 +11,8 @@
11
11
  * bundle").
12
12
  */
13
13
 
14
+ import { bareDictionaryWord, dictionaryWord, dictionaryWordNames } from './dictionary-words.ts'
15
+
14
16
  /** A field type's name: a built-in one, or one a wired bundle provides. */
15
17
  export type FormFieldType = string
16
18
 
@@ -358,6 +360,12 @@ export function withResolvedChoices(
358
360
  }
359
361
  }
360
362
 
363
+ /**
364
+ * The field types whose default value is words a visitor reads in the box, and so
365
+ * may name a dictionary item. Any other type's default is a value it compares.
366
+ */
367
+ export const WORDY_DEFAULT_TYPES: readonly string[] = ['shortAnswer', 'longAnswer']
368
+
361
369
  /** Each word a form shows a visitor, passed through `word`. */
362
370
  function mapFormWords(form: SchemaForm, word: (text: string) => string): SchemaForm {
363
371
  const text = (value: string | undefined) => (value === undefined ? undefined : word(value))
@@ -386,6 +394,9 @@ function mapFormWords(form: SchemaForm, word: (text: string) => string): SchemaF
386
394
  content: text(field.content),
387
395
  mandatoryMessage: text(field.mandatoryMessage),
388
396
  patternMessage: text(field.patternMessage),
397
+ defaultValue: WORDY_DEFAULT_TYPES.includes(field.type)
398
+ ? text(field.defaultValue)
399
+ : field.defaultValue,
389
400
  }),
390
401
  })),
391
402
  })),
@@ -408,18 +419,52 @@ export function withFormWords(form: SchemaForm, word: (text: string) => string):
408
419
  }
409
420
 
410
421
  /**
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.
422
+ * The dictionary items a form names with `{#key}` (story 297), and with a whole
423
+ * text of `#key`, as forms wrote it before (293), which is read for one release.
413
424
  */
414
425
  export function formWordNames(form: SchemaForm): string[] {
415
426
  const names = new Set<string>()
416
427
  mapFormWords(form, (text) => {
417
- if (/^#\S+$/.test(text)) names.add(text.slice(1))
428
+ for (const name of dictionaryWordNames(text)) names.add(name)
429
+ const bare = bareDictionaryWord(text)
430
+ if (bare) names.add(bare)
431
+ return text
432
+ })
433
+ return [...names]
434
+ }
435
+
436
+ /** The dictionary items a form names with `{#key}`. */
437
+ export function bracedFormWordNames(form: SchemaForm): string[] {
438
+ const names = new Set<string>()
439
+ mapFormWords(form, (text) => {
440
+ for (const name of dictionaryWordNames(text)) names.add(name)
418
441
  return text
419
442
  })
420
443
  return [...names]
421
444
  }
422
445
 
446
+ /** The items a form names with a whole text of `#key`, where `{#key}` is the way now. */
447
+ export function bareFormWords(form: SchemaForm): string[] {
448
+ const names = new Set<string>()
449
+ mapFormWords(form, (text) => {
450
+ const bare = bareDictionaryWord(text)
451
+ if (bare) names.add(bare)
452
+ return text
453
+ })
454
+ return [...names]
455
+ }
456
+
457
+ /** The form with each `#key` that names an item written `{#key}`; the same form when none does. */
458
+ export function withBracedFormWords(
459
+ form: SchemaForm,
460
+ isItem: (name: string) => boolean,
461
+ ): SchemaForm {
462
+ return withFormWords(form, (text) => {
463
+ const bare = bareDictionaryWord(text)
464
+ return bare && isItem(bare) ? dictionaryWord(bare) : text
465
+ })
466
+ }
467
+
423
468
  export interface SchemaFormGroup {
424
469
  caption?: string
425
470
  /** A simple column count; the renderer lays the fields out across it. */
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ export {
15
15
  } from '@crombolo/common'
16
16
  export * from './api-paths.ts'
17
17
  export * from './content-types.ts'
18
+ export * from './dictionary-words.ts'
18
19
  export * from './documents.ts'
19
20
  export * from './form-library.ts'
20
21
  export * from './form-submissions.ts'