diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-07-03 23:12:03 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-07-03 23:12:03 -0300 |
commit | 5408d6660b22b9ddee8c4a297c89fca92ae2d505 (patch) | |
tree | fe0bbb257fb1e478806a500d5ef8166085f8bbf7 /src/util | |
parent | 4e3cb1d34c8bd68032b736ee3f6cfe0aeb495ee4 (diff) |
content: listArtistsBy{CommentaryEntries,Name} + syntax changes
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/wiki-data.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index da8312f9..0ee474a7 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, |