-
Notifications
You must be signed in to change notification settings - Fork 4
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
fix: refactor escapeHtml fn to cast nullish values to empty string #127
fix: refactor escapeHtml fn to cast nullish values to empty string #127
Conversation
@mostafa-raafat Could you also please add a fallback value.
|
add tests to assert the fallback behaviour
@Matwog Thank you for the suggestion 👍 |
…lish-values-to-empty-string' of github.com:signavio/i18n into refactor(translate)--refactor-escapeHtml-fn-to-cast-nullish-values-to-empty-string
@@ -70,7 +70,7 @@ export default (singleton) => { | |||
) | |||
} | |||
|
|||
function applyMarkdown(translation) { | |||
function applyMarkdown(translation = '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hint: adds fallback
@@ -93,7 +93,7 @@ export default (singleton) => { | |||
return <span key={key} dangerouslySetInnerHTML={{ __html: html }} /> | |||
} | |||
|
|||
function insertInterpolations(translation, options) { | |||
function insertInterpolations(translation = '', options) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hint: adds fallback
// Used to map characters to HTML entities. | ||
const htmlEscapes = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
"'": ''', | ||
} | ||
|
||
// Used to match HTML entities and HTML characters. | ||
const reUnescapedHtml = /[&<>"']/g | ||
// Cast (null,undefined,[] and 0 to empty string => '') | ||
const reHasUnescapedHtml = RegExp(reUnescapedHtml.source) | ||
|
||
return unsafe && reHasUnescapedHtml.test(unsafe) | ||
? unsafe.replace(reUnescapedHtml, (chr) => htmlEscapes[chr]) | ||
: unsafe || '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hint: adds fallback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏆
it('should handle undefined values with markdown', () => { | ||
expect( | ||
i18n(undefined, { | ||
markdown: true, | ||
}) | ||
).toStrictEqual([]) | ||
}) | ||
|
||
it('should handle undefined values without markdown', () => { | ||
expect(i18n(undefined, {})).toStrictEqual([]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hint: adds tests
No description provided.