diff --git a/tsmonad/tests/either-tests.ts b/tsmonad/tests/either-tests.ts new file mode 100644 index 000000000..1530562be --- /dev/null +++ b/tsmonad/tests/either-tests.ts @@ -0,0 +1,45 @@ +/// + +class User { + + private age: number; + + constructor(age?: number) { + this.age = age ? age : 0; + } + + public getAge(): TsMonad.Either { + if (this.age > 0) { + return TsMonad.Either.right(this.age); + } else { + return TsMonad.Either.left('Information withheld'); + } + } +} + +module Station { + + export class BusPass { + + public isValidForRoute(route: string): boolean { + return true; + } + } + + export function getBusPass(age: number): TsMonad.Either { + if (age > 18) { + return TsMonad.Either.right(new BusPass()); + } else { + return TsMonad.Either.left('Too young for a bus pass'); + } + } +} + +var user = new User(42) + +var canRideForFree = user.getAge() + .bind(age => Station.getBusPass(age)) + .caseOf({ + right: busPass => busPass.isValidForRoute('Weston'), + left: errorMessage => { console.log(errorMessage); return false; } + }); diff --git a/tsmonad/tests/maybe-tests.ts b/tsmonad/tests/maybe-tests.ts new file mode 100644 index 000000000..76f0a7256 --- /dev/null +++ b/tsmonad/tests/maybe-tests.ts @@ -0,0 +1,20 @@ +/// + +var turns_out_to_be_100 = TsMonad.Maybe.just(10) + .caseOf({ + just: n => n * n, + nothing: () => -1 + }); + +var turns_out_to_be_nothing = TsMonad.Maybe.nothing() + .caseOf({ + just: n => n * n, + nothing: () => -1 + }); + +var turns_out_to_be_true = TsMonad.Maybe.just(123) + .lift(n => n * 2) + .caseOf({ + just: n => n === 246, + nothing: () => false + }); \ No newline at end of file diff --git a/tsmonad/tests/writer-tests.ts b/tsmonad/tests/writer-tests.ts new file mode 100644 index 000000000..fd8200aeb --- /dev/null +++ b/tsmonad/tests/writer-tests.ts @@ -0,0 +1,8 @@ +/// + +var is_true = TsMonad.Writer.writer(['Started with 0'], 0) + .bind(x => TsMonad.Writer.writer(['+ 8'], x + 8)) + .bind(x => TsMonad.Writer.writer(['- 6', '* 8'], 8 * (x - 6))) + .caseOf({ + writer: (s, v) => v === 16 && s.join(', ') === 'Started with 0, + 8, - 6, * 8' + }); \ No newline at end of file diff --git a/tsmonad/tsmonad-tests.ts b/tsmonad/tsmonad-tests.ts new file mode 100644 index 000000000..8959950d9 --- /dev/null +++ b/tsmonad/tsmonad-tests.ts @@ -0,0 +1,3 @@ +/// +/// +/// diff --git a/tsmonad/tsmonad.d.ts b/tsmonad/tsmonad.d.ts index a717dbc3e..081c446c3 100644 --- a/tsmonad/tsmonad.d.ts +++ b/tsmonad/tsmonad.d.ts @@ -1,7 +1,6 @@ // Type definitions for TsMonad // Project: https://github.com/cbowdon/TsMonad // Definitions by: Chris Bowdon -// Definitions by: Valentin Trinque // Definitions: https://github.com/borisyankov/DefinitelyTyped declare module TsMonad {