fix: add empty string handler The same fix that was applied to localized-strings. formatString() now works with a key that does not exist

This commit is contained in:
Hendrik Volschenk 2018-05-16 07:41:40 +02:00
parent cde63c758f
commit 04592cd2a8
4 changed files with 406 additions and 401 deletions

View File

@ -10,9 +10,9 @@
* then display the correct language strings or the default language (the first
* one if a match is not found).
*
* This library has been refactored to use the newly created localized-strings package so to
* This library has been refactored to use the newly created localized-strings package so to
* unify the code and make it easier to mantain
*
*
* How to use:
* Check the instructions at:
* https://github.com/stefalda/react-localization
@ -49,7 +49,7 @@ var placeholderRegex = /(\{[\d|\w]+\})/;
* eg. 1: strings.formatString(strings.question, strings.bread, strings.butter)
* eg. 2: strings.formatString(strings.question, { bread: strings.bread, butter: strings.butter }
*
* THIS METHOD OVERRIDE the one of the parent class by adding support for JSX code
* THIS METHOD OVERRIDE the one of the parent class by adding support for JSX code
*/
_localizedStrings2.default.prototype.formatString = function (str) {
for (var _len = arguments.length, valuesForPlaceholders = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
@ -57,7 +57,7 @@ _localizedStrings2.default.prototype.formatString = function (str) {
}
var hasObject = false;
var res = str.split(placeholderRegex).filter(function (textPart) {
var res = (str || '').split(placeholderRegex).filter(function (textPart) {
return !!textPart;
}).map(function (textPart, index) {
if (textPart.match(placeholderRegex)) {

784
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -156,6 +156,11 @@ describe('Main Library Functions', function () {
.toEqual([0, " ", false, " ", '', " ", null, " ", undefined, " ", NaN].join(''));
});
it('Handles empty values', () => {
expect(strings.formatString(strings.thisKeyDoesNotExist, { thisReplacement: 'doesNotExist'}))
.toEqual('');
});
describe('formatString with React components', () => {
const reactStrings = new LocalizedStrings({
en: {
@ -192,4 +197,4 @@ describe('Main Library Functions', function () {
})).toEqual(["Some ", [<span key="1" className="bold">BOLD</span>], " text"]);
});
});
});
});

View File

@ -10,9 +10,9 @@
* then display the correct language strings or the default language (the first
* one if a match is not found).
*
* This library has been refactored to use the newly created localized-strings package so to
* This library has been refactored to use the newly created localized-strings package so to
* unify the code and make it easier to mantain
*
*
* How to use:
* Check the instructions at:
* https://github.com/stefalda/react-localization
@ -31,11 +31,11 @@ const placeholderRegex = /(\{[\d|\w]+\})/;
* eg. 1: strings.formatString(strings.question, strings.bread, strings.butter)
* eg. 2: strings.formatString(strings.question, { bread: strings.bread, butter: strings.butter }
*
* THIS METHOD OVERRIDE the one of the parent class by adding support for JSX code
* THIS METHOD OVERRIDE the one of the parent class by adding support for JSX code
*/
LocalizedStrings.prototype.formatString = (str, ...valuesForPlaceholders) => {
let hasObject = false;
const res = str
const res = (str || '')
.split(placeholderRegex)
.filter(textPart => !!textPart)
.map((textPart, index) => {