From 5408d6660b22b9ddee8c4a297c89fca92ae2d505 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 3 Jul 2023 23:12:03 -0300 Subject: content: listArtistsBy{CommentaryEntries,Name} + syntax changes --- src/util/wiki-data.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/util/wiki-data.js') diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index da8312f..0ee474a 100644 --- a/src/util/wiki-data.js +++ b/src/util/wiki-data.js @@ -3,6 +3,7 @@ import { accumulateSum, empty, + stitchArrays, unique, } from './sugar.js'; @@ -208,6 +209,44 @@ export function sortByDate(data, { }); } +// Funky sort which takes a data set and a corresponding list of "counts", +// which are really arbitrary numbers representing some property of each data +// object defined by the caller. It sorts and mutates *both* of these, so the +// sorted data will still correspond to the same indexed count. +export function sortByCount(data, counts, { + greatestFirst = false, +} = {}) { + const thingToCount = new Map( + stitchArrays({thing: data, count: counts}) + .map(({thing, count}) => [thing, count])); + + data.sort((a, b) => + (greatestFirst + ? thingToCount.get(b) - thingToCount.get(a) + : thingToCount.get(a) - thingToCount.get(b))); + + counts.sort((a, b) => + (greatestFirst + ? b - a + : a - b)); + + return data; +} + +// Corresponding filter function for the above sort. By default, items whose +// corresponding count is zero will be removed. +export function filterByCount(data, counts, { + min = 1, + max = Infinity, +} = {}) { + for (let i = counts.length - 1; i >= 0; i--) { + if (counts[i] < min || counts[i] > max) { + data.splice(i, 1); + counts.splice(i, 1); + } + } +} + export function sortByPositionInParent(data, { getParent, getChildren, -- cgit 1.3.0-6-gf8a5