diff --git a/core/modules/filters/function.js b/core/modules/filters/function.js new file mode 100644 index 000000000..8d257ced7 --- /dev/null +++ b/core/modules/filters/function.js @@ -0,0 +1,45 @@ +/*\ +title: $:/core/modules/filters/function.js +type: application/javascript +module-type: filteroperator + +Filter operator returning those input titles that are returned from a function + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.function = function(source,operator,options) { + var functionName = operator.operands[0], + customDefinition = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(functionName); + if(customDefinition && customDefinition.srcVariable && customDefinition.srcVariable.isFunctionDefinition) { + var variables = Object.create(null); + // Go through each of the defined parameters, and make a variable with the value of the corresponding operand + $tw.utils.each(customDefinition.srcVariable.params,function(param,index) { + var value = operator.operands[1 + index]; // Skip over the first operand that gives the function name + variables[param.name] = value === undefined ? param["default"] || "" : value; + }); + var list = options.wiki.filterTiddlers(customDefinition.srcVariable.value,options.widget.makeFakeWidgetWithVariables(variables),source); + if(operator.prefix === "!") { + var results = []; + source(function(tiddler,title) { + if(list.indexOf(title) === -1) { + results.push(title); + } + }); + return results; + } else { + return list; + } + } + // Return an empty list if the function wasn't found + return []; +}; + +})(); diff --git a/editions/test/tiddlers/tests/data/custom-operators/Parameterised.tid b/editions/test/tiddlers/tests/data/custom-operators/Parameterised.tid index 6901710b7..2f8337b0f 100644 --- a/editions/test/tiddlers/tests/data/custom-operators/Parameterised.tid +++ b/editions/test/tiddlers/tests/data/custom-operators/Parameterised.tid @@ -13,8 +13,12 @@ title: Output <$text text={{{ [[123].multiplybysomething[]] }}}/> - <$text text={{{ [[123].multiplybysomething[x],[4]] }}}/> +| +<$text text={{{ [[123]function[.multiplybysomething]] }}}/> +- +<$text text={{{ [[123]function[.multiplybysomething],[x],[4]] }}}/> + title: ExpectedResult -
492-984
\ No newline at end of file +492-984|492-984
\ No newline at end of file diff --git a/editions/test/tiddlers/tests/data/custom-operators/Simple.tid b/editions/test/tiddlers/tests/data/custom-operators/Simple.tid index 73d46d689..089701295 100644 --- a/editions/test/tiddlers/tests/data/custom-operators/Simple.tid +++ b/editions/test/tiddlers/tests/data/custom-operators/Simple.tid @@ -5,12 +5,17 @@ tags: [[$:/tags/wiki-test-spec]] title: Output +\whitespace trim + \function .multiplybytwo() [multiply[2]] \end <$text text={{{ [[123].multiplybytwo[]] }}}/> +| +<$text text={{{ [[123]function[.multiplybytwo]] }}}/> + + title: ExpectedResult -246
\ No newline at end of file +246|246
\ No newline at end of file