From 692dd3edd9c13a61d727c03e03d90dca7d913a77 Mon Sep 17 00:00:00 2001 From: Kjell-Morten Date: Tue, 31 Mar 2020 14:14:12 +0200 Subject: [PATCH] Make filter function date more robust --- lib/filters/date-test.js | 24 ++++++++++++++++++++++++ lib/filters/date.js | 11 +++++++++-- tests/filterFunctions-test.js | 9 ++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/filters/date-test.js b/lib/filters/date-test.js index db879b2..d4c5918 100644 --- a/lib/filters/date-test.js +++ b/lib/filters/date-test.js @@ -34,6 +34,30 @@ test('should convert string value to date', (t) => { t.is(ret, expected) }) +test('should convert numeric value to date', (t) => { + const value = (new Date('2020-03-18T19:59:03Z')).getTime() + const format = 'YYYY/MM/DD HH:mm:ss' + const expected = '2020/03/18 20:59:03' + + const ret = date(value, format) + + t.is(ret, expected) +}) + +test('should treat date that has lost its inheritance as date', (t) => { + const real = new Date('2020-03-18T19:59:03Z') + const value = { + getTime: () => real.getTime(), + toISOString: () => real.toISOString() + } + const format = 'YYYY/MM/DD HH:mm:ss' + const expected = '2020/03/18 20:59:03' + + const ret = date(value, format) + + t.is(ret, expected) +}) + test('should return empty string when value is not date string', (t) => { const value = 'illegal' const format = 'YYYY/MM/DD HH:mm:ss' diff --git a/lib/filters/date.js b/lib/filters/date.js index fb5685c..4e6efaa 100644 --- a/lib/filters/date.js +++ b/lib/filters/date.js @@ -1,10 +1,17 @@ const dateTime = require('date-and-time') +const isDatish = (value) => + typeof value === 'object' && + value !== null && + !Array.isArray(value) && + typeof value.getTime === 'function' && + typeof value.toISOString === 'function' + function date (value, format) { - if (typeof value === 'string') { + if (typeof value === 'string' || typeof value === 'number') { value = new Date(value) } - if (value instanceof Date && !Number.isNaN(value.getTime())) { + if (isDatish(value) && !Number.isNaN(value.getTime())) { if (typeof format === 'string' && format !== '') { return dateTime.format(value, format) } else { diff --git a/tests/filterFunctions-test.js b/tests/filterFunctions-test.js index e7a95dd..efa5795 100644 --- a/tests/filterFunctions-test.js +++ b/tests/filterFunctions-test.js @@ -25,9 +25,12 @@ test('should generate uri with escaped characters in filter args', (t) => { }) test('should generate uri with date filter functions', (t) => { - const template = 'http://example.com/all{?updatedAfter|date(DD/MM/YYYY HH:mm:ss)}' - const params = { updatedAfter: new Date('2020-03-20T18:43:11Z') } - const expected = 'http://example.com/all?updatedAfter=20%2F03%2F2020%2019%3A43%3A11' + const template = 'http://example.com/all{?since=updatedAfter|date(DD/MM/YYYY HH:mm:ss)?,until=updatedBefore|date(YYYY-MM-DD)?}' + const params = { + updatedAfter: new Date('2020-03-20T18:43:11Z'), + updatedBefore: new Date('2020-03-22T21:00:00Z') + } + const expected = 'http://example.com/all?since=20%2F03%2F2020%2019%3A43%3A11&until=2020-03-22' const compiled = compile(template) const uri = generate(compiled, params)