I run a website called Transpapers which helps trans people get their information by automatically generating the necessary forms to file.
The business logic depends on a function called shakeTree()
, which I haven't quite documented because I'm not sure how to do it. This is my attempt to figure out what it wants to be doing.
Given a (possibly deeply nested) TypeScript object tree
and a function shake(...)
, shakeTree()
produces a list of the leaf-level field names in tree
accessed at any point by shake()
.
shakeTree()
output?Given the following:
interface Tree { [...] }
let tree: Tree = {
branch: {
leaf1: true,
leaf2: 7,
},
loop: "zoop",
};
function shake(tree) {
return tree.branch.leaf1 * tree.loop;
}
shakeTree(tree, shake)
should output ["leaf1", "loop"]
. "branch"
is not in the list because it is itself an object. "leaf2"
is not in the list because it is not accessed.
We need to produce the union of the sets of fields in the forms the user will have to file. The field names output by shakeTree()
will be passed to the function that generates the HTML form presented to the user. Otherwise we'd have to spell out the field names manually, which would be an enormous pain.
Through the magic of proxy objects.
Here is shakeTree()
in its current form, as I write this; mildly editing to save space.