Skip to content

Commit

Permalink
feat(primitives): implement unsignedInt type
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdrew committed Jan 21, 2019
1 parent 4dbb278 commit dda43ad
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { MarkdownType, markdown } from "./markdown";
import { OIDType, oid } from "./oid";
import { StringType, string } from "./string";
import { TimeType, time } from "./time";
import { UnsignedIntegerType, unsignedInt } from "./unsignedInt";
import { URIType, uri } from "./uri";
import { URLType, url } from "./url";

Expand Down Expand Up @@ -45,6 +46,8 @@ export {
StringType,
time,
TimeType,
unsignedInt,
UnsignedIntegerType,
uri,
URIType,
url,
Expand Down
20 changes: 20 additions & 0 deletions packages/primitives/src/unsignedInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Unsigned Integer FHIR Primitive Runtime Type
*/

import { Type, success, failure, identity } from "io-ts";

export class UnsignedIntegerType extends Type<number> {
readonly _tag: "UnsignedIntegerType" = "UnsignedIntegerType";
constructor() {
super(
"unsignedInt",
(m): m is number =>
typeof m === "number" && Number.isInteger(m) && m >= 0,
(m, c) => (this.is(m) ? success(m) : failure(m, c)),
identity
);
}
}

export const unsignedInt = new UnsignedIntegerType();
4 changes: 2 additions & 2 deletions packages/primitives/test/integer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Tests for Integer Runtime Type
*/

import { IntegerType, integer } from "../src";
import { integer } from "../src";
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers";

describe("IntegerType", () => {
Expand All @@ -23,7 +23,7 @@ describe("IntegerType", () => {
});

it("should type guard", () => {
const T = new IntegerType();
const T = integer;
expect(T.is(2)).toEqual(true);
expect(T.is(1.1)).toEqual(false);
expect(T.is(undefined)).toEqual(false);
Expand Down
45 changes: 45 additions & 0 deletions packages/primitives/test/unsignedInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Tests for Unsigned Integer Runtime Type
*/

import { unsignedInt } from "../src";
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers";

describe("UnsignedIntegerType", () => {
it("should succeed validating a valid value", () => {
const T = unsignedInt;
assertSuccess(T.decode(2));
});

it("should succeed validating 0", () => {
const T = unsignedInt;
assertSuccess(T.decode(0));
});

it("should return the same reference if validation succeeded and nothing changed", () => {
const T = unsignedInt;
const value = 2;
assertStrictEqual(T.decode(value), value);
});

it("should fail validating a float", () => {
const T = unsignedInt;
assertFailure(T.decode(1.1), [
"Invalid value 1.1 supplied to : unsignedInt"
]);
});

it("should fail validating a negative number", () => {
const T = unsignedInt;
assertFailure(T.decode(-2), ["Invalid value -2 supplied to : unsignedInt"]);
});

it("should type guard", () => {
const T = unsignedInt;
expect(T.is(2)).toEqual(true);
expect(T.is(0)).toEqual(true);
expect(T.is(1.1)).toEqual(false);
expect(T.is(-2)).toEqual(false);
expect(T.is(undefined)).toEqual(false);
});
});

0 comments on commit dda43ad

Please sign in to comment.