diff --git a/core/modules/filterrunprefixes/filter.js b/core/modules/filterrunprefixes/filter.js index 726732c8e..72b7ce24c 100644 --- a/core/modules/filterrunprefixes/filter.js +++ b/core/modules/filterrunprefixes/filter.js @@ -13,12 +13,12 @@ module-type: filterrunprefix /* Export our filter function */ -exports.filter = function(operationSubFunction) { +exports.filter = function(operationSubFunction,options) { return function(results,source,widget) { if(results.length > 0) { var resultsToRemove = []; $tw.utils.each(results,function(result) { - var filtered = operationSubFunction($tw.wiki.makeTiddlerIterator([result]),widget); + var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([result]),widget); if(filtered.length === 0) { resultsToRemove.push(result); } diff --git a/editions/test/tiddlers/tests/test-prefixes-filter.js b/editions/test/tiddlers/tests/test-prefixes-filter.js new file mode 100644 index 000000000..7c88aeaf2 --- /dev/null +++ b/editions/test/tiddlers/tests/test-prefixes-filter.js @@ -0,0 +1,113 @@ +/*\ +title: test-prefixes-filters.js +type: application/javascript +tags: [[$:/tags/test-spec]] +Tests the reduce prefix and filter. +\*/ +(function(){ + +/* jslint node: true, browser: true */ +/* eslint-env node, browser, jasmine */ +/* eslint no-mixed-spaces-and-tabs: ["error", "smart-tabs"]*/ +/* global $tw, require */ +"use strict"; + +describe("'reduce' and 'intersection' filter prefix tests", function() { + + var wiki = new $tw.Wiki(); + + wiki.addTiddler({ + title: "Brownies", + text: "//This is a sample shopping list item for the [[Shopping List Example]]//", + tags: ["shopping","food"], + price: "4.99", + quantity: "1" + }); + wiki.addTiddler({ + title: "Chick Peas", + text: "//This is a sample shopping list item for the [[Shopping List Example]]//", + tags: ["shopping","food"], + price: "1.32", + quantity: "5" + }); + wiki.addTiddler({ + title: "Milk", + text: "//This is a sample shopping list item for the [[Shopping List Example]]//", + tags: ["shopping", "dairy", "drinks"], + price: "0.46", + quantity: "12" + }); + wiki.addTiddler({ + title: "Rice Pudding", + price: "2.66", + quantity: "4", + tags: ["shopping", "dairy"], + text: "//This is a sample shopping list item for the [[Shopping List Example]]//" + }); + wiki.addTiddler({ + title: "Sparkling water", + tags: ["drinks", "mineral water", "textexample"], + text: "This is some text" + }); + wiki.addTiddler({ + title: "Red wine", + tags: ["drinks", "wine", "textexample"], + text: "This is some more text" + }); + wiki.addTiddler({ + title: "Cheesecake", + tags: ["cakes", "food", "textexample"], + text: "This is even even even more text" + }); + wiki.addTiddler({ + title: "Chocolate Cake", + tags: ["cakes", "food", "textexample"], + text: "This is even more text" + }); + + it("should handle the :reduce filter prefix", function() { + expect(wiki.filterTiddlers("[tag[shopping]] :reduce[get[quantity]add]").join(",")).toBe("22"); + expect(wiki.filterTiddlers("[tag[shopping]] :reduce[get[price]multiply{!!quantity}add]").join(",")).toBe("27.75"); + expect(wiki.filterTiddlers("[tag[shopping]] :reduce[compare:number:gt[0]thenaddsuffix[, ]addsuffixelse]").join(",")).toBe("Brownies, Chick Peas, Milk, Rice Pudding"); + expect(wiki.filterTiddlers("[tag[non-existent]] :reduce[get[price]multiply{!!quantity}add] :else[[0]]").join(",")).toBe("0"); + }); + + it("should handle the reduce operator", function() { + var widget = require("$:/core/modules/widgets/widget.js"); + var rootWidget = new widget.widget({ type:"widget", children:[ {type:"widget", children:[]} ] }, + { wiki:wiki, document:$tw.document}); + rootWidget.makeChildWidgets(); + var anchorWidget = rootWidget.children[0]; + rootWidget.setVariable("add-price","[get[price]multiply{!!quantity}add]"); + rootWidget.setVariable("num-items","[get[quantity]add]"); + rootWidget.setVariable("join-with-commas","[compare:number:gt[0]thenaddsuffix[, ]addsuffixelse]"); + + expect(wiki.filterTiddlers("[tag[shopping]reduce]",anchorWidget).join(",")).toBe("22"); + expect(wiki.filterTiddlers("[tag[shopping]reduce]",anchorWidget).join(",")).toBe("27.75"); + expect(wiki.filterTiddlers("[tag[shopping]reduce]",anchorWidget).join(",")).toBe("Brownies, Chick Peas, Milk, Rice Pudding"); + expect(wiki.filterTiddlers("[tag[non-existent]reduce,[0]]",anchorWidget).join(",")).toBe("0"); + }); + + it("should handle the :intersection prefix", function() { + expect(wiki.filterTiddlers("[[Sparkling water]tags[]] :intersection[[Red wine]tags[]]").join(",")).toBe("drinks,textexample"); + expect(wiki.filterTiddlers("[[Brownies]tags[]] :intersection[[Chocolate Cake]tags[]]").join(",")).toBe("food"); + expect(wiki.filterTiddlers("[tag[shopping]] :intersection[tag[food]]").join(",")).toBe("Brownies,Chick Peas"); + expect(wiki.filterTiddlers("[tag[shopping]] :intersection[tag[drinks]]").join(",")).toBe("Milk"); + expect(wiki.filterTiddlers("[tag[shopping]] :intersection[tag[wine]]").join(",")).toBe(""); + }); + + it("should handle the :filter prefix and filter operator", function() { + var widget = require("$:/core/modules/widgets/widget.js"); + var rootWidget = new widget.widget({ type:"widget", children:[ {type:"widget", children:[]} ] }, + { wiki:wiki, document:$tw.document}); + rootWidget.makeChildWidgets(); + var anchorWidget = rootWidget.children[0]; + rootWidget.setVariable("larger-than-18","[get[text]length[]compare:integer:gteq[18]]"); + expect(wiki.filterTiddlers("[tag[textexample]] :filter[get[text]length[]compare:integer:gteq[18]]",anchorWidget).join(",")).toBe("Red wine,Cheesecake,Chocolate Cake"); + expect(wiki.filterTiddlers("[tag[textexample]]",anchorWidget).join(",")).toBe("Sparkling water,Red wine,Cheesecake,Chocolate Cake"); + expect(wiki.filterTiddlers("[tag[textexample]filter]",anchorWidget).join(",")).toBe("Red wine,Cheesecake,Chocolate Cake"); + }) + +}); + +})(); \ No newline at end of file