Script will run the provided mathematical action on 2 provided values and produce a result.
The result can be stored on the context using the contextKey argument
var lh = args.lh;
var rh = args.rh;
var action = args.action;
var lhRegex = args.lhRegex;
var rhRegex = args.rhRegex;
var lRadix = args.lhRadix ? args.lRadix : 10;
var rRadix = args.rhRadix ? args.rRadix : 10;
var supportedActions = {
'+': function() { return lh + rh; },
'-': function() { return lh - rh; },
'>': function() { return lh > rh; },
'<': function() { return lh < rh; },
'*': function() { return lh * rh; },
'/': function() { return lh / rh; },
'%': function() { return lh % rh; },
'==': function() { return lh == rh; }
}
actionKeys = Object.keys(supportedActions);
//verfiy action is supported
if (!action || actionKeys.indexOf(action) < 0 ) {
return {ContentsFormat: formats.markdown, Type: entryTypes.error, Contents: '**[' + action + ']** is not a supported action, only ' + actionKeys.join() + ' are supported'};
}
//parse lh according to regex
if (lhRegex) {
var lr = new RegExp(lhRegex, 'i');
lh = lh.match(lr);
if (!lh) {
return {ContentsFormat: formats.markdown, Type: entryTypes.error, Contents: 'lh - parsed to be an empty value from regex **[' + lhRegex + ']**, on value **[' + args.lh + ']**'};
}
}
//parse rh according to regex
if (rhRegex) {
var rr = new RegExp(rhRegex, 'i');
rh = rh.match(rr);
if (!rh) {
return {ContentsFormat: formats.markdown, Type: entryTypes.error, Contents: 'rh - parsed to be an empty value from regex **[' + rhRegex + ']**, on value **[' + args.rh + ']**'};
}
}
if (isNaN(lh)) {
return {ContentsFormat: formats.markdown, Type: entryTypes.error, Contents: '**[' + lh + ']** is not a number'};
}
if (isNaN(rh)) {
return {ContentsFormat: formats.markdown, Type: entryTypes.error, Contents: '**[' + rh + ']** is not a number'};
}
lh = parseInt(lh, lRadix)
rh = parseInt(rh, rRadix)
//do action on params
var res = supportedActions[action]();
// We need to override the context value so we use setContext
if (args.contextKey) {
setContext(args.contextKey, res);
} else {
setContext('MathResult', res);
}
return {ContentsFormat: formats.markdown, Type: entryTypes.note, Contents: lh + ' ' + action + ' ' + rh + ' = ' + res};
README
Runs the provided mathematical action on 2 provided values and produce a result.
The result can be stored on the context using the contextKey argument.
Script Data
Name
Description
Script Type
javascript
Tags
Utility
Inputs
Argument Name
Description
lh
The left hand parameter.
rh
The right hand parameter.
action
The math expression (+, -, >, <, ==, *, /, %).
rhRegex
The regex used to filter only a specific part of the right hand argument.
lhRegex
The regex used to filter only a specific part of the left hand argument.
lhRadix
The radix for the left hand value. The default is 10.
rhRadix
Radix for right hand value, defaults to 10
contextKey
The path to store the result.
Outputs
Path
Description
Type
MathResult
The result of the math action. This might be overriden by contextKey.