Skip to content

Instantly share code, notes, and snippets.

@trueadm
Created November 2, 2016 17:14
Show Gist options
  • Select an option

  • Save trueadm/4af126449d760e168bdae97c2861e67f to your computer and use it in GitHub Desktop.

Select an option

Save trueadm/4af126449d760e168bdae97c2861e67f to your computer and use it in GitHub Desktop.
flatten arrays
/**
* Normalizes recursive VNode lists by flattening all nodes, filtering out `null` children and converting strings to
* text nodes.
*/
export function normalizeVNodes(nodes: VNodeRecursiveList): VNode<any>[] {
for (let i = 0; i < nodes.length; i++) {
const n = nodes[i];
if (n === null || Array.isArray(n)) {
let copy = nodes.slice(i);
const flatten = nodes.slice(i) as VNode<any>[];
while (copy.length > 0) {
const item = copy.shift();
if (item !== null) {
if (Array.isArray(item)) {
copy = (item as any).concat(copy);
} else {
flatten.push(typeof item === "string" ? $t(item) : item as VNode<any>);
}
}
}
return flatten;
} else if (typeof n === "string") {
nodes[i] = $t(n);
}
}
return nodes as VNode<any>[];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment