Fork me on GitHub
Ignitersworld Lab

coequal

A small utility function to check equality of all data types and objects in JavaScript.

coequal.js

coequal is a small utility function to check all data types and objects in javascript. coequal perform a deep comparison to find equality of data and objects.

For different objects conditions are different. Let see all senarios.

1. Array : Array of any data types are supported for comparision. For ex:


var a=[1,2,3] ,b=[1,2,3];
coequal(a,b) //returns true;

var a=[{b:"b",d:"d"} ,{e:"e",f:"f"}] ,b=[{b:"b",d:"d"} ,{e:"e",f:"f"}];
coequal(a,b) //returns true;

Javascript array also support array of different dataType in same array (But making array this way is not suggested.)

var a=[1,{e:"e",f:"f"}] ,b=[1,{e:"e",f:"f"}];
coequal(a,b) //returns true

If for you order of element does not matter for comparision

var a=[1,2,3] ,b=[1,3,2]
coequal(a,b,{orderCheck:false}) //returns true

2. Object : In object it checks the key, value pair must be same and equal upto the end level of json.

var a={
    "k1":"b",
    "k2":{
    	"k3":"abc"
    }
}
var b={
    "k2":{
    	"k3":"abc"
    },
    "k1":"b"
}
coequal(a,b) //returns true

3.Function : There are two options for comparing function. Compare by code compare by constructor. Default is constructor.

var a=function(){
	alert("a");
}
var b=function(){
	alert("a");
}
var c=a;
coequal(a,b) //return false;
coequal(a,b,{functionCheck:"code"}) //return true
coequal(a,c) //return true;

4. Dom object : Document object can be of two type

i. Collection

ii. Element

If you want to compare collection to collection or collection to element you need to activate collectionCheck in option. ie : option will be : { collectionCheck :true} You can compare svg and canvas element also.


<img src="images/image.png" class="image"  id="myimage"/>

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<circle id="mycircle" cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
</svg>
var img1=document.getElementsByClassName('image'); var img2=document.getElementById(' myimage '); var img3=document.getElementsByTagName('img'); var cir=document.getElementById("mycircle"); var cir2=document.getElementById("mycircle"); coequal(img1, img3) //return false; coequal(img1, img2) //return false; coequal(img1, img3,{collectionCheck:true}) //return true; coequal(img1, img2,{collectionCheck:true}) //return true; coequal(cir, cir2) //return true;

5. Date object : Date object are compared by milliseconds from those objects.

var date1=new Date('05/03/2013');
var date2=new Date('05/03/2013');
coequal(date1, date2) //return true;

6. Regex object : Rexed object are converted to string and compared.

var rg1=/[1-9]/g;
var rg2=/[1-9]/g;
var rg3=new RegExp('[1-9]','g');

coequal(rg1, rg2) //return true;
coequal(rg1, rg3) //return true;

7. string or number : For string or number dont use this method because they are directly comparable. But even though you can compare it.

coequal(2345,2345 ) //return true;
coequal('abcd', 'abcd') //return true;

Options

Option Allowed values Default Description
functionCheck code,constructor (string) constructor If setted as code it only checks the code within the function. It does not check the protypes.
As constructor it check only by refrence.
Used if you want to compare function.
orderCheck true,false (boolean) true If true , the array element must be on same order than only it is treated as equal. If false elements can be on any order.
collectionCheck true,false(boolean) false If you want to compare collection to collection or collection to element you need to set it as true.


Limitation


  • Object must not be cyclic object, else it ill go on infinite loop.

Want to support and contribute to coequal?

coequal needs your contribution to become best of its kind. There are lot of things you can do to contribute and support plugin.

Author

This plugin is developed and maintained by me ( Sudhanshu Yadav ignitersworld.com/me ) . If you have any query you can contact me at sudhanshuyadav2@gmail.com.

You can also follow me on twitter and github Follow @s-yadav .

Check out my other open source work