Skip to content

Instantly share code, notes, and snippets.

@zhenghaohe
Last active August 9, 2022 22:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhenghaohe/c4be5f2b00c931425c4dab31ed01353b to your computer and use it in GitHub Desktop.
Save zhenghaohe/c4be5f2b00c931425c4dab31ed01353b to your computer and use it in GitHub Desktop.
// <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