diff --git a/core/modules/filters/listops.js b/core/modules/filters/listops.js index d7a07b62a..0c30c22e7 100644 --- a/core/modules/filters/listops.js +++ b/core/modules/filters/listops.js @@ -24,7 +24,7 @@ exports.order = function(source,operator,options) { } else { source(function(tiddler,title) { results.push(title); - }); + }); } return results; }; @@ -44,7 +44,7 @@ exports.reverse = function(source,operator,options) { First entry/entries in list */ exports.first = function(source,operator,options) { - var count = parseInt(operator.operand) || 1, + var count = $tw.utils.getInt(operator.operand,1), results = []; source(function(tiddler,title) { results.push(title); @@ -56,7 +56,7 @@ exports.first = function(source,operator,options) { Last entry/entries in list */ exports.last = function(source,operator,options) { - var count = parseInt(operator.operand) || 1, + var count = $tw.utils.getInt(operator.operand,1), results = []; source(function(tiddler,title) { results.push(title); @@ -68,7 +68,7 @@ exports.last = function(source,operator,options) { All but the first entry/entries of the list */ exports.rest = function(source,operator,options) { - var count = parseInt(operator.operand) || 1, + var count = $tw.utils.getInt(operator.operand,1), results = []; source(function(tiddler,title) { results.push(title); @@ -82,7 +82,7 @@ exports.bf = exports.rest; All but the last entry/entries of the list */ exports.butlast = function(source,operator,options) { - var count = parseInt(operator.operand) || 1, + var count = $tw.utils.getInt(operator.operand,1), results = []; source(function(tiddler,title) { results.push(title); @@ -95,7 +95,7 @@ exports.bl = exports.butlast; The nth member of the list */ exports.nth = function(source,operator,options) { - var count = parseInt(operator.operand) || 1, + var count = $tw.utils.getInt(operator.operand,1), results = []; source(function(tiddler,title) { results.push(title); diff --git a/core/modules/filters/x-listops.js b/core/modules/filters/x-listops.js index d9c7b83e6..e742e3430 100644 --- a/core/modules/filters/x-listops.js +++ b/core/modules/filters/x-listops.js @@ -29,7 +29,7 @@ Extended filter operators to manipulate the current list. exports.putbefore = function (source, operator) { var results = prepare_results(source), index = results.indexOf(operator.operand), - count = parseInt(operator.suffix) || 1; + count = $tw.utils.getInt(operator.suffix,1); return (index === -1) ? results.slice(0, -1) : results.slice(0, index).concat(results.slice(-count)).concat(results.slice(index, -count)); @@ -41,7 +41,7 @@ Extended filter operators to manipulate the current list. exports.putafter = function (source, operator) { var results = prepare_results(source), index = results.indexOf(operator.operand), - count = parseInt(operator.suffix) || 1; + count = $tw.utils.getInt(operator.suffix,1); return (index === -1) ? results.slice(0, -1) : results.slice(0, index + 1).concat(results.slice(-count)).concat(results.slice(index + 1, -count)); @@ -53,7 +53,7 @@ Extended filter operators to manipulate the current list. exports.replace = function (source, operator) { var results = prepare_results(source), index = results.indexOf(operator.operand), - count = parseInt(operator.suffix) || 1; + count = $tw.utils.getInt(operator.suffix,1); return (index === -1) ? results.slice(0, -count) : results.slice(0, index).concat(results.slice(-count)).concat(results.slice(index + 1, -count)); @@ -64,7 +64,7 @@ Extended filter operators to manipulate the current list. */ exports.putfirst = function (source, operator) { var results = prepare_results(source), - count = parseInt(operator.suffix) || 1; + count = $tw.utils.getInt(operator.suffix,1); return results.slice(-count).concat(results.slice(0, -count)); }; @@ -73,7 +73,7 @@ Extended filter operators to manipulate the current list. */ exports.putlast = function (source, operator) { var results = prepare_results(source), - count = parseInt(operator.suffix) || 1; + count = $tw.utils.getInt(operator.suffix,1); return results.slice(count).concat(results.slice(0, count)); }; @@ -83,7 +83,7 @@ Extended filter operators to manipulate the current list. exports.move = function (source, operator) { var results = prepare_results(source), index = results.indexOf(operator.operand), - count = parseInt(operator.suffix) || 1, + count = $tw.utils.getInt(operator.suffix,1), marker = results.splice(index, 1), offset = (index + count) > 0 ? index + count : 0; return results.slice(0, offset).concat(marker).concat(results.slice(offset)); @@ -129,7 +129,7 @@ Extended filter operators to manipulate the current list. exports.prepend = function (source, operator) { var prepend = $tw.utils.parseStringArray(operator.operand, "true"), results = prepare_results(source), - count = parseInt(operator.suffix) || prepend.length; + count = $tw.utils.getInt(operator.suffix,prepend.length); return (prepend.length === 0) ? results : (operator.prefix) ? prepend.slice(-count).concat(results) : prepend.slice(0, count).concat(results); diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index a52f0cbf3..391cdd961 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -51,6 +51,15 @@ exports.warning = function(text) { exports.log(text,"brown/orange"); }; +/* +Return the integer represented by the str (string). +Return the dflt (default) parameter if str is not a base-10 number. +*/ +exports.getInt = function(str,deflt) { + var i = parseInt(str,10); + return isNaN(i) ? deflt : i; +} + /* Repeatedly replaces a substring within a string. Like String.prototype.replace, but without any of the default special handling of $ sequences in the replace string */ @@ -363,15 +372,15 @@ exports.formatDateString = function(date,template) { }] ]; // If the user wants everything in UTC, shift the datestamp - // Optimize for format string that essentially means + // Optimize for format string that essentially means // 'return raw UTC (tiddlywiki style) date string.' if(t.indexOf("[UTC]") == 0 ) { - if(t == "[UTC]YYYY0MM0DD0hh0mm0ssXXX") + if(t == "[UTC]YYYY0MM0DD0hh0mm0ssXXX") return $tw.utils.stringifyDate(new Date()); var offset = date.getTimezoneOffset() ; // in minutes date = new Date(date.getTime()+offset*60*1000) ; t = t.substr(5) ; - } + } while(t.length){ var matchString = ""; $tw.utils.each(matches, function(m) { @@ -491,7 +500,7 @@ exports.entityDecode = function(s) { e = s.substr(1,s.length-2); // Strip the & and the ; if(e.charAt(0) === "#") { if(e.charAt(1) === "x" || e.charAt(1) === "X") { - return converter(parseInt(e.substr(2),16)); + return converter(parseInt(e.substr(2),16)); } else { return converter(parseInt(e.substr(1),10)); } @@ -724,7 +733,7 @@ High resolution microsecond timer for profiling exports.timer = function(base) { var m; if($tw.node) { - var r = process.hrtime(); + var r = process.hrtime(); m = r[0] * 1e3 + (r[1] / 1e6); } else if(window.performance) { m = performance.now();