From cfcb255c237d3b759182e37e079756a2cd1af5f3 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Wed, 9 May 2012 09:40:10 +0100 Subject: [PATCH] Added limit filter operator --- core/modules/wiki.filters.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/modules/wiki.filters.js b/core/modules/wiki.filters.js index acf496ba7..902078812 100644 --- a/core/modules/wiki.filters.js +++ b/core/modules/wiki.filters.js @@ -31,6 +31,8 @@ The operators look like `[operator[operand]]`, where `operator` is one of: * ''title'': selects the tiddler with the title given in the operand * ''is'': tests whether a tiddler is a member of the system defined set named in the operand (see below) * ''has'': tests whether a tiddler has a specified field +* ''sort'': sorts the tiddlers by a given field +* ''limit'': limits the number of subresults * ''tag'': tests whether a given tag is (`[tag[mytag]]`) or is not (`[!tag[mytag]]`) present on the tiddler * '''': tests whether a tiddler field has a specified value (`[modifier[Jeremy]]`) or not (`[!modifier[Jeremy]]`) @@ -192,6 +194,16 @@ exports.operators = { return "this.sortTiddlers(subResults,\"" + $tw.utils.stringify(operator.operand) + "\"," + desc + ");"; } }, + "limit": { + selector: function(operator) { + throw "Cannot use limit operator at the start of a filter operation"; + }, + filter: function(operator) { + var limit = parseInt(operator.operand,10), + base = operator.prefix === "!" ? 0 : limit; + return "if(subResults.length > " + limit + ") {subResults.splice(" + base + ",subResults.length-" + limit + ");}"; + } + }, "field": { selector: function(operator) { var op = operator.prefix === "!" ? "!" : "=";