npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

array-extended

v0.0.11

Published

Additional array extensions with a chainable api

Downloads

351,876

Readme

Build Status

browser support

array-extended

array-extended is a Javascript library that can be used standalone or incorporated into extended

var array = require("array-extended");

Or

var myextended = require("extended")
	.register(require("array-extended"));

Installation

npm install array-extended

Or download the source (minified)

Usage

array-extended includes many useful methods that for array. All of the functions can be chained together using array-exteded as a function or you can use the functions directly as shown in the examples.

The following native ES5 methods are included for completeness.

  • forEach
  • map
  • filter
  • reduce
  • reduceRight
  • some
  • every
  • indexOf
  • lastIndexOf

Note forEach returns the original array for chaining purposes.

sum

Sums the values of an array.


array.sum([1,2,3]); //6

array([1,2,3]).sum().value(); //6

avg

Finds the average of an array of numbers.


array.avg([1,2,3]); //2

array([1,2,3]).avg().value(); //2

sort

Sorts an array based on a property, by natural ordering, or by a custom comparator.

Note this does not change the original array.


array.sort([{a: 1},{a: 2},{a: -2}], "a"); //[{a: -2},{a: 1},{a: 2}];

array([{a: 1},{a: 2},{a: -2}]).sort("a"); //[{a: -2},{a: 1},{a: 2}];

min

Finds the minimum value in an array based on a property, by natural ordering, or by a custom comparator.


array.min([ 3, -3, -2, -1, 1, 2]); //-3

array.min([{a: 1},{a: 2},{a: -2}], "a"); //{a : -2}

array([ 3, -3, -2, -1, 1, 2]).min(); //-3

array([{a: 1},{a: 2},{a: -2}]).min("a"); //{a : -2}

max

Finds the maximum value in an array based on a property, by natural ordering, or by a custom comparator.


array.max([ 3, -3, -2, -1, 1, 2]); //2

array.max([{a: 1},{a: 2},{a: -2}], "a"); //{a : 2}

array([ 3, -3, -2, -1, 1, 2]).max(); //2

array([{a: 1},{a: 2},{a: -2}]).max("a"); //{a : 2}

difference

Finds the difference between two arrays.

array.difference([1, 2, 3], [2]); //[1, 3]
array.difference([true, false], [false]); //[true]
array.difference(["a", "b", 3], [3]); //["a", "b"]

var a = , b = , c = ;
array.difference([{a: 1}, {a: 2}, {a: 3}], [{a: 2}, {a: 3}]); //[{a: 1}]

array([true, false]).difference([false]).value()// [true]
array([1, 2, 3]).difference([2]).value()// [1, 3]
array([1, 2, 3]).difference([2], [3]).value(); //[1]
array(["a", "b", 3]).difference([3]).value(); ["a", "b"]
array([{a: 1}, {a: 2}, {a: 3}]).difference([{a: 2}, {a: 3}]).value(); [{a: 1}]

unique

Removed duplicate values from an array.


array.unique([1, 2, 2, 3, 3, 3, 4, 4, 4]); //[1, 2, 3, 4]
array([1, 2, 2, 3, 3, 3, 4, 4, 4]).unique().value();  //[1, 2, 3, 4]

array(["a", "b", "b"]).unique().value(); //["a", "b"]
array.unique(["a", "b", "b"]); //["a", "b"]

rotate

Rotates an array by the number of places for 1 position by default.


var arr = array(["a", "b", "c", "d"])
arr.rotate().value();   //["b", "c", "d", "a"]
arr.rotate(2).value();  //["c", "d", "a", "b"]
arr.rotate(3).value();  //["d", "a", "b", "c"]
arr.rotate(4).value();  //["a", "b", "c", "d"]
arr.rotate(-1).value(); //["d", "a", "b", "c"]
arr.rotate(-2).value(); //["c", "d", "a", "b"]
arr.rotate(-3).value(); //["b", "c", "d", "a"]
arr.rotate(-4).value(); //["a", "b", "c", "d"]

var arr = ["a", "b", "c", "d"];
array.rotate(arr);     //["b", "c", "d", "a"]
array.rotate(arr, 2);  //["c", "d", "a", "b"]
array.rotate(arr, 3);  //["d", "a", "b", "c"]
array.rotate(arr, 4);  //["a", "b", "c", "d"]
array.rotate(arr, -1)  //["d", "a", "b", "c"]
array.rotate(arr, -2); //["c", "d", "a", "b"]
array.rotate(arr, -3); //["b", "c", "d", "a"]
array.rotate(arr, -4); //["a", "b", "c", "d"]

permutations

Finds all permutations of an array.


array([1, 2, 3]).permutations(); //[
                                 //   [ 1, 2, 3 ],
                                 //   [ 1, 3, 2 ],
                                 //   [ 2, 3, 1 ],
                                 //   [ 2, 1, 3 ],
                                 //   [ 3, 1, 2 ],
                                 //   [ 3, 2, 1 ]
                                 //]

array([1, 2, 3]).permutations(2);//[
                                 //   [ 1, 2],
                                 //   [ 1, 3],
                                 //   [ 2, 3],
                                 //   [ 2, 1],
                                 //   [ 3, 1],
                                 //   [ 3, 2]
                                 //]

array.permutations([1, 2, 3]);   //[
                                 //   [ 1, 2, 3 ],
                                 //   [ 1, 3, 2 ],
                                 //   [ 2, 3, 1 ],
                                 //   [ 2, 1, 3 ],
                                 //   [ 3, 1, 2 ],
                                 //   [ 3, 2, 1 ]
                                 //]

array.permutations(([1, 2, 3], 2);//[
                                 //   [ 1, 2],
                                 //   [ 1, 3],
                                 //   [ 2, 3],
                                 //   [ 2, 1],
                                 //   [ 3, 1],
                                 //   [ 3, 2]
                                 //]

zip

Zips the values of multiple arrays into a single array.


array([1]).zip([2], [3]).value();                    //[
                                                     //  [ 1, 2, 3 ]
                                                     //];

array([1, 2]).zip([2], [3]).value();                 //[
                                                     //  [ 1, 2, 3 ],
                                                     //  [2, null, null]
                                                     //]

array([1, 2, 3]).zip([ 4, 5, 6 ], b).value();        //[
                                                     //  [1, 4, 7],
                                                     //  [2, 5, 8],
                                                     //  [3, 6, 9]
                                                     //]

array([1, 2]).zip([ 4, 5, 6 ],  [7, 8, 9 ]).value(); //[
                                                     //  [1, 4, 7],
                                                     //  [2, 5, 8]
                                                     //]

array([ 4, 5, 6 ]).zip([1, 2], [8]).value(),         //[
                                                     //  [4, 1, 8],
                                                     //  [5, 2, null],
                                                     //  [6, null, null]
                                                     //]


array.zip([1], [2], [3]);                   //[
                                            //  [ 1, 2, 3 ]
                                            //]

array.zip([1, 2], [2], [3]);                //[
                                            //  [ 1, 2, 3 ],
                                            //  [2, null, null]
                                            //]

array.zip([1, 2, 3], [4,5,6],  [7, 8, 9 ]); //[
                                            //  [1, 4, 7],
                                            //  [2, 5, 8],
                                            //  [3, 6, 9]
                                            //]

array.zip([1, 2], [4,5,6],  [7, 8, 9 ]);    //[
                                            //  [1, 4, 7],
                                            //  [2, 5, 8]
                                            //]

array.zip([ 4, 5, 6 ], [1, 2], [8]);        //[
                                            //  [4, 1, 8],
                                            //  [5, 2, null],
                                            //  [6, null, null]
                                            //]

transpose

Transpose a multi dimensional array.

array([[1, 2, 3],[4, 5, 6]]).transpose().value();   //[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
array([[1, 2],[3, 4],[5, 6]]).transpose().value();  //[ [ 1, 3, 5 ], [ 2, 4, 6 ] ]
array([[1],[3, 4],[5, 6]]).transpose().value();     //[ [1] ]


array.transpose([[1, 2, 3],[4, 5, 6]]);             //[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
array.transpose([[1, 2],[3, 4],[5, 6]]);            //[ [ 1, 3, 5 ], [ 2, 4, 6 ] ]
array.transpose([[1],[3, 4],[5, 6]]);               //[ [1] ]

valuesAt

Gathers values and the specified indexes.


var arr = array(["a", "b", "c", "d"]);
arr.valuesAt(1, 2, 3).value();      //["b", "c", "d"]
arr.valuesAt(1, 2, 3, 4).value();   //["b", "c", "d", null]
arr.valuesAt(0, 3).value();         //["a", "d"]

arr = ["a", "b", "c", "d"];
array.valuesAt(arr, 1, 2, 3);       //["b", "c", "d"]
array.valuesAt(arr, 1, 2, 3, 4);    //["b", "c", "d", null]
array.valuesAt(arr, 0, 3);          //["a", "d"]

union

Finds the union of two arrays.

array(["a", "b", "c"]).union(["b", "c", "d"]).value();  //["a", "b", "c", "d"]);
array(["a"]).union(["b"], ["c"], ["d"], ["c"]).value(); //["a", "b", "c", "d"]);

array.union(["a", "b", "c"], ["b", "c", "d"]);          //["a", "b", "c", "d"]);
array.union(["a"], ["b"], ["c"], ["d"], ["c"]);         //["a", "b", "c", "d"]);

intersect

Finds the intersection of two arrays.

array([1, 2]).intersect([2, 3], [2, 3, 5]).value();                     //[2]);
array([1, 2, 3]).intersect([2, 3, 4, 5], [2, 3, 5]).value();            //[2, 3]);
array([1, 2, 3, 4]).intersect([2, 3, 4, 5], [2, 3, 4, 5]).value();      //[2, 3, 4]);
array([1, 2, 3, 4, 5]).intersect([1, 2, 3, 4, 5], [1, 2, 3]).value();   //[1, 2, 3]);
array([[1, 2, 3, 4, 5],[1, 2, 3, 4, 5],[1, 2, 3]]).intersect().value(); //[1, 2, 3]);

array.intersect([1, 2], [2, 3], [2, 3, 5]);                             //[2]
array.intersect([1, 2, 3], [2, 3, 4, 5], [2, 3, 5]);                    //[2, 3]
array.intersect([1, 2, 3, 4], [2, 3, 4, 5], [2, 3, 4, 5]);              //[2, 3, 4]
array.intersect([1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3]);           //[1, 2, 3]);
array.intersect([[1, 2, 3, 4, 5],[1, 2, 3, 4, 5], [1, 2, 3]]);          //[1, 2, 3]);

powerSet

Finds the powerset of a given array.

array([1, 2, 3]).powerSet().value();
array.powerSet([1, 2, 3]);
//[
//  [],
//  [ 1 ],
//  [ 2 ],
//  [ 1, 2 ],
//  [ 3 ],
//  [ 1, 3 ],
//  [ 2, 3 ],
//  [ 1, 2, 3 ]
//]

cartesian

Finds the cartesian product of arrays.

array([1, 2]).cartesian([2, 3]).value();
array.cartesian([1, 2], [2, 3]);
//[
//  [1, 2],
//  [1, 3],
//  [2, 2],
//  [2, 3]
//]

compact

Compacts the values of an array.

array([1, null, null, x, 2]).compact().value(); //[1, 2]

array([1, 2]).compact().value();  //[1, 2]


array.compact([1, null, null, x, 2]); //[1, 2]
array.compact([1, 2]); //[1, 2]

multiply

Repoduces the values in an array the given number of times.

array([1, 2]).multiply(2).value(); //[1, 2, 1, 2, 1, 2]

array.multiply([1, 2, 3], 2); //[1, 2, 3, 1, 2, 3]

flatten

Flatten multiple arrays into a single array.


array([ [1], [2], [3] ]).flatten().value(); //[1, 2, 3]

array.flatten([1, 2], [2, 3], [3, 4]); //[1, 2, 2, 3, 3, 4]

pluck

Pluck properties from values in an array.

var arr = [
    {name: {first: "Fred", last: "Jones"}, age: 50, roles: ["a", "b", "c"]},
    {name: {first: "Bob", last: "Yukon"}, age: 40, roles: ["b", "c"]},
    {name: {first: "Alice", last: "Palace"}, age: 35, roles: ["c"]},
    {name: {first: "Johnny", last: "P."}, age: 56, roles: []}
];

array.pluck(arr, "name.first"); //["Fred", "Bob", "Alice", "Johnny"]
array(arr).pluck("age"); //[50, 40, 35, 56]

invoke

Invokes the specified method on each value in an array.


function person(name, age) {
    return {
        getName: function () {
            return name;
        },

        getOlder: function () {
            age++;
            return this;
        },

        getAge: function () {
            return age;
        }
    };
};

var arr = [person("Bob", 40), person("Alice", 35), person("Fred", 50), person("Johnny", 56)];

array.invoke(arr, "getName"); //["Bob", "Alice", "Fred", "Johnny"];
array(arr).invoke("getName").value(); //["Bob", "Alice", "Fred", "Johnny"];

array(arr).invoke("getOlder").invoke("getAge").value(); //[41, 36, 51, 57];