Skip to content

Commit

Permalink
Implement date filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Mar 30, 2020
1 parent 322fec9 commit 2bb4230
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ const template = 'http://example.com/{section|prepend(local_)}'
//--> http://example.com/local_news
```

#### `date(format)`
Formats a date according to the given date format string.
Uses `date-and-time` under the hood, so refer to
[their documentation](https://github.com/knowledgecode/date-and-time#formatdateobj-formatstring-utc).

”Example:
```
const params = {updatedAfter: new Date('2020-03-19T14:08:44Z')}
const template = 'http://example.com/all{?updatedAfter|date(DD/MM/YYYY HH:mm:ss)}'
...
//--> http://example.com/all?updatedAfter=20%2F03%2F2020%2019%3A43%3A11
```

#### `lower()`
Transform the given value to lower case.

Expand Down
64 changes: 64 additions & 0 deletions lib/filters/date-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import test from 'ava'

import date from './date'

// Tests

test('should format date with provided string', (t) => {
const value = new Date('2020-03-18T19:59:03Z')
const format = 'DD.MM.YYYY'
const expected = '18.03.2020'

const ret = date(value, format)

t.is(ret, expected)
})

test('should format date and time with provided string', (t) => {
const value = new Date('2020-03-18T19:59:03Z')
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 convert string value to date', (t) => {
const value = '2020-03-18T19:59:03Z'
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'
const expected = ''

const ret = date(value, format)

t.is(ret, expected)
})

test('should return empty string when value is not a date', (t) => {
const value = null
const format = 'YYYY/MM/DD HH:mm:ss'
const expected = ''

const ret = date(value, format)

t.is(ret, expected)
})

test('should return iso string when format is not a string', (t) => {
const value = new Date('2020-03-18T19:59:03Z')
const expected = '2020-03-18T19:59:03.000Z'

const ret = date(value)

t.is(ret, expected)
})
18 changes: 18 additions & 0 deletions lib/filters/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const dateTime = require('date-and-time')

function date (value, format) {
if (typeof value === 'string') {
value = new Date(value)
}
if (value instanceof Date && !Number.isNaN(value.getTime())) {
if (typeof format === 'string' && format !== '') {
return dateTime.format(value, format)
} else {
return value.toISOString()
}
} else {
return ''
}
}

module.exports = date
1 change: 1 addition & 0 deletions lib/filters/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
prepend: require('./prepend'),
append: require('./append'),
date: require('./date'),
upper: require('./upper'),
lower: require('./lower'),
wrap: require('./wrap'),
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
"coveralls": "^3.0.11",
"nyc": "^15.0.0",
"standard": "^14.3.3"
},
"dependencies": {
"date-and-time": "^0.13.1"
}
}
11 changes: 11 additions & 0 deletions tests/filterFunctions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ test('should generate uri with escaped characters in filter args', (t) => {

t.is(uri, expected)
})

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 compiled = compile(template)
const uri = generate(compiled, params)

t.is(uri, expected)
})

0 comments on commit 2bb4230

Please sign in to comment.