diff --git a/core/modules/filters/lookup.js b/core/modules/filters/lookup.js new file mode 100644 index 000000000..3ab7f42e6 --- /dev/null +++ b/core/modules/filters/lookup.js @@ -0,0 +1,30 @@ +/*\ +title: $:/core/modules/filters/lookup.js +type: application/javascript +module-type: filteroperator + +Filter operator that looks up values via a title prefix + +[lookup:[]] + +Prepends the prefix to the selected items and returns the specified field value + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.lookup = function(source,operator,options) { + var results = []; + source(function(tiddler,title) { + results.push(options.wiki.getTiddlerText(operator.operand + title) || options.wiki.getTiddlerText(operator.operand + operator.suffix)); + }); + return results; +}; + +})(); diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index 1ae98d496..beaee32ea 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -82,6 +82,11 @@ describe("Filter tests", function() { // Our tests + it("should handle the lookup operator", function() { + expect(wiki.filterTiddlers("Six Seventh 8 +[lookup[Tiddler]]").join(",")).toBe("Missing inaction from TiddlerOne,,Tidd"); + expect(wiki.filterTiddlers("Six Seventh 8 +[lookup:8[Tiddler]]").join(",")).toBe("Missing inaction from TiddlerOne,Tidd,Tidd"); + }); + it("should retrieve shadow tiddlers", function() { expect(wiki.getTiddlerText("Tiddler8")).toBe("Tidd"); }); diff --git a/editions/tw5.com/tiddlers/lookup Operator (Examples).tid b/editions/tw5.com/tiddlers/lookup Operator (Examples).tid new file mode 100644 index 000000000..052e1d2bd --- /dev/null +++ b/editions/tw5.com/tiddlers/lookup Operator (Examples).tid @@ -0,0 +1,7 @@ +created: 20170907144257037 +modified: 20170907144559822 +title: lookup Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 "[all[shadows+tiddlers]tag[$:/tags/PageControls]lookup[$:/config/PageControlButtons/Visibility/]]" "Retrieve the visibility status of each page control button">> +<<.operator-example 2 "[all[shadows+tiddlers]tag[$:/tags/PageControls]lookup:show[$:/config/PageControlButtons/Visibility/]]" "Retrieve the visibility status of each page control button, this time with a default value">> diff --git a/editions/tw5.com/tiddlers/lookup Operator.tid b/editions/tw5.com/tiddlers/lookup Operator.tid new file mode 100644 index 000000000..f0cc8d3d3 --- /dev/null +++ b/editions/tw5.com/tiddlers/lookup Operator.tid @@ -0,0 +1,24 @@ +caption: lookup +created: 20170907103639431 +modified: 20170907144703051 +op-input: a [[selection of titles|Title Selection]] +op-output: the lookup values corresponding to each input title +op-parameter: prefix applied to input titles to yield title of lookup tiddler from which value is retrieved +op-parameter-name: P +op-purpose: applies a prefix to each input title to yield the title of a tiddler from which the final value is retrieved +op-suffix: the default value to be used for missing lookups +op-suffix-name: D +tags: [[Filter Operators]] +title: lookup Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.14">> + +The action of this operator is as follows: + +* Apply the specified prefix to each input tiddler title, yielding a new list of tiddler titles +* Transclude the value of each of those tiddlers +** Substitute the default value for missing or empty tiddlers +* Return the list of values + +<<.operator-examples "lookup">>