Last active
August 9, 2022 22:20
-
-
Save zhenghaohe/c4be5f2b00c931425c4dab31ed01353b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <body> | |
// <div> | |
// <span> | |
// bar1 | |
// bar2 | |
// </span> | |
// </div> | |
// <div> | |
// baz | |
// </div> | |
// </body> | |
const tree = { | |
children: [ | |
{ children: [{ children: ['bar1', 'bar2'], tag: 'span' }], tag: 'div' }, | |
{ children: ['baz'], tag: 'div' }, | |
], | |
tag: 'body', | |
} | |
function foo(root) { | |
const recur = (root) => | |
root.children | |
? [ | |
`<${root.tag}>`, | |
...root.children.flatMap(recur), | |
`</${root.tag}>`, | |
] | |
: root | |
return recur(root).join('\n') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment