-
-
Save jed/964769 to your computer and use it in GitHub Desktop.
walk and filter DOM nodes by function
This file contains 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
function d( | |
a, // a function to filter the node results (optional) | |
b, // the ancestor element to walk (optional, defaults to document) | |
c // placeholder for the result node list | |
){ | |
b === c && // if b is undefined, | |
(b=document); // use the document | |
c = c || []; // intialize the node list if it doesn't exist | |
if (b) { // if the element exists | |
if ( // and | |
!a || a(b) // matches the filter function if one exists, | |
) c.push(b); // push it to the results. | |
d(a, b.firstChild, c); // repeat for the first child | |
d(a, b.nextSibling, c) // repeat for the next sibling | |
} | |
return c // return the results | |
} |
This file contains 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
function d(a,b,c){b===c&&(b=document);c||(c=[]);if(b){if(!a||a(b))c.push(b);d(a,b.firstChild,c);d(a,b.nextSibling,c)}return c} |
This file contains 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains 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
{ | |
"name": "walk", | |
"keywords": ["walk", "filter", "DOM"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moving the assignment for
c
to the first point where it is needed saves 2 bytes:Switching the ternaries (except for the first one) for Boolean operations saves another 2:
Moving the return to the front and bringing the ternary back saves 2 more:
And then, if you were allowed to, you could turn it into a slick arrow function saving 14 more bytes: