3 ms·
I generally save comments for WTF code, or to put links that document what's going on. I try to abstract things as a lot of folks are mentioning here, but that
by bdcravens 5y ago
I generally save comments for WTF code, or to put links that document what's going on. I try to abstract things as a lot of folks are mentioning here, but that just pushes things to another place. Eventually someone is going to need to debug that code. Here's a Puppeteer example, where text is being split into nodes to extract based on line breaks:
/* https://medium.com/@roxeteer/javascript-one-liner-to-get-elements-text-content-without-its-child-nodes-8e59269d1e71 */
return await this.page.$eval(selector, e => {
const s = [].reduce.call(e.childNodes, function(a, b) { return a + (b.nodeType === 3 ? b.textContent : ''); }, '');
return s.replace(/\s+/g,' ').trim();
});
- ninthcat 5y agoIf you rewrite it so it's no longer a cryptic one-liner, it's probably easier to understand this way without a comment than the other way with one. return this.page.$eval(selector, (node) => Array .from(node.childNodes) .filter((child) => child.nodeType == Node.TEXT_NODE) .map((child) => child.nodeValue) .join('') .replace(/\s+/g, ' ') .trim() );
- dmurray 5y agoHope this refactor was well unit tested, because it looks to do something a lot different from the original code.