diff --git a/core/modules/filterrunprefixes/cascade.js b/core/modules/filterrunprefixes/cascade.js new file mode 100644 index 000000000..819221f9a --- /dev/null +++ b/core/modules/filterrunprefixes/cascade.js @@ -0,0 +1,36 @@ +/*\ +title: $:/core/modules/filterrunprefixes/cascade.js +type: application/javascript +module-type: filterrunprefix +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter prefix function +*/ +exports.cascade = function(operationSubFunction,options) { + return function(results,source,widget) { + if(results.length !== 0) { + var filterList = operationSubFunction(source,widget); + var inputResults = results.toArray(); + results.clear(); + $tw.utils.each(inputResults,function(title) { + var result = ""; // If no filter matches, we return an empty string + $tw.utils.each(filterList,function(filter) { + var output = options.wiki.filterTiddlers(filter,widget,options.wiki.makeTiddlerIterator([title])); + if(output.length !== 0) { + result = output[0]; + return false; + } + }); + results.push(result); + }); + } + } +}; + +})(); \ No newline at end of file diff --git a/editions/test/tiddlers/tests/test-prefixes-filter.js b/editions/test/tiddlers/tests/test-prefixes-filter.js index e6c418d84..7656633ed 100644 --- a/editions/test/tiddlers/tests/test-prefixes-filter.js +++ b/editions/test/tiddlers/tests/test-prefixes-filter.js @@ -288,6 +288,31 @@ describe("'reduce' and 'intersection' filter prefix tests", function() { tags: ["cakes","with tea"], text: "Does anyone eat pound cake?" }); + wiki.addTiddler({ + title: "$:/filter1", + text: "[tag[cakes]then[It is customary]]", + tags: "$:/tags/Filter" + }); + wiki.addTiddler({ + title: "$:/filter2", + text: "[tag[shopping]then[It is not customary]]", + tags: "$:/tags/Filter" + }); + wiki.addTiddler({ + title: "$:/filter3", + text: "[[Just a default]]", + tags: "$:/tags/Filter" + }); + wiki.addTiddler({ + title: "$:/tags/Filter", + list: "$:/filter1 $:/filter2 $:/filter3" + }); + + it("should handle the :cascade filter prefix", function() { + expect(wiki.filterTiddlers("[[Rice Pudding]] :cascade[all[shadows+tiddlers]tag[$:/tags/Filter]get[text]]").join(",")).toBe("It is not customary"); + expect(wiki.filterTiddlers("[[chocolate cake]] :cascade[all[shadows+tiddlers]tag[$:/tags/Filter]get[text]]").join(",")).toBe("It is customary"); + expect(wiki.filterTiddlers("[[Sparkling water]] :cascade[all[shadows+tiddlers]tag[$:/tags/Filter]get[text]]").join(",")).toBe("Just a default"); + }); it("should handle the :reduce filter prefix", function() { expect(wiki.filterTiddlers("[tag[shopping]] :reduce[get[quantity]add]").join(",")).toBe("22");