Skip to content

Commit

Permalink
feat: remove extra fold function
Browse files Browse the repository at this point in the history
  • Loading branch information
jderochervlk committed Aug 2, 2023
1 parent ff91481 commit 77444ce
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RemoteData represents a value that can be in one of four states:
## Example in React

```tsx
import * as RD from '@jvlk/fp-ts-remote-data'
import * as D from '@jvlk/fp-ts-remote-data'
import { pipe } from 'fp-ts/function'

import useBlogRequest from './useBlogRequest'
Expand All @@ -28,7 +28,7 @@ function Component() {

return pipe(
blog,
RD.fold(
D.fold(
() => <Loading />),
err => <Error error={err}>,
() => <Empty/>,
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RemoteData represents a value that can be in one of four states:
## Example in React

```tsx
import * as RD from '@jvlk/fp-ts-remote-data'
import * as D from '@jvlk/fp-ts-remote-data'
import { pipe } from 'fp-ts/function'

import useBlogRequest from './useBlogRequest'
Expand All @@ -31,7 +31,7 @@ function Component() {

return pipe(
blog,
RD.fold(
D.fold(
() => <Loading />),
err => <Error error={err}>,
() => <Empty/>,
Expand Down
38 changes: 19 additions & 19 deletions docs/modules/instances.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export declare const chain: <E, A = E, B = A>(
**Example**

```ts
import * as RD from '@jvlk/fp-ts-remote-data'
import * as D from '@jvlk/fp-ts-remote-data'
import { pipe } from 'fp-ts/function'

pipe(
RD.success(42),
RD.chain((n) => (n > 10 ? RD.success(n) : RD.failure(Error('number is too small'))))
D.success(42),
D.chain((n) => (n > 10 ? D.success(n) : D.failure(Error('number is too small'))))
)
```

Expand All @@ -64,12 +64,12 @@ export declare const chainW: <D, A, B>(
**Example**

```ts
import * as RD from '@jvlk/fp-ts-remote-data'
import * as D from '@jvlk/fp-ts-remote-data'
import { pipe } from 'fp-ts/function'

pipe(
RD.failure<string, string>('error'), // RemoteData<string, string>
RD.chainW(() => RD.failure(Error('number is too small'))) // => RemoteData<Error | string, string>
D.failure<string, string>('error'), // RemoteData<string, string>
D.chainW(() => D.failure(Error('number is too small'))) // => RemoteData<Error | string, string>
)
```

Expand All @@ -87,18 +87,18 @@ export declare const fold: <E, A, B>(
failure: (e: E) => B,
empty: () => B,
success: (a: A) => B
) => (rd: RemoteData<E, A>) => B
) => (d: RemoteData<E, A>) => B
```

**Example**

```ts
import * as RD from '@jvlk/fp-ts-remote-data'
import * as D from '@jvlk/fp-ts-remote-data'
import { pipe } from 'fp-ts/function'

const resultOne = pipe(
RD.success(42),
RD.foldW(
D.success(42),
D.fold(
() => 'loading',
(e) => `${e}`,
() => 'empty',
Expand All @@ -121,18 +121,18 @@ export declare const foldW: <E, A, U, T, V, Z>(
failure: (e: E) => T,
empty: () => V,
success: (a: A) => Z
) => (rd: RemoteData<E, A>) => U | T | V | Z
) => (d: RemoteData<E, A>) => U | T | V | Z
```

**Example**

```ts
import * as RD from '@jvlk/fp-ts-remote-data'
import * as D from '@jvlk/fp-ts-remote-data'
import { pipe } from 'fp-ts/function'

const resultOne = pipe(
RD.success(42),
RD.foldW(
D.success(42),
D.foldW(
() => 'loading',
(e) => Error(e),
() => null,
Expand All @@ -154,17 +154,17 @@ export declare const map: <A, B>(f: (a: A) => B) => <E>(fa: RemoteData<E, A>) =>
**Example**

```ts
import * as RD from '@jvlk/fp-ts-remote-data'
import * as D from '@jvlk/fp-ts-remote-data'
import { pipe } from 'fp-ts/function'

pipe(
RD.success(42),
RD.map((n) => n + 10) // => success(52)
D.success(42),
D.map((n) => n + 10) // => success(52)
)

pipe(
RD.empty,
RD.map((n) => n + 10) // => empty
D.empty,
D.map((n) => n + 10) // => empty
)
```

Expand Down
51 changes: 25 additions & 26 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import { isEmpty, isFailure, isLoading, isSuccess } from './refinements'

/**
* @example
* import * as RD from '@jvlk/fp-ts-remote-data'
* import * as D from '@jvlk/fp-ts-remote-data'
* import { pipe } from 'fp-ts/function'
*
* pipe(
* RD.success(42),
* RD.map(n => n + 10) // => success(52)
* D.success(42),
* D.map(n => n + 10) // => success(52)
* )
*
* pipe(
* RD.empty,
* RD.map(n => n + 10) // => empty
* D.empty,
* D.map(n => n + 10) // => empty
* )
*
* @category instance operations
Expand All @@ -33,12 +33,12 @@ export const map: <A, B>(
* Less strict version of [`chain`](#chain) that allows you to **W**iden the failure type.
*
* @example
* import * as RD from '@jvlk/fp-ts-remote-data'
* import * as D from '@jvlk/fp-ts-remote-data'
* import { pipe } from 'fp-ts/function'
*
* pipe(
* RD.failure<string, string>('error'), // RemoteData<string, string>
* RD.chainW(() => RD.failure(Error('number is too small'))) // => RemoteData<Error | string, string>
* D.failure<string, string>('error'), // RemoteData<string, string>
* D.chainW(() => D.failure(Error('number is too small'))) // => RemoteData<Error | string, string>
* )
*
* @category instance operations
Expand All @@ -51,12 +51,12 @@ export const chainW =

/**
* @example
* import * as RD from '@jvlk/fp-ts-remote-data'
* import * as D from '@jvlk/fp-ts-remote-data'
* import { pipe } from 'fp-ts/function'
*
* pipe(
* RD.success(42),
* RD.chain(n => n > 10 ? RD.success(n) : RD.failure(Error('number is too small')))
* D.success(42),
* D.chain(n => n > 10 ? D.success(n) : D.failure(Error('number is too small')))
* )
*
* @category instance operations
Expand Down Expand Up @@ -90,12 +90,12 @@ export const reduceRight: <A, B>(
* Fold and return different types in the response.
*
* @example
* import * as RD from '@jvlk/fp-ts-remote-data'
* import * as D from '@jvlk/fp-ts-remote-data'
* import { pipe } from 'fp-ts/function'
*
* const resultOne = pipe(
* RD.success(42),
* RD.foldW(
* D.success(42),
* D.foldW(
* () => "loading",
* e => Error(e),
* () => null,
Expand All @@ -113,26 +113,26 @@ export const foldW =
empty: () => V,
success: (a: A) => Z
) =>
(rd: RemoteData<E, A>) => {
return isLoading(rd)
(d: RemoteData<E, A>) => {
return isLoading(d)
? loading()
: isFailure(rd)
? failure(rd.failure)
: isEmpty(rd)
: isFailure(d)
? failure(d.failure)
: isEmpty(d)
? empty()
: success(rd.success)
: success(d.success)
}

/**
* Fold and return the same type in the response.
*
* @example
* import * as RD from '@jvlk/fp-ts-remote-data'
* import * as D from '@jvlk/fp-ts-remote-data'
* import { pipe } from 'fp-ts/function'
*
* const resultOne = pipe(
* RD.success(42),
* RD.foldW(
* D.success(42),
* D.fold(
* () => "loading",
* e => `${e}`,
* () => "empty",
Expand All @@ -149,7 +149,6 @@ export const fold =
failure: (e: E) => B,
empty: () => B,
success: (a: A) => B
) =>
(rd: RemoteData<E, A>) => {
return foldW<E, A, B, B, B, B>(loading, failure, empty, success)(rd)
) => {
return foldW<E, A, B, B, B, B>(loading, failure, empty, success)
}

0 comments on commit 77444ce

Please sign in to comment.