0.4.0 • Published 9 years ago

array-etc v0.4.0

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

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.

0.4.0

9 years ago

0.3.0

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago

0.0.0

9 years ago