From 680fe41ed7410dc9aba5986133abb04b5c48e7bb Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 23 Jun 2014 08:09:59 +0100 Subject: [PATCH] Add isEqual() method to tiddlers --- core/modules/tiddler.js | 50 ++++++- editions/test/tiddlers/tests/test-tiddler.js | 137 +++++++++++++++++++ 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 editions/test/tiddlers/tests/test-tiddler.js diff --git a/core/modules/tiddler.js b/core/modules/tiddler.js index 6529970d3..430e831fe 100644 --- a/core/modules/tiddler.js +++ b/core/modules/tiddler.js @@ -18,7 +18,7 @@ exports.hasTag = function(tag) { exports.isPlugin = function() { return this.fields.type === "application/json" && this.hasField("plugin-type"); -} +}; exports.isDraft = function() { return this.hasField("draft.of"); @@ -57,4 +57,52 @@ 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) { + 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; +}; + })(); diff --git a/editions/test/tiddlers/tests/test-tiddler.js b/editions/test/tiddlers/tests/test-tiddler.js new file mode 100644 index 000000000..f85ee9c6a --- /dev/null +++ b/editions/test/tiddlers/tests/test-tiddler.js @@ -0,0 +1,137 @@ +/*\ +title: test-tiddlers.js +type: application/javascript +tags: [[$:/tags/test-spec]] + +Tests the tiddler object + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +describe("Tiddler tests", function() { + + function compareTiddlers(fieldsA,fieldsB,excludeFields) { + var tiddlerA = new $tw.Tiddler(fieldsA), + tiddlerB = new $tw.Tiddler(fieldsB); + return tiddlerA.isEqual(tiddlerB,excludeFields); + } + + // Our tests + + it("should compare identical tiddlers", function() { + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + })).toEqual(true); + }); + + it("should compare different tiddlers", function() { + expect(compareTiddlers({ + title: "HelloThere2", + text: "one", + tags: ["one","two","three"] + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + })).toEqual(false); + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","three"] + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + })).toEqual(false); + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","two","three"], + caption: "Test" + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + })).toEqual(false); + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"], + caption: "Test" + })).toEqual(false); + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + },{ + title: "HelloThere", + text: "one" + })).toEqual(false); + }); + + it("should compare different tiddlers with exclusions", function() { + expect(compareTiddlers({ + title: "HelloThere2", + text: "one", + tags: ["one","two","three"] + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + },["title"])).toEqual(true); + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","three"] + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + },["tags"])).toEqual(true); + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","two","three"], + caption: "Test" + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + },["caption"])).toEqual(true); + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + },{ + title: "HelloThere", + text: "one", + tags: ["one","two","three"], + caption: "Test" + },["caption"])).toEqual(true); + expect(compareTiddlers({ + title: "HelloThere", + text: "one", + tags: ["one","two","three"] + },{ + title: "HelloThere", + text: "one" + },["tags"])).toEqual(true); + }); + +}); + +})();