Skip to content

Commit

Permalink
feat(primitives): implement instant type
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdrew committed Jan 21, 2019
1 parent 95ffb77 commit a612f07
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* FHIR Primitive Runtime Types
*/

import { BooleanType, boolean } from "./boolean";
import { Base64BinaryType, base64binary } from "./base64binary";
import { BooleanType, boolean } from "./boolean";
import { CanonicalType, canonical } from "./canonical";
import { DecimalType, decimal } from "./decimal";
import { InstantType, instant } from "./instant";
import { IntegerType, integer } from "./integer";
import { StringType, string } from "./string";
import { URIType, uri } from "./uri";
Expand All @@ -20,6 +21,8 @@ export {
CanonicalType,
decimal,
DecimalType,
instant,
InstantType,
integer,
IntegerType,
string,
Expand Down
21 changes: 21 additions & 0 deletions packages/primitives/src/instant.ts
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();
38 changes: 38 additions & 0 deletions packages/primitives/test/instant.test.ts
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);
});
});

0 comments on commit a612f07

Please sign in to comment.