From b9df224f99c425923c1fd8c25826e4d00a5fb39e Mon Sep 17 00:00:00 2001 From: Evan Balster Date: Sun, 2 Dec 2018 02:39:28 -0600 Subject: [PATCH] Change to natural counting in range[N] operator (#3609) * Add range operator and documentation * Use 1-based counting in range[N], update docs --- core/modules/filters/range.js | 10 +++++++++- editions/tw5.com/tiddlers/filters/range.tid | 21 ++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/core/modules/filters/range.js b/core/modules/filters/range.js index b08bce1bb..f46d10699 100644 --- a/core/modules/filters/range.js +++ b/core/modules/filters/range.js @@ -34,8 +34,16 @@ exports.range = function(source,operator,options) { } switch(parts.length) { case 1: - beg = 0; end = parts[0]; + if (end >= 1) { + beg = 1; + } + else if (end <= -1) { + beg = -1; + } + else { + return []; + } inc = 1; break; case 2: diff --git a/editions/tw5.com/tiddlers/filters/range.tid b/editions/tw5.com/tiddlers/filters/range.tid index 0b6fdf96b..25d5ce316 100644 --- a/editions/tw5.com/tiddlers/filters/range.tid +++ b/editions/tw5.com/tiddlers/filters/range.tid @@ -18,28 +18,31 @@ op-output: a series of evenly spaced numbers ranging from `` to `` <$list variable=n filter="[range[$range$]]"><> \end -The `range` operator allows a range of numbers to be enumerated, similar to a `for` loop in other programming languages. It's useful in combination with the [[Formula Plugin]]. +The `range` operator produces a list of numbers counting up or down. It's useful for counting and numbering, or in combination with the [[Formula Plugin]]. |!Purpose|produce a range of numbers| |!Input|ignored.| |!Parameter|1-3 numbers separated by `,` or `;`.| -|!Output|A range of numbers starting with | +|!Output|A series of evenly spaced numbers ranging from `` to ``| |!`!` Output|As ''Output'', but with order reversed.| The parameter has three forms: -* `` -* `,` -* `,,` +|Parameter|Output|h +|``|Whole numbers up to ``.| +|`,`|Numbers from `` to ``, spaced by whole numbers.| +|`,,`|Numbers from `` to `` spaced out by ``.| Each part must be a number, and works as follows: -* ``: start counting at this number. Defaults to 0. +* ``: start counting at this number. +** Defaults to 1 if `` is at least 1 (or -1 if `` is at most -1). * ``: stop counting at this number. -** It will be included unless it falls between two steps. +** This number will appear in the list unless it falls between two steps. * ``: count up (or down) by this amount. -** It may be negated so it counts in the right direction. -** It cannot be zero. +** Defaults to 1. +** Cannot be zero. +** We always count from `` toward ``, whether `` is positive or negative. The number of decimal points in the output is fixed, and based on the parameter with the //most// decimal points.