« get me outta code hell

data: withSortedList: ensure unstableSortIndices maps from orig order - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-02-24 12:20:47 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-02-24 13:35:52 -0400
commit80ca33f781b710074b11ed68eae6fc2ec314ea84 (patch)
tree035fde4c9e2c6b81b3083156fe6439c267269414
parent5bd2da3172114494fb61d9b5942771f387959fec (diff)
data: withSortedList: ensure unstableSortIndices maps from orig order
This code is terrifying.
-rw-r--r--src/data/composite/data/withSortedList.js17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js
index a7942ab4..3a069cf7 100644
--- a/src/data/composite/data/withSortedList.js
+++ b/src/data/composite/data/withSortedList.js
@@ -60,10 +60,14 @@ export default templateCompositeFrom({
         const equalSymbols =
           new Map();
 
-        const indexMap =
+        const symbolToIndex =
           new Map(Array.from(symbols,
             (symbol, index) => [symbol, index]));
 
+        const indexToSymbol =
+          new Map(Array.from(symbols,
+            (symbol, index) => [index, symbol]));
+
         const assertEqual = (symbol1, symbol2) => {
           if (equalSymbols.has(symbol1)) {
             equalSymbols.get(symbol1).add(symbol2);
@@ -75,8 +79,8 @@ export default templateCompositeFrom({
         symbols.sort((symbol1, symbol2) => {
           const comparison =
             sortFn(
-              list[indexMap.get(symbol1)],
-              list[indexMap.get(symbol2)]);
+              list[symbolToIndex.get(symbol1)],
+              list[symbolToIndex.get(symbol2)]);
 
           if (comparison === 0) {
             assertEqual(symbol1, symbol2);
@@ -87,7 +91,7 @@ export default templateCompositeFrom({
         });
 
         const sortIndices =
-          symbols.map(symbol => indexMap.get(symbol));
+          symbols.map(symbol => symbolToIndex.get(symbol));
 
         const sortedList =
           sortIndices.map(index => list[index]);
@@ -118,7 +122,10 @@ export default templateCompositeFrom({
             }, [0]);
 
         const unstableSortIndices =
-          sortIndices.map(stable => stableToUnstable[stable]);
+          Array.from(
+            {length: list.length},
+            (_, index) =>
+              stableToUnstable[symbols.indexOf(indexToSymbol.get(index))]);
 
         return continuation({
           ['#sortedList']: sortedList,