From dda9cbfeaa29d5ebd81924c4b744b6ac7f206985 Mon Sep 17 00:00:00 2001 From: Adam Mendlik Date: Tue, 23 Jan 2024 10:12:53 -0700 Subject: [PATCH] Add case conversion Handlebars helpers --- docs/handlebars/README.md | 4 +++- lambda/es-proxy-layer/lib/handlebars.js | 26 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/handlebars/README.md b/docs/handlebars/README.md index d337851d..3cdf72b3 100644 --- a/docs/handlebars/README.md +++ b/docs/handlebars/README.md @@ -60,6 +60,9 @@ QnABot also provides these additional helpers: |getSlot | Returns named slot value if it is defined, or default value. | {{getSlot '_slotName_' '_default_'}} | |randomPick | Randomly return a string selected from a list. | {{randomPick
"Greetings."
"Hi there!"
"Howdy"
"Hello, how are you?"
"Whassup dude!"
}}| |signS3URL | Converts S3 URL to a signed URL with 300 sec expiration. S3 bucket name must start with QNA or qna, or policy granting bucket read access must be added to ESProxyLambdaRole. | {{signS3URL 'https://qnabot-images.s3.amazonaws.com/testimage.png'}}| +|`toUpperCase` | Convert a string to UPPER CASE | `{{toUpperCase 'hello, world!'}}` | +|`toLowerCase` | Convert a string to lower case | `{{toLowerCase 'hello, world!'}}` | +|`toTitleCase` | Convert a string to Title Case | `{{toTitleCase 'hello, world!'}}` | ## Comments Use the handlebars comment syntax to make your handlebars easier to understand.. @@ -127,4 +130,3 @@ Here is a link to my S3 doc: [link]({{signS3URL 'https://qnabot-docs.s3.amazonaw ``` - diff --git a/lambda/es-proxy-layer/lib/handlebars.js b/lambda/es-proxy-layer/lib/handlebars.js index f0eb2d86..529a8fe3 100644 --- a/lambda/es-proxy-layer/lib/handlebars.js +++ b/lambda/es-proxy-layer/lib/handlebars.js @@ -195,6 +195,32 @@ Handlebars.registerHelper('randomPick', function () { return item; }); +Handlebars.registerHelper('toLowerCase', function(str) { + if (str === null) + return false; + + str = str.toString(); + return str.toLowerCase(); +}); + +Handlebars.registerHelper('toUpperCase', function(str) { + if (str === null) + return false; + + str = str.toString(); + return str.toUpperCase(); +}); + +Handlebars.registerHelper('toTitleCase', function(str) { + if (str === null) + return false; + + str = str.toString(); + return str.toLowerCase().split(' ').map(function (word) { + return (word.charAt(0).toUpperCase() + word.slice(1)); + }).join(' '); +}); + async function replaceAsync(str, regex) { let m; let matches = [];