Skip to content

Commit

Permalink
DateType added
Browse files Browse the repository at this point in the history
  • Loading branch information
Malte Legenhausen authored and gcanti committed Oct 4, 2018
1 parent 3cbf6ee commit 019a189
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ assert.strictEqual(T.encode(some(1)), 1)

## `Date`

### `date`

```ts
importdate } from 'io-ts-types/lib/Date/date'

const input = new Date(1973, 10, 30)
date.decode(input) // right(new Date(...))
```

### `DateFromISOString`

```ts
Expand Down
16 changes: 16 additions & 0 deletions src/Date/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as t from 'io-ts'
import { identity } from 'fp-ts/lib/function'

export class DateType extends t.Type<Date> {
readonly _tag: 'DateType' = 'DateType'
constructor() {
super(
'Date',
(m): m is Date => m instanceof Date,
(m, c) => (this.is(m) ? t.success(m) : t.failure(m, c)),
identity
)
}
}

export const date: DateType = new DateType()
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Date
export { date, DateType } from './Date/date'
export { DateFromISOString, DateFromISOStringType } from './Date/DateFromISOString'
export { DateFromNumber, DateFromNumberType } from './Date/DateFromNumber'
export { DateFromUnixTime, DateFromUnixTimeType } from './Date/DateFromUnixTime'
Expand Down
11 changes: 11 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { iso } from 'newtype-ts'
import { prismNonZero, NonZero } from 'newtype-ts/lib/NonZero'

import {
date,
DateFromISOString,
DateFromNumber,
DateFromUnixTime,
Expand Down Expand Up @@ -224,6 +225,16 @@ describe('number', () => {
})

describe('Date', () => {
describe('Date', () => {
it('should decode Date values', () => {
assert.deepEqual(date.decode(new Date(0)), right(new Date(0)))
})

it('should not decode non-Date values', () => {
assert.deepEqual(PathReporter.report(date.decode(1)), ['Invalid value 1 supplied to : Date'])
})
})

it('DateFromISOString', () => {
const T = DateFromISOString
const d = new Date(1973, 10, 30)
Expand Down

0 comments on commit 019a189

Please sign in to comment.