mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-08-07 06:14:44 +00:00
Allow functions to take multivalued parameters
This commit is contained in:
parent
22eea56b53
commit
d20b02e606
@ -16,8 +16,8 @@ exports.function = function(source,operator,options) {
|
|||||||
var functionName = operator.operands[0],
|
var functionName = operator.operands[0],
|
||||||
params = [],
|
params = [],
|
||||||
results;
|
results;
|
||||||
$tw.utils.each(operator.operands.slice(1),function(param) {
|
$tw.utils.each(operator.multiValueOperands.slice(1),function(paramList) {
|
||||||
params.push({value: param});
|
params.push({value: paramList[0] || "",multiValue: paramList});
|
||||||
});
|
});
|
||||||
// console.log(`Calling ${functionName} with params ${JSON.stringify(params)}`);
|
// console.log(`Calling ${functionName} with params ${JSON.stringify(params)}`);
|
||||||
var variableInfo = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(functionName,{params: params, source: source});
|
var variableInfo = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(functionName,{params: params, source: source});
|
||||||
|
@ -20,8 +20,8 @@ exports["[unknown]"] = function(source,operator,options) {
|
|||||||
// Check for a user defined filter operator
|
// Check for a user defined filter operator
|
||||||
if(operator.operator.indexOf(".") !== -1) {
|
if(operator.operator.indexOf(".") !== -1) {
|
||||||
var params = [];
|
var params = [];
|
||||||
$tw.utils.each(operator.operands,function(param) {
|
$tw.utils.each(operator.multiValueOperands,function(paramList) {
|
||||||
params.push({value: param});
|
params.push({value: paramList[0] || "",multiValue: paramList});
|
||||||
});
|
});
|
||||||
var variableInfo = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(operator.operator,{params: params, source: source});
|
var variableInfo = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(operator.operator,{params: params, source: source});
|
||||||
if(variableInfo && variableInfo.srcVariable) {
|
if(variableInfo && variableInfo.srcVariable) {
|
||||||
|
@ -116,7 +116,7 @@ allowSelfAssigned: if true, includes the current widget in the context chain ins
|
|||||||
|
|
||||||
Returns an object with the following fields:
|
Returns an object with the following fields:
|
||||||
|
|
||||||
params: array of {name:,value:} or {value:} of parameters to be applied
|
params: array of {name:,value:,multiValue:} of parameters to be applied (name is optional)
|
||||||
text: text of variable, with parameters properly substituted
|
text: text of variable, with parameters properly substituted
|
||||||
resultList: result of variable evaluation as an array
|
resultList: result of variable evaluation as an array
|
||||||
srcVariable: reference to the object defining the variable
|
srcVariable: reference to the object defining the variable
|
||||||
@ -142,7 +142,9 @@ Widget.prototype.getVariableInfo = function(name,options) {
|
|||||||
params = self.resolveVariableParameters(variable.params,actualParams);
|
params = self.resolveVariableParameters(variable.params,actualParams);
|
||||||
// Substitute any parameters specified in the definition
|
// Substitute any parameters specified in the definition
|
||||||
$tw.utils.each(params,function(param) {
|
$tw.utils.each(params,function(param) {
|
||||||
value = $tw.utils.replaceString(value,new RegExp("\\$" + $tw.utils.escapeRegExp(param.name) + "\\$","mg"),param.value);
|
if("name" in param) {
|
||||||
|
value = $tw.utils.replaceString(value,new RegExp("\\$" + $tw.utils.escapeRegExp(param.name) + "\\$","mg"),param.value);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
value = self.substituteVariableReferences(value,options);
|
value = self.substituteVariableReferences(value,options);
|
||||||
resultList = [value];
|
resultList = [value];
|
||||||
@ -156,9 +158,13 @@ Widget.prototype.getVariableInfo = function(name,options) {
|
|||||||
variables[param.name] = param["default"];
|
variables[param.name] = param["default"];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Parameters are an array of {value:} or {name:, value:} pairs
|
// Parameters are an array of {name:, value:, multivalue:} pairs (name and multivalue are optional)
|
||||||
$tw.utils.each(params,function(param) {
|
$tw.utils.each(params,function(param) {
|
||||||
variables[param.name] = param.value;
|
if(param.multiValue) {
|
||||||
|
variables[param.name] = param.multiValue;
|
||||||
|
} else {
|
||||||
|
variables[param.name] = param.value || "";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
resultList = this.wiki.filterTiddlers(value,this.makeFakeWidgetWithVariables(variables),options.source);
|
resultList = this.wiki.filterTiddlers(value,this.makeFakeWidgetWithVariables(variables),options.source);
|
||||||
value = resultList[0] || "";
|
value = resultList[0] || "";
|
||||||
@ -197,22 +203,24 @@ Widget.prototype.getVariable = function(name,options) {
|
|||||||
/*
|
/*
|
||||||
Maps actual parameters onto formal parameters, returning an array of {name:,value:} objects
|
Maps actual parameters onto formal parameters, returning an array of {name:,value:} objects
|
||||||
formalParams - Array of {name:,default:} (default value is optional)
|
formalParams - Array of {name:,default:} (default value is optional)
|
||||||
actualParams - Array of string values or {name:,value:} (name is optional)
|
actualParams - Array of string values or {name:,value:,multiValue} (name and multiValue is optional)
|
||||||
*/
|
*/
|
||||||
Widget.prototype.resolveVariableParameters = function(formalParams,actualParams) {
|
Widget.prototype.resolveVariableParameters = function(formalParams,actualParams) {
|
||||||
formalParams = formalParams || [];
|
formalParams = formalParams || [];
|
||||||
actualParams = actualParams || [];
|
actualParams = actualParams || [];
|
||||||
var nextAnonParameter = 0, // Next candidate anonymous parameter in macro call
|
var nextAnonParameter = 0, // Next candidate anonymous parameter in macro call
|
||||||
paramInfo, paramValue,
|
paramInfo, paramValue, paramMultiValue,
|
||||||
results = [];
|
results = [];
|
||||||
// Step through each of the parameters in the macro definition
|
// Step through each of the parameters in the macro definition
|
||||||
for(var p=0; p<formalParams.length; p++) {
|
for(var p=0; p<formalParams.length; p++) {
|
||||||
// Check if we've got a macro call parameter with the same name
|
// Check if we've got a macro call parameter with the same name
|
||||||
paramInfo = formalParams[p];
|
paramInfo = formalParams[p];
|
||||||
paramValue = undefined;
|
paramValue = undefined;
|
||||||
|
paramMultiValue = undefined;
|
||||||
for(var m=0; m<actualParams.length; m++) {
|
for(var m=0; m<actualParams.length; m++) {
|
||||||
if(typeof actualParams[m] !== "string" && actualParams[m].name === paramInfo.name) {
|
if(typeof actualParams[m] !== "string" && actualParams[m].name === paramInfo.name) {
|
||||||
paramValue = actualParams[m].value;
|
paramValue = actualParams[m].value;
|
||||||
|
paramMultiValue = actualParams[m].multiValue || [paramValue]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If not, use the next available anonymous macro call parameter
|
// If not, use the next available anonymous macro call parameter
|
||||||
@ -222,11 +230,13 @@ Widget.prototype.resolveVariableParameters = function(formalParams,actualParams)
|
|||||||
if(paramValue === undefined && nextAnonParameter < actualParams.length) {
|
if(paramValue === undefined && nextAnonParameter < actualParams.length) {
|
||||||
var param = actualParams[nextAnonParameter++];
|
var param = actualParams[nextAnonParameter++];
|
||||||
paramValue = typeof param === "string" ? param : param.value;
|
paramValue = typeof param === "string" ? param : param.value;
|
||||||
|
paramMultiValue = typeof param === "string" ? [param] : (param.multiValue || [paramValue]);
|
||||||
}
|
}
|
||||||
// If we've still not got a value, use the default, if any
|
// If we've still not got a value, use the default, if any
|
||||||
paramValue = paramValue || paramInfo["default"] || "";
|
paramValue = paramValue || paramInfo["default"] || "";
|
||||||
|
paramMultiValue = paramMultiValue || [paramValue];
|
||||||
// Store the parameter name and value
|
// Store the parameter name and value
|
||||||
results.push({name: paramInfo.name, value: paramValue});
|
results.push({name: paramInfo.name, value: paramValue, multiValue: paramMultiValue});
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
title: MultiValuedVariables/Parameters
|
||||||
|
description: Multi-valued function parameters
|
||||||
|
type: text/vnd.tiddlywiki-multiple
|
||||||
|
tags: [[$:/tags/wiki-test-spec]]
|
||||||
|
|
||||||
|
title: Output
|
||||||
|
|
||||||
|
\function myfunc(input) [(input)sort[]]
|
||||||
|
|
||||||
|
<$let
|
||||||
|
input={{{ [all[tiddlers]] }}}
|
||||||
|
output={{{ [function[myfunc],(input)] }}}
|
||||||
|
>
|
||||||
|
<$text text={{{ [(output)] +[join[-]] }}}/>
|
||||||
|
</$let>
|
||||||
|
+
|
||||||
|
title: ExpectedResult
|
||||||
|
|
||||||
|
<p>
|
||||||
|
$:/core-ExpectedResult-Output
|
||||||
|
</p>
|
@ -0,0 +1,21 @@
|
|||||||
|
title: MultiValuedVariables/ParametersShortcut
|
||||||
|
description: Multi-valued function parameters using shortcut syntax
|
||||||
|
type: text/vnd.tiddlywiki-multiple
|
||||||
|
tags: [[$:/tags/wiki-test-spec]]
|
||||||
|
|
||||||
|
title: Output
|
||||||
|
|
||||||
|
\function my.func(input) [(input)sort[]]
|
||||||
|
|
||||||
|
<$let
|
||||||
|
input={{{ [all[tiddlers]] }}}
|
||||||
|
output={{{ [my.func(input)] }}}
|
||||||
|
>
|
||||||
|
<$text text={{{ [(output)] +[join[-]] }}}/>
|
||||||
|
</$let>
|
||||||
|
+
|
||||||
|
title: ExpectedResult
|
||||||
|
|
||||||
|
<p>
|
||||||
|
$:/core-ExpectedResult-Output
|
||||||
|
</p>
|
@ -61,10 +61,10 @@ describe("Widget module", function() {
|
|||||||
childNode = childNode.children[0];
|
childNode = childNode.children[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(childNode.getVariableInfo("macro",{allowSelfAssigned:true}).params).toEqual([{name:"a",value:"aa"}]);
|
expect(childNode.getVariableInfo("macro",{allowSelfAssigned:true}).params).toEqual([{name:"a",value:"aa",multiValue:["aa"]}]);
|
||||||
|
|
||||||
// function params
|
// function params
|
||||||
expect(childNode.getVariableInfo("fn", {allowSelfAssigned:true}).params).toEqual([{name:"f",value:"ff"}]);
|
expect(childNode.getVariableInfo("fn", {allowSelfAssigned:true}).params).toEqual([{name:"f",value:"ff",multiValue:["ff"]}]);
|
||||||
// functions have a text and a value
|
// functions have a text and a value
|
||||||
expect(childNode.getVariableInfo("x", {allowSelfAssigned:true}).text).toBe("fff");
|
expect(childNode.getVariableInfo("x", {allowSelfAssigned:true}).text).toBe("fff");
|
||||||
expect(childNode.getVariableInfo("x", {allowSelfAssigned:true}).srcVariable.value).toBe("[<fn>]");
|
expect(childNode.getVariableInfo("x", {allowSelfAssigned:true}).srcVariable.value).toBe("[<fn>]");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user