All files / assert/deep-equal/lib main.js

100% Statements 132/132
100% Branches 35/35
100% Functions 1/1
100% Lines 132/132

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 85x 80x 80x 5x 5x 63x 148x 2x 2x 148x 12x 12x 148x 4x 4x 148x 4x 4x 148x 9x 1x 1x 9x 148x 5x 3x 3x 5x 4x 1x 1x 4x 1x 1x 35x 35x 148x 2x 2x 33x 33x 33x 33x 148x 58x 2x 2x 58x 31x 148x 42x 42x 11x 11x 42x 20x 148x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var objectKeys = require( '@stdlib/utils/keys' );
var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' );
var isDate = require( '@stdlib/assert/is-date-object' );
var isError = require( '@stdlib/assert/is-error' );
var isBuffer = require( '@stdlib/assert/is-buffer' );
var isRegExp = require( '@stdlib/assert/is-regexp' );
 
 
// MAIN //
 
/**
* Tests for deep equality between two values.
*
* @param {*} a - first comparison value
* @param {*} b - second comparison value
* @returns {boolean} boolean indicating if `a` is deep equal to `b`
*
* @example
* var bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, 3 ] );
* // returns true
*
* @example
* var bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, '3' ] );
* // returns false
*
* @example
* var bool = deepEqual( { 'a': 2 }, { 'a': [ 2 ] } );
* // returns false
*
* @example
* var bool = deepEqual( [], {} );
* // returns false
*
* @example
* var bool = deepEqual( null, null );
* // returns true
*/
function deepEqual( a, b ) {
	var aKeys;
	var bKeys;
	var typeA;
	var typeB;
	var key;
	var i;
 
	typeA = typeof a;
	typeB = typeof b;
	if ( a === null || typeA !== 'object' ) {
		if ( b === null || typeB !== 'object' ) {
			return a === b;
		}
		return false;
	}
	// Case: `a` is of type 'object'
	if ( typeB !== 'object' ) {
		return false;
	}
	if ( getPrototypeOf( a ) !== getPrototypeOf( b ) ) {
		return false;
	}
	if ( isDate( a ) ) {
		return a.getTime() === b.getTime();
	}
	if ( isRegExp( a ) ) {
		return a.source === b.source && a.flags === b.flags;
	}
	if ( isError( a ) ) {
		if ( a.message !== b.message || a.name !== b.name ) {
			return false;
		}
	}
	if ( isBuffer( a ) ) {
		if ( a.length !== b.length ) {
			return false;
		}
		for ( i = 0; i < a.length; i++ ) {
			if ( a[ i ] !== b[ i ] ) {
				return false;
			}
		}
		return true;
	}
	aKeys = objectKeys( a );
	bKeys = objectKeys( b );
	if ( aKeys.length !== bKeys.length ) {
		return false;
	}
	aKeys.sort();
	bKeys.sort();
 
	// Cheap key test:
	for ( i = 0; i < aKeys.length; i++ ) {
		if ( aKeys[ i ] !== bKeys[ i ] ) {
			return false;
		}
	}
	// Possibly expensive deep equality test for each corresponding key:
	for ( i = 0; i < aKeys.length; i++ ) {
		key = aKeys[ i ];
		if ( !deepEqual( a[ key ], b[ key ] ) ) {
			return false;
		}
	}
	return typeA === typeB;
}
 
 
// EXPORTS //
 
module.exports = deepEqual;