« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/things/content
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/composite/things/content')
-rw-r--r--src/data/composite/things/content/withAnnotationParts.js149
1 files changed, 11 insertions, 138 deletions
diff --git a/src/data/composite/things/content/withAnnotationParts.js b/src/data/composite/things/content/withAnnotationParts.js
index a7b2d1f0..290a7292 100644
--- a/src/data/composite/things/content/withAnnotationParts.js
+++ b/src/data/composite/things/content/withAnnotationParts.js
@@ -4,68 +4,8 @@ import {transposeArrays} from '#sugar';
 import {is} from '#validators';
 
 import {raiseOutputWithoutDependency} from '#composite/control-flow';
-
-import {
-  withFilteredList,
-  withMappedList,
-  withPropertyFromList,
-  withUnflattenedList,
-} from '#composite/data';
-
-function* splitTextNodeAroundCommas(node) {
-  let textNode = {
-    i: node.i,
-    iEnd: null,
-    type: 'text',
-    data: '',
-  };
-
-  let parseFrom = 0;
-  for (const match of node.data.matchAll(/, */g)) {
-    const {index} = match, [{length}] = match;
-
-    textNode.data += node.data.slice(parseFrom, index);
-
-    if (textNode.data) {
-      textNode.iEnd = textNode.i + textNode.data.length;
-      yield textNode;
-    }
-
-    yield {
-      i: node.i + index,
-      iEnd: node.i + index + length,
-      type: 'comma-separator',
-    };
-
-    textNode = {
-      i: node.i + index + length,
-      iEnd: null,
-      type: 'text',
-      data: '',
-    };
-
-    parseFrom = index + length;
-  }
-
-  if (parseFrom !== node.data.length) {
-    textNode.data += node.data.slice(parseFrom);
-    textNode.iEnd = node.iEnd;
-  }
-
-  if (textNode.data) {
-    yield textNode;
-  }
-}
-
-function* splitTextNodesAroundCommas(nodes) {
-  for (const node of nodes) {
-    if (node.type === 'text' && node.data.includes(',')) {
-      yield* splitTextNodeAroundCommas(node);
-    } else {
-      yield node;
-    }
-  }
-}
+import {withPropertyFromList} from '#composite/data';
+import {splitContentNodesAround, withContentNodes} from '#composite/wiki-data';
 
 export default templateCompositeFrom({
   annotation: `withAnnotationParts`,
@@ -84,81 +24,19 @@ export default templateCompositeFrom({
       output: input.value({'#annotationParts': []}),
     }),
 
-    // Get the list of nodes including custom comma-separator nodes.
-
-    {
-      dependencies: ['annotation'],
-      compute: (continuation, {
-        ['annotation']: annotation,
-      }) => continuation({
-        ['#nodes']:
-          Array.from(
-            splitTextNodesAroundCommas(
-              parseInput(annotation))),
-      }),
-    },
-
-    // Join the nodes into arrays for each range between comma separators,
-    // excluding the comma-separator nodes themselves.
-
-    withMappedList({
-      list: '#nodes',
-      map: input.value(node => node.type === 'comma-separator'),
-    }).outputs({
-      '#mappedList': '#commaSeparatorFilter',
+    withContentNodes({
+      from: 'annotation',
     }),
 
-    withMappedList({
-      list: '#commaSeparatorFilter',
-      filter: '#commaSeparatorFilter',
-      map: input.value((_node, index) => index),
-    }),
-
-    withFilteredList({
-      list: '#mappedList',
-      filter: '#commaSeparatorFilter',
-    }).outputs({
-      '#filteredList': '#commaSeparatorIndices',
+    splitContentNodesAround({
+      nodes: '#contentNodes',
+      around: input.value(/, */g),
     }),
 
     {
-      dependencies: ['#nodes', '#commaSeparatorFilter'],
-
+      dependencies: ['#contentNodeLists', input('mode')],
       compute: (continuation, {
-        ['#nodes']: nodes,
-        ['#commaSeparatorFilter']: commaSeparatorFilter,
-      }) => continuation({
-        ['#nodes']:
-          nodes.map((node, index) =>
-            (commaSeparatorFilter[index]
-              ? null
-              : node)),
-      }),
-    },
-
-    {
-      dependencies: ['#commaSeparatorIndices'],
-      compute: (continuation, {
-        ['#commaSeparatorIndices']: commaSeparatorIndices,
-      }) => continuation({
-        ['#unflattenIndices']:
-          [0, ...commaSeparatorIndices],
-      }),
-    },
-
-    withUnflattenedList({
-      list: '#nodes',
-      indices: '#unflattenIndices',
-    }).outputs({
-      '#unflattenedList': '#nodeLists',
-    }),
-
-    // Raise output now, if we're looking for node lists.
-
-    {
-      dependencies: ['#nodeLists', input('mode')],
-      compute: (continuation, {
-        ['#nodeLists']: nodeLists,
+        ['#contentNodeLists']: nodeLists,
         [input('mode')]: mode,
       }) =>
         (mode === 'nodes'
@@ -166,14 +44,11 @@ export default templateCompositeFrom({
           : continuation()),
     },
 
-    // Get the start of the first node, and the end of the last node,
-    // within each list.
-
     {
-      dependencies: ['#nodeLists'],
+      dependencies: ['#contentNodeLists'],
 
       compute: (continuation, {
-        ['#nodeLists']: nodeLists,
+        ['#contentNodeLists']: nodeLists,
       }) => continuation({
         ['#firstNodes']:
           nodeLists.map(list => list.at(0)),
@@ -197,8 +72,6 @@ export default templateCompositeFrom({
       '#lastNodes.iEnd': '#endIndices',
     }),
 
-    // Slice the content text within the bounds of each node list.
-
     {
       dependencies: [
         'annotation',