Skip to content

Commit

Permalink
Introduce "field" suffix for "has" filter operator (#2066)
Browse files Browse the repository at this point in the history
* has:field — tested & documented

allows to test whether a field exists

* fixed inverted condition

* added from version to docs
  • Loading branch information
tobibeer authored and Jermolene committed Feb 22, 2017
1 parent cd2bc88 commit 6085936
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
42 changes: 30 additions & 12 deletions core/modules/filters/has.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
8 changes: 8 additions & 0 deletions editions/test/tiddlers/tests/test-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down
8 changes: 5 additions & 3 deletions editions/tw5.com/tiddlers/filters/examples/has.tid
Original file line number Diff line number Diff line change
@@ -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]]">>
20 changes: 11 additions & 9 deletions editions/tw5.com/tiddlers/filters/has.tid
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
created: 20140410103123179
modified: 20150203191843000
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
created: 20140410103123179
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-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
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

<<.operator-examples "has">>
<<.operator-examples "has">>

3 comments on commit 6085936

@Jermolene
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tobibeer over on the mailing list, @telmiger asks:

I am confused by the description of the new filter operator suffix has:field as it seems that the examples filter the opposite of what is stated in the documentation.

To me, the examples make sense, the documentation does not. It would be very logical, if the parts marked yellow could be removed/added in the docs:

output
suffix field
» those input tiddlers in which field F does not exist
! output

suffix field
» those input tiddlers in which field F does not exists

Am I missing something?

This is just a detail – many thanks for the great new version anyway!!!

It looks like he might be right, what do you reckon?

@tobibeer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jermolene: Indeed, looks like the docs were inverted from what they should. Looks like it was fixed in the meantime. Thanks and sry for the confusion. :-)

@Jermolene
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tobibeer

Please sign in to comment.