|
|
@ -131,9 +131,9 @@ function deepCopy (obj) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Utility function for comparing things |
|
|
|
* Utility functions for comparing things |
|
|
|
* Works for numbers, strings and booleans |
|
|
|
|
|
|
|
* Assumes type checking was already done (a and b already have the same type) |
|
|
|
* Assumes type checking was already done (a and b already have the same type) |
|
|
|
|
|
|
|
* compareNSB works for numbers, strings and booleans |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function compareNSB (a, b) { |
|
|
|
function compareNSB (a, b) { |
|
|
|
if (a < b) { return -1; } |
|
|
|
if (a < b) { return -1; } |
|
|
@ -141,11 +141,25 @@ function compareNSB (a, b) { |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function compareArrays (a, b) { |
|
|
|
|
|
|
|
var i, comp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < Math.min(a.length, b.length); i += 1) { |
|
|
|
|
|
|
|
comp = compareThings(a[i], b[i]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (comp !== 0) { return comp; } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Common section was identical, longest one wins
|
|
|
|
|
|
|
|
return compareNSB(a.length, b.length); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Compare things |
|
|
|
* Compare things |
|
|
|
* Things are defined as any native types (string, number, boolean, null, date) and objects |
|
|
|
* Things are defined as any native types (string, number, boolean, null, date) and objects |
|
|
|
* In the case of objects and arrays, we compare the serialized versions |
|
|
|
* In the case of objects and arrays, we compare the serialized versions |
|
|
|
* If two objects dont have the same type, the (arbitrary) type hierarchy is: null, number, strings, boolean, dates, objects, arrays |
|
|
|
* If two objects dont have the same type, the (arbitrary) type hierarchy is: null, number, strings, boolean, dates, arrays, objects |
|
|
|
* Return -1 if a < b, 1 if a > b and 0 if a = b (note that equality here is NOT the same as defined in areThingsEqual!) |
|
|
|
* Return -1 if a < b, 1 if a > b and 0 if a = b (note that equality here is NOT the same as defined in areThingsEqual!) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function compareThings (a, b) { |
|
|
|
function compareThings (a, b) { |
|
|
@ -168,6 +182,10 @@ function compareThings (a, b) { |
|
|
|
// Dates
|
|
|
|
// Dates
|
|
|
|
if (util.isDate(a)) { return util.isDate(b) ? compareNSB(a.getTime(), b.getTime()) : -1; } |
|
|
|
if (util.isDate(a)) { return util.isDate(b) ? compareNSB(a.getTime(), b.getTime()) : -1; } |
|
|
|
if (util.isDate(b)) { return util.isDate(a) ? compareNSB(a.getTime(), b.getTime()) : 1; } |
|
|
|
if (util.isDate(b)) { return util.isDate(a) ? compareNSB(a.getTime(), b.getTime()) : 1; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Arrays (first element is most significant and so on)
|
|
|
|
|
|
|
|
if (util.isArray(a)) { return util.isArray(b) ? compareArrays(a, b) : -1; } |
|
|
|
|
|
|
|
if (util.isArray(b)) { return util.isArray(a) ? compareArrays(a, b) : 1; } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|