diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-10-05 23:22:43 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-02-25 20:03:26 -0400 |
commit | 140a757cde7fdb1a72b56c5d39de713595c053ad (patch) | |
tree | 4b77582b88d57e39c19a092dada304bf46bfa56a /src/common-util | |
parent | 81f9eed9ac7c25bb9dd11a0e0a8c7bda83cd14bc (diff) |
content, data: experimental art tag sidebar
Diffstat (limited to 'src/common-util')
-rw-r--r-- | src/common-util/sugar.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js index 89699f60..66e160aa 100644 --- a/src/common-util/sugar.js +++ b/src/common-util/sugar.js @@ -781,6 +781,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); +} + // Delicious function annotations, such as: // // (*bound) soWeAreBackInTheMine |