1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 10:29:54 +00:00

Introduce "field" suffix for "has" filter operator (#2066)

* has:field — tested & documented

allows to test whether a field exists

* fixed inverted condition

* added from version to docs
This commit is contained in:
Tobias Beer 2017-02-22 13:13:59 +01:00 committed by Jeremy Ruston
parent cd2bc88658
commit 6085936475
4 changed files with 54 additions and 24 deletions

View File

@ -16,19 +16,37 @@ Filter operator for checking if a tiddler has the specified field
Export our filter function
*/
exports.has = function(source,operator,options) {
var results = [];
if(operator.prefix === "!") {
source(function(tiddler,title) {
if(!tiddler || (tiddler && (!$tw.utils.hop(tiddler.fields,operator.operand) || tiddler.fields[operator.operand] === ""))) {
results.push(title);
}
});
var results = [],
invert = operator.prefix === "!";
if(operator.suffix === "field") {
if(invert) {
source(function(tiddler,title) {
if(!tiddler || (tiddler && (!$tw.utils.hop(tiddler.fields,operator.operand)))) {
results.push(title);
}
});
} else {
source(function(tiddler,title) {
if(tiddler && $tw.utils.hop(tiddler.fields,operator.operand)) {
results.push(title);
}
});
}
} else {
source(function(tiddler,title) {
if(tiddler && $tw.utils.hop(tiddler.fields,operator.operand) && !(tiddler.fields[operator.operand] === "" || tiddler.fields[operator.operand].length === 0)) {
results.push(title);
}
});
if(invert) {
source(function(tiddler,title) {
if(!tiddler || !$tw.utils.hop(tiddler.fields,operator.operand) || (tiddler.fields[operator.operand] !== "")) {
results.push(title);
}
});
} else {
source(function(tiddler,title) {
if(tiddler && $tw.utils.hop(tiddler.fields,operator.operand) && !(tiddler.fields[operator.operand] === "" || tiddler.fields[operator.operand].length === 0)) {
results.push(title);
}
});
}
}
return results;
};

View File

@ -71,11 +71,13 @@ describe("Filter tests", function() {
title: "a fourth tiddler",
text: "The quality of mercy is not drained by [[Tiddler Three]]",
tags: [],
empty: "not",
modifier: "JohnDoe"});
wiki.addTiddler({
title: "one",
text: "This is the text of tiddler [[one]]",
list: "[[Tiddler Three]] [[TiddlerOne]]",
empty: "",
modifier: "JohnDoe"});
// Our tests
@ -191,6 +193,12 @@ describe("Filter tests", function() {
expect(wiki.filterTiddlers("[!has[modified]sort[title]]").join(",")).toBe("a fourth tiddler,one");
});
it("should handle the has:field operator", function() {
expect(wiki.filterTiddlers("[has:field[empty]sort[title]]").join(",")).toBe("a fourth tiddler,one");
expect(wiki.filterTiddlers("[!has:field[empty]sort[title]]").join(",")).toBe("$:/TiddlerTwo,Tiddler Three,TiddlerOne");
});
it("should handle the limit operator", function() {
expect(wiki.filterTiddlers("[!is[system]sort[title]limit[2]]").join(",")).toBe("a fourth tiddler,one");
expect(wiki.filterTiddlers("[prefix[Tid]sort[title]limit[1]]").join(",")).toBe("Tiddler Three");

View File

@ -1,9 +1,11 @@
created: 20150118165921000
modified: 20150118183219000
created: 20151111150157416
emptyfield:
modified: 20151111150201093
tags: [[has Operator]] [[Operator Examples]]
title: has Operator (Examples)
type: text/vnd.tiddlywiki
<<.operator-example 1 "[has[color]]">>
<<.operator-example 2 "[tag[Concepts]!has[modified]]">>
<<.operator-example 3 "[has:field[emptyfield]]">>
<<.operator-example 4 "[all[current]!has:field[doesntexist]]">>

View File

@ -1,14 +1,16 @@
caption: has
created: 20140410103123179
modified: 20150203191843000
modified: 20151111150117431
op-input: a [[selection of titles|Title Selection]]
op-neg-output: ''without suffix'' or with ''suffix `value`''<br>&raquo; those input tiddlers in which field <<.place F>> does <<.em not>> exist or has an empty value<br>''suffix `field`''<br>&raquo; those input tiddlers in which field <<.place F>> exists
op-output: ''without suffix'' or with ''suffix `value`''<br>&raquo; those input tiddlers in which field <<.place F>> has a non-empty value<br>''suffix `field`''<br>&raquo; those input tiddlers in which field <<.place F>> does <<.em not>> exist
op-parameter: the name of a [[field|TiddlerFields]]
op-parameter-name: F
op-purpose: filter the input by field existence
op-suffix: <<.from-version "5.1.14">> optionally, the name of a [[field|TiddlerFields]]
op-suffix-name: F
tags: [[Filter Operators]] [[Common Operators]] [[Field Operators]] [[Negatable Operators]]
title: has Operator
type: text/vnd.tiddlywiki
caption: has
op-purpose: filter the input by field existence
op-input: a [[selection of titles|Title Selection]]
op-parameter: the name of a [[field|TiddlerFields]]
op-parameter-name: F
op-output: those input tiddlers in which field <<.place F>> has a non-empty value
op-neg-output: those input tiddlers in which field <<.place F>> does <<.em not>> exist or has an empty value
<<.operator-examples "has">>
<<.operator-examples "has">>