framework/js/lib/utils/extractText.js

20 lines
402 B
JavaScript
Raw Normal View History

2015-07-17 16:13:28 +08:00
/**
* 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;
}