From 2e2ed7902ccb7c2f8093634d8abbade0765bd97c Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Wed, 19 Jun 2019 12:11:02 +0100 Subject: [PATCH] Add sentencecase operator Fixes #4000 --- core/modules/filters/strings.js | 4 ++++ core/modules/utils/utils.js | 7 +++++++ editions/prerelease/tiddlers/Release 5.1.20.tid | 2 +- .../examples/sentencecase Operator (Examples).tid | 8 ++++++++ .../tiddlers/filters/lowercase Operator.tid | 4 ++-- .../tiddlers/filters/sentencecase Operator.tid | 15 +++++++++++++++ .../tiddlers/filters/uppercase Operator.tid | 4 ++-- 7 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 editions/tw5.com/tiddlers/filters/examples/sentencecase Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/filters/sentencecase Operator.tid diff --git a/core/modules/filters/strings.js b/core/modules/filters/strings.js index d844f7c56..d2a9b5f0b 100644 --- a/core/modules/filters/strings.js +++ b/core/modules/filters/strings.js @@ -26,6 +26,10 @@ exports.lowercase = makeStringBinaryOperator( function(a) {return [("" + a).toLowerCase()];} ); +exports.sentencecase = makeStringBinaryOperator( + function(a) {return [$tw.utils.toSentenceCase(a)];} +); + exports.trim = makeStringBinaryOperator( function(a) {return [$tw.utils.trim(a)];} ); diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index 672961a40..1b1ccb548 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -92,6 +92,13 @@ exports.trim = function(str) { } }; +/* +Convert a string to sentence case (ie capitalise each initial letter) +*/ +exports.toSentenceCase = function(str) { + return (str || "").toLowerCase().replace(/(^|\s)\S/g, function(c) {return c.toUpperCase();}); +} + /* Find the line break preceding a given position in a string Returns position immediately after that line break, or the start of the string diff --git a/editions/prerelease/tiddlers/Release 5.1.20.tid b/editions/prerelease/tiddlers/Release 5.1.20.tid index d33006329..c1e52d6b2 100644 --- a/editions/prerelease/tiddlers/Release 5.1.20.tid +++ b/editions/prerelease/tiddlers/Release 5.1.20.tid @@ -11,7 +11,7 @@ type: text/vnd.tiddlywiki Added several new [[Mathematics Operators]] for working with numbers: [[negate|negate Operator]], [[abs|abs Operator]], [[ceil|ceil Operator]], [[floor|floor Operator]], [[round|round Operator]], [[trunc|trunc Operator]], [[untrunc|untrunc Operator]], [[sign|sign Operator]], [[add|add Operator]], [[subtract|subtract Operator]], [[multiply|multiply Operator]], [[divide|divide Operator]], [[remainder|remainder Operator]], [[max|max Operator]], [[min|min Operator]], [[fixed|fixed Operator]], [[precision|precision Operator]], [[exponential|exponential Operator]], [[sum|sum Operator]], [[product|product Operator]], [[maxall|maxall Operator]] and [[minall|minall Operator]]. -Added several new string operators: [[length|length Operator]], [[uppercase|uppercase Operator]], [[lowercase|lowercase Operator]], [[trim|trim Operator]], [[split|split Operator]], [[splitregexp|splitregexp Operator]] and [[join|join Operator]]. +Added several new string operators: [[length|length Operator]], [[uppercase|uppercase Operator]], [[lowercase|lowercase Operator]], [[sentencecase|sentencecase Operator]], [[trim|trim Operator]], [[split|split Operator]], [[splitregexp|splitregexp Operator]] and [[join|join Operator]]. There is now finer control over TiddlyWiki's default behaviour of removing duplicates from filter results, making it much easier to work with the new mathematics and string operators: diff --git a/editions/tw5.com/tiddlers/filters/examples/sentencecase Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/sentencecase Operator (Examples).tid new file mode 100644 index 000000000..5dd2a26bc --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/sentencecase Operator (Examples).tid @@ -0,0 +1,8 @@ +created: 20190619110741485 +modified: 20190619110812080 +tags: [[uppercase Operator]] [[Operator Examples]] +title: sentencecase Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 "[[abc def ghi jkl]sentencecase[]]">> +<<.operator-example 2 "[tag[HelloThere]sentencecase[]]">> diff --git a/editions/tw5.com/tiddlers/filters/lowercase Operator.tid b/editions/tw5.com/tiddlers/filters/lowercase Operator.tid index 1efaec980..110c79345 100644 --- a/editions/tw5.com/tiddlers/filters/lowercase Operator.tid +++ b/editions/tw5.com/tiddlers/filters/lowercase Operator.tid @@ -1,6 +1,6 @@ caption: lowercase created: 20190613153650010 -modified: 20190613153726427 +modified: 20190619110848522 op-input: a [[selection of titles|Title Selection]] op-output: the input titles with each uppercase letter replaced by the equivalent lowercase letter op-purpose: returns each item in the list as lowercase @@ -10,6 +10,6 @@ type: text/vnd.tiddlywiki <<.from-version "5.1.20">> -See also [[uppercase Operator]]. +See also [[uppercase Operator]] and [[sentencecase Operator]]. <<.operator-examples "lowercase">> diff --git a/editions/tw5.com/tiddlers/filters/sentencecase Operator.tid b/editions/tw5.com/tiddlers/filters/sentencecase Operator.tid new file mode 100644 index 000000000..dc2262ee2 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/sentencecase Operator.tid @@ -0,0 +1,15 @@ +caption: sentencecase +created: 20190619110607457 +modified: 20190619110736016 +op-input: a [[selection of titles|Title Selection]] +op-output: the input titles with each word capitalised +op-purpose: returns each item in the list capitalised +tags: [[Filter Operators]] +title: sentencecase Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.20">> + +See also [[lowercase Operator]], [[uppercase Operator]]. + +<<.operator-examples "sentencecase">> diff --git a/editions/tw5.com/tiddlers/filters/uppercase Operator.tid b/editions/tw5.com/tiddlers/filters/uppercase Operator.tid index f52031bee..192922a3c 100644 --- a/editions/tw5.com/tiddlers/filters/uppercase Operator.tid +++ b/editions/tw5.com/tiddlers/filters/uppercase Operator.tid @@ -1,6 +1,6 @@ caption: uppercase created: 20190613153420821 -modified: 20190613153538557 +modified: 20190619110837473 op-input: a [[selection of titles|Title Selection]] op-output: the input titles with each lowercase letter replaced by the equivalent uppercase letter op-purpose: returns each item in the list as uppercase @@ -10,6 +10,6 @@ type: text/vnd.tiddlywiki <<.from-version "5.1.20">> -See also [[lowercase Operator]]. +See also [[lowercase Operator]] and [[sentencecase Operator]]. <<.operator-examples "uppercase">>