1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-25 23:03:15 +00:00

Now fixing bug mentioned in groups (#3188)

* fixed the "0 is not a number bug" in listops and x-listops

* Fixed one comment

* "default" is not a good name for a variable

* Following code styles.
Moving getInt to utils.

* Removing unwanted spaces introduced by me
This commit is contained in:
Skeeve 2018-04-02 20:40:47 +02:00 committed by Jeremy Ruston
parent 501ad2798f
commit 6401b5c886
3 changed files with 27 additions and 18 deletions

View File

@ -24,7 +24,7 @@ exports.order = function(source,operator,options) {
} else { } else {
source(function(tiddler,title) { source(function(tiddler,title) {
results.push(title); results.push(title);
}); });
} }
return results; return results;
}; };
@ -44,7 +44,7 @@ exports.reverse = function(source,operator,options) {
First entry/entries in list First entry/entries in list
*/ */
exports.first = function(source,operator,options) { exports.first = function(source,operator,options) {
var count = parseInt(operator.operand) || 1, var count = $tw.utils.getInt(operator.operand,1),
results = []; results = [];
source(function(tiddler,title) { source(function(tiddler,title) {
results.push(title); results.push(title);
@ -56,7 +56,7 @@ exports.first = function(source,operator,options) {
Last entry/entries in list Last entry/entries in list
*/ */
exports.last = function(source,operator,options) { exports.last = function(source,operator,options) {
var count = parseInt(operator.operand) || 1, var count = $tw.utils.getInt(operator.operand,1),
results = []; results = [];
source(function(tiddler,title) { source(function(tiddler,title) {
results.push(title); results.push(title);
@ -68,7 +68,7 @@ exports.last = function(source,operator,options) {
All but the first entry/entries of the list All but the first entry/entries of the list
*/ */
exports.rest = function(source,operator,options) { exports.rest = function(source,operator,options) {
var count = parseInt(operator.operand) || 1, var count = $tw.utils.getInt(operator.operand,1),
results = []; results = [];
source(function(tiddler,title) { source(function(tiddler,title) {
results.push(title); results.push(title);
@ -82,7 +82,7 @@ exports.bf = exports.rest;
All but the last entry/entries of the list All but the last entry/entries of the list
*/ */
exports.butlast = function(source,operator,options) { exports.butlast = function(source,operator,options) {
var count = parseInt(operator.operand) || 1, var count = $tw.utils.getInt(operator.operand,1),
results = []; results = [];
source(function(tiddler,title) { source(function(tiddler,title) {
results.push(title); results.push(title);
@ -95,7 +95,7 @@ exports.bl = exports.butlast;
The nth member of the list The nth member of the list
*/ */
exports.nth = function(source,operator,options) { exports.nth = function(source,operator,options) {
var count = parseInt(operator.operand) || 1, var count = $tw.utils.getInt(operator.operand,1),
results = []; results = [];
source(function(tiddler,title) { source(function(tiddler,title) {
results.push(title); results.push(title);

View File

@ -29,7 +29,7 @@ Extended filter operators to manipulate the current list.
exports.putbefore = function (source, operator) { exports.putbefore = function (source, operator) {
var results = prepare_results(source), var results = prepare_results(source),
index = results.indexOf(operator.operand), index = results.indexOf(operator.operand),
count = parseInt(operator.suffix) || 1; count = $tw.utils.getInt(operator.suffix,1);
return (index === -1) ? return (index === -1) ?
results.slice(0, -1) : results.slice(0, -1) :
results.slice(0, index).concat(results.slice(-count)).concat(results.slice(index, -count)); 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) { exports.putafter = function (source, operator) {
var results = prepare_results(source), var results = prepare_results(source),
index = results.indexOf(operator.operand), index = results.indexOf(operator.operand),
count = parseInt(operator.suffix) || 1; count = $tw.utils.getInt(operator.suffix,1);
return (index === -1) ? return (index === -1) ?
results.slice(0, -1) : results.slice(0, -1) :
results.slice(0, index + 1).concat(results.slice(-count)).concat(results.slice(index + 1, -count)); 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) { exports.replace = function (source, operator) {
var results = prepare_results(source), var results = prepare_results(source),
index = results.indexOf(operator.operand), index = results.indexOf(operator.operand),
count = parseInt(operator.suffix) || 1; count = $tw.utils.getInt(operator.suffix,1);
return (index === -1) ? return (index === -1) ?
results.slice(0, -count) : results.slice(0, -count) :
results.slice(0, index).concat(results.slice(-count)).concat(results.slice(index + 1, -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) { exports.putfirst = function (source, operator) {
var results = prepare_results(source), 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)); 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) { exports.putlast = function (source, operator) {
var results = prepare_results(source), 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)); 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) { exports.move = function (source, operator) {
var results = prepare_results(source), var results = prepare_results(source),
index = results.indexOf(operator.operand), index = results.indexOf(operator.operand),
count = parseInt(operator.suffix) || 1, count = $tw.utils.getInt(operator.suffix,1),
marker = results.splice(index, 1), marker = results.splice(index, 1),
offset = (index + count) > 0 ? index + count : 0; offset = (index + count) > 0 ? index + count : 0;
return results.slice(0, offset).concat(marker).concat(results.slice(offset)); 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) { exports.prepend = function (source, operator) {
var prepend = $tw.utils.parseStringArray(operator.operand, "true"), var prepend = $tw.utils.parseStringArray(operator.operand, "true"),
results = prepare_results(source), results = prepare_results(source),
count = parseInt(operator.suffix) || prepend.length; count = $tw.utils.getInt(operator.suffix,prepend.length);
return (prepend.length === 0) ? results : return (prepend.length === 0) ? results :
(operator.prefix) ? prepend.slice(-count).concat(results) : (operator.prefix) ? prepend.slice(-count).concat(results) :
prepend.slice(0, count).concat(results); prepend.slice(0, count).concat(results);

View File

@ -51,6 +51,15 @@ exports.warning = function(text) {
exports.log(text,"brown/orange"); 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 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 // 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.' // 'return raw UTC (tiddlywiki style) date string.'
if(t.indexOf("[UTC]") == 0 ) { if(t.indexOf("[UTC]") == 0 ) {
if(t == "[UTC]YYYY0MM0DD0hh0mm0ssXXX") if(t == "[UTC]YYYY0MM0DD0hh0mm0ssXXX")
return $tw.utils.stringifyDate(new Date()); return $tw.utils.stringifyDate(new Date());
var offset = date.getTimezoneOffset() ; // in minutes var offset = date.getTimezoneOffset() ; // in minutes
date = new Date(date.getTime()+offset*60*1000) ; date = new Date(date.getTime()+offset*60*1000) ;
t = t.substr(5) ; t = t.substr(5) ;
} }
while(t.length){ while(t.length){
var matchString = ""; var matchString = "";
$tw.utils.each(matches, function(m) { $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 ; e = s.substr(1,s.length-2); // Strip the & and the ;
if(e.charAt(0) === "#") { if(e.charAt(0) === "#") {
if(e.charAt(1) === "x" || e.charAt(1) === "X") { if(e.charAt(1) === "x" || e.charAt(1) === "X") {
return converter(parseInt(e.substr(2),16)); return converter(parseInt(e.substr(2),16));
} else { } else {
return converter(parseInt(e.substr(1),10)); return converter(parseInt(e.substr(1),10));
} }
@ -724,7 +733,7 @@ High resolution microsecond timer for profiling
exports.timer = function(base) { exports.timer = function(base) {
var m; var m;
if($tw.node) { if($tw.node) {
var r = process.hrtime(); var r = process.hrtime();
m = r[0] * 1e3 + (r[1] / 1e6); m = r[0] * 1e3 + (r[1] / 1e6);
} else if(window.performance) { } else if(window.performance) {
m = performance.now(); m = performance.now();