Skip to content

Commit

Permalink
fix: passed format parameter value truncation in options argument for…
Browse files Browse the repository at this point in the history
… custom formatter, fixes #1715
  • Loading branch information
adrai committed Jan 4, 2022
1 parent 466b8ed commit ef13e2e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 21.6.5

- fix: passed format parameter value truncation in options argument for custom formatter [1715](https://github.com/i18next/i18next/issues/1715)

## 21.6.4

- fix: skipOnVariables (and all other interpolation options should respect defaults) [1711](https://github.com/i18next/i18next/issues/1711)
Expand Down
36 changes: 7 additions & 29 deletions i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -1765,31 +1765,8 @@
if (Array.isArray(arr)) return arr;
}

function _iterableToArrayLimit(arr, i) {
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;

try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);

if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}

return _arr;
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
}

function _arrayLikeToArray(arr, len) {
Expand All @@ -1815,8 +1792,8 @@
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}

function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
function _toArray(arr) {
return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest();
}

function ownKeys$4(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
Expand All @@ -1842,10 +1819,11 @@
if (!opt) return;

var _opt$split = opt.split(':'),
_opt$split2 = _slicedToArray(_opt$split, 2),
_opt$split2 = _toArray(_opt$split),
key = _opt$split2[0],
val = _opt$split2[1];
rest = _opt$split2.slice(1);

var val = rest.join(':');
if (val.trim() === 'false') formatOptions[key.trim()] = false;
if (val.trim() === 'true') formatOptions[key.trim()] = true;
if (!isNaN(val.trim())) formatOptions[key.trim()] = parseInt(val.trim(), 10);
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function parseFormatStr(formatStr) {

opts.forEach((opt) => {
if (!opt) return;
const [key, val] = opt.split(':');
const [key, ...rest] = opt.split(':');
const val = rest.join(':');

if (val.trim() === 'false') formatOptions[key.trim()] = false;
if (val.trim() === 'true') formatOptions[key.trim()] = true;
Expand Down
13 changes: 13 additions & 0 deletions test/i18next.translation.formatting.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ describe('i18next.translation.formatting', () => {
intlRelativeTimeWithOptionsExplicit:
'Lorem {{val, relativetime(range: quarter; style: narrow;)}}',
intlList: 'A list of {{val, list}}',
keyCustomFormatWithColon:
'Before {{date, customdate(format: EEEE d MMMM yyyy HH:mm; otherParam: 0)}}',
},
},
},
Expand All @@ -59,6 +61,9 @@ describe('i18next.translation.formatting', () => {
instance.services.formatter.add('encodeuricomponent', (value, lng, options) => {
return encodeURIComponent(value);
});
instance.services.formatter.add('customdate', (value, lng, options) => {
return `customized date in format ${options.format} (and other param ${options.otherParam})`;
});
done();
},
);
Expand Down Expand Up @@ -209,6 +214,14 @@ describe('i18next.translation.formatting', () => {
},
]);

// custom
tests = tests.concat([
{
args: ['keyCustomFormatWithColon', { date: new Date(Date.UTC(2022, 0, 4, 14, 33, 10)) }],
expected: 'Before customized date in format EEEE d MMMM yyyy HH:mm (and other param 0)',
},
]);

tests.forEach((test) => {
it('correctly formats translations for ' + JSON.stringify(test.args), () => {
expect(instance.t.apply(instance, test.args)).to.eql(test.expected);
Expand Down

0 comments on commit ef13e2e

Please sign in to comment.