diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-10-05 23:22:43 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-10-05 23:22:43 -0300 |
commit | 62d7e0146d471e7623e4ec384a928a828b8780e9 (patch) | |
tree | 790fb2246ccc9a6dbf64db6d21c6fa4886aac79c /src/util | |
parent | abb73f5e8a2361d133736a6cc5bed5c98206a814 (diff) |
content, data: experimental art tag sidebar
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/wiki-data.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index fecf94e1..a85dd9a2 100644 --- a/src/util/wiki-data.js +++ b/src/util/wiki-data.js @@ -97,6 +97,38 @@ export function chunkMultipleArrays(...args) { return results; } +// This (or its helper function) should probably be a generator, but generators +// are scary... Note that the root node is never considered a leaf, even if it +// doesn't have any branches. It does NOT pay attention to the *values* of the +// leaf nodes - it's suited to handle this kind of form: +// +// { +// foo: { +// bar: {}, +// baz: {}, +// qux: { +// woz: {}, +// }, +// }, +// } +// +// for which it outputs ['bar', 'baz', 'woz']. +// +export function collectTreeLeaves(tree) { + const recursive = ([key, value]) => + (value instanceof Map + ? (value.size === 0 + ? [key] + : Array.from(value.entries()).flatMap(recursive)) + : (empty(Object.keys(value)) + ? [key] + : Object.entries(value).flatMap(recursive))); + + const root = Symbol(); + const leaves = recursive([root, tree]); + return (leaves[0] === root ? [] : leaves); +} + // Sorting functions - all utils here are mutating, so make sure to initially // slice/filter/somehow generate a new array from input data if retaining the // initial sort matters! (Spoilers: If what you're doing involves any kind of |