framework/js/lib/utils/extractText.js
Toby Zerner 722058f2fb Move generic util into lib
Might come in handy for the admin section later on
2015-09-04 12:15:41 +09:30

20 lines
402 B
JavaScript

/**
* Extract the text nodes from a virtual element.
*
* @param {VirtualElement} vdom
* @return {String}
*/
export default function extractText(vdom) {
let text = '';
if (vdom instanceof Array) {
text += vdom.map(element => extractText(element)).join('');
} else if (typeof vdom === 'object') {
text += extractText(vdom.children);
} else {
text += vdom;
}
return text;
}