From 4f65953da9fadcb3ad3b6b9e3b7baeea75bdb330 Mon Sep 17 00:00:00 2001 From: Saq Imtiaz Date: Sat, 30 Oct 2021 10:52:38 +0200 Subject: [PATCH] Added zero based zth[] operator and documentation (#6150) --- core/modules/filters/listops.js | 12 ++++++++++++ .../examples/zth Operator (Examples).tid | 10 ++++++++++ editions/tw5.com/tiddlers/filters/nth.tid | 16 +++++++++------- .../tw5.com/tiddlers/filters/zth Operator.tid | 18 ++++++++++++++++++ 4 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 editions/tw5.com/tiddlers/filters/examples/zth Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/filters/zth Operator.tid diff --git a/core/modules/filters/listops.js b/core/modules/filters/listops.js index 0c30c22e7..ab194339a 100644 --- a/core/modules/filters/listops.js +++ b/core/modules/filters/listops.js @@ -103,4 +103,16 @@ exports.nth = function(source,operator,options) { return results.slice(count - 1,count); }; +/* +The zero based nth member of the list +*/ +exports.zth = function(source,operator,options) { + var count = $tw.utils.getInt(operator.operand,0), + results = []; + source(function(tiddler,title) { + results.push(title); + }); + return results.slice(count,count + 1); +}; + })(); diff --git a/editions/tw5.com/tiddlers/filters/examples/zth Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/zth Operator (Examples).tid new file mode 100644 index 000000000..32f610213 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/zth Operator (Examples).tid @@ -0,0 +1,10 @@ +created: 20211029023246203 +modified: 20211029023559616 +tags: [[Operator Examples]] [[zth Operator]] +title: zth Operator (Examples) +type: text/vnd.tiddlywiki + +<<.using-days-of-week>> + +<<.operator-example 1 "[list[Days of the Week]zth[]]">> +<<.operator-example 2 "[list[Days of the Week]zth[5]]">> diff --git a/editions/tw5.com/tiddlers/filters/nth.tid b/editions/tw5.com/tiddlers/filters/nth.tid index 032ac53ca..fc3315d72 100644 --- a/editions/tw5.com/tiddlers/filters/nth.tid +++ b/editions/tw5.com/tiddlers/filters/nth.tid @@ -1,15 +1,17 @@ +caption: nth created: 20150122204111000 -modified: 20150203192048000 +modified: 20211029023739450 +op-input: a [[selection of titles|Title Selection]] +op-output: the <<.place N>>th input title +op-parameter: an integer, defaulting to 1 +op-parameter-name: N +op-purpose: select the <<.place N>>th input title tags: [[Filter Operators]] [[Order Operators]] title: nth Operator type: text/vnd.tiddlywiki -caption: nth -op-purpose: select the <<.place N>>th input title -op-input: a [[selection of titles|Title Selection]] -op-parameter: an integer, defaulting to 1 -op-parameter-name: N -op-output: the <<.place N>>th input title <<.place N>> is one-based. In other words, `nth[1]` has the same effect as the <<.olink first>> operator. +<<.tip "See <<.olink zth>> for an equivalent operator with a 0 based parameter">> + <<.operator-examples "nth">> diff --git a/editions/tw5.com/tiddlers/filters/zth Operator.tid b/editions/tw5.com/tiddlers/filters/zth Operator.tid new file mode 100644 index 000000000..4c8323c7e --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/zth Operator.tid @@ -0,0 +1,18 @@ +caption: zth +created: 20211029023242012 +modified: 20211029023711450 +op-input: a [[selection of titles|Title Selection]] +op-output: the <<.place Z>>th input title +op-parameter: an integer, defaulting to 0 +op-parameter-name: Z +op-purpose: select the <<.place Z>>th input title +tags: [[Filter Operators]] [[Order Operators]] +title: zth Operator +type: text/vnd.tiddlywiki + +<<.from-version 5.2.1>> +<<.place Z>> is zero-based. In other words, `zth[0]` has the same effect as `nth[1]` and the <<.olink first>> operator. + +<<.tip "See <<.olink nth>> for an equivalent operator with a 1 based parameter">> + +<<.operator-examples "zth">>