node package manager

equality

A little utility for testing equality

Equality

A little utility for testing equality

Install

sudo npm install equality

Test

git clone git://github.com/apeace/equality.git
cd equality
sudo npm install
vows test/* --spec

Examples

var equality = require( 'equality' );
 
// deep 
var A = { a: { b: 'c' }, d: { e: 'f' } },
    B = { a: { b: 'c' }, d: { e: 'f' } },
    C = { x: 10, y: { e: 'f' } };
 
equality.deep( A, B );     // ==> true 
equality.deep( B, C );     // ==> false 
equality.deep( A.d, C.y ); // ==> true 
 
// deeper! 
var X = { a: A, b: B, c: C },
    Y = { a: A, b: B, c: C },
    Z = { a: C, b: A, c: B };
 
equality.deep( X, Y );     // ==> true 
equality.deep( Y, Z );     // ==> false 
equality.deep( Y.b, Z.c ); // ==> true 
 
// whoa... 
var D = [ 1, 2, 3, 1 ],
    E = [ 1, 1, 3, 2 ],
    F = [ 6, 7, 8 ];
 
equality.members( D, E ); // ==> true 
equality.members( E, F ); // ==> false 

Methods

  • deep( obj1, obj2 ) tests for deep equality between two objects
  • members( arr1, arr2 ) tests whether two arrays have the same members, regardless of order
  • hash( obj ) returns a hash string that uniquely identifies the given object (based on its value)
  • type( obj1, obj2 ) tests whether the two objects have the same type

Future

Just released this real quick because I wanted it for a project. Planning on adding some more interesting methods and lots more tests. Feel free to pull request, of course.

License

Copyright (c) 2011 Andrew Peace

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.