Skip to content

Commit

Permalink
No, really, though...
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmay committed Jul 16, 2018
1 parent 9c284b5 commit 706bfc7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/guards/guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ValueTypeGuard } from "../types";

const isTypeof = <T>(type: T): ValueTypeGuard<T> => (x: T): x is T => typeof x === typeof type;

const isString = isTypeof(String());
const isNumber = isTypeof(Number());
const isBoolean = isTypeof(Boolean());

const isArray = <T>(xs: T[]): xs is T[] => Array.isArray(xs);

export { isString, isNumber, isBoolean, isArray, isTypeof };
32 changes: 32 additions & 0 deletions src/operators/operators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { TypedTest, Erratic } from "../types";

const isNull = <T>(x: Erratic<T>): x is null => x === null;
const isUndefined = <T>(x: Erratic<T>): x is undefined => x === undefined;

const isErratic = <T>(x: Erratic<T>): x is null | undefined =>
isNull(x) || isUndefined(x);

const and = <T>(...tests: TypedTest<T>[]) => (x: Erratic<T>): x is T =>
isErratic(x) ? false : tests.every(test => test(x));

const or = <T>(...tests: TypedTest<T>[]) => (x: Erratic<T>): x is T =>
isErratic(x) ? false : tests.some(test => test(x));

const optional = <T>(test: TypedTest<T>) => (x: Erratic<T>): x is T =>
isNull(x) ? false : isUndefined(x) ? true : test(x);

const nullable = <T>(test: TypedTest<T>) => (x: Erratic<T>): x is T =>
isNull(x) ? true : isUndefined(x) ? false : test(x);

const erratic = <T>(test: TypedTest<T>) => (x: Erratic<T>): x is T =>
isErratic(x) ? true : test(x);

const compose = and;

const customTest = <T>(test: TypedTest<T>) => (x: Erratic<T>): x is T =>
isErratic(x) ? false : test(x);

const unsafeTest = <T>(test: TypedTest<Erratic<T>>) => (x: Erratic<T>): x is T =>
test(x);

export { compose, and, or, optional, nullable, erratic, customTest, unsafeTest };

0 comments on commit 706bfc7

Please sign in to comment.