From b9a7c49fd7b82045b7b2af8878b6ea760b38d21b Mon Sep 17 00:00:00 2001 From: Nelson Daniel Troncoso Date: Tue, 27 Nov 2018 10:14:35 -0800 Subject: [PATCH] updated object-comparison functionality. --- libraries/object-comparison/main.ts | 4 ++++ libraries/object-comparison/test/test-async.ts | 11 +++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/libraries/object-comparison/main.ts b/libraries/object-comparison/main.ts index f1dd0b0..d41a2cb 100644 --- a/libraries/object-comparison/main.ts +++ b/libraries/object-comparison/main.ts @@ -20,6 +20,10 @@ export function areSimilar(a: any, b: any, ...exceptionList: Array): boo return false; } + if ((Array.isArray(a) && !Array.isArray(b)) || (!Array.isArray(a) && Array.isArray(b))) { + return false; + } + // Object similarity is determined by the same set of keys NOT in // the exception list (although not necessarily the same order), and equivalent values for every // corresponding key NOT in the exception list. diff --git a/libraries/object-comparison/test/test-async.ts b/libraries/object-comparison/test/test-async.ts index 5053a94..b683246 100644 --- a/libraries/object-comparison/test/test-async.ts +++ b/libraries/object-comparison/test/test-async.ts @@ -28,15 +28,9 @@ import { areSimilar } from '../main'; // similar assert.strictEqual(areSimilar({ a: 4 }, { a: 4 }), true); assert.strictEqual(areSimilar({ a: 4, b: '2' }, { a: 4, b: '2' }), true); - assert.strictEqual(areSimilar(['a'], { 0: 'a' }), true); assert.strictEqual(areSimilar({ a: 4, b: '1' }, { b: '1', a: 4 }), true); // out of order assert.strictEqual(areSimilar({}, {}), true); - // TBD: is this the right behavior? if not, easy fix: check if one is array and the other is not. - assert.strictEqual(areSimilar([], {}), true); // treating arrays and objects to be similar if they have similar keys - assert.strictEqual(areSimilar(['a'], { '0': 'a' }), true); // no distinction between array and object with similar keys - assert.strictEqual(areSimilar(['a'], { 0: 'a' }), true); - // not similar assert.strictEqual(areSimilar({ a: 4 }, { a: '4' }), false); assert.strictEqual(areSimilar({ a: 4 }, { b: '4', c: '2' }), false); @@ -44,6 +38,11 @@ import { areSimilar } from '../main'; assert.strictEqual(areSimilar({ a: 4 }, { b: '4' }), false); assert.strictEqual(areSimilar([1], { 1: '0' }), false); assert.strictEqual(areSimilar([0, 1, 2, 3], [3, 2, 1, 0]), false); + + // this is strict about the type of object. + assert.strictEqual(areSimilar([], {}), false); + assert.strictEqual(areSimilar(['a'], { '0': 'a' }), false); + assert.strictEqual(areSimilar(['a'], { 0: 'a' }), false); } @test async 'objects and with filter applied'() {