Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add orElseFirstIOK and orElseFirstTaskK #1655

Merged
merged 2 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/modules/IOEither.ts.md
Expand Up @@ -53,6 +53,7 @@ Added in v2.0.0
- [fromOptionK](#fromoptionk)
- [orElse](#orelse)
- [orElseFirst](#orelsefirst)
- [orElseFirstIOK](#orelsefirstiok)
- [orElseFirstW](#orelsefirstw)
- [orElseW](#orelsew)
- [orLeft](#orleft)
Expand Down Expand Up @@ -519,6 +520,16 @@ export declare const orElseFirst: <E, B>(onLeft: (e: E) => IOEither<E, B>) => <A

Added in v2.11.0

## orElseFirstIOK

**Signature**

```ts
export declare const orElseFirstIOK: <E, B>(onLeft: (e: E) => I.IO<B>) => <A>(ma: IOEither<E, A>) => IOEither<E, A>
```

Added in v2.11.8

## orElseFirstW

**Signature**
Expand Down
24 changes: 24 additions & 0 deletions docs/modules/TaskEither.ts.md
Expand Up @@ -65,6 +65,8 @@ Added in v2.0.0
- [fromTaskOptionK](#fromtaskoptionk)
- [orElse](#orelse)
- [orElseFirst](#orelsefirst)
- [orElseFirstIOK](#orelsefirstiok)
- [orElseFirstTaskK](#orelsefirsttaskk)
- [orElseFirstW](#orelsefirstw)
- [orElseW](#orelsew)
- [orLeft](#orleft)
Expand Down Expand Up @@ -696,6 +698,28 @@ export declare const orElseFirst: <E, B>(

Added in v2.11.0

## orElseFirstIOK

**Signature**

```ts
export declare const orElseFirstIOK: <E, B>(onLeft: (e: E) => IO<B>) => <A>(ma: TaskEither<E, A>) => TaskEither<E, A>
```

Added in v2.11.8

## orElseFirstTaskK

**Signature**

```ts
export declare const orElseFirstTaskK: <E, B>(
onLeft: (e: E) => T.Task<B>
) => <A>(ma: TaskEither<E, A>) => TaskEither<E, A>
```

Added in v2.11.8

## orElseFirstW

**Signature**
Expand Down
10 changes: 10 additions & 0 deletions dtslint/ts3.5/IOEither.ts
Expand Up @@ -53,6 +53,16 @@ pipe(
_.orElseFirstW((a) => _.left(a.length))
)

//
// orElseFirstIOK
//

// $ExpectType IOEither<string, never>
pipe(
_.left('a'),
_.orElseFirstIOK((a) => IO.of(a.length))
)

//
// orLeft
//
Expand Down
21 changes: 21 additions & 0 deletions dtslint/ts3.5/TaskEither.ts
Expand Up @@ -2,6 +2,7 @@ import * as _ from '../../src/TaskEither'
import * as T from '../../src/Task'
import * as E from '../../src/Either'
import * as TO from '../../src/TaskOption'
import * as IO from '../../src/IO'
import * as IOE from '../../src/IOEither'
import { pipe } from '../../src/function'

Expand Down Expand Up @@ -55,6 +56,26 @@ pipe(
_.orElseFirstW((a) => _.left(a.length))
)

//
// orElseFirstIOK
//

// $ExpectType TaskEither<string, never>
pipe(
_.left('a'),
_.orElseFirstIOK((a) => IO.of(a.length))
)

//
// orElseFirstTaskK
//

// $ExpectType TaskEither<string, never>
pipe(
_.left('a'),
_.orElseFirstTaskK((a) => T.of(a.length))
)

//
// orLeft
//
Expand Down
7 changes: 7 additions & 0 deletions src/IOEither.ts
Expand Up @@ -263,6 +263,13 @@ export const orElseFirstW: <E1, E2, B>(
onLeft: (e: E1) => IOEither<E2, B>
) => <A>(ma: IOEither<E1, A>) => IOEither<E1 | E2, A> = orElseFirst as any

/**
* @category combinators
* @since 2.11.8
*/
export const orElseFirstIOK: <E, B>(onLeft: (e: E) => IO<B>) => <A>(ma: IOEither<E, A>) => IOEither<E, A> = (onLeft) =>
orElseFirst(fromIOK(onLeft))

/**
* @category combinators
* @since 2.11.0
Expand Down
16 changes: 16 additions & 0 deletions src/TaskEither.ts
Expand Up @@ -349,6 +349,22 @@ export const orElseFirstW: <E1, E2, B>(
onLeft: (e: E1) => TaskEither<E2, B>
) => <A>(ma: TaskEither<E1, A>) => TaskEither<E1 | E2, A> = orElseFirst as any

/**
* @category combinators
* @since 2.11.8
*/
export const orElseFirstIOK: <E, B>(onLeft: (e: E) => IO<B>) => <A>(ma: TaskEither<E, A>) => TaskEither<E, A> = (
onLeft
) => orElseFirst(fromIOK(onLeft))

/**
* @category combinators
* @since 2.11.8
*/
export const orElseFirstTaskK: <E, B>(onLeft: (e: E) => Task<B>) => <A>(ma: TaskEither<E, A>) => TaskEither<E, A> = (
onLeft
) => orElseFirst(fromTaskK(onLeft))

/**
* @category combinators
* @since 2.11.0
Expand Down
6 changes: 6 additions & 0 deletions test/IOEither.ts
Expand Up @@ -274,6 +274,12 @@ describe('IOEither', () => {
U.deepStrictEqual(pipe(_.left('aa'), f)(), E.left('aa!'))
})

it('orElseFirstIOK', () => {
const f = _.orElseFirstIOK((e: string) => I.of(e.length))
U.deepStrictEqual(pipe(_.right(1), f)(), E.right(1))
U.deepStrictEqual(pipe(_.left('a'), f)(), E.left('a'))
})

it('orLeft', () => {
const f = _.orLeft((e: string) => I.of(e + '!'))
U.deepStrictEqual(pipe(_.right(1), f)(), E.right(1))
Expand Down
12 changes: 12 additions & 0 deletions test/TaskEither.ts
Expand Up @@ -431,6 +431,18 @@ describe('TaskEither', () => {
U.deepStrictEqual(await pipe(_.left('aa'), f)(), E.left('aa!'))
})

it('orElseFirstIOK', async () => {
const f = _.orElseFirstIOK((e: string) => I.of(e.length))
U.deepStrictEqual(await pipe(_.right(1), f)(), E.right(1))
U.deepStrictEqual(await pipe(_.left('a'), f)(), E.left('a'))
})

it('orElseFirstTaskK', async () => {
const f = _.orElseFirstTaskK((e: string) => T.of(e.length))
U.deepStrictEqual(await pipe(_.right(1), f)(), E.right(1))
U.deepStrictEqual(await pipe(_.left('a'), f)(), E.left('a'))
})

it('orLeft', async () => {
const f = _.orLeft((e: string) => T.of(e + '!'))
U.deepStrictEqual(await pipe(_.right(1), f)(), E.right(1))
Expand Down