Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dashpass Golf

c='children',l='reduce' - This creates two variables that I can reuse. Obviously, l is shorter than reduce, so I want to create aliases for the things we're gonna reuse. What these strings do will be explained later

r=e=> is creating a function called r that takes an argument e and returns what's after => (which I'm about to explain in a second)

e[c]&&e[c].length>0 - Remember that c = 'children'. We're checking that the argument supplied to function r (e) both has a property called children and that it contains more than 1 element in the list (the purpose of r (also called recurse https://gist.github.com/crutchcorn/e4a5f7e1d7c160d2a5d856ab66bfd657 for normal code version) is to be able to go down a tree of elements in a webpage and return a list of every child of any element)

[...e[c]] - We're taking the children of the argument and turning it into an array (previously, it was a list a lot LIKE an array, but it didn't have the reduce function we'll need to call in a second)

[...e[c]][l]((p,d)=>....[...e[c]]) - This takes that array we just made and runs the reduce method (function assigned to ALL arrays) in order to create a list. You might notice we did [...e[c]] again. This is so that we can set the starting value of reduce (that p is assigned to on the first run of reduce) to the list of children of e

[...e[c]][l]((p,d)=>[...p,...r(d)],[...e[c]]):[e] Adding in the middle part now - it will run r again with the child of the children (d) and run r on it, therefore returning it's child which will return it's children, etc etc until it can't return anything further

Tada! We just recreated that gist with MUCH less characters

Now, what are we using r for?

[...document.querySelectorAll('[class^=Grid_gridCell]')] - You might've noticed the ... again. This is because, once again, we're converting something that's a lot LIKE an array to an actual array (same reasons as above). querySelectorAll will look for all elements that contain the data inside of it. In this case, we're looking for any element that has a class attribute that's value STARTS in Grid_gridCell

docItems ][l](_,f=>); - this one is a bit odd to explain lol. See, we don't NEED to use reduce here, we really just need to run a function on every item in that document.query array we just made. BUT, [l] is much smaller than .map, so we're just reusing reduce but not setting an initial value and ignoring the previous return values (_ isn't used anywhere) (edited)

r(f)[l]((p,e)=>...,0) - This is just taking every item in Grid and getting all of their children. We're going to need the final value of this (to count if we want to remove an item or not), so we start with an initial value of 0 which in JS is the same thing as false

((p,e)=>p||!/dashpass/.exec(e.outerHTML),0) Now, we're making sure that the previous element is true (which should only happen if something matches, but not other time, but because reduce returns a value every time, we need to carry the results from before in order to get the real return). Then, we're just checking if the string dashpass is anywhere in the HTML of the element. If it exists, then it should be marked to remove the parent item (and make the 0 now be true)

,0)?f.remove():{}); - If it's true, remove the parent, otherwise don't do anything And that's the whole thing! 😄

https://twitter.com/crutchcorn/status/1041524520165683200?s=19

c='children',l='reduce',r=e=>e[c]&&e[c].length>0?[...e[c]][l]((p,d)=>[...p,...r(d)],[...e[c]]):[e];
[...document.querySelectorAll('[class^=Grid_gridCell]')][l]((_,f)=>r(f)[l]((p,e)=>p||!/dashpass/.exec(e.outerHTML),0)?f.remove():{});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment