adds last, first, remove, contains, isEmpty and few more methods/properties to Array utilizing Object.defineProperty
Arrayrange13 instead 1 2 3;Arraymin1 3 instead Mathminapplynull1 3;Arraymax1 3 instead Mathmaxapplynull1 3;arraycontainso instead arrayindexOfo != -1arrayremoveo instead arraysplicearrayindexOfo 1 //remove returns true when o was removedarrayreplaceo n instead arrayarrayindexOfo = n;arrayfindOnetest instead arrayfiltertest0;arrayclear instead arraylength = 0arraycopy instead arrayslice0arrayunique instead arrayfilterreturn i === aindexOfitm;arrayuniquepushitem instead if ~selfindexOfitem selfpushitem;arrayuniquemergearr instead as previous but for all items in arrarrayinsertiitem instead arraysplicei 0 item; //plus insert can take variable number of args, not just onearrayisEmpty instead arraylength == 0arrayfirst instead array0arraylast instead arrayarraylength-1
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 version 1.2.0-rc2 in it's method assigns to arr.last and this collides with our 'last' getter. So beware, I have created a pull request so that ngSanitize doesn't break, but who knows when it is going to be merged. For now, use ngSanitize from my fork: compatible ngSanitize
##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 modified and we can't have that. But if you are building an end product used by end-user, I think it is quite allright to do that.
Missing any sugar? Please do submit a pull or feature request.