Skip to content

Commit

Permalink
Optional: add setOption
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Mar 4, 2021
1 parent e39570e commit e51c2a5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ high state of flux, you're at risk of it changing without notice.
- `Optional`
- add `composeLens` (@gcanti)
- add `composePrism` (@gcanti)
- add `setOption` (@gcanti)

# 2.3.6

Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Optional.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Added in v2.3.0
- [prop](#prop)
- [props](#props)
- [right](#right)
- [setOption](#setoption)
- [some](#some)
- [traverse](#traverse)
- [compositions](#compositions)
Expand Down Expand Up @@ -262,6 +263,16 @@ export declare const right: <S, E, A>(sea: Optional<S, Either<E, A>>) => Optiona

Added in v2.3.0

## setOption

**Signature**

```ts
export declare const setOption: <A>(a: A) => <S>(optional: Optional<S, A>) => (s: S) => O.Option<S>
```

Added in v2.3.7

## some

Return a `Optional` from a `Optional` focused on the `Some` of a `Option` type.
Expand Down
6 changes: 6 additions & 0 deletions src/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ export const composePrism = <A, B>(ab: Prism<A, B>) => <S>(sa: Optional<S, A>):
export const modifyOption: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => Option<S> =
_.optionalModifyOption

/**
* @category combinators
* @since 2.3.7
*/
export const setOption = <A>(a: A): (<S>(optional: Optional<S, A>) => (s: S) => Option<S>) => modifyOption(() => a)

/**
* @category combinators
* @since 2.3.0
Expand Down
6 changes: 6 additions & 0 deletions test/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,10 @@ describe('Optional', () => {
assert.deepStrictEqual(f([1, 2, 3]), O.some([2, 2, 3]))
assert.deepStrictEqual(f([-1, 2, 3]), O.none)
})

it('setOption', () => {
const sa = pipe(_.id<ReadonlyArray<number>>(), _.index(0))
assert.deepStrictEqual(pipe(sa, _.setOption(2))([]), O.none)
assert.deepStrictEqual(pipe(sa, _.setOption(2))([1, 3]), O.some([2, 3]))
})
})

0 comments on commit e51c2a5

Please sign in to comment.