Skip to content

Commit

Permalink
Optional: add missing fromNullable combinator, closes #133
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 13, 2020
1 parent 74dba26 commit 28868ea
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ high state of flux, you're at risk of it changing without notice.
- `Prism`
- (\*) remove `fromSome` constructor (@gcanti)
- (\*) change `fromNullable` signature (@gcanti)
- `Optional`
- add missing `fromNullable` combinator, closes #133 (@gcanti)

(\*) breaking change

Expand Down
13 changes: 13 additions & 0 deletions docs/modules/Optional.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Added in v2.3.0
- [component](#component)
- [filter](#filter)
- [findFirst](#findfirst)
- [fromNullable](#fromnullable)
- [index](#index)
- [key](#key)
- [left](#left)
Expand Down Expand Up @@ -126,6 +127,18 @@ export declare const findFirst: <A>(predicate: Predicate<A>) => <S>(sa: Optional

Added in v2.3.2

## fromNullable

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

**Signature**

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

Added in v2.3.3

## index

Return a `Optional` from a `Optional` focused on a `ReadonlyArray`
Expand Down
10 changes: 10 additions & 0 deletions src/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ export const modifyOption: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>)
*/
export const modify: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => S = _.optionalModify

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

/**
* @category combinators
* @since 2.3.0
Expand Down
16 changes: 6 additions & 10 deletions src/Prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ export const composeOptional = <A, B>(ab: Optional<A, B>) => <S>(sa: Prism<S, A>
// combinators
// -------------------------------------------------------------------------------------

/**
* @category combinators
* @since 2.3.0
*/
export const set: <A>(a: A) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismSet

/**
* @category combinators
* @since 2.3.0
Expand All @@ -142,16 +148,6 @@ export const fromNullable: <S, A>(sa: Prism<S, A>) => Prism<S, NonNullable<A>> =
/*#__PURE__*/
compose(_.prismFromNullable())

/**
* @category combinators
* @since 2.3.0
*/
export const set: <A>(a: A) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismSet

// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------

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

it('fromNullable', () => {
interface S {
a?: number
}
const sa = pipe(_.id<S>(), _.prop('a'), _.fromNullable)
assert.deepStrictEqual(sa.getOption({}), O.none)
assert.deepStrictEqual(sa.getOption({ a: undefined }), O.none)
assert.deepStrictEqual(sa.getOption({ a: 1 }), O.some(1))
assert.deepStrictEqual(sa.set(2)({}), {})
assert.deepStrictEqual(sa.set(2)({ a: undefined }), { a: undefined })
assert.deepStrictEqual(sa.set(2)({ a: 1 }), { a: 2 })
})
})

0 comments on commit 28868ea

Please sign in to comment.