generated from Marius9595/boilerplate-ts
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Completable<T> { | ||
complete<S>(onSuccess: (value: T) => S, onFailure: (error: Error) => S): Promise<S>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Completable } from './completable'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { assert, describe, expect, it } from 'vitest'; | ||
import { Future } from './future'; | ||
|
||
describe('Future monad', () => { | ||
it.each([ | ||
{ | ||
completionType: 'success', | ||
action: () => Promise.resolve(2), | ||
expected: { | ||
ifSuccess: (value: number) => expect(value).toBe(2), | ||
ifFailure: (_) => assert.fail('Error should not be thrown'), | ||
}, | ||
}, | ||
{ | ||
completionType: 'failure', | ||
action: () => Promise.reject<number>(new Error('Error')), | ||
expected: { | ||
ifSuccess: () => assert.fail('Value should not be emitted'), | ||
ifFailure: (error: Error) => expect(error).toEqual(new Error('Error')), | ||
}, | ||
}, | ||
])('should handle $completionType completion', async ({ action, expected }) => { | ||
const future = Future.of(action); | ||
await future.complete(expected.ifSuccess, expected.ifFailure); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
completionType: 'Success', | ||
future: Future.of(() => Promise.resolve(2)), | ||
expected: { | ||
ifSuccess: (value: number) => expect(value).toBe(4), | ||
ifFailure: (_) => assert.fail('Error should not be thrown'), | ||
}, | ||
}, | ||
{ | ||
completionType: 'Failure', | ||
future: Future.of(() => Promise.reject<number>(new Error('Error'))), | ||
expected: { | ||
ifSuccess: () => assert.fail('Value should not be emitted'), | ||
ifFailure: (error: Error) => expect(error).toEqual(new Error('Error')), | ||
}, | ||
}, | ||
])('$completionType completion should handle map correctly', async ({ future, expected }) => { | ||
const actual = future.map((value) => value * 2); | ||
await actual.complete(expected.ifSuccess, expected.ifFailure); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
completionType: 'Success', | ||
future: Future.of(() => Promise.resolve(2)), | ||
expected: { | ||
ifSuccess: (value: number) => expect(value).toBe(4), | ||
ifFailure: (error: Error) => assert.fail('Error should not be thrown'), | ||
}, | ||
}, | ||
{ | ||
completionType: 'Failure', | ||
future: Future.of(() => Promise.reject<number>(new Error('Error'))), | ||
expected: { | ||
ifSuccess: () => assert.fail('Value should not be emitted'), | ||
ifFailure: (error: Error) => expect(error).toEqual(new Error('Error')), | ||
}, | ||
}, | ||
])('$completionType completion should handle flatMap correctly', async ({ future, expected }) => { | ||
const actual = future.flatMap((value) => Future.of(() => Promise.resolve(value * 2))); | ||
await actual.complete(expected.ifSuccess, expected.ifFailure); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Monad } from '../monad'; | ||
import { Completable } from '../complete'; | ||
|
||
class Future<T> implements Monad<T>, Completable<T> { | ||
private constructor(private readonly action: () => Promise<T>) {} | ||
|
||
static of<T>(action: () => Promise<T>): Future<T> { | ||
return new Future(action); | ||
} | ||
|
||
map<U>(transform: (value: T) => U): Future<U> { | ||
return new Future<U>(() => this.action().then(transform)); | ||
} | ||
|
||
flatMap<U>(transform: (value: T) => Future<U>): Future<U> { | ||
return new Future<U>(() => this.action().then((value) => transform(value).action())); | ||
} | ||
|
||
complete<S>(onSuccess: (value: T) => S, onFailure: (error: Error) => S): Promise<S> { | ||
return this.action().then(onSuccess).catch(onFailure); | ||
} | ||
} | ||
|
||
export { Future }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Future } from './future'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './either'; | ||
export * from './future'; | ||
export * from './option'; | ||
export * from './try'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters