Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

are_same

oatkiller edited this page Sep 13, 2010 · 4 revisions

are_same compares passed arguments and returns true if they are ‘the same’. The same means different things for different arguments. Its not the same as == or ===.

(function () {

// these are the exactly the same. 
// anything the compares the same with === is the same in are_same
o.are_same(1,1); // true. 

// you can compare lots of parameters.
o.are_same('foo','foo','foo','foo'); // true. 

// for objects, o.are_same recurses into the object and compares their properties.
o.are_same([1,2,3],[1,2,3],[1,2,3]); // true. 

// even those these arrays have the same elements, they arent in the same order.
// property names have to match as well.
o.are_same([1,3,2],[1,2,3]); // false.

// objects have to have the same properties. cant be missing any.
o.are_same([1,2],[1,2,3]); // false. 

// are_same can recurse into objects
o.are_same({
	name: 'robert',
	age: 23,
	favorites: {
		book: 'Wuthering Heights',
		album: 'Bricolages - Ryuichi Sakamoto',
		tea: 'jasmine'
	}
},{
	name: 'robert',
	age: 23,
	favorites: {
		book: 'Wuthering Heights',
		album: 'Bricolages - Ryuichi Sakamoto',
		tea: 'jasmine'
	}
}}; // true. 

//  i changed my favorite book to War & Peace in one record, so now these aren't the same.
o.are_same({
	name: 'robert',
	age: 23,
	favorites: {
		book: 'War & Peace',
		album: 'Bricolages - Ryuichi Sakamoto',
		tea: 'jasmine'
	}
},{
	name: 'robert',
	age: 23,
	favorites: {
		book: 'Wuthering Heights',
		album: 'Bricolages - Ryuichi Sakamoto',
		tea: 'jasmine'
	}
}}; // false

})();
Clone this wiki locally