{"dist":{"shasum":"8fb6ecf3ecbb2ff8df733926a356d643dd104f2d","tarball":"https://registry.npmjs.org/optics-ts/-/optics-ts-1.1.0-pre.1.tgz","fileCount":20,"integrity":"sha512-edojl0zfztNDptZ+51/T4s/1S1mzg6yUzqo5k4m13+QWrx8IKbfAlsbxspozG96knTMD7xk8BOF8h1evTjbBlg==","signatures":[{"sig":"MEUCIQDS0A6WP8ZpabocGpd9JHJY1ZkMsV3F/Q4iaaVimiKOYgIgMgrh5Alt60ivC+dOhKSLhQgBcA7tZVdgid6Snw3s3k0=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":95235,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeqdWJCRA9TVsSAnZWagAAFpUP/2I8lYtUp2dEhr6esaof\nUbqoQLIk4v/Xco+0eDEPT2tlrE70bTdwERsQIdsG610wK2iy7ZXQCNxAppb/\nqXBK1rc2PCkzDX7CDANOqyGTA501leIYAGpYad/acvhYtwOO3fBdWahY1Z35\nxUujVZ9qqaG0b7VGxF2rruDXrrcdDj8dg1t+FIKl7DDcSc16e/H228gPHwNy\nKsgiVwVT539hjzA1hMpCMGLo4nPArL5ta7E7qh6ppmkskzwSz4ZV/YCMAT3n\nRJvFFKdtsTy4Ky32MtTcD9VPfD/dBSXviRbMsGLUgHdNZyDf5wSV3jSHShzz\nQIJ8L2oiZkKST+yFC7VaIkcRadAMiuIvI7JGt8ONo9p1VLLx7XIPOyTQAZB6\nxtNQEMeiTKRAE/HklMZq8snuG4mmHS4JU3UFsc6l2h7IrUThUN0f1J+TAr/E\nekSMFu6SVZfBRlmlG4Q9hXQeO2ZUQl/mseNn4yXSeWV6L09g0Qhd5LV1YbTB\ndr35EmqFOWXrlHmR7dvLYRIe84esDJ0wbK2d5GMemz96HkkwTunlpI4Sk4Ns\ndU2wcChkqfqOEJhKBA+WjD9Q4zsaNwDs2QChROSJNeJl3Zh6trwBNUH1iKwH\n2Awj+xEmqcY8ZZY0QT1Yw3M8Kn97pabPM1ZwZCAT5HKQ4m9zhBPup0EsP1Fr\n3DoX\r\n=7mv3\r\n-----END PGP SIGNATURE-----\r\n"},"main":"./dist/lib/index.js","name":"optics-ts","author":{"name":"Petri Lehtinen","email":"petri@digip.org"},"module":"./dist/esm/index.js","readme":"# optics-ts\n\n[![CircleCI](https://circleci.com/gh/akheron/optics-ts.svg?style=shield)](https://circleci.com/gh/akheron/optics-ts)\n\n`optics-ts` provides type-safe, ergonomic, polymorphic optics for\nTypeScript:\n\n- **Optics** allow you to read or modify values from deeply nested\n  data structures, while keeping all data immutable.\n- **Ergonomic**: Optics are composed with method chaining, making it\n  easy and fun!\n- **Polymorphic**: When writing through the optics, you can change the\n  data types in the nested structure.\n- **Type-safe**: The compiler will type check all operations you do.\n  No `any`, ever.\n\n`optics-ts` supports equivalences, isomorphisms, lenses, prisms,\ntraversals, getters, affine folds and folds.\n\n<!-- START doctoc generated TOC please keep comment here to allow auto update -->\n<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Tutorial](#tutorial)\n  - [Lens](#lens)\n  - [Prism](#prism)\n  - [Traversal](#traversal)\n  - [Other types of optics](#other-types-of-optics)\n  - [Polymorphism](#polymorphism)\n- [API reference](#api-reference)\n  - [Types of optics](#types-of-optics)\n  - [Method chaining](#method-chaining)\n  - [Type parameters](#type-parameters)\n  - [Top-level functions](#top-level-functions)\n    - [`optic<S>(): Equivalence<S, _, S>`](#optics-equivalences-_-s)\n    - [`optic_<S>(): Equivalence<S, _, S>`](#optic_s-equivalences-_-s)\n    - [`get<S, A>(optic: Optic<S, _, A>) => (source: S) => A`](#gets-aoptic-optics-_-a--source-s--a)\n    - [`preview<S, A>(optic: Optic<S, _, A>) => (source: S) => A | undefined`](#previews-aoptic-optics-_-a--source-s--a--undefined)\n    - [`collect<S, A>(optic: Optic<S, _, A>) => (source: S) => A[]`](#collects-aoptic-optics-_-a--source-s--a)\n    - [`modify<S, T, A>(optic: Optic<S, T, A>) => <B>(f: (a: A) => B) => (source: S) => T<B>`](#modifys-t-aoptic-optics-t-a--bf-a-a--b--source-s--tb)\n    - [`set<S, T, A>(optic: Optic<S, T, A>) => <B>(value: B) => (source: S) => T<B>`](#sets-t-aoptic-optics-t-a--bvalue-b--source-s--tb)\n    - [`compose<S, A1, A2><optic1: Optic<S, _, A1>, optic2: Optic<A1, _, A2>): Optic<S, _, A2>`](#composes-a1-a2optic1-optics-_-a1-optic2-optica1-_-a2-optics-_-a2)\n  - [Creating optics](#creating-optics)\n  - [Isomorphisms](#isomorphisms)\n    - [`iso<U>(there: (a: A) => U, back: (u: U) => A): Iso<S, _, U>`](#isouthere-a-a--u-back-u-u--a-isos-_-u)\n  - [Lenses](#lenses)\n    - [`prop<K extends keyof A>(key: K): Lens<S, _, A[K]>`](#propk-extends-keyof-akey-k-lenss-_-ak)\n    - [`path<K1, K2, ...>(keys: [K1, K2, ...]): Lens<S, _, A[K1][K2]...>`](#pathk1-k2-keys-k1-k2--lenss-_-ak1k2)\n    - [`pick<K extends keyof A>(keys: K[]): Lens<S, _, Pick<A, K>>`](#pickk-extends-keyof-akeys-k-lenss-_-picka-k)\n  - [Prisms](#prisms)\n    - [`optional(): Prism<S, _, Exclude<A, undefined>>`](#optional-prisms-_-excludea-undefined)\n    - [`guard<U extends A>(g: (a: A) => a is U): Prism<S, _, U>`](#guardu-extends-ag-a-a--a-is-u-prisms-_-u)\n    - [`guard_<F extends HKT>(): <U extends A>(g: (a: A) => a is U) => Prism<S, T · F, U>`](#guard_f-extends-hkt-u-extends-ag-a-a--a-is-u--prisms-t-%C2%B7-f-u)\n    - [`index(i: number): Prism<S, _, ElemType<A>>`](#indexi-number-prisms-_-elemtypea)\n    - [`head(): Prism<S, _, ElemType<A>>`](#head-prisms-_-elemtypea)\n    - [`find(p: (e: ElemType<A>) => boolean): Prism<S, _, ElemType<A>>`](#findp-e-elemtypea--boolean-prisms-_-elemtypea)\n    - [`when(f: (a: A) => boolean): Prism<S, _, A>`](#whenf-a-a--boolean-prisms-_-a)\n  - [Traversals](#traversals)\n    - [`elems(): Traversal<S, _, ElemType<A>>`](#elems-traversals-_-elemtypea)\n  - [Getters](#getters)\n    - [`to<B>(f: (a: A) => B): Getter<S, B>`](#tobf-a-a--b-getters-b)\n  - [Composing](#composing)\n    - [`compose<B>(other: Optic<A, _, B>): Optic<S, _, B>`](#composebother-optica-_-b-optics-_-b)\n  - [Strings](#strings)\n    - [`chars(): Traversal<S, _, string>`](#chars-traversals-_-string)\n    - [`words(): Traversal<S, _, string>`](#words-traversals-_-string)\n- [Prior art](#prior-art)\n\n<!-- END doctoc generated TOC please keep comment here to allow auto update -->\n\n## Installation\n\n```\nnpm install --save optics-ts\n```\n\nor\n\n```\nyarn add optics-ts\n```\n\n## Tutorial\n\nThe following import is assumed in all the examples below:\n\n```typescript\nimport * as O from 'optics-ts'\n```\n\n### Lens\n\nLens is the most common optic you're going to use. You can create an\noptic for a data structure by calling `O.optic()`, and turn in into a\nlens that focuses on a property of an object with `.prop()`:\n\n```typescript\ntype Data = {\n  foo: { bar: number }\n  other: string\n}\nconst foo = O.optic<Data>().prop('foo')\n```\n\n`foo` is now a lens that focuses on `Data.foo`.\n\nTo dig deeper, just call `.prop()` again:\n\n```typescript\nconst bar = O.optic<Data>().prop('foo').prop('bar')\n// or from the `foo` lens we defined above\nconst bar = foo.prop('bar')\n// or use .path() to compose multiple prop lenses with a single call\nconst bar = O.optic<Data>().path(['foo', 'bar'])\n```\n\nUse `get()` to read a value through the lens:\n\n```typescript\nconst data: Data = {\n  foo: { bar: 42 },\n  other: 'stuff',\n}\n\nO.get(lens)(data)\n// => 42\n```\n\nUse `set()` or `modify()` to write the focused value through the lens:\n\n```typescript\nO.set(lens)(99)(data)\n// => {\n//   foo: { bar: 99 },\n//   other: 'stuff'\n// }\n\nO.modify(lens)(x => x * 100)(data)\n// => {\n//   foo: { bar: 4200 },\n//   other: 'stuff'\n// }\n```\n\nWriting through optics always creates a new data structure instead of\nmodifying the existing one. In other words, data is immutable.\n\n### Prism\n\nLenses are great for focusing to a part of a larger structure. Prisms\nare much like lenses, but they don't necessarily match anything, i.e.\nthey can have zero focuses.\n\nA practical example is focusing on a branch of a union type. Here, the\n`User.age` field can be `number` or `undefined`. With the `.optional()`\nprism we can focus only when the value is a `number`, and do nothing\nwhen it's `undefined`:\n\n```typescript\ntype User = {\n  name: string\n  age?: number | undefined\n}\n\nconst age = O.optic<User>().prop('age').optional()\n```\n\nYou read through a prism using the `preview()` function. When the prism\ndoesn't match, it returns `undefined`.\n\n```typescript\nconst userWithAge: User = {\n  name: 'Betty',\n  age: 42,\n}\nO.preview(age)(userWithAge)\n// ==> 42\n\nconst userWithoutAge: User = {\n  name: 'Max',\n  age: undefined,\n}\nO.preview(age)(userWithoutAge)\n// ==> undefined\n```\n\nIf the prism doesn't match, `preview()` returns undefined, as seen above.\n\nYou can write through a prism normally with `set()` and `modify()`. If\nthe prism doesn't match, the value is unchanged:\n\n```typescript\nO.modify(age)(n => n + 1)(userWithAge)\n// ==> {\n//   name: 'Betty',\n//   age: 43,\n// }\n\nO.set(age)(60)(userWithoutAge)\n// ==> {\n//   name: 'Max',\n//   age: undefined,\n// }\n```\n\n`.guard()` is another method that creates a prism. It's a generalization\nof `.optional()` in the sense that you can match on any branch of a\nunion type instead of just the non-`undefined` part:\n\n```typescript\ninterface Square {\n  kind: 'square'\n  size: number\n}\ninterface Rectangle {\n  kind: 'rectangle'\n  width: number\n  height: number\n}\ntype Shape = Square | Rectangle\n\nfunction isRectangle(s: Shape): s is Rectangle {\n  return s.kind === 'rectangle'\n}\n\nconst rectWidth = O.optic<Shape>().guard(isRectangle).prop('width')\n\nO.preview(rectWidth)({ kind: 'square', size: 10 })\n// ==> undefined\n\nO.preview(rectWidth)({ kind: 'rectangle', width: 5, height: 7 })\n// ==> 5\n\nO.modify(rectWidth)(w => w * 2)({ kind: 'rectangle', width: 5, height: 7 })\n// ==> { kind: 'rectangle', width: 10, height: 7 })\n```\n\nNotice how above we used `.guard(...).prop(...)`, composing a prism with\na lens. This yields a prism, so we used `preview()` to read through it.\nSee [Types of optics](#types-of-optics) for the rules of composition.\n\n### Traversal\n\nThe next optic type is the traversal. While lenses have 1 focus and\nprisms have 0 or 1 focus (no match or match), traversals have 0 or\nmore focuses.\n\nThe simplest example of a traversal is to focus on the elements of an\narray. To create such a traversal, use `.elems()`:\n\n```typescript\ntype Person {\n  name: string\n  friends: Person[]\n}\n\nconst friendsNames = O.optic<Person>()\n  .prop('friends')\n  .elems()\n  .prop('name')\n```\n\nTo read through a traversal, call `collect()` to collect all focused\nelements into an array:\n\n```typescript\nconst john = { name: 'John', friends: [] }\nconst bruce = { name: 'Bruce', friends: [] }\nconst amy = { name: 'Amy', friends: [john, bruce] }\n\nO.collect(friendsNames)(amy)\n// ==> [ 'John', 'Bruce' ]\n```\n\nWriting through a traversal writes to all focused values:\n\n```typescript\nO.modify(friendsNames)(name => `${name} Wayne`)(amy)\n// ==> {\n//   name: 'Amy',\n//   friends: [\n//     { name: 'John Wayne', friends: [] },\n//     { name: 'Bruce Wayne', friends: [] },\n//   ],\n// }\n```\n\nNote again how we used `.prop(...).elems(...).prop(...)`, composing a\nlens with a traversal, and then with a lens again. This yields a\ntraversal. See [Types of optics](#types-of-optics) for more info.\n\nIt's sometimes useful to further focus on certain elements of a\ntraversal. This can be done by composing a traversal with a prism like\n`.when()` that skips items that don't match a predicate:\n\n```typescript\nconst even = O.optic<number[]>()\n  .elems()\n  .when(n => n % 2 === 0)\n\nO.modify(even)(n => -n)([1, 2, 3, 4, 5])\n// ==> [1, -2, 3, -4, 5]\n```\n\n### Other types of optics\n\nIn fact, calling `O.optic()` also yields an optic, but instead of\nbeing a lens, prism or traversal, it's an equivalence. As the name\nsuggests, equivalence keeps the value equal, in both reading and\nwriting directions:\n\n```typescript\nconst str = O.optic<string>()\n\nO.get(str)('original')\n// ==> 'original'\n\nO.set(str)('new')('original')\n// ==> 'new' ('original' is discarded)\n```\n\nSupported optic types also include _isomorphism_, which can be used to\ndo 2-way data transformations. Furthermore, there are read-only optics\nanaloguous to isomorphism, prism, and traversal. While isomorphism is\n2-way transformation (a mapping function and its inverse), _getter_ is a\none-way transformation (just a mapping function). _Affine fold_ and\n_fold_ are read-only variants of prism and traversal.\n\n### Polymorphism\n\nOptics can be polymorphic, which means you can change the type of the\nfocus when you write through an optic. Since this is a relatively rare\nuse case, and may be confusing if done by accident, polymorphic optics\nare created with `optic_()` (note the underscore):\n\n```typescript\ntype Data = {\n  foo: { bar: string }\n  other: boolean\n}\nconst bar = O.optic_<Data>().path(['foo', 'bar'])\n```\n\nLet's modify `bar` to contain the length of the original string instead:\n\n```typescript\nconst data: Data = {\n  foo: { bar: 'hello there' },\n  other: true,\n}\n\nconst updated = O.modify(bar)(str => str.length)(data)\n// ==> {\n//   foo: { bar: 11 },\n//   other: true\n// }\n```\n\nThis is a type-safe operation, i.e. the compiler knows that the type of\n`updated.foo.bar` is `number`, editor autocomplete works correctly, etc.\n\nIf you ever see a `DisallowedTypeChange` type being returned from an\n`optics-ts` function, it means that you tried to change the type when\nwriting through a non-polymorphic (monomorphic) optic.\n\n## API reference\n\n### Types of optics\n\nThe supported optic classes are Equivalence, Iso (isomorphism), Lens,\nPrism, Traversal, Getter, AffineFold and Fold.\n\nEquivalence, Iso, Lens, Prism and Traversal are read-write, i.e. you can\nread and write through them. Getter, AffineFold and Fold are read-only.\n\nEquivalence, Iso, Lens and Getter have one focus, i.e. you can\nalways read and write through them (Getter doesn't support writing).\nPrism and AffineFold have zero or one focus, i.e. reading may yield no\nvalue and writing may have no effect if there's no focus. Traversal and\nFold have zero or more focuses, meaning that reading yields zero or more\nvalues, and writing modifies zero or more values.\n\nAny optic can be composed with another optic. The type of the resulting\noptic can be determined from this diagram:\n\n```\nEquivalence -> Iso -> Lens ---> Prism ------> Traversal\n                      |         |             |\n                      v         v             v\n                      Getter -> AffineFold -> Fold\n```\n\nWhen you compose two optics A and B, the result is the nearest optic\nthat you get by following the arrows starting from both A and B.\n\nFor example, composing a Getter with a Traversal yields a Fold.\nComposing an Iso with a Prism yields a Prism.\n\nThe naming of the optic classes is taken from\n[Glassery](http://oleg.fi/gists/posts/2017-04-18-glassery.html) by Oleg\nGrenrus.\n\n### Method chaining\n\nOptics are composed with method chaining. This means that each optic\ntype has all the methods documented below, regardless of the type of the\noptic that the method creates. The only difference is the return type,\nwhich is determined by the composition rules above.\n\nFor example, assume we have a variable `myLens` that holds a `Lens`, and\ncall `.optional()` on it:\n\n```typescript\nconst newOptic = myLens.optional()\n```\n\n`.optional()` creates a prism, so `newOptic` will be a composition of\nlens and prism, i.e. a prism.\n\n### Type parameters\n\nAll read-write optics have 3 type parameters: `<S, T, A>`, and all\nread-only optics have 2 type parameters: `<S, A>`:\n\n- `S` is the source on which the optic operates\n\n- `A` is the type of the focus or focuses\n\n- `T` is a \"higher-kinded type\" or a \"partially applied type operator\"\n  that yields the output type when applied to some type `B`.\n\nConceptually, when you write a value of type `B`, the output type will\nbe `S` with `A` replaced by `B` at the focus(es) of the optic. `T` is\nthe mechanism that transforms `B` to the output type. This construct\nmakes it possible for the optics to be polymorphic on the type level.\nThe read-only optics don't need `T` because you cannot write through\nthem.\n\nIn the following, we leave the exact definition of `T` for each optic\nout for clarity, writing just `_` in its place. It's usually clear fom\nhow the optic works what will come out if you put a different type in.\n\nIn the documentation of functions that can be used to write through an\noptic, the return type is denoted by `T<B>`. While not valid TypeScript\nsyntax (because `T` is a type parameter instead of a concrete type),\nthis captures the meaning quite well: `B` is applied to the\nhigher-kinded type `T`, yielding the output type.\n\nInterested readers can refer to [hkt.ts](src/hkt.ts) to see how the\nhigher-kinded types / partially applied type operators are actually\nimplemented.\n\n### Top-level functions\n\nThese functions are available as top level exports of the `optics-ts`\nmodule.\n\nMost functions have `Optic` in their signature. It means that multiple\noptics work with the function. The optic classes that are actually\napplicable are documented in the function description.\n\n#### `optic<S>(): Equivalence<S, _, S>`\n\nCreate a monomorphic equivalence for `S`. If you ever see the type\n`DisallowedTypeChange`, it means that you have attempted to change a\ntype with a monomorphic optic.\n\n#### `optic_<S>(): Equivalence<S, _, S>`\n\nCreate a polymorphic equivalence for `S`.\n\n#### `get<S, A>(optic: Optic<S, _, A>) => (source: S) => A`\n\nRead a value through an `Equivalence`, `Iso`, `Lens` or `Getter`.\n\n#### `preview<S, A>(optic: Optic<S, _, A>) => (source: S) => A | undefined`\n\nRead a value through a `Prism`, `Traversal`, `AffineFold` or `Fold`. For\n`Prism` and `AffineFold`, return `undefined` if the optic doesn't match\n(has zero focuses). For `Traversal` and `Fold`, returns the value of the\nfirst focus, or `undefined` if there are no focuses.\n\n#### `collect<S, A>(optic: Optic<S, _, A>) => (source: S) => A[]`\n\nRead all focused values through a `Prism`, `Traversal`, `AffineFold` or\n`Fold`. For `Prism` and `AffineFold`, the return value is an array of 0\nor 1 elements. For `Traversal` and `Fold`, the return value is an array\nof zero or more elements.\n\n#### `modify<S, T, A>(optic: Optic<S, T, A>) => <B>(f: (a: A) => B) => (source: S) => T<B>`\n\nModify the focused value(s) through an `Equivalence`, `Iso`, `Lens`,\n`Prism` or `Traversal`. Returns an updated copy of `source` with all\nfocuses modified by mapping them through the function `f`.\n\n#### `set<S, T, A>(optic: Optic<S, T, A>) => <B>(value: B) => (source: S) => T<B>`\n\nWrite a constant value through an `Equivalence`, `Iso`, `Lens`, `Prism`\nor `Traversal`. Returns an updated copy of `source` with all focuses\nreplaced by `value`.\n\n#### `compose<S, A1, A2><optic1: Optic<S, _, A1>, optic2: Optic<A1, _, A2>): Optic<S, _, A2>`\n\nCompose two optics. If the first optic is from `S` to `A1`, and the\nsecond optic is from `A1` to `A2`, the result is from `S` to `A2`.\n\nSee [Types of optics](#types-of-optics) for the rules of composition.\n\n### Creating optics\n\nThe methods documented below are available on all optics types:\n`Equivalence`, `Iso`, `Lens`, `Prism`, `Traversal`, `Getter`,\n`AffineFold` and `Fold`. The documented return type is the type of the\noptic that these methods create. The actual return type is the\ncomposition of the optic on which the method is called and on the optic\nthat the method creates.\n\nNote that there are no functions to create `AffineFold` or `Fold`\noptics. You can only get these by composing other types of optics.\n`Equivalence` can be created by calling the top-level `optic()`\nfunction.\n\n### Isomorphisms\n\nIsomorphisms have the type `Iso<S, T, A>`. In the following, we omit\nthe exact definition of `T` for clarity, and use `_` instead. See\n[Type parameters](#type-parameters) for the meanings of type\nparameters.\n\n#### `iso<U>(there: (a: A) => U, back: (u: U) => A): Iso<S, _, U>`\n\nCreate an isomorphism from functions `there` and `back`. `there` takes\nthe focus and transforms it to another value. `back` is the inverse of\n`there`.\n\nNote that `iso` is monomorphic. There's no polymorphic alternative\n(yet).\n\n### Lenses\n\nLenses have the type `Lens<S, T, A>`. In the following, we omit the\nexact definition of `T` for clarity, and use `_` instead. See [Type\nparameters](#type-parameters) for the meanings of type parameters.\n\n#### `prop<K extends keyof A>(key: K): Lens<S, _, A[K]>`\n\nCreate a lens that focuses on the property `K` of `A`.\n\n**Note:** `prop()` only works for string properties, even though\nTypeScript's type system also allows array's numeric indices when using\n`keyof`. Use the `index()` prism to focus on an array element at a given\nindex.\n\n#### `path<K1, K2, ...>(keys: [K1, K2, ...]): Lens<S, _, A[K1][K2]...>`\n\nA shortcut for focusing on chain of properties.\n\n```typescript\nfoo.path(['a', 'b', 'c'])\n```\n\nis equal to\n\n```typescript\nfoo.prop('a').prop('b').prop('c')\n```\n\n#### `pick<K extends keyof A>(keys: K[]): Lens<S, _, Pick<A, K>>`\n\nCreate a lens that focuses on a sub-object of `A` with the given\nproperties. When writing through a polymorphic `.pick()` lens, you can\nadd or remove properties.\n\nExample:\n\n```typescript\nconst data = {\n  foo: 'something',\n  bar: 42,\n  baz: true,\n}\nconst lens = O.optic_<typeof data>().pick(['foo', 'bar'])\n\nO.get(lens)(data)\n// ==> {\n//  foo: 'something',\n//  baz: true,\n// }\n\nO.set(lens)({ quux: null })(data)\n// ==> {\n//   quux: null,\n//   baz: true,\n// }\n\n// monomorphic version of the same lens\nconst monoLens = O.optic<typeof data>().compose(lens)\n\nO.set(monoLens)({ quux: null })(data)\n// ==> DisallowedTypeChange\n```\n\n#### `prependTo(): Lens<S, _, ElemType<A> | undefined>`\n\n#### `appendTo(): Lens<S, _, ElemType<A> | undefined>`\n\nOnly works on arrays.\n\nCreate a lens that focuses on the part _before the first element_ or\n_after the last element_ of the focus array. When written through,\nprepends or appends the value to the array. When read through, returns\nundefined (as there's nothing before or after the array).\n\nIf `undefined` is written, the array is not changed. When an element of\na different type `B` is written, the resulting array will have the type\n`Array<A | B>`.\n\n### Prisms\n\nPrisms have the type `Prism<S, T, A>`. In the following, we omit the\nexact definition of `T` for clarity, and use `_` instead. See [Type\nparameters](#type-parameters) for the meanings of type parameters.\n\n#### `optional(): Prism<S, _, Exclude<A, undefined>>`\n\nCreate a prism that focuses on the non-`undefined` subtype of `A`.\n\n#### `guard<U extends A>(g: (a: A) => a is U): Prism<S, _, U>`\n\nCreate a prism that focuses on the subtype `U` of `A` that matches the\ntype guard `g`.\n\nNote that `guard()` is monomorphic. Use `guard_` if you want a\npolymorphic guard.\n\n#### `guard_<F extends HKT>(): <U extends A>(g: (a: A) => a is U) => Prism<S, T · F, U>`\n\nCreate a prism that focuses on the subtype of `A` that matches the type\nguard `g`. When written to, uses the higher-kinded type `F` to construct\nthe output type.\n\n#### `index(i: number): Prism<S, _, ElemType<A>>`\n\nOnly works on arrays and strings.\n\nCreate a prism that focuses on the element type of the array `A`, or on\na substring of length 1 if `A` is `string`.\n\nWhen an element of a different type `B` is written to an array, the\nresulting array will have the type `Array<A | B>`.\n\nWhen writing to a string, only strings can be written. The length of the\nwritten string can be something else than 1.\n\n#### `head(): Prism<S, _, ElemType<A>>`\n\nShort for `index(0)`.\n\n#### `find(p: (e: ElemType<A>) => boolean): Prism<S, _, ElemType<A>>`\n\nOnly works on array types. `ElemType<A>` is the element type of the\narray type `A`.\n\nLike `.index()`, but the index to be focused on is determined by\nfinding the first element that matches the given predicate.\n\nWhen a different type `B` is written through this optic, the resulting\narray will have the type `Array<A | B>`.\n\n#### `when(f: (a: A) => boolean): Prism<S, _, A>`\n\nCreate a prism that skips the focus if it doesn't match the given\npredicate. Especially useful for filtering the focuses of a travesal.\n\nWhen a different type `B` is written through this optic, the resulting\nvalue will have the type `A | B`.\n\n### Traversals\n\nTraversals have the type `Traversal<S, T, A>`. In the following, we\nomit the exact definition of `T` for clarity, and use `_` instead. See\n[Type parameters](#type-parameters) for the meanings of type\nparameters.\n\n#### `elems(): Traversal<S, _, ElemType<A>>`\n\nOnly works on array types. `ElemType<A>` is the element type of the\narray type `A`.\n\nCreate a traversal that focuses on all the elements of the array.\n\n### Getters\n\nGetters have the type `Getter<S, A>`.\n\n#### `to<B>(f: (a: A) => B): Getter<S, B>`\n\nCreate a getter that applies the function `f` to its focus.\n\n### Composing\n\n#### `compose<B>(other: Optic<A, _, B>): Optic<S, _, B>`\n\n### Strings\n\nThe following optics only work on strings.\n\n#### `chars(): Traversal<S, _, string>`\n\nCreate a traversal that focuses on all the characters of the current\nstring focus.\n\nWhen written through, characters can be removed by writing the empty\nstring, or changed to longer strings.\n\n#### `words(): Traversal<S, _, string>`\n\nCreate a traversal that focuses on all the words of the current string\nfocus. Words are substrings that are separated by whitespace.\n\nWhen written through, words can be removed by writing the empty string,\nor changed to longer or shorter strings.\n\n## Prior art\n\nThere are many existing optics libraries of varying degree for\nJavaScript, but only few for TypeScript. It's generally hard to create\ngood typings for optics in TypeScript, and the task becomes impossible\nif one tried to retrofit types on an existing JavaScript implementation.\n\n[monocle-ts](https://github.com/gcanti/monocle-ts) is probably the most\npopular TypeScript optics library. It lacks polymorphism, and creating\noptics is verbose and cumbersome. You first create optics for each level\nof the data structure, and then compose them with separate function\ncalls. This also often requires you to declare many unnecessary\nintermediate types for your data.\n\n[@grammarly/focal](https://github.com/grammarly/focal) is not an optics\nlibrary per se, rather an UI framework. Its optics are very limited.\n","license":"MIT","scripts":{"test":"jest","build":"rm -rf dist/ && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json","doctoc":"doctoc README.md --title '## Table of Contents' && prettier --write README.md","generate-index":"ts-node ./scripts/generate-index.ts > src/index.ts && prettier --write src/index.ts","prepublishOnly":"yarn run build"},"typings":"./dist/lib/index.d.ts","_npmUser":{"name":"akheron","email":"petri@digip.org"},"keywords":["optics","lens","prism","traveral","isomorphism","immutable","functional-programming"],"repository":{"url":"https://github.com/akheron/optics-ts","type":"git"},"description":"Type-safe, ergonomic, polymorphic optics for TypeScript","directories":{},"licenseText":"MIT License\n\nCopyright (c) 2020 Petri Lehtinen\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","maintainers":[{"name":"akheron","email":"petri@digip.org"}],"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"jest":"^25.1.0","doctoc":"^1.4.0","ts-jest":"^25.2.1","ts-node":"^8.8.1","prettier":"^2.0.2","typescript":"^3.8.3","@types/jest":"^25.1.4","@babel/types":"^7.9.0"},"_npmOperationalInternal":{"tmp":"tmp/optics-ts_1.1.0-pre.1_1588188553255_0.34048600341329505","host":"s3://npm-registry-packages"},"_id":"optics-ts@1.1.0-pre.1","version":"1.1.0-pre.1"}