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

xrray

v4.7.9

Published

Some utility to deal with Arrays

Downloads

122

Readme

Xrray

Eases the handling of Arrays.

Intro

Attaches some functions to a given Constructor extending (or being) Array. Every added manipulating function has an "first letter in caps" equivalent that initializes a new Array within which the changes take place, thus not changing the initial one. Every manipulating function returns itself.

Examples

Setup

    const xrray = require("xrray");
    //Xrray is getting assigned to the default Constructor
    let Xrray = xrray();

Usage

    let a = new Xrray("a", undefined, "b", "c", null, "d");
    console.log(a.Clear());   //Xrray ["a", "b", "c", "d"]
    console.log(a);           //Xrray ["a", undefined, "b", "c", null, "d"]

    a.clear().add("e", "f");
    console.log(a);           //Xrray ["a", "b", "c", "d", "e", "f"]

    console.log(a.set(["d", "c", "b", "a"]).Reverse());   //Xrray ["a", "b", "c", "d"]
    console.log(a);                                       //Xrray ["d", "c", "b", "a"]

    a.set([1, 2, 3, "a", "b", "c"]);

    console.log(a.index("a", "b"));     //Xrray [3, 4]
    console.log(a.Get(1, 2, 3));        //Xrray [2, 3, "a"]

    //Detected as values
    console.log(a.Remove("a", "c"));    //Xrray [1, 2, 3, "b"]
    //Detected as Indexes
    console.log(a.Remove(1, 2));        //Xrray [1, "a", "b", "c"]

    //Force Type value
    console.log(a.RemoveV(1, 2));       //Xrray [3, "a", "b", "c"]
    //Force Type index
    console.log(a.RemoveI(1, 2));       //Xrray [1, "a", "b", "c"]

    //Detected as value
    console.log(a.Swap(2, "b"));        //Xrray [1, "b", 3, "a", 2, "c"]
    //Detected as indexes
    console.log(a.Swap(1, 3));          //Xrray [1, "a", 3, 2, "b", "c"]

    //(everything! not just the second pair) Detected as values
    console.log(a.Swap([1, 2], [2, "c"]));            //Xrray ["c", 1, 3, "a", "b", 2]
    //Would be equal to
    console.log(a.SwapV([1], [2]).swapV(2, "c"));     //Xrray ["c", 1, 3, "a", "b", 2]

    console.log(a.Rem(3));        //Xrray [1, 2, 3]
    //Spelled backwards... thus rem backwards
    console.log(a.Mer(3));        //Xrray ["a", "b", "c"]

    console.log(a.set("middle")); //Xrray ["middle"]

    console.log(a.Add("last"));   //Xrray ["middle", "last"]
    console.log(a.Dda("first"));  //Xrray ["first", "middle"]

    console.log(a.empty);         //false
    console.log(a.Set().empty);   //true

Exceptions

    //note that xrray is written small here (thus it is the required package)
    const Exception = xrray.Exception;
    //The following Exceptions all inherit from xrray.Exception
    const IndexOutOfBoundsException = xrray.IndexOutOfBoundsException;
    const InvalidIntegerException = xrray.InvalidIntegerException;
    const InvalidInputException = xrray.InvalidInputException;
    const InvalidConstructorException = xrray.InvalidConstructorException;
    const InvalidValueException = xrray.InvalidValueException;

    let a = new Xrray("a", "b", "c");

    try {
      a.index("w");
    } catch (e) {
      if(e instanceof InvalidValueException){
        console.log(e.message);           //InvalidValueException: Unable to find given value: String w; within: a,b,c;
        console.log("Value:" + e.value);  //Value: w
        console.log("Array:" + e.array);  //Array: a,b,c
      }
    }
    try {
      a.RemoveI("first");
    } catch (e) {
      if(e instanceof InvalidIntegerException){
        console.log(e.message);           //InvalidIntegerException: Given value must be an Integer.
      }
    }
    try {
      a.RemoveI(5);
    } catch (e) {
      if(e instanceof IndexOutOfBoundsException){
        console.log(e.message);           //IndexOutOfBoundsException: Given value "5" must be in range 0-2.
      }
    }
    try {
      a.swapI(["a"],["b","c"])
    } catch (e) {
      if(e instanceof InvalidInputException){
        console.log(e.message);           //InvalidInputException: Given input is invalid.
                                          //Parameter i1 and i2 must ether be two indexes, or two index-Arrays of the same length.
      }
    }
    try {
      let Xrray2 = xrray(class NotAnArray {constructor() {}});
    } catch (e) {
      if(e instanceof InvalidConstructorException){
        console.log(e.message);
      }
    }

Test

    //To test if xrray has been installed on a constructor call:
    console.log(Xrray.xrray);           //true
    Xrray.prototype.add = undefined;
    console.log(Xrray.xrray);           //false

    //To test if xrray has been installed on an instance call:
    let a = new Xrray();
    console.log(a.xrray);         //true
    a.add = undefined;
    console.log(a.xrray);         //false

API

Comming soon...