DEV Community

Cover image for Essential Vanilla JavaScript Functions

Essential Vanilla JavaScript Functions

Amit Merchant on September 22, 2017

Essential Vanilla JavaScript Functions Some of the missing functions in JavaScript in vanilla form (inspired by PHP). You can just try ...
Collapse
 
lexlohr profile image
Alex Lohr

Interesting approach for array_unique (even taking into account that objects and arrays in the array might have different pointers, but have the same content. There's just one minor issue: x in y will also return true for the constructor attribute, so better use y.hasOwnProperty(x). If your array contains 'constructor' as a string, your function will filter it out.

Collapse
 
amit_merchant profile image
Amit Merchant

Yeah. We can implement it that way as well. If you want you can send over a pull request on github.com/amitmerchant1990/essent...

Collapse
 
lexlohr profile image
Alex Lohr

There you go.

Collapse
 
rehia profile image
Jérôme Avoustin

Sorry, but while the big majority of these functions are pretty useless (you might find them in lodash or even in the language itself), the implementations show a big misunderstanding of how JavaScript works.
You can do pretty much anything with Array functions like map, reduce, filter, slice and concat. They are helpful because they do not mutate the original array, but produce a new one.
And finally, it is really not necessary to pollute your code with such ugly names, even if PHP provides them. Next readers won't necessary know a lot about PHP.

Collapse
 
eerk profile image
eerk

This, and you can even use the ...spread operator instead of concat, to create a new array from an existing one.
The above functions are translated a bit too literally from PHP, or perhaps the intention was to make them work for ES5 / lower?

Collapse
 
hoffmann profile image
Peter Hoffmann

Why are map, reject and taken part of your list (they already exist in vanilla JS)? When (besides an job interview) did you have the need for strrev?
Besides that a very nice exercise.

Collapse
 
amit_merchant profile image
Amit Merchant • Edited

Pardon me for that. I was curious and was just trying to do my implementation.

Collapse
 
weswedding profile image
Weston Wedding

Gotta give a shout out to Locutus here, a source for hundreds of Javascript implementations of PHP functions (and other languages).

Collapse
 
tobbwiz profile image
Bernard Sibanda

I love what you have done. Keep it up. The raised point on mutation is a good one. These need to process cloned objects not original. With regard these (map, reduce, filter, slice and concat), blind use of these is as good as blind use of frameworks.

Collapse
 
meisekimiu profile image
Natalie Martin

Neat! I love doing things in Vanilla JavaScript over including a big library whenever I can. However, to make the code a bit nicer I'd suggest adding the array and string methods to their actual object prototypes if possible. In my opinion it makes the code look much nicer and is a convenience Javascript has the original PHP functions do not. (That's the reason PHP has so many functions that start with "array" or "string")

Collapse
 
gcdcoder profile image
Gustavo Castillo • Edited

Awesome utilities, just one question, what is this line for?

ret += "~" + j + "^" + keyify(obj[j]) + "%";

Why do you use "~", ^ and "%"?

Collapse
 
gregorgonzalez profile image
Gregor Gonzalez

Yes! This is exactly what I needed. As a PHP developer I always use those functions. Thank you.

Collapse
 
ryanhaber profile image
Ryan Haber

Fun. Thanks!