« get me outta code hell

data: withSortedList: general 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 12:37:44 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-02-24 13:35:52 -0400
commit5447c0ac287aec69ce649f22ad926cd2c8b78e6c (patch)
tree783d0cef0d8687d0c4578013399648488c2b8e97 /src
parent90dec66baa7c71fcacb53426a77213c4f8dd710a (diff)
data: withSortedList: general cleanup
Still magic.
Diffstat (limited to 'src')
-rw-r--r--src/data/composite/data/withSortedList.js67
1 files changed, 34 insertions, 33 deletions
diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js
index 1d9188f8..51309e19 100644
--- a/src/data/composite/data/withSortedList.js
+++ b/src/data/composite/data/withSortedList.js
@@ -54,19 +54,21 @@ export default templateCompositeFrom({
         [input('list')]: list,
         [input('sort')]: sortFn,
       }) {
-        const symbols =
-          Array.from({length: list.length}, () => Symbol());
+        const originalIndices =
+          Array.from({length: list.length}, (_, index) => index);
 
-        const equalSymbols =
-          new Map();
+        const symbols = [];
+        const symbolToIndex = new Map();
+        const indexToSymbol = new Map();
 
-        const symbolToIndex =
-          new Map(Array.from(symbols,
-            (symbol, index) => [symbol, index]));
+        for (const index of originalIndices) {
+          const symbol = Symbol();
+          symbols.push(symbol);
+          symbolToIndex.set(symbol, index);
+          indexToSymbol.set(index, symbol);
+        }
 
-        const indexToSymbol =
-          new Map(Array.from(symbols,
-            (symbol, index) => [index, symbol]));
+        const equalSymbols = new Map();
 
         const assertEqual = (symbol1, symbol2) => {
           if (equalSymbols.has(symbol1)) {
@@ -76,6 +78,9 @@ export default templateCompositeFrom({
           }
         };
 
+        const isEqual = (symbol1, symbol2) =>
+          !!equalSymbols.get(symbol1)?.has(symbol2);
+
         symbols.sort((symbol1, symbol2) => {
           const comparison =
             sortFn(
@@ -96,33 +101,29 @@ export default templateCompositeFrom({
         const sortedList =
           sortIndices.map(index => list[index]);
 
+        const push = (array, value) => {
+          array.push(value);
+          return array;
+        };
+
         const stableToUnstable =
           symbols
-            .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 {
-                accumulator.push(last + 1);
-              }
-
-              return accumulator;
-            }, [0]);
+            .map(
+              (current, index) =>
+                (index === 0
+                  ? false
+                  : isEqual(current, symbols[index - 1])))
+            .reduce(
+              (accumulator, equalsPrevious, index) =>
+                (index === 0
+                  ? accumulator
+                  : (equalsPrevious
+                      ? push(accumulator, accumulator.at(-1))
+                      : push(accumulator, accumulator.at(-1) + 1))),
+              [0]);
 
         const unstableSortIndices =
-          Array.from({length: list.length}, (_, index) => index)
+          originalIndices
             .map(index => indexToSymbol.get(index))
             .map(symbol => symbols.indexOf(symbol))
             .map(stable => stableToUnstable[stable]);