Code coverage report for node-sameValuesAs\same-values-as.js

Statements: 80.65% (75 / 93)      Branches: 73.49% (61 / 83)      Functions: 100% (11 / 11)      Lines: 86.3% (63 / 73)      Ignored: 1 branch     

All files » node-sameValuesAs\ » same-values-as.js
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 133 134 135 136 137 138 139 140 141 142 143  1   1 33 33 33 16       1 30     1 68 45   42     1 57 57 57 17     1   42   42 18     42 40   30   29 29   29                           29 29 29             4     25 25 10         10 10       25   25 46 46   46               38   14     38     16     1 25 25 307 23     1 58 58     1 124     1 29 10 10           1 87     1 1        
"use strict"
var global, exports;
 
function sortObject(a,b) {
    var sa = JSON.stringify(a).split('').sort().join('');
    var sb = JSON.stringify(b).split('').sort().join('');
    if (sa < sb) return -1;
    Eif (sa > sb) return 1;
    return 0;
}
 
function sameValuesAs (actual, expected) {
    return recurseObject(actual, expected, '');
}
 
function recurseObject(actual, expected, message) {
    if (actual === expected) return true;
    if (dateLike(actual) && (dateLike(expected) === dateLike(actual))) return true; // optimistic date comparison
 
    return objectSameValues(actual, expected, message);
}
 
function dateLike(val) {
    Iif (val instanceof Date) return val.getTime();
    var maybeDate = Date.parse(val);
    if (Number.isNaN(maybeDate)) return undefined;
    return maybeDate;
}
 
function objectSameValues(a, b, message) {
 
    Iif (message === '0') message = ''; 
 
    if (message !== '' && message !== undefined) {
        message = message + ' ';
    }
 
    if (isUndefinedOrNull(a) && isUndefinedOrNull(b)) return true;
    if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) throw new Error(message + 'Object instances do not match');
 
    if (a.prototype !== b.prototype) throw new Error(message + 'Object prototypes are not equal');
 
    Iif (isArguments(a) !== isArguments(b)) throw new Error(message + 'One of the objects is not a function');
    Iif (isArguments(a) && isArguments(b)) return recurseObject(pSlice.call(a), pSlice.call(b), message);
 
    Iif (isBuffer(a)) {
        if (!isBuffer(b)) throw new Error(message + 'One of the objects is not a buffer');
        if (a.length !== b.length) throw new Error(message + 'The buffer lengths do not match');
        for (i = 0; i < a.length; i++) {
            if (a[i] !== b[i]) { 
                throw new Error(message + 'The buffer values do not match');
            }
        }
        return true;
    }
 
    // We now either have an array or an object.
    // We want to match arrays regardless of order ('cos we're mad)
    
    var ka,kb;
    try {
        ka = Object.keys(a),
        kb = Object.keys(b);
    } 
    catch (e) {
 
        //happens when one is a string literal and the other isn't
        // happens when two primitives don't match, ie. 1 and 2
        throw new Error(message + 'expected ' + '\''+ b + '\'' + ' does not match actual ' + '\'' + a + '\'');;
    }
 
    var arrays = Array.isArray(a);
    if (arrays) {
        Iif (! Array.isArray(b)) {
            throw new Error(message + ' One of the objects is not an array');
        }
 
        // sort both arrays for testing below
        a.sort(sortObject); // this is very expensive,
        b.sort(sortObject); // but does the trick for just about anything.
    }
 
    // test object or array deeply
    var allKeys = toUnique(ka.concat(kb));
 
    for (var i = allKeys.length - 1; i >= 0; i--) {
        var key = allKeys[i];
        message = key;
 
        if (isEmptyOrUndefined(a[key]) && isEmptyOrUndefined(b[key])) {
 
            // this is a weird special case for my own purposes.
            // An empty array on an object is considered the same as
            // a missing value, but not the same as null
        }
        else {
 
            if (arrays) {
                // If we have an array then the key is the position in the array
                message = "Array position " + message;
            }
 
            recurseObject(a[key], b[key], message)
        }
    }
    return true;
}
 
function toUnique(a){
    var c,b=a.length;
    if (b==0)return a;
    while(c=--b)while(c--)a[b]!==a[c]||a.splice(c,1);
    return a;
}
 
function isEmptyOrUndefined(value) {
    Iif (value === null) return false;
    return value === undefined || value.length === 0;
}
 
function isUndefinedOrNull(value) {
    return value === null || value === undefined;
}
 
function isBuffer (x) {
    if (!x || typeof x !== 'object' || typeof x.length !== 'number') return false;
    Eif (typeof x.copy !== 'function' || typeof x.slice !== 'function') {
        return false;
    }
    if (x.length > 0 && typeof x[0] !== 'number') return false;
    return true;
}
 
function isArguments(x) {
	return typeof x === "object" && ( "callee" in x ) && typeof x.length === "number";
}
 
(function (provides) {
    provides.compare = sameValuesAs;
/* istanbul ignore next */ // `this` branch doesn't get followed
})(global || exports || this);