
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
array-sugar
Advanced tools
adds last, first, remove, contains, isEmpty and few more methods/properties to Array utilizing Object.defineProperty
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.range(1,3) instead [1, 2, 3];
Array.min([1, 3]) instead Math.min.apply(null,[1, 3]);
Array.max([1, 3]) instead Math.max.apply(null,[1, 3]);
array.contains(o) instead array.indexOf(o) != -1
array.remove(o) instead array.splice(array.indexOf(o), 1) //remove returns true when o was removed
array.replace(o, n) instead array[array.indexOf(o)] = n;
array.findOne(test) instead array.filter(test)[0];
array.clear() instead array.length = 0
array.copy() instead array.slice(0)
array.unique() instead array.filter(function(function(itm,i,a){return i === a.indexOf(itm);}))
array.unique.push(item) instead if (~self.indexOf(item)) { self.push(item); }
array.unique.merge(arr) instead as previous but for all items in arr
array.insert(i,item) instead array.splice(i, 0, item); //plus insert can take variable number of args, not just one
array.isEmpty instead array.length == 0
array.first instead array[0]
array.last instead array[array.length-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.
Angular ngSanitize module in it's method assigns to arr.last and this collides with our 'last' getter.
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.
FAQs
adds last, first, remove, contains, isEmpty and few more methods/properties to Array utilizing Object.defineProperty
The npm package array-sugar receives a total of 106 weekly downloads. As such, array-sugar popularity was classified as not popular.
We found that array-sugar demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.