Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/maybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Just<A> {
return this.value
}

orJustCall(): A {
return this.value
}

}

class Nothing {
Expand Down Expand Up @@ -93,6 +97,10 @@ class Nothing {
return value
}

orJustCall<B>(f: () => B): B {
return f()
}

}

function isNil<T>(value: ?T): boolean {
Expand Down
2 changes: 2 additions & 0 deletions src/maybe.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare class Just<A> {
map<B: AnyVal>(f: (_: A) => B): Maybe<B>;
orElse<B>(m: Maybe<B>): Maybe<A>;
orJust<B>(value: B): A;
orJustCall<B>(f: () => B): A;
}

declare class Nothing {
Expand All @@ -26,6 +27,7 @@ declare class Nothing {
map<A, B: AnyVal>(f: (_: A) => B): Nothing;
orElse<B>(m: Maybe<B>): Maybe<B>;
orJust<B>(value: B): B;
orJustCall<B>(f: () => B): B;
}

declare export function just<T>(value: AnyVal): Just<T>;
Expand Down
11 changes: 11 additions & 0 deletions src/maybe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ 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(() => '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())
Expand Down