Skip to content

Commit

Permalink
Make filter function date more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Mar 31, 2020
1 parent 1beb16d commit 692dd3e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
24 changes: 24 additions & 0 deletions lib/filters/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 9 additions & 2 deletions lib/filters/date.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions tests/filterFunctions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 692dd3e

Please sign in to comment.