-
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 integer type
- Loading branch information
Showing
5 changed files
with
56 additions
and
4 deletions.
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
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,20 @@ | ||
/** | ||
* Integer FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { Type, success, failure, identity } from "io-ts"; | ||
|
||
export class IntegerType extends Type<number> { | ||
readonly _tag: "IntegerType" = "IntegerType"; | ||
constructor() { | ||
super( | ||
"integer", | ||
(m): m is number => typeof m === "number" && Number.isInteger(m), | ||
(m, c) => | ||
this.is(m) && Number.isInteger(m) ? success(m) : failure(m, c), | ||
identity | ||
); | ||
} | ||
} | ||
|
||
export const integer = new IntegerType(); |
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,31 @@ | ||
/** | ||
* Tests for Integer Runtime Type | ||
*/ | ||
|
||
import { IntegerType, integer } from "../src"; | ||
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers"; | ||
|
||
describe("IntegerType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = integer; | ||
assertSuccess(T.decode(2)); | ||
}); | ||
|
||
it("should return the same reference if validation succeeded and nothing changed", () => { | ||
const T = integer; | ||
const value = 2; | ||
assertStrictEqual(T.decode(value), value); | ||
}); | ||
|
||
it("should fail validating an invalid value", () => { | ||
const T = integer; | ||
assertFailure(T.decode(1.1), ["Invalid value 1.1 supplied to : integer"]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = new IntegerType(); | ||
expect(T.is(2)).toEqual(true); | ||
expect(T.is(1.1)).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |