Skip to content

Commit

Permalink
feat(primitives): implement string type
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdrew committed Jan 21, 2019
1 parent 85eb7b1 commit 97fec4a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
lerna-debug.log
node_modules
coverage
3 changes: 2 additions & 1 deletion packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

import { BooleanType, boolean } from "./boolean";
import { IntegerType, integer } from "./integer";
import { StringType, string } from "./string";

export { BooleanType, boolean, IntegerType, integer };
export { BooleanType, boolean, IntegerType, integer, StringType, string };
3 changes: 1 addition & 2 deletions packages/primitives/src/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export class IntegerType extends Type<number> {
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),
(m, c) => (this.is(m) ? success(m) : failure(m, c)),
identity
);
}
Expand Down
21 changes: 21 additions & 0 deletions packages/primitives/src/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* String FHIR Primitive Runtime Type
*/

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

const STRING_REGEX = /[ \r\n\t\S]+/;

export class StringType extends Type<string> {
readonly _tag: "StringType" = "StringType";
constructor() {
super(
"string",
(m): m is string => typeof m === "string" && STRING_REGEX.test(m),
(m, c) => (this.is(m) ? success(m) : failure(m, c)),
identity
);
}
}

export const string = new StringType();
37 changes: 37 additions & 0 deletions packages/primitives/test/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Tests for String Runtime Type
*/

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

describe("StringType", () => {
it("should succeed validating a valid value", () => {
const T = string;
assertSuccess(T.decode("abc"));
});

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

it("should fail validating an invalid value", () => {
const T = string;
assertFailure(T.decode(2), ["Invalid value 2 supplied to : string"]);
});

// TODO: Improve unicode check in regex
xit("should fail validating a string with invalid characters", () => {
const T = string;
assertFailure(T.decode("\0"), ["Invalid value 2 supplied to : string"]);
});

it("should type guard", () => {
const T = string;
expect(T.is("abc")).toEqual(true);
expect(T.is(true)).toEqual(false);
expect(T.is(undefined)).toEqual(false);
});
});
21 changes: 13 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2184,13 +2184,6 @@ fb-watchman@^2.0.0:
dependencies:
bser "^2.0.0"

"fhir-ts-codegen@file:packages/fhir-ts-codegen":
version "0.0.6"
dependencies:
clime "^0.5.9"
glob "^7.1.3"
ts-simple-ast "^14.4.2"

figgy-pudding@^3.4.1, figgy-pudding@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/figgy-pudding/-/figgy-pudding-3.5.1.tgz#862470112901c727a0e495a80744bd5baa1d6790"
Expand Down Expand Up @@ -2298,6 +2291,11 @@ form-data@~2.3.2:
combined-stream "^1.0.6"
mime-types "^2.1.12"

fp-ts@^1.0.0, fp-ts@^1.12.3:
version "1.12.3"
resolved "https://registry.yarnpkg.com/fp-ts/-/fp-ts-1.12.3.tgz#2a694e8ba5707d3a32c82b0e0d6871050a3392a9"
integrity sha512-0l3UGPhoU8tn8xu7xZviis09iulDtQK7yE594dEFMdgT+RovdlRhJRCEupFgKxHQjvgghG+y907psuKiW28sjA==

fragment-cache@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/fragment-cache/-/fragment-cache-0.2.1.tgz#4290fad27f13e89be7f33799c6bc5a0abfff0d19"
Expand Down Expand Up @@ -2846,6 +2844,13 @@ invert-kv@^2.0.0:
resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-2.0.0.tgz#7393f5afa59ec9ff5f67a27620d11c226e3eec02"
integrity sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==

io-ts@^1.5.2:
version "1.5.2"
resolved "https://registry.yarnpkg.com/io-ts/-/io-ts-1.5.2.tgz#e57ca59689ead86736542e27d44a9a0358d37931"
integrity sha512-97sFhiqyKqCj2iLJ3NUuAMDYh2pQm/VuSHu/zNbCdZt+I53PiQ/nkKIP7gKpM1fOAa9+xfHS675Ifit+4lP8kw==
dependencies:
fp-ts "^1.0.0"

ip-regex@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-3.0.0.tgz#0a934694b4066558c46294244a23cc33116bf732"
Expand Down Expand Up @@ -5992,7 +5997,7 @@ tslint-config-prettier@^1.15.0:
resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.17.0.tgz#946ed6117f98f3659a65848279156d87628c33dc"
integrity sha512-NKWNkThwqE4Snn4Cm6SZB7lV5RMDDFsBwz6fWUkTxOKGjMx8ycOHnjIbhn7dZd5XmssW3CwqUjlANR6EhP9YQw==

tslint@^5.11.0:
tslint@^5.11.0, tslint@^5.12.1:
version "5.12.1"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.12.1.tgz#8cec9d454cf8a1de9b0a26d7bdbad6de362e52c1"
integrity sha512-sfodBHOucFg6egff8d1BvuofoOQ/nOeYNfbp7LDlKBcLNrL3lmS5zoiDGyOMdT7YsEXAwWpTdAHwOGOc8eRZAw==
Expand Down

0 comments on commit 97fec4a

Please sign in to comment.