-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(primitives): implement instant type
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Instant FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { Type, success, failure, identity } from "io-ts"; | ||
|
||
const INSTANT_REGEX = /([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))/; | ||
|
||
export class InstantType extends Type<string> { | ||
readonly _tag: "InstantType" = "InstantType"; | ||
constructor() { | ||
super( | ||
"instant", | ||
(m): m is string => typeof m === "string" && INSTANT_REGEX.test(m), | ||
(m, c) => (this.is(m) ? success(m) : failure(m, c)), | ||
identity | ||
); | ||
} | ||
} | ||
|
||
export const instant = new InstantType(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Tests for Instant Runtime Type | ||
*/ | ||
|
||
import { instant } from "../src"; | ||
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers"; | ||
|
||
describe("InstantType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = instant; | ||
assertSuccess(T.decode("2015-02-07T13:28:17.239+02:00")); | ||
}); | ||
|
||
it("should return the same reference if validation succeeded and nothing changed", () => { | ||
const T = instant; | ||
const value = "2015-02-07T13:28:17.239+02:00"; | ||
assertStrictEqual(T.decode(value), value); | ||
}); | ||
|
||
it("should fail validating an invalid value", () => { | ||
const T = instant; | ||
assertFailure(T.decode(2), ["Invalid value 2 supplied to : instant"]); | ||
}); | ||
|
||
it("should fail validating non YYYY-MM-DDThh:mm:ss.sss+zz:zz formatted date", () => { | ||
const T = instant; | ||
assertFailure(T.decode("Mon Jan 21 2019 10:55:23 GMT-0600"), [ | ||
'Invalid value "Mon Jan 21 2019 10:55:23 GMT-0600" supplied to : instant' | ||
]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = instant; | ||
expect(T.is("2015-02-07T13:28:17.239+02:00")).toEqual(true); | ||
expect(T.is(true)).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |