diff --git a/boot/boot.js b/boot/boot.js index 76eeb4be3..78906fb8f 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -51,6 +51,63 @@ $tw.utils.isArray = function(value) { return Object.prototype.toString.call(value) == "[object Array]"; }; +/* +Check if an array is equal by value and by reference. +*/ +$tw.utils.isArrayEqual = function(array1,array2) { + if(array1 === array2) { + return true; + } + array1 = array1 || []; + array2 = array2 || []; + if(array1.length !== array2.length) { + return false; + } + return array1.every(function(value,index) { + return value === array2[index]; + }); +}; + +/* +Push entries onto an array, removing them first if they already exist in the array + array: array to modify (assumed to be free of duplicates) + value: a single value to push or an array of values to push +*/ +$tw.utils.pushTop = function(array,value) { + var t,p; + if($tw.utils.isArray(value)) { + // Remove any array entries that are duplicated in the new values + if(value.length !== 0) { + if(array.length !== 0) { + if(value.length < array.length) { + for(t=0; t=0; t--) { + p = value.indexOf(array[t]); + if(p !== -1) { + array.splice(t,1); + } + } + } + } + // Push the values on top of the main array + array.push.apply(array,value); + } + } else { + p = array.indexOf(value); + if(p !== -1) { + array.splice(p,1); + } + array.push(value); + } + return array; +}; + /* Determine if a value is a date */ @@ -867,6 +924,61 @@ $tw.Tiddler.prototype.hasField = function(field) { return $tw.utils.hop(this.fields,field); }; +/* +Compare two tiddlers for equality +tiddler: the tiddler to compare +excludeFields: array of field names to exclude from the comparison +*/ +$tw.Tiddler.prototype.isEqual = function(tiddler,excludeFields) { + if(!(tiddler instanceof $tw.Tiddler)) { + return false; + } + excludeFields = excludeFields || []; + var self = this, + differences = []; // Fields that have differences + // Add to the differences array + function addDifference(fieldName) { + // Check for this field being excluded + if(excludeFields.indexOf(fieldName) === -1) { + // Save the field as a difference + $tw.utils.pushTop(differences,fieldName); + } + } + // Returns true if the two values of this field are equal + function isFieldValueEqual(fieldName) { + var valueA = self.fields[fieldName], + valueB = tiddler.fields[fieldName]; + // Check for identical string values + if(typeof(valueA) === "string" && typeof(valueB) === "string" && valueA === valueB) { + return true; + } + // Check for identical array values + if($tw.utils.isArray(valueA) && $tw.utils.isArray(valueB) && $tw.utils.isArrayEqual(valueA,valueB)) { + return true; + } + // Check for identical date values + if($tw.utils.isDate(valueA) && $tw.utils.isDate(valueB) && valueA.getTime() === valueB.getTime()) { + return true; + } + // Otherwise the fields must be different + return false; + } + // Compare our fields + for(var fieldName in this.fields) { + if(!isFieldValueEqual(fieldName)) { + addDifference(fieldName); + } + } + // There's a difference for every field in the other tiddler that we don't have + for(fieldName in tiddler.fields) { + if(!(fieldName in this.fields)) { + addDifference(fieldName); + } + } + // Return whether there were any differences + return differences.length === 0; +}; + /* Register and install the built in tiddler field modules */ diff --git a/core/modules/tiddler.js b/core/modules/tiddler.js index efec78b5c..8715fedfd 100644 --- a/core/modules/tiddler.js +++ b/core/modules/tiddler.js @@ -87,57 +87,6 @@ exports.getFieldStringBlock = function(options) { return fields.join("\n"); }; -/* -Compare two tiddlers for equality -tiddler: the tiddler to compare -excludeFields: array of field names to exclude from the comparison -*/ -exports.isEqual = function(tiddler,excludeFields) { - if(!(tiddler instanceof $tw.Tiddler)) { - return false; - } - excludeFields = excludeFields || []; - var self = this, - differences = []; // Fields that have differences - // Add to the differences array - function addDifference(fieldName) { - // Check for this field being excluded - if(excludeFields.indexOf(fieldName) === -1) { - // Save the field as a difference - $tw.utils.pushTop(differences,fieldName); - } - } - // Returns true if the two values of this field are equal - function isFieldValueEqual(fieldName) { - var valueA = self.fields[fieldName], - valueB = tiddler.fields[fieldName]; - // Check for identical string values - if(typeof(valueA) === "string" && typeof(valueB) === "string" && valueA === valueB) { - return true; - } - // Check for identical array values - if($tw.utils.isArray(valueA) && $tw.utils.isArray(valueB) && $tw.utils.isArrayEqual(valueA,valueB)) { - return true; - } - // Otherwise the fields must be different - return false; - } - // Compare our fields - for(var fieldName in this.fields) { - if(!isFieldValueEqual(fieldName)) { - addDifference(fieldName); - } - } - // There's a difference for every field in the other tiddler that we don't have - for(fieldName in tiddler.fields) { - if(!(fieldName in this.fields)) { - addDifference(fieldName); - } - } - // Return whether there were any differences - return differences.length === 0; -}; - exports.getFieldDay = function(field) { if(this.cache && this.cache.day && $tw.utils.hop(this.cache.day,field) ) { return this.cache.day[field]; diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index e3390f9c7..8dba0ce0a 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -132,23 +132,6 @@ exports.count = function(object) { return Object.keys(object || {}).length; }; -/* -Check if an array is equal by value and by reference. -*/ -exports.isArrayEqual = function(array1,array2) { - if(array1 === array2) { - return true; - } - array1 = array1 || []; - array2 = array2 || []; - if(array1.length !== array2.length) { - return false; - } - return array1.every(function(value,index) { - return value === array2[index]; - }); -}; - /* Determine whether an array-item is an object-property */ @@ -161,46 +144,6 @@ exports.hopArray = function(object,array) { return false; }; -/* -Push entries onto an array, removing them first if they already exist in the array - array: array to modify (assumed to be free of duplicates) - value: a single value to push or an array of values to push -*/ -exports.pushTop = function(array,value) { - var t,p; - if($tw.utils.isArray(value)) { - // Remove any array entries that are duplicated in the new values - if(value.length !== 0) { - if(array.length !== 0) { - if(value.length < array.length) { - for(t=0; t=0; t--) { - p = value.indexOf(array[t]); - if(p !== -1) { - array.splice(t,1); - } - } - } - } - // Push the values on top of the main array - array.push.apply(array,value); - } - } else { - p = array.indexOf(value); - if(p !== -1) { - array.splice(p,1); - } - array.push(value); - } - return array; -}; - /* Remove entries from an array array: array to modify