« get me outta code hell

data: withSortedList: syntax cleanup - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-02-24 10:36:41 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-02-24 13:35:51 -0400
commit716fe469bdfef789357a57d61dd69291491a1d4f (patch)
tree1a302438fd3f31e22dc36099d422ec14fffb4ab5 /src
parent4fcfbca35a925b79e83f857a251243279de76e61 (diff)
data: withSortedList: syntax cleanup
Diffstat (limited to 'src')
-rw-r--r--src/data/composite/data/withSortedList.js53
1 files changed, 29 insertions, 24 deletions
diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js
index 4ab0dfb1..a7942ab4 100644
--- a/src/data/composite/data/withSortedList.js
+++ b/src/data/composite/data/withSortedList.js
@@ -64,6 +64,14 @@ export default templateCompositeFrom({
           new Map(Array.from(symbols,
             (symbol, index) => [symbol, index]));
 
+        const assertEqual = (symbol1, symbol2) => {
+          if (equalSymbols.has(symbol1)) {
+            equalSymbols.get(symbol1).add(symbol2);
+          } else {
+            equalSymbols.set(symbol1, new Set([symbol2]));
+          }
+        };
+
         symbols.sort((symbol1, symbol2) => {
           const comparison =
             sortFn(
@@ -71,17 +79,8 @@ export default templateCompositeFrom({
               list[indexMap.get(symbol2)]);
 
           if (comparison === 0) {
-            if (equalSymbols.has(symbol1)) {
-              equalSymbols.get(symbol1).add(symbol2);
-            } else {
-              equalSymbols.set(symbol1, new Set([symbol2]));
-            }
-
-            if (equalSymbols.has(symbol2)) {
-              equalSymbols.get(symbol2).add(symbol1);
-            } else {
-              equalSymbols.set(symbol2, new Set([symbol1]));
-            }
+            assertEqual(symbol1, symbol2);
+            assertEqual(symbol2, symbol1);
           }
 
           return comparison;
@@ -95,22 +94,28 @@ export default templateCompositeFrom({
 
         const stableToUnstable =
           symbols
-            .map((symbol, index) =>
-              index > 0 &&
-              equalSymbols.get(symbols[index - 1])?.has(symbol))
-            .reduce((accumulator, collapseEqual) => {
-              if (empty(accumulator)) {
-                accumulator.push(0);
+            .map((current, index) => {
+              if (index === 0) {
+                return false;
+              }
+
+              const previous = symbols[index - 1];
+              return equalSymbols.get(previous)?.has(current);
+            })
+            .reduce((accumulator, collapseEqual, index) => {
+              if (index === 0) {
+                return accumulator;
+              }
+
+              const last = accumulator.at(-1);
+              if (collapseEqual) {
+                accumulator.push(last);
               } else {
-                const last = accumulator.at(-1);
-                if (collapseEqual) {
-                  accumulator.push(last);
-                } else {
-                  accumulator.push(last + 1);
-                }
+                accumulator.push(last + 1);
               }
+
               return accumulator;
-            }, []);
+            }, [0]);
 
         const unstableSortIndices =
           sortIndices.map(stable => stableToUnstable[stable]);