-
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 unsignedInt type
- Loading branch information
Showing
4 changed files
with
70 additions
and
2 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
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(); |
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,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); | ||
}); | ||
}); |