-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add O.fromExecution and O.fromPromise
- Loading branch information
Showing
5 changed files
with
110 additions
and
0 deletions.
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,49 @@ | ||
import { expectType } from 'ts-expect' | ||
import { O } from '../..' | ||
|
||
const okJSON = `{"name": "John"}` | ||
const badJSON = `{name": John}` | ||
|
||
type User = { | ||
readonly name: string | ||
} | ||
|
||
const parseJSON = | ||
<T>(value: string) => | ||
() => { | ||
return JSON.parse(value) as T | ||
} | ||
|
||
describe('fromExecution', () => { | ||
it('provides correct types', () => { | ||
expectType<O.Option<User>>(O.fromExecution(parseJSON<User>(okJSON))) | ||
}) | ||
|
||
it('returns None', () => { | ||
expect(O.fromExecution(parseJSON(badJSON))).toEqual(O.None) | ||
}) | ||
|
||
it('returns Some', () => { | ||
expect(O.fromExecution(parseJSON(okJSON))).toEqual( | ||
O.Some({ | ||
name: 'John', | ||
}), | ||
) | ||
}) | ||
|
||
it('*', () => { | ||
const { Some, None } = O | ||
|
||
expect( | ||
/* | ||
type User = { readonly name: string } | ||
const parseJSON = <T>(value: string) => () => JSON.parse(value) as T | ||
const okJSON = `{"name": "John"}` | ||
const badJSON = `{name": John}` | ||
*/ | ||
O.fromExecution(parseJSON<User>(okJSON)), | ||
).toEqual(Some({ name: 'John' })) | ||
|
||
expect(O.fromExecution(parseJSON<User>(badJSON))).toEqual(None) | ||
}) | ||
}) |
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,28 @@ | ||
import { expectType } from 'ts-expect' | ||
import { O } from '../..' | ||
|
||
describe('fromPromise', () => { | ||
it('provides correct types', () => { | ||
expectType<Promise<O.Option<number>>>(O.fromPromise(Promise.resolve(42))) | ||
}) | ||
|
||
it('returns None', async () => { | ||
expect(await O.fromPromise(Promise.reject('hello world'))).toEqual(O.None) | ||
}) | ||
|
||
it('returns Some', async () => { | ||
expect(await O.fromPromise(Promise.resolve('hello world'))).toEqual( | ||
O.Some('hello world'), | ||
) | ||
}) | ||
|
||
it('*', async () => { | ||
const { Some, None } = O | ||
|
||
expect(await O.fromPromise(Promise.resolve('hello world'))).toEqual( | ||
Some('hello world'), | ||
) | ||
|
||
expect(await O.fromPromise(Promise.reject('oops'))).toEqual(None) | ||
}) | ||
}) |
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