« get me outta code hell

withStrings.js « language « things « composite « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/things/language/withStrings.js
blob: 3b8d46b3f285580ffc38c316b5ee6927af6a1294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {logWarn} from '#cli';
import {input, templateCompositeFrom} from '#composite';
import {empty, withEntries} from '#sugar';
import {languageOptionRegex} from '#wiki-data';

import {withResultOfAvailabilityCheck} from '#composite/control-flow';

export default templateCompositeFrom({
  annotation: `withStrings`,

  inputs: {
    from: input({defaultDependency: 'strings'}),
  },

  outputs: ['#strings'],

  steps: () => [
    withResultOfAvailabilityCheck({
      from: input('from'),
    }).outputs({
      '#availability': '#stringsAvailability',
    }),

    withResultOfAvailabilityCheck({
      from: 'inheritedStrings',
    }).outputs({
      '#availability': '#inheritedStringsAvailability',
    }),

    {
      dependencies: [
        '#stringsAvailability',
        '#inheritedStringsAvailability',
      ],

      compute: (continuation, {
        ['#stringsAvailability']: stringsAvailability,
        ['#inheritedStringsAvailability']: inheritedStringsAvailability,
      }) =>
        (stringsAvailability || inheritedStringsAvailability
          ? continuation()
          : continuation.raiseOutput({'#strings': null})),
    },

    {
      dependencies: [input('from'), '#inheritedStringsAvailability'],
      compute: (continuation, {
        [input('from')]: strings,
        ['#inheritedStringsAvailability']: inheritedStringsAvailability,
      }) =>
        (inheritedStringsAvailability
          ? continuation()
          : continuation.raiseOutput({'#strings': strings})),
    },

    {
      dependencies: ['inheritedStrings', '#stringsAvailability'],
      compute: (continuation, {
        ['inheritedStrings']: inheritedStrings,
        ['#stringsAvailability']: stringsAvailability,
      }) =>
        (stringsAvailability
          ? continuation()
          : continuation.raiseOutput({'#strings': inheritedStrings})),
    },

    {
      dependencies: [input('from'), 'inheritedStrings', 'code'],
      compute(continuation, {
        [input('from')]: strings,
        ['inheritedStrings']: inheritedStrings,
        ['code']: code,
      }) {
        const validStrings = {
          ...inheritedStrings,
          ...strings,
        };

        const optionsFromTemplate = template =>
          Array.from(template.matchAll(languageOptionRegex))
            .map(({groups}) => groups.name);

        for (const [key, providedTemplate] of Object.entries(strings)) {
          const inheritedTemplate = inheritedStrings[key];
          if (!inheritedTemplate) continue;

          const providedOptions = optionsFromTemplate(providedTemplate);
          const inheritedOptions = optionsFromTemplate(inheritedTemplate);

          const missingOptionNames =
            inheritedOptions.filter(name => !providedOptions.includes(name));

          const misplacedOptionNames =
            providedOptions.filter(name => !inheritedOptions.includes(name));

          if (!empty(missingOptionNames) || !empty(misplacedOptionNames)) {
            logWarn`Not using ${code ?? '(no code)'} string ${key}:`;
            if (!empty(missingOptionNames))
              logWarn`- Missing options: ${missingOptionNames.join(', ')}`;
            if (!empty(misplacedOptionNames))
              logWarn`- Unexpected options: ${misplacedOptionNames.join(', ')}`;

            validStrings[key] = inheritedStrings[key];
          }
        }

        return continuation({'#strings': validStrings});
      },
    },
  ],
});