mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-02-14 10:00:02 +00:00
Update range operator to use multiple operands
We still also support the old way of packing all three parameters into one operand with a delimiter
This commit is contained in:
parent
137df37bc7
commit
e6fd0caf6b
@ -17,9 +17,13 @@ Export our filter function
|
||||
*/
|
||||
exports.range = function(source,operator,options) {
|
||||
var results = [];
|
||||
// Split the operand into numbers delimited by these symbols
|
||||
var parts = operator.operand.split(/[,:;]/g),
|
||||
beg, end, inc, i, fixed = 0;
|
||||
// For backwards compatibility, if there is only one operand, try to split it using one of the delimiters
|
||||
var parts = operator.operands || [];
|
||||
if(parts.length === 1) {
|
||||
parts = operator.operand.split(/[,:;]/g);
|
||||
}
|
||||
// Process the parts
|
||||
var beg, end, inc, i, fixed = 0;
|
||||
for (i=0; i<parts.length; i++) {
|
||||
// Validate real number
|
||||
if(!/^\s*[+-]?((\d+(\.\d*)?)|(\.\d+))\s*$/.test(parts[i])) {
|
||||
|
@ -491,6 +491,20 @@ function runTests(wiki) {
|
||||
expect(wiki.filterTiddlers("[list[TiddlerSeventh]before[MissingTiddler]]").join(",")).toBe("a fourth tiddler");
|
||||
});
|
||||
|
||||
it("should handle the range operator", function() {
|
||||
expect(wiki.filterTiddlers("[range[10:0:2]]").join(",")).toBe("10,8,6,4,2,0");
|
||||
expect(wiki.filterTiddlers("[range[10;0;2]]").join(",")).toBe("10,8,6,4,2,0");
|
||||
expect(wiki.filterTiddlers("[range[1.001,5,1]]").join(",")).toBe("1.001,2.001,3.001,4.001");
|
||||
expect(wiki.filterTiddlers("[range[0,10]]").join(",")).toBe("0,1,2,3,4,5,6,7,8,9,10");
|
||||
expect(wiki.filterTiddlers("[range[0],[10]]").join(",")).toBe("0,1,2,3,4,5,6,7,8,9,10");
|
||||
expect(wiki.filterTiddlers("[range[10,0]]").join(",")).toBe("10,9,8,7,6,5,4,3,2,1,0");
|
||||
expect(wiki.filterTiddlers("[range[10],[0]]").join(",")).toBe("10,9,8,7,6,5,4,3,2,1,0");
|
||||
expect(wiki.filterTiddlers("[range[0,10,2]]").join(",")).toBe("0,2,4,6,8,10");
|
||||
expect(wiki.filterTiddlers("[range[0],[10],[2]]").join(",")).toBe("0,2,4,6,8,10");
|
||||
expect(wiki.filterTiddlers("[range[10,0,2]]").join(",")).toBe("10,8,6,4,2,0");
|
||||
expect(wiki.filterTiddlers("[range[10],[0],[2]]").join(",")).toBe("10,8,6,4,2,0");
|
||||
});
|
||||
|
||||
it("should handle the string operators", function() {
|
||||
expect(wiki.filterTiddlers("John Paul George Ringo +[length[]]").join(",")).toBe("4,4,6,5");
|
||||
expect(wiki.filterTiddlers("John Paul George Ringo +[uppercase[]]").join(",")).toBe("JOHN,PAUL,GEORGE,RINGO");
|
||||
|
@ -1,14 +1,15 @@
|
||||
created: 20171221184734665
|
||||
modified: 20171229211834620
|
||||
modified: 20210907170339891
|
||||
tags: [[Filter Operators]] [[Negatable Operators]]
|
||||
title: range Operator
|
||||
type: text/vnd.tiddlywiki
|
||||
caption: range
|
||||
op-purpose: generate a range of numbers
|
||||
op-input: ignored
|
||||
op-parameter: a range specification, like `[1,5]`
|
||||
op-parameter: a range specification, like `[1],[5]`
|
||||
op-parameter-name: N
|
||||
op-output: a series of evenly spaced numbers ranging from `<begin>` to `<end>`
|
||||
op-neg-output: a series of evenly spaced numbers ranging from `<begin>` to `<end>` in reverse order
|
||||
|
||||
\define range_example(range)
|
||||
```
|
||||
@ -18,20 +19,22 @@ op-output: a series of evenly spaced numbers ranging from `<begin>` to `<end>`
|
||||
<$list variable=n filter="[range[$range$]]"><<n>> </$list>
|
||||
\end
|
||||
|
||||
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]].
|
||||
The `range` operator produces a list of numbers counting up or down. It is useful for counting and numbering.
|
||||
|
||||
|!Purpose|produce a range of numbers|
|
||||
|!Input|ignored.|
|
||||
|!Parameter|1-3 numbers separated by `,` or `;`.|
|
||||
|!Output|A series of evenly spaced numbers ranging from `<begin>` to `<end>`|
|
||||
|!`!` Output|As ''Output'', but with order reversed.|
|
||||
<<.from-version "5.2.0">> The range operator has been updated to use multiple operands to specify its parameters. Prior to this version, the range operator only had one operand, with the three parts delimited by `,`, `;` or `:`.
|
||||
|
||||
The parameter has three forms:
|
||||
```
|
||||
[range[<begin>]]
|
||||
[range[<begin>],[<end>]]
|
||||
[range[<begin>],[<end>],[<step>]]
|
||||
```
|
||||
|
||||
|Parameter|Output|h
|
||||
|`<end>`|Whole numbers up to `<end>`.|
|
||||
|`<begin>,<end>`|Numbers from `<begin>` to `<end>`, spaced by whole numbers.|
|
||||
|`<begin>,<end>,<step>`|Numbers from `<begin>` to `<end>` spaced out by `<step>`.|
|
||||
The behaviour depends on the number of operands:
|
||||
|
||||
|Parameter |Output |h
|
||||
|`<end>` |Whole numbers up to `<end>` |
|
||||
|`<begin>,<end>` |Numbers from `<begin>` to `<end>`, spaced by whole numbers |
|
||||
|`<begin>,<end>,<step>` |Numbers from `<begin>` to `<end>` spaced out by `<step>` |
|
||||
|
||||
Each part must be a number, and works as follows:
|
||||
|
||||
@ -44,19 +47,18 @@ Each part must be a number, and works as follows:
|
||||
** Cannot be zero.
|
||||
** We always count from `<begin>` toward `<end>`, whether `<step>` is positive or negative.
|
||||
|
||||
The number of decimal points in the output is fixed, and based on the parameter with the //most// decimal points.
|
||||
The number of decimal points in the output is fixed, and based on the operand with the //most// decimal points.
|
||||
|
||||
To prevent the browser from freezing, `range` is currently limited to 10,000 values.
|
||||
|
||||
|
||||
!!Examples
|
||||
|
||||
<<range_example "7">>
|
||||
|
||||
<<range_example "1, 10">>
|
||||
<<range_example "1],[10">>
|
||||
|
||||
<<range_example "17,13">>
|
||||
<<range_example "17],[13">>
|
||||
|
||||
<<range_example "1.001, 5, 1">>
|
||||
<<range_example "1.001],[5],[1">>
|
||||
|
||||
<<range_example ".5,1.4,.004">>
|
||||
<<range_example ".5],[1.4],[.004">>
|
Loading…
x
Reference in New Issue
Block a user