I managed to fix it, although probably not in the best way...

The issue seems to arise from zero making it to the return statement as a 
number.  I can surmise earlier versions of TiddlyWiki had a check for this 
case and a string conversion...  But this does not occur here.

Here is the revised code:  (The only change is to the final return line)

/*\
title: $:/.tb/macros/calc.js
type: application/javascript
module-type: macro

Computes a (Field) value +,-,*,/ a provided value.

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro
*/

exports.name = "calc";

exports.params = [
{name: "value"},
{name: "operation"},
{name: "until"},
{name: "beyond"},
{name: "decimals"},
{name: "tiddler"}
];

/*
Run the macro
*/
exports.run = function(value, operation, until, beyond, decimals, tiddler) {
if(!value) {
return;
}
if("" == operation){
operation = "0";
}

var 
curr,dec,op,r,result,val,
ops = ["+","-","*","/"];

curr = parseFloat(
0 > value.indexOf('!!') ?
value :
this.wiki.getTextReference(value, "NaN", tiddler || 
this.getVariable("currentTiddler"))
);

until = parseFloat(until);
decimals = parseInt(decimals);

op = operation.charAt(0);
val = parseFloat(0 > ops.indexOf(op) ? operation : operation.substr(1));
op = 0 > ops.indexOf(op) ? "+" : op;

if(isNaN(curr)) {
result = "NaN";
} else {
switch (op){
case "-": result = curr - val; break;
case "*": result = curr * val; break;
case "/": result = curr / val; break;
case "+":
default: result = curr + val; 
}
if(!isNaN(until)) {
if (!(
"+" == op || "*" == op ?
result <= until :
result >= until
)) {
if("true" == beyond) {
result = true;
} else {
result = until;
}
}
if(beyond && result !== true) {
result = false;
}
}
}

if(!isNaN(result)){
r = result.toString();
dec = r.indexOf('.');
if(dec > -1){
dec = r.substr(dec).length;
if(
!isNaN(decimals) && dec > decimals ||
isNaN(decimals) && dec > 2
){
result = result.toFixed(isNaN(decimals) ? 2 : decimals);
} 
}
}
return result.toString();
};

})();




On Thursday, 17 September 2015 00:27:08 UTC-5, Evan Balster wrote:
>
> I'm running into a most perplexing bug with Tobias' calc 
> <http://tobibeer.github.io/tb5/#calc> macro.
>
> "<<calc 1 -1>>"
>
> This code, run in Tobias Beer's own wiki (version 5.1.7), will yield a 
> result of "0".
>
> Run in an empty TiddlyWiki based on version 5.1.9 (but with calc 
> installed), in the same browser, it returns an empty string.  Expressions 
> evaluating to positive or negative numbers return normal results in both 
> wikis.
>
> Notably, I needed to add "plugin-type" (in place of the older module-type) 
> in order to integrate the macro -- but I don't understand why this would 
> affect the output.  I'm looking over the JavaScript code and I'm just 
> completely baffled by this behavior.  What's going on?
>

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tiddlywiki+unsubscr...@googlegroups.com.
To post to this group, send email to tiddlywiki@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/e340e1fb-5e52-4603-9e4f-d73bfe46e4f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to