diff --git a/core/modules/filters/has.js b/core/modules/filters/has.js index 7a1738782..ab73c1e60 100644 --- a/core/modules/filters/has.js +++ b/core/modules/filters/has.js @@ -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; }; diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index 2af6c2e36..1ae98d496 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -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"); diff --git a/editions/tw5.com/tiddlers/filters/examples/has.tid b/editions/tw5.com/tiddlers/filters/examples/has.tid index 7691d9fc4..57941bd94 100644 --- a/editions/tw5.com/tiddlers/filters/examples/has.tid +++ b/editions/tw5.com/tiddlers/filters/examples/has.tid @@ -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]]">> \ No newline at end of file diff --git a/editions/tw5.com/tiddlers/filters/has.tid b/editions/tw5.com/tiddlers/filters/has.tid index 65be9505e..a90f1e2c4 100644 --- a/editions/tw5.com/tiddlers/filters/has.tid +++ b/editions/tw5.com/tiddlers/filters/has.tid @@ -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`''
» those input tiddlers in which field <<.place F>> does <<.em not>> exist or has an empty value
''suffix `field`''
» those input tiddlers in which field <<.place F>> exists +op-output: ''without suffix'' or with ''suffix `value`''
» those input tiddlers in which field <<.place F>> has a non-empty value
''suffix `field`''
» 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">> \ No newline at end of file