Skip to content

Instantly share code, notes, and snippets.

@veryjos
Created August 18, 2019 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veryjos/a04fc3de57eab1a6f61bff4677769b29 to your computer and use it in GitHub Desktop.
Save veryjos/a04fc3de57eab1a6f61bff4677769b29 to your computer and use it in GitHub Desktop.
function* dfs(node) {
if (!node) return;
if (node.type === 'text') {
for (const ch of node.val) {
yield ch;
}
}
for (const child of (node.children || [])) {
yield *dfs(child);
}
}
function* zip(a, b) {
while (true) {
const nextA = a.next(), nextB = b.next();
if (nextA.done && nextB.done) break;
yield [nextA.value, nextB.value];
}
}
const rootA = {
type: 'text',
val: 'weed',
children: [
{
val: 245,
},
{
val: 999,
children: [
{
type: 'text',
val: 'g',
},
{
type: 'text',
val: 'uy',
}
]
},
{
type: 'text',
val: 'f',
}
],
};
const rootB = {
type: 'span',
children: [
{
type: 'text',
val: 'weed',
},
{
type: 'span',
children: [
{
type: 'text',
val: 'guy',
},
{
type: 'text',
val: 'f',
}
]
}
],
};
const match = () => {
for (const [a, b] of zip(dfs(rootA), dfs(rootB))) {
if (a !== b) {
return false;
}
}
return true;
}
console.log(match());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment