-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
subjectPatternError
option to allow users to override the…
… default error when `subjectPattern` doesn't match (#76) * feat: add subjectPatternError option to set custom sub validation error * fix: pu same string into one variable in test * chore: some console log for debugging stuff that doesn't happen on local * fix variable name * feat: added templates * Apply suggestions from code review Co-authored-by: Jan Amann <[email protected]> * message formatter generalized * Update src/formatMessage.test.js * Fix lint Co-authored-by: Jan Amann <[email protected]>
- Loading branch information
Showing
8 changed files
with
107 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = function formatMessage(message, values) { | ||
let formatted = message; | ||
if (values) { | ||
Object.entries(values).forEach(([key, value]) => { | ||
formatted = formatted.replace(new RegExp(`{${key}}`, 'g'), value); | ||
}); | ||
} | ||
return formatted; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const formatMessage = require('./formatMessage'); | ||
|
||
it('handles a string without variables', () => { | ||
const message = 'this is test'; | ||
expect(formatMessage(message)).toEqual(message); | ||
}); | ||
|
||
it('replaces a variable', () => { | ||
expect( | ||
formatMessage('this is {subject} test', {subject: 'my subject'}) | ||
).toEqual('this is my subject test'); | ||
}); | ||
|
||
it('replaces multiple variables', () => { | ||
expect( | ||
formatMessage('this {title} is {subject} test', { | ||
subject: 'my subject', | ||
title: 'my title' | ||
}) | ||
).toEqual('this my title is my subject test'); | ||
}); | ||
|
||
it('replaces multiple instances of a variable', () => { | ||
expect( | ||
formatMessage( | ||
'99 bottles of {beverage} on the wall, 99 bottles of {beverage}.', | ||
{beverage: 'beer'} | ||
) | ||
).toEqual('99 bottles of beer on the wall, 99 bottles of beer.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters