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 non and anon #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions docs/modules/Iso.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ Added in v2.3.0
- [composePrism](#composeprism)
- [composeTraversal](#composetraversal)
- [constructors](#constructors)
- [anon](#anon)
- [id](#id)
- [iso](#iso)
- [non](#non)
- [reverse](#reverse)
- [converters](#converters)
- [asLens](#aslens)
Expand Down Expand Up @@ -382,6 +384,19 @@ Added in v2.3.8

# constructors

## anon

Create an `Iso<Option<A>, A>` given a `Predicate<A>` and some value to
correspond to `none`.

**Signature**

```ts
export declare const anon: <A>(a: A) => (pred: Predicate<A>) => Iso<Option<A>, A>
```

Added in v2.4.0

## id

**Signature**
Expand All @@ -402,6 +417,55 @@ export declare const iso: <S, A>(get: (s: S) => A, reverseGet: (a: A) => S) => I

Added in v2.3.8

## non

Create an `Iso<Option<A>, A>` given an `Eq<A>` and some value to correspond
to `none`.

**Signature**

```ts
export declare const non: <A>(eq: Eq<A>) => (a: A) => Iso<Option<A>, A>
```

**Example**

```ts
import { eqNumber } from 'fp-ts/Eq'
import { pipe } from 'fp-ts/function'
import { non } from 'monocle-ts/Iso'
import { atKey, composeIso, id } from 'monocle-ts/Lens'

// Can be used to have "defaults"
const a = pipe(id<Record<string, number>>(), atKey('a'), composeIso(non(eqNumber)(0)))
assert.deepStrictEqual(a.get({}), 0)
assert.deepStrictEqual(a.get({ a: 1 }), 1)
assert.deepStrictEqual(a.set(0)({ a: 1 }), {})
assert.deepStrictEqual(a.set(1)({}), { a: 1 })
```

**Example**

```ts
import { eqString } from 'fp-ts/Eq'
import { pipe } from 'fp-ts/function'
import { none } from 'fp-ts/Option'
import { getEq } from 'fp-ts/ReadonlyRecord'
import { non } from 'monocle-ts/Iso'
import { atKey, composeIso, id } from 'monocle-ts/Lens'

// Or delete surrounding structure when a nested value becomes empty
const b = pipe(
id<Record<string, Record<string, string>>>(),
atKey('hello'),
composeIso(non(getEq(eqString))({})),
atKey('world')
)
assert.deepStrictEqual(b.set(none)({ hello: { world: '!' } }), {})
```

Added in v2.4.0

## reverse

**Signature**
Expand Down
64 changes: 62 additions & 2 deletions src/Iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
*/
import { Category2 } from 'fp-ts/lib/Category'
import { Either } from 'fp-ts/lib/Either'
import { flow, identity, Predicate, Refinement } from 'fp-ts/lib/function'
import { Eq } from 'fp-ts/lib/Eq'
import { flow, identity, not, Predicate, Refinement } from 'fp-ts/lib/function'
import { Functor, Functor1, Functor2, Functor3 } from 'fp-ts/lib/Functor'
import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from 'fp-ts/lib/HKT'
import { Invariant2 } from 'fp-ts/lib/Invariant'
import { Option } from 'fp-ts/lib/Option'
import { fromPredicate, getOrElse, Option } from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/pipeable'
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray'
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord'
Expand Down Expand Up @@ -61,6 +62,65 @@ export const iso: <S, A>(get: Iso<S, A>['get'], reverseGet: Iso<S, A>['reverseGe
*/
export const id = <S>(): Iso<S, S> => iso(identity, identity)

/**
* Create an `Iso<Option<A>, A>` given an `Eq<A>` and some value to correspond
* to `none`.
*
* @example
* import { eqNumber } from 'fp-ts/Eq'
* import { pipe } from 'fp-ts/function'
* import { non } from 'monocle-ts/Iso'
* import { atKey, composeIso, id } from 'monocle-ts/Lens'
*
* // Can be used to have "defaults"
* const a = pipe(
* id<Record<string, number>>(),
* atKey('a'),
* composeIso(non(eqNumber)(0))
* )
* assert.deepStrictEqual(a.get({}), 0)
* assert.deepStrictEqual(a.get({ a: 1 }), 1)
* assert.deepStrictEqual(a.set(0)({ a: 1 }), {})
* assert.deepStrictEqual(a.set(1)({}), { a: 1 })
*
*
* @example
* import { eqString } from 'fp-ts/Eq'
* import { pipe } from 'fp-ts/function'
* import { none } from 'fp-ts/Option'
* import { getEq } from 'fp-ts/ReadonlyRecord'
* import { non } from 'monocle-ts/Iso'
* import { atKey, composeIso, id } from 'monocle-ts/Lens'
*
* // Or delete surrounding structure when a nested value becomes empty
* const b = pipe(
* id<Record<string, Record<string, string>>>(),
* atKey('hello'),
* composeIso(non(getEq(eqString))({})),
* atKey('world'),
* )
* assert.deepStrictEqual(b.set(none)({ hello: { world: '!' } }), {})
*
* @category constructors
* @since 2.4.0
*/
export const non = <A>(eq: Eq<A>) => (a: A): Iso<Option<A>, A> => ({
get: getOrElse(() => a),
reverseGet: fromPredicate((b) => !eq.equals(a, b))
})

/**
* Create an `Iso<Option<A>, A>` given a `Predicate<A>` and some value to
* correspond to `none`.
*
* @category constructors
* @since 2.4.0
*/
export const anon = <A>(a: A) => (pred: Predicate<A>): Iso<Option<A>, A> => ({
get: getOrElse(() => a),
reverseGet: fromPredicate(not(pred))
})

// -------------------------------------------------------------------------------------
// converters
// -------------------------------------------------------------------------------------
Expand Down
17 changes: 17 additions & 0 deletions test/Iso.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as assert from 'assert'
import * as Eq from 'fp-ts/lib/Eq'
import * as Id from 'fp-ts/lib/Identity'
import * as O from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/pipeable'
Expand Down Expand Up @@ -292,4 +293,20 @@ describe('Iso', () => {
U.deepStrictEqual(optional.set(1)({ a: [-1] }), { a: [-1] })
U.deepStrictEqual(optional.set(3)({ a: [-1, 2] }), { a: [-1, 3] })
})

it('non', () => {
const sa = _.non(Eq.eqNumber)(0)
U.deepStrictEqual(sa.get(O.some(1)), 1)
U.deepStrictEqual(sa.get(O.none), 0)
U.deepStrictEqual(sa.reverseGet(0), O.none)
U.deepStrictEqual(sa.reverseGet(1), O.some(1))
})

it('anon', () => {
const sa = _.anon(0)((a) => a === 0)
U.deepStrictEqual(sa.get(O.some(1)), 1)
U.deepStrictEqual(sa.get(O.none), 0)
U.deepStrictEqual(sa.reverseGet(0), O.none)
U.deepStrictEqual(sa.reverseGet(1), O.some(1))
})
})