Tired of not having a last property on array in Javascript? Tired of not having contains method? This little script solves those by adding a bit of sugar allowing you to do:
Array instead 1 2 3; Array instead Mathmin; Array instead Mathmax; array instead array != -1 array instead array //remove returns true when o was removed array instead arrayarray = n; array instead array0; arrayclear instead arraylength = 0 array instead array array instead array
Usable in any environment that supports Object.defineProperty(oldest would probably be IE9).
Available through npm and bower, just require(or include with regular script tag) and you should be good to go.
npm install array-sugar
bower install array-sugar
jspm install npm:array-sugar
Then require('array-sugar'); and you're done.
known incompatibilities
Angular ngSanitize module in it's method assigns to arr.last and this collides with our 'last' getter.
Is it a good idea to extend array.prototype?
If I am writing a web app, NOT a library/framework, I don't consider this a bad practice. I have quite few libraries and I do not use array-sugar in them even if It would be helpful most of the time. Users of these libraries would have their Array unexpectedly extended and that is not a good practice. If you are building an end product used by end-user, I think it is quite allright to have these methods straight on array.prototype.
Missing any sugar? Please do submit a pull or feature request.