From 3f84d1e12fe78ea25645a7a46a169f0185a58fd0 Mon Sep 17 00:00:00 2001 From: Ben Styles Date: Thu, 8 Feb 2018 11:46:53 +0000 Subject: [PATCH 1/2] Add orJustCall, with tests --- src/maybe.js | 8 ++++++++ src/maybe.js.flow | 2 ++ src/maybe.test.js | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/maybe.js b/src/maybe.js index c391cf7..5e5bb52 100644 --- a/src/maybe.js +++ b/src/maybe.js @@ -55,6 +55,10 @@ class Just { return this.value } + orJustCall(): A { + return this.value + } + } class Nothing { @@ -93,6 +97,10 @@ class Nothing { return value } + orJustCall(f: () => B): B { + return f() + } + } function isNil(value: ?T): boolean { diff --git a/src/maybe.js.flow b/src/maybe.js.flow index 332f766..25d8156 100644 --- a/src/maybe.js.flow +++ b/src/maybe.js.flow @@ -14,6 +14,7 @@ declare class Just { map(f: (_: A) => B): Maybe; orElse(m: Maybe): Maybe; orJust(value: B): A; + orJustCall(f: () => B): A; } declare class Nothing { @@ -26,6 +27,7 @@ declare class Nothing { map(f: (_: A) => B): Nothing; orElse(m: Maybe): Maybe; orJust(value: B): B; + orJustCall(f: () => B): B; } declare export function just(value: AnyVal): Just; diff --git a/src/maybe.test.js b/src/maybe.test.js index bc41836..7332e57 100644 --- a/src/maybe.test.js +++ b/src/maybe.test.js @@ -44,6 +44,18 @@ test('orJust', () => { expect(y.orJust()).toBe('hello') }) +test('orJustCall', () => { + const x = maybe(null) + const orValue = x.orJustCall(() => 'hi') + expect(orValue).toBe('hi') + const y = maybe('hello') + expect(y.orJustCall()).toBe('hello') + expect(y.orJustCall(() => 'bye')).toBe('hello') + const fn = jest.fn(() => 'bye') + expect(y.orJustCall(fn)).toBe('hello') + expect(fn).not.toHaveBeenCalled() +}) + test('map value', () => { const x = maybe('bob') const result = x.map(v => v.toUpperCase()) From 47ed9465dc2693e5bd7a7a668bec7967b3cc83ff Mon Sep 17 00:00:00 2001 From: Ben Styles Date: Thu, 8 Feb 2018 12:00:38 +0000 Subject: [PATCH 2/2] removed failing (and unnecessary) expectation --- src/maybe.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/maybe.test.js b/src/maybe.test.js index 7332e57..998080e 100644 --- a/src/maybe.test.js +++ b/src/maybe.test.js @@ -49,7 +49,6 @@ test('orJustCall', () => { const orValue = x.orJustCall(() => 'hi') expect(orValue).toBe('hi') const y = maybe('hello') - expect(y.orJustCall()).toBe('hello') expect(y.orJustCall(() => 'bye')).toBe('hello') const fn = jest.fn(() => 'bye') expect(y.orJustCall(fn)).toBe('hello')