mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-09-28 15:08:46 +00:00
Add isEqual() method to tiddlers
This commit is contained in:
parent
32a1da7673
commit
680fe41ed7
@ -18,7 +18,7 @@ exports.hasTag = function(tag) {
|
|||||||
|
|
||||||
exports.isPlugin = function() {
|
exports.isPlugin = function() {
|
||||||
return this.fields.type === "application/json" && this.hasField("plugin-type");
|
return this.fields.type === "application/json" && this.hasField("plugin-type");
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.isDraft = function() {
|
exports.isDraft = function() {
|
||||||
return this.hasField("draft.of");
|
return this.hasField("draft.of");
|
||||||
@ -57,4 +57,52 @@ exports.getFieldStringBlock = function(options) {
|
|||||||
return fields.join("\n");
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
137
editions/test/tiddlers/tests/test-tiddler.js
Normal file
137
editions/test/tiddlers/tests/test-tiddler.js
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
})();
|
Loading…
Reference in New Issue
Block a user