Permalink
Browse files

Move `areEqual` function to separate project.

  • Loading branch information...
1 parent 474cdaa commit ef2f1ee2f5a98a264d457923b70c48b2bc1520f4 @dervus dervus committed Dec 6, 2012
Showing with 0 additions and 191 deletions.
  1. +0 −80 test/support/common.js
  2. +0 −6 test/units.js
  3. +0 −6 test/units/support.js
  4. +0 −99 test/units/support/common.js
@@ -9,84 +9,6 @@ var _common = module.exports = {};
$$.extend(_common, $$);
-function kindOf(object) {
- return /\[object (.+?)\]/.exec(Object.prototype.toString.call(object))[1];
-}
-
-
-function areEqual(value1, value2) {
- var kind1 = kindOf(value1),
- kind2 = kindOf(value2);
-
- if (kind1 != kind2) {
- return false;
- } else if (Number.isNaN(value1) && Number.isNaN(value2)) {
- return true;
- } else {
- switch (kind1) {
- case 'Object': return areEqualObjects(value1, value2);
- case 'Array': return areEqualArrays(value1, value2);
- case 'Date': return value1.getTime() === value2.getTime();
- default: return value1 === value2;
- }
- }
-}
-
-
-function areEqualArrays(array1, array2) {
- var index, length;
-
- if (array1.length !== array2.length) {
- return false;
- }
-
- for (index = 0, length = array1.length; index < length; index += 1) {
- if (!areEqual(array1[index], array2[index])) {
- return false;
- }
- }
-
- return true;
-}
-
-
-function areEqualObjects(object1, object2) {
- var keys1, keys2, index, length, currentKey;
-
- if (Object.getPrototypeOf(object1) !== Object.getPrototypeOf(object2)) {
- return false;
- }
-
- keys1 = Object.keys(object1);
- keys2 = Object.keys(object2);
-
- if (keys1.length !== keys2.length) {
- return false;
- }
-
- keys1.sort();
- keys2.sort();
-
- length = keys1.length;
-
- for (index = 0; index < length; index += 1) {
- if (keys1[index] !== keys2[index]) {
- return false;
- }
- }
-
- for (index = 0; index < length; index += 1) {
- currentKey = keys1[index];
-
- if (!areEqual(object1[currentKey], object2[currentKey])) {
- return false;
- }
- }
-
- return true;
-}
-
-
function makeClassConstructor(Class, params) {
var mapKeys = params.map || {},
requiredKeys = params.required || [],
@@ -120,6 +42,4 @@ function makeClassConstructor(Class, params) {
}
-_common.kindOf = kindOf;
-_common.areEqual = areEqual;
_common.makeClassConstructor = makeClassConstructor;
View
@@ -1,6 +0,0 @@
-'use strict';
-
-
-describe('Code units.', function () {
- require('./units/support');
-});
@@ -1,6 +0,0 @@
-'use strict';
-
-
-describe('Test support.', function () {
- require('./support/common');
-});
@@ -1,99 +0,0 @@
-'use strict';
-
-
-var assert = require('assert');
-var _common = require('../../support/common');
-
-
-describe('Common.', function () {
- describe('#areEqual()', function () {
- var test = _common.areEqual;
-
- it('should return false when the passed objects are of different types', function () {
- assert(!test(42, 'answer'));
- assert(!test(false, null));
- });
-
- it('should return true when both of the passed objects are NaN', function () {
- assert(test(NaN, NaN));
- assert(!test(NaN, 42));
- assert(!test(null, NaN));
- });
-
- it('should treat numbers', function () {
- assert(test(-346, -346));
- assert(test(0, 0));
- assert(test(2376, 2376));
- assert(test(-672.234, -672.234));
- assert(test(9.358546212888048e-14, 9.358546212888048e-14));
- assert(!test(5, -5));
- assert(!test(8234, 7823));
- assert(!test(-564.23466, 0.236852109));
- assert(!test(9.358546212888048e-14, 9.358546212888047e-14));
- });
-
- it('should treat strings', function () {
- assert(test('hello world', 'hello world'));
- assert(!test('john', 'joe'));
- });
-
- it('should treat flat arrays', function () {
- assert(test([1, 2, 3, 4], [1, 2, 3, 4]));
- assert(!test([2, 1, 4], [1, 2, 4]));
- });
-
- it('should treat nested arrays', function () {
- assert(test([5, 6, [9, 3], 7], [5, 6, [9, 3], 7]));
- assert(!test([7, 7, [3, 8, 1], 1], [7, 7, [], 1]));
- });
-
- it('should treat flat hash-like objects', function () {
- var object = {
- x: 12,
- y: 34,
- z: -3
- };
-
- assert(test(object, { x: 12, y: 34, z: -3 }));
- assert(test(object, { z: -3, x: 12, y: 34 }));
- assert(!test(object, { x: 12, y: 34, z: -3, trash: 0 }));
- assert(!test(object, { x: 12, y: 34 }));
- });
-
- it('should treat nested hash-like objects', function () {
- var object = {
- foo: 'hello',
- bar: {
- baz: 42
- }
- };
-
- assert(test(object, { bar: { baz: 42 }, foo: 'hello' }));
- assert(!test(object, { foo: 'hello' }));
- assert(!test(object, { bar: { baz: 42 } }));
- assert(!test(object, { bar: { baz: null }, foo: 'hello' }));
- });
-
- it('should treat constructed objects', function () {
- function Foo(x, y) {
- this.x = x;
- this.y = y;
- }
-
- function Bar(text) {
- this.text = text;
- }
-
- function Baz() {
- Bar.apply(this, arguments);
- }
- _common.inherits(Baz, Bar);
-
- assert(test(new Foo(1, 2), new Foo(1, 2)));
- assert(!test(new Foo(1, 2), new Foo(1, 4)));
- assert(test(new Bar('hello'), new Bar('hello')))
- assert(!test(new Bar('snow'), new Bar('stone')))
- assert(!test(new Bar('world'), new Baz('world')))
- });
- });
-});

0 comments on commit ef2f1ee

Please sign in to comment.