Created
August 18, 2019 01:35
-
-
Save veryjos/a04fc3de57eab1a6f61bff4677769b29 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
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