Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Update format-date to return empty string instead of 'Invalid Date' #155

Merged
merged 7 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
.idea
jkuester marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/openrosa-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const openrosa_xpath_extensions = function() {
format_date = function(date, format) {
date = asDate(date);
format = asString(format);
if(isNaN(date)) return 'Invalid Date';
if(isNaN(date)) return '';
let c, i, sb = '';
const year = 1900 + date.getYear();
const month = 1 + date.getMonth();
Expand Down
2 changes: 1 addition & 1 deletion test/integration/openrosa-xpath/format-date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('#format-date()', () => {
[`format-date('${date.toString()}', '%e | %a' )`, doc,
`${date.getDate()} | ${['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][date.getDay()]}`
],
['format-date("not a date", "%M")', doc, 'Invalid Date' ],
['format-date("not a date", "%M")', doc, '' ],
].forEach(([expr, node, expected]) => {
assertStringValue(node, null, expr, expected);
// do the same tests for the alias format-date-time()
Expand Down
44 changes: 43 additions & 1 deletion test/unit/openrosa-extensions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('openrosa-extensions', () => {
});

describe('func', () => {
const { date, 'date-time':dateTime, min, max, number } = extensions.func;
const { date, 'date-time':dateTime, 'format-date':formatDate, min, max, number } = extensions.func;

describe('date()', () => {
[ 'asdf', 123, true ].forEach(arg => {
Expand Down Expand Up @@ -177,5 +177,47 @@ describe('openrosa-extensions', () => {
});
});
});

describe('format-date()', () => {
// eslint-disable-next-line no-global-assign
before(() => window = {});
// eslint-disable-next-line no-global-assign
after(() => window = undefined);
jkuester marked this conversation as resolved.
Show resolved Hide resolved

[
[ '2015-09-02', '2015-09-02', '%Y-%m-%d' ],
[ '1999-12-12', '1999-12-12', '%Y-%m-%d' ],
[ '15-9-2', '2015-9-2', '%y-%n-%e' ],
[ '99-12-12', '1999-12-12', '%y-%n-%e' ],
[ 'Sun Dec', '1999-12-12', '%a %b' ],
[ '(2015/10/01)', new Date('2015-10-01T00:00:00.000'), '(%Y/%m/%d)' ],
[ '2000 06 02', 11111, '%Y %m %d' ],
[ '2014201409092222', ['2014-09-22'], '%Y%Y%m%m%d%d' ],
[ '', '', '%Y-%m-%d' ],
[ '', NaN, '%Y-%m-%d' ],
[ '', 'NaN', '%Y-%m-%d' ],
[ '', 'invalid', '%Y-%m-%d' ],
[ '', '11:11', '%Y-%m-%d' ],
[ '', true, '%Y-%m-%d' ],
[ '', false, '%Y-%m-%d' ],
].forEach(([ expected, ...args ]) => {
it(`should convert ${JSON.stringify(args)} to ${expected}`, () => {
// when
const actual = formatDate(...args.map(wrapVal));

// then
assert.equal(actual.v, expected);
});
});

[
['1999-12-12'],
[]
].forEach(args => {
it(`should throw an error when ${JSON.stringify(args)} is provided`, () => {
assert.throws(() => formatDate(...args.map(wrapVal)),'format-date() :: not enough args');
});
});
});
});
});