mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 18:17:20 +00:00
Coding style tweaks for #3346
This commit is contained in:
parent
be58de8409
commit
8520c9d8fd
@ -17,23 +17,22 @@ Export our filter function
|
|||||||
*/
|
*/
|
||||||
exports.range = function(source,operator,options) {
|
exports.range = function(source,operator,options) {
|
||||||
var results = [];
|
var results = [];
|
||||||
|
|
||||||
// Split the operand into numbers delimited by these symbols
|
// Split the operand into numbers delimited by these symbols
|
||||||
var parts = operator.operand.split(/[,:;]/g), beg, end, inc, i, fixed = 0;
|
var parts = operator.operand.split(/[,:;]/g),
|
||||||
|
beg, end, inc, i, fixed = 0;
|
||||||
for (i = 0; i < parts.length; ++i) {
|
for (i=0; i<parts.length; i++) {
|
||||||
// Validate real number
|
// Validate real number
|
||||||
if (!/^\s*[+-]?((\d+(\.\d*)?)|(\.\d+))\s*$/.test(parts[i]))
|
if(!/^\s*[+-]?((\d+(\.\d*)?)|(\.\d+))\s*$/.test(parts[i])) {
|
||||||
return ["range: bad number \""+parts[i]+"\""];
|
return ["range: bad number \"" + parts[i] + "\""];
|
||||||
|
}
|
||||||
// Count digits; the most precise number determines decimal places in output.
|
// Count digits; the most precise number determines decimal places in output.
|
||||||
var frac = /\.\d+/.exec(parts[i]);
|
var frac = /\.\d+/.exec(parts[i]);
|
||||||
if (frac) fixed = Math.max(fixed, frac[0].length-1);
|
if(frac) {
|
||||||
|
fixed = Math.max(fixed,frac[0].length-1);
|
||||||
|
}
|
||||||
parts[i] = parseFloat(parts[i]);
|
parts[i] = parseFloat(parts[i]);
|
||||||
}
|
}
|
||||||
|
switch(parts.length) {
|
||||||
switch (parts.length) {
|
|
||||||
case 1:
|
case 1:
|
||||||
beg = 0;
|
beg = 0;
|
||||||
end = parts[0];
|
end = parts[0];
|
||||||
@ -50,30 +49,42 @@ exports.range = function(source,operator,options) {
|
|||||||
inc = Math.abs(parts[2]);
|
inc = Math.abs(parts[2]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if(inc === 0) {
|
||||||
if (inc === 0) return ["range: increment 0 causes infinite loop"];
|
return ["range: increment 0 causes infinite loop"];
|
||||||
|
}
|
||||||
// May need to count backwards
|
// May need to count backwards
|
||||||
var direction = ((end<beg) ? -1 : 1);
|
var direction = ((end < beg) ? -1 : 1);
|
||||||
inc *= direction;
|
inc *= direction;
|
||||||
|
|
||||||
// Estimate number of resulting elements
|
// Estimate number of resulting elements
|
||||||
if ((end-beg)/inc > 10000) return ["range: too many steps (over 10K)"];
|
if((end - beg) / inc > 10000) {
|
||||||
|
return ["range: too many steps (over 10K)"];
|
||||||
|
}
|
||||||
// Avoid rounding error on last step
|
// Avoid rounding error on last step
|
||||||
end += direction * 0.5 * Math.pow(0.1, fixed);
|
end += direction * 0.5 * Math.pow(0.1,fixed);
|
||||||
|
|
||||||
var safety = 10010;
|
var safety = 10010;
|
||||||
|
|
||||||
// Enumerate the range
|
// Enumerate the range
|
||||||
if (end<beg) {for (i = beg; i > end; i += inc) {results.push(i.toFixed(fixed)); if (--safety<0) break;}}
|
if (end<beg) {
|
||||||
else {for (i = beg; i < end; i += inc) {results.push(i.toFixed(fixed)); if (--safety<0) break;}}
|
for(i=beg; i>end; i+=inc) {
|
||||||
|
results.push(i.toFixed(fixed));
|
||||||
if (safety<0) return ["range: unexpectedly large output"];
|
if(--safety<0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for(i=beg; i<end; i+=inc) {
|
||||||
|
results.push(i.toFixed(fixed));
|
||||||
|
if(--safety<0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(safety<0) {
|
||||||
|
return ["range: unexpectedly large output"];
|
||||||
|
}
|
||||||
// Reverse?
|
// Reverse?
|
||||||
if (operator.prefix === "!") results.reverse();
|
if(operator.prefix === "!") {
|
||||||
|
results.reverse();
|
||||||
|
}
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user