Skip to content

Commit

Permalink
Prism: change fromNullable signature
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 13, 2020
1 parent ecd5a36 commit 74dba26
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 32 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ high state of flux, you're at risk of it changing without notice.

- **Experimental**
- `Prism`
- remove `fromSome` constructor (@gcanti)
- (\*) remove `fromSome` constructor (@gcanti)
- (\*) change `fromNullable` signature (@gcanti)

(\*) breaking change

# 2.3.2

Expand Down
26 changes: 13 additions & 13 deletions docs/modules/Lens.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Added in v2.3.0
- [component](#component)
- [filter](#filter)
- [findFirst](#findfirst)
- [fromNullable](#fromnullable)
- [index](#index)
- [key](#key)
- [left](#left)
Expand All @@ -50,7 +51,6 @@ Added in v2.3.0
- [composeOptional](#composeoptional)
- [composePrism](#composeprism)
- [constructors](#constructors)
- [fromNullable](#fromnullable)
- [id](#id)
- [converters](#converters)
- [asOptional](#asoptional)
Expand Down Expand Up @@ -126,6 +126,18 @@ export declare const findFirst: <A>(predicate: Predicate<A>) => <S>(sa: Lens<S,

Added in v2.3.2

## fromNullable

Return a `Optional` from a `Lens` focused on a nullable value

**Signature**

```ts
export declare const fromNullable: <S, A>(sa: Lens<S, A>) => Optional<S, NonNullable<A>>
```

Added in v2.3.0

## index

Return a `Optional` from a `Lens` focused on a `ReadonlyArray`
Expand Down Expand Up @@ -278,18 +290,6 @@ Added in v2.3.0

# constructors

## fromNullable

Return a `Optional` from a `Lens` focused on a nullable value

**Signature**

```ts
export declare const fromNullable: <S, A>(sa: Lens<S, A>) => Optional<S, NonNullable<A>>
```

Added in v2.3.0

## id

**Signature**
Expand Down
24 changes: 13 additions & 11 deletions docs/modules/Prism.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Added in v2.3.0
- [component](#component)
- [filter](#filter)
- [findFirst](#findfirst)
- [fromNullable](#fromnullable)
- [index](#index)
- [key](#key)
- [left](#left)
Expand All @@ -48,7 +49,6 @@ Added in v2.3.0
- [composeLens](#composelens)
- [composeOptional](#composeoptional)
- [constructors](#constructors)
- [fromNullable](#fromnullable)
- [fromPredicate](#frompredicate)
- [id](#id)
- [converters](#converters)
Expand Down Expand Up @@ -127,6 +127,18 @@ export declare const findFirst: <A>(predicate: Predicate<A>) => <S>(sa: Prism<S,

Added in v2.3.2

## fromNullable

Return a `Prism` from a `Prism` focused on a nullable value

**Signature**

```ts
export declare const fromNullable: <S, A>(sa: Prism<S, A>) => Prism<S, NonNullable<A>>
```

Added in v2.3.3

## index

Return a `Optional` from a `Prism` focused on a `ReadonlyArray`
Expand Down Expand Up @@ -299,16 +311,6 @@ Added in v2.3.0

# constructors

## fromNullable

**Signature**

```ts
export declare const fromNullable: <A>() => Prism<A, NonNullable<A>>
```

Added in v2.3.0

## fromPredicate

**Signature**
Expand Down
2 changes: 1 addition & 1 deletion src/Lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const modify = <A>(f: (a: A) => A) => <S>(sa: Lens<S, A>) => (s: S): S =>
/**
* Return a `Optional` from a `Lens` focused on a nullable value
*
* @category constructors
* @category combinators
* @since 2.3.0
*/
export const fromNullable = <S, A>(sa: Lens<S, A>): Optional<S, NonNullable<A>> =>
Expand Down
16 changes: 10 additions & 6 deletions src/Prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ export const fromPredicate: {
<A>(predicate: Predicate<A>): Prism<A, A>
} = _.prismFromPredicate

/**
* @category constructors
* @since 2.3.0
*/
export const fromNullable: <A>() => Prism<A, NonNullable<A>> = _.prismFromNullable

// -------------------------------------------------------------------------------------
// converters
// -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -138,6 +132,16 @@ export const modifyOption: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S)
*/
export const modify: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismModify

/**
* Return a `Prism` from a `Prism` focused on a nullable value
*
* @category combinators
* @since 2.3.3
*/
export const fromNullable: <S, A>(sa: Prism<S, A>) => Prism<S, NonNullable<A>> =
/*#__PURE__*/
compose(_.prismFromNullable())

/**
* @category combinators
* @since 2.3.0
Expand Down
9 changes: 9 additions & 0 deletions test/Prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,13 @@ describe('Prism', () => {
)
assert.deepStrictEqual(modify(O.some(['a'])), O.some(['A']))
})

it('fromNullable', () => {
type S = O.Option<number | undefined>
const sa = pipe(_.id<S>(), _.some, _.fromNullable)
assert.deepStrictEqual(sa.getOption(O.none), O.none)
assert.deepStrictEqual(sa.getOption(O.some(undefined)), O.none)
assert.deepStrictEqual(sa.getOption(O.some(1)), O.some(1))
assert.deepStrictEqual(sa.reverseGet(1), O.some(1))
})
})

0 comments on commit 74dba26

Please sign in to comment.