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-etc

v0.4.0

Published

Collection of supplementary JavaScript Array methods that works across module packaging systems and can be included piece-wise

Downloads

12

Readme

Build Status npm version Bower version

A collection of handy array methods that works across the various JavaScript packaging systems, including DOM/Browser, CommonJS/Node.js, and AMD/Require.js. Methods can be included piecewise. E-mail me if you find a Array method that you think should be included because it would be useful for other people as well.

Usage

Accessing these extensions will differ depending on our module system.

Web page

If we are on Browser or AMD, we can access these methods directly from an Array object. For example, we can do:

['a', 'b','c'].equals(['a', 'b'])

Node wrapper

However, if we are on CommonJS/Node, we can access these methods through a wrapper. So instead of the call above, we would have:

array(['a', 'b','c']).equals(['a', 'b'])

where array is a constructed function that wraps around our Array of interest:

var array = require('array-etc')(['equals']);

This extra wrapper is a special arrangement we added on Node, in order to avoid global conflicts with the Array.prototype object. We may have other libraries or other versions of this library in our dependency tree. Unbeknownst to us, these libraries may add methods of similar names to the Array.prototype object. We can attach the methods locally to a wrapping function to avoid these potential collisions.

Since unintentional collisions are a lot harder with script tag loads or AMD, where the end developer actively controls the loading of the modules, we have not seen a case for extending the wrapping function to these systems as well.

Node loader

Using a wrapper on Node ensures safety. However, it does introduce a speed bump in that a extra function call must be required before the string can be operated.

If safety is not an issue, you can use the direct syntax in Node as well with a loader call. For example:

require('array-etc').load(['equals']);

This will load equals into Array.prototype

Installation

Web page

If you use bower, run

bower install array-etc

If you use npm, run

npm install array-etc

Add the corresponding script tag to your page. e.g.

<script src="/bower_components/array-etc/lib/equals.js"></script>

API

Node

In your shell, run

npm install array-etc

In your file, write

var arrload = require('array-etc');
var arrwrap = arrload(['equals']);

This creates a custom wrapper function, which you can use to access the individual methods.

Library Files

lib/equals.js

This defines a customizable comparison method anchored at Array.prototype.equals for simple arrays . This universal implementation does not use JSON.stringify, which is problematic for arrays containing functions.

Comparison is as simple as:

['a', 'b','c'].equals(['a', 'b'])

Because the individual comparison function uses the triple equals operator, Array.prototype.equals has the following special-case behaviors:

  • Objects that look the same, but were created at different points will NOT be equal.
  • Functions that look the same, but were created at different points will NOT be equal.

Of course, if you don't like this behavior, you can modify it. You just have to provide your own comparison function. For example, if you want to use JSON.stringify for the individual comparisons, you can write:

['a', 'b','c'].equals(['a', 'b'], function(a,b) {
    return JSON.stringify(a) == JSON.stringify(b);
});

And if you don't want to pass in this new comparison function every time, you can replace it with:

Array.prototype.equals.eq = function(a,b) {
    return JSON.stringify(a) == JSON.stringify(b);
}

On Node, this would be:

var array = require('array-etc')(['equals']);

array.equals.eq = function(a,b) {
    return JSON.stringify(a) == JSON.stringify(b);
}

Limitations

This implementation was designed for simple arrays. It should work for DAG nested arrays as well. However, it might not return for complex arrays with circular dependencies.

Technical Support

E-mail me if you have problems or questions.