Link Search Menu Expand Document

index overview

Added in v1.0.0


Table of contents


constructor

At (class)

Signature

export declare class At<S, I, A> {
  constructor(readonly at: (i: I) => Lens<S, A>)
}

Added in v1.2.0

fromIso (method)

lift an instance of At using an Iso

Signature

fromIso<T>(iso: Iso<T, S>): At<T, I, A>

Added in v1.2.0

_tag (property)

Signature

readonly _tag: "At"

Added in v1.0.0

Fold (class)

Signature

export declare class Fold<S, A> {
  constructor(readonly foldMap: <M>(M: Monoid<M>) => (f: (a: A) => M) => (s: S) => M)
}

Added in v1.0.0

compose (method)

compose a Fold with a Fold

Signature

compose<B>(ab: Fold<A, B>): Fold<S, B>

Added in v1.0.0

composeFold (method)

Alias of compose

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B>

Added in v1.0.0

composeGetter (method)

compose a Fold with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Fold<S, B>

Added in v1.0.0

composeTraversal (method)

compose a Fold with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Fold<S, B>

Added in v1.0.0

composeOptional (method)

compose a Fold with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Fold<S, B>

Added in v1.0.0

composeLens (method)

compose a Fold with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Fold<S, B>

Added in v1.0.0

composePrism (method)

compose a Fold with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Fold<S, B>

Added in v1.0.0

composeIso (method)

compose a Fold with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Fold<S, B>

Added in v1.0.0

find (method)

find the first target of a Fold matching the predicate

Signature

find<B extends A>(p: Refinement<A, B>): (s: S) => Option<B>
find(p: Predicate<A>): (s: S) => Option<A>

Added in v1.0.0

headOption (method)

get the first target of a Fold

Signature

headOption(s: S): Option<A>

Added in v1.0.0

_tag (property)

Signature

readonly _tag: "Fold"

Added in v1.0.0

getAll (property)

get all the targets of a Fold

Signature

readonly getAll: (s: S) => Array<A>

Added in v1.0.0

exist (property)

check if at least one target satisfies the predicate

Signature

readonly exist: (p: Predicate<A>) => Predicate<S>

Added in v1.0.0

all (property)

check if all targets satisfy the predicate

Signature

readonly all: (p: Predicate<A>) => Predicate<S>

Added in v1.0.0

Getter (class)

Signature

export declare class Getter<S, A> {
  constructor(readonly get: (s: S) => A)
}

Added in v1.0.0

asFold (method)

view a Getter as a Fold

Signature

asFold(): Fold<S, A>

Added in v1.0.0

compose (method)

compose a Getter with a Getter

Signature

compose<B>(ab: Getter<A, B>): Getter<S, B>

Added in v1.0.0

composeGetter (method)

Alias of compose

Signature

composeGetter<B>(ab: Getter<A, B>): Getter<S, B>

Added in v1.0.0

composeFold (method)

compose a Getter with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B>

Added in v1.0.0

composeLens (method)

compose a Getter with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Getter<S, B>

Added in v1.0.0

composeIso (method)

compose a Getter with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Getter<S, B>

Added in v1.0.0

composeTraversal (method)

compose a Getter with a Optional

Signature

composeTraversal<B>(ab: Traversal<A, B>): Fold<S, B>

Added in v1.0.0

composeOptional (method)

compose a Getter with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Fold<S, B>

Added in v1.0.0

composePrism (method)

compose a Getter with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Fold<S, B>

Added in v1.0.0

_tag (property)

Signature

readonly _tag: "Getter"

Added in v1.0.0

Index (class)

Signature

export declare class Index<S, I, A> {
  constructor(readonly index: (i: I) => Optional<S, A>)
}

Added in v1.2.0

fromAt (static method)

Signature

static fromAt<T, J, B>(at: At<T, J, Option<B>>): Index<T, J, B>

Added in v1.2.0

fromIso (method)

lift an instance of Index using an Iso

Signature

fromIso<T>(iso: Iso<T, S>): Index<T, I, A>

Added in v1.2.0

_tag (property)

Signature

readonly _tag: "Index"

Added in v1.0.0

Iso (class)

Laws:

  1. reverseGet(get(s)) = s
  2. get(reversetGet(a)) = a

Signature

export declare class Iso<S, A> {
  constructor(readonly get: (s: S) => A, readonly reverseGet: (a: A) => S)
}

Added in v1.0.0

reverse (method)

reverse the Iso: the source becomes the target and the target becomes the source

Signature

reverse(): Iso<A, S>

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S

Added in v1.0.0

asLens (method)

view an Iso as a Lens

Signature

asLens(): Lens<S, A>

Added in v1.0.0

asPrism (method)

view an Iso as a Prism

Signature

asPrism(): Prism<S, A>

Added in v1.0.0

asOptional (method)

view an Iso as a Optional

Signature

asOptional(): Optional<S, A>

Added in v1.0.0

asTraversal (method)

view an Iso as a Traversal

Signature

asTraversal(): Traversal<S, A>

Added in v1.0.0

asFold (method)

view an Iso as a Fold

Signature

asFold(): Fold<S, A>

Added in v1.0.0

asGetter (method)

view an Iso as a Getter

Signature

asGetter(): Getter<S, A>

Added in v1.0.0

asSetter (method)

view an Iso as a Setter

Signature

asSetter(): Setter<S, A>

Added in v1.0.0

compose (method)

compose an Iso with an Iso

Signature

compose<B>(ab: Iso<A, B>): Iso<S, B>

Added in v1.0.0

composeIso (method)

Alias of compose

Signature

composeIso<B>(ab: Iso<A, B>): Iso<S, B>

Added in v1.0.0

composeLens (method)

compose an Iso with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Lens<S, B>

Added in v1.0.0

composePrism (method)

compose an Iso with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Prism<S, B>

Added in v1.0.0

composeOptional (method)

compose an Iso with an Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Optional<S, B>

Added in v1.0.0

composeTraversal (method)

compose an Iso with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B>

Added in v1.0.0

composeFold (method)

compose an Iso with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B>

Added in v1.0.0

composeGetter (method)

compose an Iso with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Getter<S, B>

Added in v1.0.0

composeSetter (method)

compose an Iso with a Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B>

Added in v1.0.0

_tag (property)

Signature

readonly _tag: "Iso"

Added in v1.0.0

unwrap (property)

Signature

readonly unwrap: (s: S) => A

Added in v1.0.0

to (property)

Signature

readonly to: (s: S) => A

Added in v1.0.0

wrap (property)

Signature

readonly wrap: (a: A) => S

Added in v1.0.0

from (property)

Signature

readonly from: (a: A) => S

Added in v1.0.0

Lens (class)

Laws:

  1. get(set(a)(s)) = a
  2. set(get(s))(s) = s
  3. set(a)(set(a)(s)) = set(a)(s)

Signature

export declare class Lens<S, A> {
  constructor(readonly get: (s: S) => A, readonly set: (a: A) => (s: S) => S)
}

Added in v1.0.0

fromPath (static method)

Signature

static fromPath<S>(): LensFromPath<S>

Example

import { Lens } from 'monocle-ts'

type Person = {
  name: string
  age: number
  address: {
    city: string
  }
}

const city = Lens.fromPath<Person>()(['address', 'city'])

const person: Person = { name: 'Giulio', age: 43, address: { city: 'Milan' } }

assert.strictEqual(city.get(person), 'Milan')
assert.deepStrictEqual(city.set('London')(person), { name: 'Giulio', age: 43, address: { city: 'London' } })

Added in v1.0.0

fromProp (static method)

Returns a Lens from a type and a prop

Signature

static fromProp<S>(): <P extends keyof S>(prop: P) => Lens<S, S[P]>

Example

import { Lens } from 'monocle-ts'

type Person = {
  name: string
  age: number
}

const age = Lens.fromProp<Person>()('age')

const person: Person = { name: 'Giulio', age: 43 }

assert.strictEqual(age.get(person), 43)
assert.deepStrictEqual(age.set(44)(person), { name: 'Giulio', age: 44 })

Added in v1.0.0

fromProps (static method)

Returns a Lens from a type and an array of props

Signature

static fromProps<S>(): <P extends keyof S>(props: Array<P>) => Lens<S, { [K in P]: S[K] }>

Example

import { Lens } from 'monocle-ts'

interface Person {
  name: string
  age: number
  rememberMe: boolean
}

const lens = Lens.fromProps<Person>()(['name', 'age'])

const person: Person = { name: 'Giulio', age: 44, rememberMe: true }

assert.deepStrictEqual(lens.get(person), { name: 'Giulio', age: 44 })
assert.deepStrictEqual(lens.set({ name: 'Guido', age: 47 })(person), { name: 'Guido', age: 47, rememberMe: true })

Added in v1.0.0

fromNullableProp (static method)

Returns a Lens from a nullable (A | null | undefined) prop

Signature

static fromNullableProp<S>(): <A extends S[K], K extends keyof S>(
    k: K,
    defaultValue: A
  ) => Lens<S, NonNullable<S[K]>>

Example

import { Lens } from 'monocle-ts'

interface Outer {
  inner?: Inner
}

interface Inner {
  value: number
  foo: string
}

const inner = Lens.fromNullableProp<Outer>()('inner', { value: 0, foo: 'foo' })
const value = Lens.fromProp<Inner>()('value')
const lens = inner.compose(value)

assert.deepStrictEqual(lens.set(1)({}), { inner: { value: 1, foo: 'foo' } })
assert.strictEqual(lens.get({}), 0)
assert.deepStrictEqual(lens.set(1)({ inner: { value: 1, foo: 'bar' } }), { inner: { value: 1, foo: 'bar' } })
assert.strictEqual(lens.get({ inner: { value: 1, foo: 'bar' } }), 1)

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S

Added in v1.0.0

asOptional (method)

view a Lens as a Optional

Signature

asOptional(): Optional<S, A>

Added in v1.0.0

asTraversal (method)

view a Lens as a Traversal

Signature

asTraversal(): Traversal<S, A>

Added in v1.0.0

asSetter (method)

view a Lens as a Setter

Signature

asSetter(): Setter<S, A>

Added in v1.0.0

asGetter (method)

view a Lens as a Getter

Signature

asGetter(): Getter<S, A>

Added in v1.0.0

asFold (method)

view a Lens as a Fold

Signature

asFold(): Fold<S, A>

Added in v1.0.0

compose (method)

compose a Lens with a Lens

Signature

compose<B>(ab: Lens<A, B>): Lens<S, B>

Added in v1.0.0

composeLens (method)

Alias of compose

Signature

composeLens<B>(ab: Lens<A, B>): Lens<S, B>

Added in v1.0.0

composeGetter (method)

compose a Lens with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Getter<S, B>

Added in v1.0.0

composeFold (method)

compose a Lens with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B>

Added in v1.0.0

composeOptional (method)

compose a Lens with an Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Optional<S, B>

Added in v1.0.0

composeTraversal (method)

compose a Lens with an Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B>

Added in v1.0.0

composeSetter (method)

compose a Lens with an Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B>

Added in v1.0.0

composeIso (method)

compose a Lens with an Iso

Signature

composeIso<B>(ab: Iso<A, B>): Lens<S, B>

Added in v1.0.0

composePrism (method)

compose a Lens with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Optional<S, B>

Added in v1.0.0

_tag (property)

Signature

readonly _tag: "Lens"

Added in v1.0.0

Optional (class)

Laws:

  1. pipe(getOption(s), fold(() => s, a => set(a)(s))) = s
  2. getOption(set(a)(s)) = pipe(getOption(s), map(_ => a))
  3. set(a)(set(a)(s)) = set(a)(s)

Signature

export declare class Optional<S, A> {
  constructor(readonly getOption: (s: S) => Option<A>, readonly set: (a: A) => (s: S) => S)
}

Added in v1.0.0

fromPath (static method)

Returns an Optional from a nullable (A | null | undefined) prop

Signature

static fromPath<S>(): OptionalFromPath<S>

Example

import { Optional } from 'monocle-ts'

interface Phone {
  number: string
}
interface Employment {
  phone?: Phone
}
interface Info {
  employment?: Employment
}
interface Response {
  info?: Info
}

const numberFromResponse = Optional.fromPath<Response>()(['info', 'employment', 'phone', 'number'])

const response1: Response = {
  info: {
    employment: {
      phone: {
        number: '555-1234',
      },
    },
  },
}
const response2: Response = {
  info: {
    employment: {},
  },
}

numberFromResponse.getOption(response1) // some('555-1234')
numberFromResponse.getOption(response2) // none

Added in v2.1.0

fromNullableProp (static method)

Signature

static fromNullableProp<S>(): <K extends keyof S>(k: K) => Optional<S, NonNullable<S[K]>>

Example

import { Optional } from 'monocle-ts'

interface S {
  a: number | undefined | null
}

const optional = Optional.fromNullableProp<S>()('a')

const s1: S = { a: undefined }
const s2: S = { a: null }
const s3: S = { a: 1 }

assert.deepStrictEqual(optional.set(2)(s1), s1)
assert.deepStrictEqual(optional.set(2)(s2), s2)
assert.deepStrictEqual(optional.set(2)(s3), { a: 2 })

Added in v1.0.0

fromOptionProp (static method)

Returns an Optional from an option (Option<A>) prop

Signature

static fromOptionProp<S>(): <P extends OptionPropertyNames<S>>(prop: P) => Optional<S, OptionPropertyType<S, P>>

Example

import { Optional } from 'monocle-ts'
import * as O from 'fp-ts/Option'

interface S {
  a: O.Option<number>
}

const optional = Optional.fromOptionProp<S>()('a')
const s1: S = { a: O.none }
const s2: S = { a: O.some(1) }
assert.deepStrictEqual(optional.set(2)(s1), s1)
assert.deepStrictEqual(optional.set(2)(s2), { a: O.some(2) })

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S

Added in v1.0.0

modifyOption (method)

Signature

modifyOption(f: (a: A) => A): (s: S) => Option<S>

Added in v1.0.0

asTraversal (method)

view a Optional as a Traversal

Signature

asTraversal(): Traversal<S, A>

Added in v1.0.0

asFold (method)

view an Optional as a Fold

Signature

asFold(): Fold<S, A>

Added in v1.0.0

asSetter (method)

view an Optional as a Setter

Signature

asSetter(): Setter<S, A>

Added in v1.0.0

compose (method)

compose a Optional with a Optional

Signature

compose<B>(ab: Optional<A, B>): Optional<S, B>

Added in v1.0.0

composeOptional (method)

Alias of compose

Signature

composeOptional<B>(ab: Optional<A, B>): Optional<S, B>

Added in v1.0.0

composeTraversal (method)

compose an Optional with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B>

Added in v1.0.0

composeFold (method)

compose an Optional with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B>

Added in v1.0.0

composeSetter (method)

compose an Optional with a Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B>

Added in v1.0.0

composeLens (method)

compose an Optional with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Optional<S, B>

Added in v1.0.0

composePrism (method)

compose an Optional with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Optional<S, B>

Added in v1.0.0

composeIso (method)

compose an Optional with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Optional<S, B>

Added in v1.0.0

composeGetter (method)

compose an Optional with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Fold<S, B>

Added in v1.0.0

_tag (property)

Signature

readonly _tag: "Optional"

Added in v1.0.0

Prism (class)

Laws:

  1. pipe(getOption(s), fold(() => s, reverseGet)) = s
  2. getOption(reverseGet(a)) = some(a)

Signature

export declare class Prism<S, A> {
  constructor(readonly getOption: (s: S) => Option<A>, readonly reverseGet: (a: A) => S)
}

Added in v1.0.0

fromPredicate (static method)

Signature

static fromPredicate<S, A extends S>(refinement: Refinement<S, A>): Prism<S, A>
static fromPredicate<A>(predicate: Predicate<A>): Prism<A, A>

Added in v1.0.0

some (static method)

Signature

static some<A>(): Prism<Option<A>, A>

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S

Added in v1.0.0

modifyOption (method)

Signature

modifyOption(f: (a: A) => A): (s: S) => Option<S>

Added in v1.0.0

set (method)

set the target of a Prism with a value

Signature

set(a: A): (s: S) => S

Added in v1.0.0

asOptional (method)

view a Prism as a Optional

Signature

asOptional(): Optional<S, A>

Added in v1.0.0

asTraversal (method)

view a Prism as a Traversal

Signature

asTraversal(): Traversal<S, A>

Added in v1.0.0

asSetter (method)

view a Prism as a Setter

Signature

asSetter(): Setter<S, A>

Added in v1.0.0

asFold (method)

view a Prism as a Fold

Signature

asFold(): Fold<S, A>

Added in v1.0.0

compose (method)

compose a Prism with a Prism

Signature

compose<B>(ab: Prism<A, B>): Prism<S, B>

Added in v1.0.0

composePrism (method)

Alias of compose

Signature

composePrism<B>(ab: Prism<A, B>): Prism<S, B>

Added in v1.0.0

composeOptional (method)

compose a Prism with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Optional<S, B>

Added in v1.0.0

composeTraversal (method)

compose a Prism with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B>

Added in v1.0.0

composeFold (method)

compose a Prism with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B>

Added in v1.0.0

composeSetter (method)

compose a Prism with a Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B>

Added in v1.0.0

composeIso (method)

compose a Prism with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Prism<S, B>

Added in v1.0.0

composeLens (method)

compose a Prism with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Optional<S, B>

Added in v1.0.0

composeGetter (method)

compose a Prism with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Fold<S, B>

Added in v1.0.0

_tag (property)

Signature

readonly _tag: "Prism"

Added in v1.0.0

Setter (class)

Signature

export declare class Setter<S, A> {
  constructor(readonly modify: (f: (a: A) => A) => (s: S) => S)
}

Added in v1.0.0

set (method)

Signature

set(a: A): (s: S) => S

Added in v1.0.0

compose (method)

compose a Setter with a Setter

Signature

compose<B>(ab: Setter<A, B>): Setter<S, B>

Added in v1.0.0

composeSetter (method)

Alias of compose

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B>

Added in v1.0.0

composeTraversal (method)

compose a Setter with a Traversal

Signature

composeTraversal<B>(ab: Traversal<A, B>): Setter<S, B>

Added in v1.0.0

composeOptional (method)

compose a Setter with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Setter<S, B>

Added in v1.0.0

composeLens (method)

compose a Setter with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Setter<S, B>

Added in v1.0.0

composePrism (method)

compose a Setter with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Setter<S, B>

Added in v1.0.0

composeIso (method)

compose a Setter with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Setter<S, B>

Added in v1.0.0

_tag (property)

Signature

readonly _tag: "Setter"

Added in v1.0.0

Traversal (class)

Signature

export declare class Traversal<S, A> {
  constructor(
    // Van Laarhoven representation
    readonly modifyF: ModifyF<S, A>
  )
}

Added in v1.0.0

modify (method)

Signature

modify(f: (a: A) => A): (s: S) => S

Added in v1.0.0

set (method)

Signature

set(a: A): (s: S) => S

Added in v1.0.0

filter (method)

focus the items matched by a traversal to those that match a predicate

Signature

filter<B extends A>(refinement: Refinement<A, B>): Traversal<S, B>
filter(predicate: Predicate<A>): Traversal<S, A>

Example

import { fromTraversable, Lens } from 'monocle-ts'
import { Traversable } from 'fp-ts/Array'

interface Person {
  name: string
  cool: boolean
}

const peopleTraversal = fromTraversable(Traversable)<Person>()
const coolLens = Lens.fromProp<Person>()('cool')
const people = [
  { name: 'bill', cool: false },
  { name: 'jill', cool: true },
]

const actual = peopleTraversal
  .filter((p) => p.name === 'bill')
  .composeLens(coolLens)
  .set(true)(people)

assert.deepStrictEqual(actual, [
  { name: 'bill', cool: true },
  { name: 'jill', cool: true },
])

Added in v1.0.0

asFold (method)

view a Traversal as a Fold

Signature

asFold(): Fold<S, A>

Added in v1.0.0

asSetter (method)

view a Traversal as a Setter

Signature

asSetter(): Setter<S, A>

Added in v1.0.0

compose (method)

compose a Traversal with a Traversal

Signature

compose<B>(ab: Traversal<A, B>): Traversal<S, B>

Added in v1.0.0

composeTraversal (method)

Alias of compose

Signature

composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B>

Added in v1.0.0

composeFold (method)

compose a Traversal with a Fold

Signature

composeFold<B>(ab: Fold<A, B>): Fold<S, B>

Added in v1.0.0

composeSetter (method)

compose a Traversal with a Setter

Signature

composeSetter<B>(ab: Setter<A, B>): Setter<S, B>

Added in v1.0.0

composeOptional (method)

compose a Traversal with a Optional

Signature

composeOptional<B>(ab: Optional<A, B>): Traversal<S, B>

Added in v1.0.0

composeLens (method)

compose a Traversal with a Lens

Signature

composeLens<B>(ab: Lens<A, B>): Traversal<S, B>

Added in v1.0.0

composePrism (method)

compose a Traversal with a Prism

Signature

composePrism<B>(ab: Prism<A, B>): Traversal<S, B>

Added in v1.0.0

composeIso (method)

compose a Traversal with a Iso

Signature

composeIso<B>(ab: Iso<A, B>): Traversal<S, B>

Added in v1.0.0

composeGetter (method)

compose a Traversal with a Getter

Signature

composeGetter<B>(ab: Getter<A, B>): Fold<S, B>

Added in v1.0.0

_tag (property)

Signature

readonly _tag: "Traversal"

Added in v1.0.0

fromFoldable

Create a Fold from a Foldable

Signature

export declare function fromFoldable<F extends URIS3>(F: Foldable3<F>): <U, L, A>() => Fold<Kind3<F, U, L, A>, A>
export declare function fromFoldable<F extends URIS2>(F: Foldable2<F>): <L, A>() => Fold<Kind2<F, L, A>, A>
export declare function fromFoldable<F extends URIS>(F: Foldable1<F>): <A>() => Fold<Kind<F, A>, A>
export declare function fromFoldable<F>(F: Foldable<F>): <A>() => Fold<HKT<F, A>, A>

Added in v1.0.0

fromTraversable

Create a Traversal from a Traversable

Signature

export declare function fromTraversable<T extends URIS3>(
  T: Traversable3<T>
): <U, L, A>() => Traversal<Kind3<T, U, L, A>, A>
export declare function fromTraversable<T extends URIS2>(T: Traversable2<T>): <L, A>() => Traversal<Kind2<T, L, A>, A>
export declare function fromTraversable<T extends URIS>(T: Traversable1<T>): <A>() => Traversal<Kind<T, A>, A>
export declare function fromTraversable<T>(T: Traversable<T>): <A>() => Traversal<HKT<T, A>, A>

Example

import { Lens, fromTraversable } from 'monocle-ts'
import { Traversable } from 'fp-ts/Array'

interface Tweet {
  text: string
}

interface Tweets {
  tweets: Tweet[]
}

const tweetsLens = Lens.fromProp<Tweets>()('tweets')
const tweetTextLens = Lens.fromProp<Tweet>()('text')
const tweetTraversal = fromTraversable(Traversable)<Tweet>()
const composedTraversal = tweetsLens.composeTraversal(tweetTraversal).composeLens(tweetTextLens)

const tweet1: Tweet = { text: 'hello world' }
const tweet2: Tweet = { text: 'foobar' }
const model: Tweets = { tweets: [tweet1, tweet2] }

const actual = composedTraversal.modify((text) => text.split('').reverse().join(''))(model)

assert.deepStrictEqual(actual, { tweets: [{ text: 'dlrow olleh' }, { text: 'raboof' }] })

Added in v1.0.0

utils

LensFromPath (interface)

Signature

export interface LensFromPath<S> {
  <
    K1 extends keyof S,
    K2 extends keyof S[K1],
    K3 extends keyof S[K1][K2],
    K4 extends keyof S[K1][K2][K3],
    K5 extends keyof S[K1][K2][K3][K4]
  >(
    path: [K1, K2, K3, K4, K5]
  ): Lens<S, S[K1][K2][K3][K4][K5]>
  <K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2], K4 extends keyof S[K1][K2][K3]>(
    path: [K1, K2, K3, K4]
  ): Lens<S, S[K1][K2][K3][K4]>
  <K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2]>(path: [K1, K2, K3]): Lens<S, S[K1][K2][K3]>
  <K1 extends keyof S, K2 extends keyof S[K1]>(path: [K1, K2]): Lens<S, S[K1][K2]>
  <K1 extends keyof S>(path: [K1]): Lens<S, S[K1]>
}

Added in v1.3.0

ModifyF (type alias)

Signature

export type ModifyF<S, A> = traversal.ModifyF<S, A>

Added in v1.0.0

OptionalFromPath (interface)

Signature

export interface OptionalFromPath<S> {
  <
    K1 extends keyof S,
    K2 extends keyof NonNullable<S[K1]>,
    K3 extends keyof NonNullable<NonNullable<S[K1]>[K2]>,
    K4 extends keyof NonNullable<NonNullable<NonNullable<S[K1]>[K2]>[K3]>,
    K5 extends keyof NonNullable<NonNullable<NonNullable<NonNullable<S[K1]>[K2]>[K3]>[K4]>
  >(
    path: [K1, K2, K3, K4, K5]
  ): Optional<S, NonNullable<NonNullable<NonNullable<NonNullable<NonNullable<S[K1]>[K2]>[K3]>[K4]>[K5]>>

  <
    K1 extends keyof S,
    K2 extends keyof NonNullable<S[K1]>,
    K3 extends keyof NonNullable<NonNullable<S[K1]>[K2]>,
    K4 extends keyof NonNullable<NonNullable<NonNullable<S[K1]>[K2]>[K3]>
  >(
    path: [K1, K2, K3, K4]
  ): Optional<S, NonNullable<NonNullable<NonNullable<NonNullable<S[K1]>[K2]>[K3]>[K4]>>

  <K1 extends keyof S, K2 extends keyof NonNullable<S[K1]>, K3 extends keyof NonNullable<NonNullable<S[K1]>[K2]>>(
    path: [K1, K2, K3]
  ): Optional<S, NonNullable<NonNullable<NonNullable<S[K1]>[K2]>[K3]>>

  <K1 extends keyof S, K2 extends keyof NonNullable<S[K1]>>(path: [K1, K2]): Optional<
    S,
    NonNullable<NonNullable<S[K1]>[K2]>
  >

  <K1 extends keyof S>(path: [K1]): Optional<S, NonNullable<S[K1]>>
}

Added in v2.1.0

at

Signature

export declare const at: typeof at

Added in v2.3.0

index

Signature

export declare const index: typeof index

Added in v2.3.0

iso

Signature

export declare const iso: typeof iso

Added in v2.3.0

lens

Signature

export declare const lens: typeof lens

Added in v2.3.0

optional

Signature

export declare const optional: typeof optional

Added in v2.3.0

prism

Signature

export declare const prism: typeof prism

Added in v2.3.0

traversal

Signature

export declare const traversal: typeof traversal

Added in v2.3.0