The best approach may be to put the logic in flattenChildren, though there you only have the full key, not the parts. (Also your code here is wrong because you want to be checking for `nameSoFar + (nameSoFar ? SUBSEPARATOR : SEPARATOR) + key`, not just `key`.)