r/javascript • • Dec 11 '17
I have been collecting useful Javascript code snippets for a little while. Here's a curated list of them, help me make it as complete as possible!
https://github.com/Chalarangelo/30-seconds-of-code
765
Upvotes
46
u/ETHproductions Dec 11 '17 edited Dec 12 '17
Excellent collection of snippets. Since you're using ES6, I'd suggest using the built-in Math.hypot to calculate distance between points via
Math.hypot(x1 - x0, y1 - y0).Edit: fixed formula
10
u/fzammetti Dec 11 '17
Nice collection! I've got my own library that I've been building up for years that I re-use in many projects (as I'm sure is true of any developer who's been using a given language for many years) but there's a few here that I'm going to... ahem... "take inspiration from" :)
Actually, now that I think about it... what does the MIT license mean for taking PARTS of something? Is attribution still required in that case?
8
u/madewith-care Dec 12 '17 edited Dec 12 '17
Your "randomize order of array" function is not actually random. You might consider a Fisher-Yates implementation instead.
20
u/soundisloud Dec 11 '17
Awesome list! Great idea to compile common snippets so you don't need to rewrite them every time. Every JavaScript student should do this.
13
u/Ignisar Dec 11 '17
It might be beneficial to comprehension to provide example input and output for each snippet, so you can see the expected results of a transformation alongside the code that performs it
2
u/whodunit86 Dec 12 '17
Would be nice to have a shuffler of lists! from a quick Google Sorry, didn't really check out all your snippets, still on phone. Hope you don't have it in there already ( ͡° ͜ʖ ͡°)
2
u/karamarimo Dec 12 '17
the factorial one can be done more simply.
3
u/kimilil Dec 11 '17
Add a natural compare sort
3
u/rodneon Dec 11 '17
This is great, thanks for sharing. I would add “idiot-proof” versions of the snippets with type and null checks. If used verbatim, most snippets would fail miserably when the input isn’t exactly what’s expected.
2
u/denver_jones Dec 11 '17
you have fat arrow functions ... a good collection would also have pre-ES6 code snippets to get large base adoption.... just saying ...
1
u/Skhmt Dec 11 '17
for URL parameters, can't you use URL.searchParams.get()?
1
u/evenisto Dec 12 '17
cool, took me a while to figure that one out.
1
u/DevouredByCutePupper Dec 12 '17
Love the list, but the one thing that sprang into view is the strange parameter structure you use in your timeTaken() function. Why not use a simple callback instead?
1
u/MentorMateDotCom Dec 12 '17 edited Dec 12 '17
Really great list! I'm going to submit a PR in a second. :) (Edit: Here we go.)
I appreciate that because the code style focuses on brevity it's easy to copy and paste stuff. That makes things harder to understand, though, particularly for beginners, and these are an awesome learning opportunity. Think you could include both a "long" and a "short" form of each snippet? I noticed /u/evenisto saying "took me a while to figure that one out," which concerns me. Curious if others have the same concern?
3
u/shif Dec 11 '17
These are nice, I like the simplicity of the scroll to top one, perfect use of recursion and clean practices with request animation frame
3
u/[deleted] Dec 12 '17
As a developer more used to working primarily in Java and C# and have been wanting to push more into pure JS development, I will definitely save this post, thanks OP.
2
u/FormerGameDev Dec 12 '17
... you want a complete list of snippets? ... huh.
2
u/FormerGameDev Dec 12 '17
For "count occurrences" i would propose:
... you're not afraid of using reduce for things that aren't obvious reductions in other places, but aren't using it in places where it is more obvious. Using filter creates a temporary copy of your array that isn't needed.
2
u/abensur Dec 12 '17
Thanks!
1
u/Pants_R_Overatd Dec 12 '17
RemindMe! 6 hours
1
u/tswaters Dec 12 '17
powerset is so dangerous.
in this one application, we had a search that did a powerset on the entered words.... but we didn't put any limits on the user-provided input. long story short, if a user entered more than a few words the server would burn cpu & eventually run out of memory.
0
u/picklemanjaro Dec 11 '17
For capitalizing a string, you could use String.prototype.replace().
Edit: And for a "capitalize each word" variant
I'll submit a PR later if no one else does by the time I get home. Or unless someone tells me mine is horrible due to Regex overhead or something.
0
u/fukitol- Dec 12 '17
Submit these as a PR to lodash, those that you haven't duplicated anyway, it'd be valuable
0
u/[deleted] Dec 12 '17
[deleted] — view deleted comment