🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

array-sugar

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-sugar

adds last, first, remove, contains, isEmpty and few more methods/properties to Array utilizing Object.defineProperty

1.1.1
Source
npm
Version published
Weekly downloads
164
102.47%
Maintainers
1
Weekly downloads
 
Created
Source

array-sugar

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.insert(i,item)   instead      array.splice(i, 0, item); //plus it can take variable number of args
     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 in index.html) and you should be good to go.

npm install array-sugar
bower install array-sugar

Tests via nodeunit included.

#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

Missing any sugar? Please do submit a pull or feature request.

Keywords

array

FAQs

Package last updated on 20 Dec 2014

Did you know?

Socket

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.

Install

Related posts