Skip to content

Commit

Permalink
feat: added error handling to _dateAdd, wrote a test for _dateAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
fwalzel committed Aug 26, 2024
1 parent 30a091c commit 3606bfc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dist/handlebars-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@
* @returns {string}
*/
function (dateInput, offset, unit, options) {

if (typeof dateInput !== 'number' && typeof dateInput !== 'string')
throw new Error('@ handlebars-i18n: invalid first argument "dateInput" was given for _dateAdd.');

if (typeof offset !== 'number')
throw new Error('@ handlebars-i18n: invalid second argument "offset" was given for _dateAdd.');

const date = __createDateObj(dateInput);

unit = unit || 'hour';
Expand Down
18 changes: 18 additions & 0 deletions test/handlebars-i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ describe('handlebars-i18n Tests', function () {
assert.isFunction(hI18n.helpers._date);
});

it('after method call init() HandlebarsEnvironment object should have a function _dateAdd', function () {
assert.isFunction(hI18n.helpers._dateAdd);
});

it('after method call init() HandlebarsEnvironment object should have a function _dateRel', function () {
assert.isFunction(hI18n.helpers._dateRel);
});
Expand Down Expand Up @@ -256,6 +260,20 @@ describe('handlebars-i18n Tests', function () {
});


/****************************************
Tests against function _date
****************************************/


it('function _dateAdd should return "12/17/1995" when called with parameters "December 17, 1995 02:00:00", 1, and "day"', function () {
i18next.changeLanguage('en');
const res = hI18n.helpers._dateAdd('December 17, 1995 02:00:00', 1, "day");
assert.equal('12/18/1995', res);
});




/****************************************
Tests against function _dateRel
****************************************/
Expand Down

0 comments on commit 3606bfc

Please sign in to comment.