Skip to content

Commit

Permalink
Add suffix and removesuffix filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jermolene committed Aug 28, 2014
1 parent 798ed46 commit dcf4e93
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
28 changes: 28 additions & 0 deletions core/modules/filters/removesuffix.js
@@ -0,0 +1,28 @@
/*\
title: $:/core/modules/filters/removesuffix.js
type: application/javascript
module-type: filteroperator
Filter operator for removing a suffix from each title in the list. Titles that do not end with the suffix are removed.
\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Export our filter function
*/
exports.removesuffix = function(source,operator,options) {
var results = [];
source(function(tiddler,title) {
if(title.substr(-operator.operand.length).toLowerCase() === operator.operand.toLowerCase()) {
results.push(title.substr(operator.operand.length));
}
});
return results;
};

})();
36 changes: 36 additions & 0 deletions core/modules/filters/suffix.js
@@ -0,0 +1,36 @@
/*\
title: $:/core/modules/filters/suffix.js
type: application/javascript
module-type: filteroperator
Filter operator for checking if a title ends with a suffix
\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Export our filter function
*/
exports.suffix = function(source,operator,options) {
var results = [];
if(operator.prefix === "!") {
source(function(tiddler,title) {
if(title.substr(-operator.operand.length).toLowerCase() !== operator.operand.toLowerCase()) {
results.push(title);
}
});
} else {
source(function(tiddler,title) {
if(title.substr(-operator.operand.length).toLowerCase() === operator.operand.toLowerCase()) {
results.push(title);
}
});
}
return results;
};

})();
Expand Up @@ -12,4 +12,4 @@ For example:
|`[tag[task]!prefix[hidden]]` |Returns tiddlers tagged `task` whose titles do not start with `hidden` |
|`[prefix[$:/]]` |Equivalent to `[is[system]]` |

See also [[FilterOperator: removeprefix]].
See also [[FilterOperator: removeprefix]], [[FilterOperator: removesuffix]] and [[FilterOperator: removesuffix]].
Expand Up @@ -11,4 +11,4 @@ For example:
|!Filter String |!Description |
|`tid-one tid-two three +[removeprefix[tid-]]` |Returns `one`, `two` |

See also [[FilterOperator: prefix]].
See also [[FilterOperator: prefix]], [[FilterOperator: suffix]] and [[FilterOperator: removesuffix]].
14 changes: 14 additions & 0 deletions editions/tw5.com/tiddlers/filters/FilterOperator removesuffix.tid
@@ -0,0 +1,14 @@
created: 20140828133830424
modified: 20140828133830424
tags: filters
title: FilterOperator: removesuffix
type: text/vnd.tiddlywiki

The ''removesuffix'' filter operator returns the titles in the current list that end with a specified suffix with the suffix removed.

For example:

|!Filter String |!Description |
|`tid-one tid-two three +[removeprefix[tid-]]` |Returns `one`, `two` |

See also [[FilterOperator: suffix]], [[FilterOperator: prefix]] and [[FilterOperator: removeprefix]].
15 changes: 15 additions & 0 deletions editions/tw5.com/tiddlers/filters/FilterOperator suffix.tid
@@ -0,0 +1,15 @@
created: 20140828133830424
modified: 20140828133830424
tags: filters
title: FilterOperator: suffix
type: text/vnd.tiddlywiki

The ''suffix'' filter operator returns the titles in the current list that end with a specified suffix. If the ''suffix'' operator is preceded by `!` then it returns the titles that do not end with the specified suffix.

For example:

|!Filter String |!Description |
|`[tag[task]!suffix[hidden]]` |Returns tiddlers tagged `task` whose titles do not end with `hidden` |
|`[suffix[.jpg]]` |Returns tiddlers whose titles end with `.jpg` |

See also [[FilterOperator: removesuffix]], [[FilterOperator: prefix]] and [[FilterOperator: removeprefix]].

0 comments on commit dcf4e93

Please sign in to comment.