Created
November 2, 2016 17:14
-
-
Save trueadm/4af126449d760e168bdae97c2861e67f to your computer and use it in GitHub Desktop.
flatten arrays
This file contains hidden or 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
| /** | |
| * 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