« get me outta code hell

flash.js « files « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/files/flash.js
blob: 3e4f750f54d195c45ceb8072060be54927b96a9b (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
import {sortFlashesChronologically} from '#sort';

export default ({
  documentModes: {allInOne},
  thingConstructors: {Flash, FlashAct, FlashSide},
}) => ({
  title: `Process flashes file`,
  file: 'flashes.yaml',

  documentMode: allInOne,
  documentThing: document =>
    ('Side' in document
      ? FlashSide
   : 'Act' in document
      ? FlashAct
      : Flash),

  connect(results) {
    let thing, i;

    for (i = 0; thing = results[i]; i++) {
      if (thing.isFlashSide) {
        const side = thing;
        const acts = [];

        for (i++; thing = results[i]; i++) {
          if (thing.isFlashAct) {
            const act = thing;
            const flashes = [];

            for (i++; thing = results[i]; i++) {
              if (thing.isFlash) {
                const flash = thing;

                flash.act = act;
                flashes.push(flash);

                continue;
              }

              i--;
              break;
            }

            act.side = side;
            act.flashes = flashes;
            acts.push(act);

            continue;
          }

          if (thing.isFlash) {
            throw new Error(`Flashes must be under an act`);
          }

          i--;
          break;
        }

        side.acts = acts;

        continue;
      }

      if (thing.isFlashAct) {
        throw new Error(`Acts must be under a side`);
      }

      if (thing.isFlash) {
        throw new Error(`Flashes must be under a side and act`);
      }
    }
  },

  sort({flashData}) {
    sortFlashesChronologically(flashData);
  },
});