Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add case conversion Handlebars helpers #719

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/handlebars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>"Greetings."<br>"Hi there!"<br>"Howdy"<br>"Hello, how are you?"<br>"Whassup dude!"<br>}}|
|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..
Expand Down Expand Up @@ -127,4 +130,3 @@ Here is a link to my S3 doc: [link]({{signS3URL 'https://qnabot-docs.s3.amazonaw


```

26 changes: 26 additions & 0 deletions lambda/es-proxy-layer/lib/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down