From 9b27f82a805df51ebc240593978a549138588c6c Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Fri, 21 Jun 2019 02:24:02 -0500 Subject: [PATCH] Fix sentencecase operator, add titlecase operator (#4006) --- core/modules/filters/strings.js | 4 ++++ core/modules/utils/utils.js | 11 +++++++++-- .../sentencecase Operator (Examples).tid | 6 ++++-- .../examples/titlecase Operator (Examples).tid | 10 ++++++++++ .../tiddlers/filters/sentencecase Operator.tid | 8 +++++--- .../tiddlers/filters/titlecase Operator.tid | 17 +++++++++++++++++ 6 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 editions/tw5.com/tiddlers/filters/examples/titlecase Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/filters/titlecase Operator.tid diff --git a/core/modules/filters/strings.js b/core/modules/filters/strings.js index d2a9b5f0b..c62dcaac6 100644 --- a/core/modules/filters/strings.js +++ b/core/modules/filters/strings.js @@ -30,6 +30,10 @@ exports.sentencecase = makeStringBinaryOperator( function(a) {return [$tw.utils.toSentenceCase(a)];} ); +exports.titlecase = makeStringBinaryOperator( + function(a) {return [$tw.utils.toTitleCase(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 1b1ccb548..289f132f8 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -93,12 +93,19 @@ exports.trim = function(str) { }; /* -Convert a string to sentence case (ie capitalise each initial letter) +Convert a string to sentence case (ie capitalise first letter) */ exports.toSentenceCase = function(str) { - return (str || "").toLowerCase().replace(/(^|\s)\S/g, function(c) {return c.toUpperCase();}); + return (str || "").replace(/^\S/, function(c) {return c.toUpperCase();}); } +/* +Convert a string to title case (ie capitalise each initial letter) +*/ +exports.toTitleCase = function(str) { + return (str || "").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/tw5.com/tiddlers/filters/examples/sentencecase Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/sentencecase Operator (Examples).tid index 5dd2a26bc..0511eafee 100644 --- a/editions/tw5.com/tiddlers/filters/examples/sentencecase Operator (Examples).tid +++ b/editions/tw5.com/tiddlers/filters/examples/sentencecase Operator (Examples).tid @@ -1,8 +1,10 @@ created: 20190619110741485 -modified: 20190619110812080 +modified: 20190620140353983 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[]]">> +<<.operator-example 2 "[[abc deF gHi jKL]sentencecase[]]">> +<<.operator-example 3 "[[abc deF gHi jKL]lowercase[]sentencecase[]]">> +<<.operator-example 4 "[tag[HelloThere]sentencecase[]]">> diff --git a/editions/tw5.com/tiddlers/filters/examples/titlecase Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/titlecase Operator (Examples).tid new file mode 100644 index 000000000..ff52026cf --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/titlecase Operator (Examples).tid @@ -0,0 +1,10 @@ +created: 20190620140005348 +modified: 20190620140412655 +tags: [[uppercase Operator]] [[Operator Examples]] +title: sentencecase Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 "[[abc def ghi jkl]titlecase[]]">> +<<.operator-example 2 "[[abc deF gHi jKL]titlecase[]]">> +<<.operator-example 3 "[[abc deF gHi jKL]lowercase[]titlecase[]]">> +<<.operator-example 4 "[tag[HelloThere]titlecase[]]">> diff --git a/editions/tw5.com/tiddlers/filters/sentencecase Operator.tid b/editions/tw5.com/tiddlers/filters/sentencecase Operator.tid index dc2262ee2..1bcd3cb19 100644 --- a/editions/tw5.com/tiddlers/filters/sentencecase Operator.tid +++ b/editions/tw5.com/tiddlers/filters/sentencecase Operator.tid @@ -2,14 +2,16 @@ 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 +op-output: the input titles with each first letter capitalised +op-purpose: returns each item in the list with the first letter capitalised tags: [[Filter Operators]] title: sentencecase Operator type: text/vnd.tiddlywiki <<.from-version "5.1.20">> -See also [[lowercase Operator]], [[uppercase Operator]]. +See also [[lowercase Operator]], [[uppercase Operator]], [[titlecase Operator]]. + +<<.tip " This operator does not change anything except the first letter of each title, so `[[aBcD eFgH]]` would become `[[ABcD eFgH]]`. If you want `[[Abcd efgh]]`, then use a filter like `[lowercase[]sentencecase[]]`.">> <<.operator-examples "sentencecase">> diff --git a/editions/tw5.com/tiddlers/filters/titlecase Operator.tid b/editions/tw5.com/tiddlers/filters/titlecase Operator.tid new file mode 100644 index 000000000..2b869faaf --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/titlecase Operator.tid @@ -0,0 +1,17 @@ +caption: titlecase +created: 20190620135008529 +modified: 20190620135008529 +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 with each word capitalised +tags: [[Filter Operators]] +title: titlecase Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.20">> + +See also [[lowercase Operator]], [[uppercase Operator]], [[sentencecase Operator]]. + +<<.tip " This operator does not change anything except the first letter of each word, so `[[aBcD eFgH]]` would become `[[ABcD EFgH]]`. If you want `[[Abcd Efgh]]`, then use a filter like `[lowercase[]titlecase[]]`.">> + +<<.operator-examples "titlecase">>