{"name":"monocle-ts","version":"2.1.0","description":"A porting of scala monocle library to TypeScript","main":"lib/index.js","module":"es6/index.js","typings":"lib/index.d.ts","sideEffects":false,"scripts":{"lint":"tslint -p tsconfig.tslint.json src/**/*.ts test/**/*.ts","jest":"jest","jest-coverage":"jest --ci --coverage","prettier":"prettier --no-semi --single-quote --print-width 120 --parser typescript --list-different \"{src,test,examples}/**/*.ts\"","fix-prettier":"prettier --no-semi --single-quote --print-width 120 --parser typescript --write \"{src,test,examples}/**/*.ts\"","test":"npm run prettier && npm run lint && npm run dtslint && npm run jest && npm run docs","clean":"rimraf lib/* es6/*","build":"npm run clean && tsc && tsc -p tsconfig.es6.json && npm run import-path-rewrite","prepublish":"npm run build","docs-fix-prettier":"prettier --no-semi --single-quote --print-width 120 --parser markdown --write \"README.md\"","dtslint":"dtslint dtslint","mocha":"TS_NODE_CACHE=false mocha -r ts-node/register test/*.ts","docs":"docs-ts","import-path-rewrite":"import-path-rewrite"},"repository":{"type":"git","url":"git+https://github.com/gcanti/monocle-ts.git"},"author":{"name":"Giulio Canti","email":"giulio.canti@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/gcanti/monocle-ts/issues"},"homepage":"https://github.com/gcanti/monocle-ts","peerDependencies":{"fp-ts":"^2.0.0"},"devDependencies":{"@types/jest":"22.2.2","@types/node":"7.0.4","docs-ts":"^0.3.1","dtslint":"github:gcanti/dtslint","fp-ts":"^2.0.0","import-path-rewrite":"github:gcanti/import-path-rewrite","jest":"^24.3.0","mocha":"^5.2.0","prettier":"^1.15.3","rimraf":"^2.6.3","ts-jest":"^24.0.0","ts-node":"^8.0.2","tslint":"^5.12.1","tslint-config-standard":"^8.0.1","typescript":"^3.7.2"},"tags":["typescript","functional-programming","optics","lens","fp-ts"],"keywords":["typescript","functional-programming","optics","lens","fp-ts"],"jest":{"collectCoverage":true,"transform":{"^.+\\.tsx?$":"ts-jest"},"testRegex":"test","moduleFileExtensions":["ts","js"],"testPathIgnorePatterns":["helpers.ts"]},"gitHead":"106ec023cc31d0926a228371cda7780765f140e5","_id":"monocle-ts@2.1.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.3","_npmUser":{"name":"gcanti","email":"giulio.canti@gmail.com"},"dist":{"integrity":"sha512-YWH8TDcZSEeCaxp0ZjgccLChnaDvTiUHILpuI0DSZeASYSCgwdz+h6+uEG4QCwYreGsn1oI18ysrcUPv6xWK7w==","shasum":"5c1bbade7952b57e33f2179866467e87d9ed5977","tarball":"http://registry-npm.ywwl.com/monocle-ts/download/monocle-ts-2.1.0.tgz","fileCount":32,"unpackedSize":147197,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeMxh5CRA9TVsSAnZWagAAaG4QAJ58LI2euaYYUtcXcXFJ\nux1qZ1eyMR/XN2mpQxBFn8M0mtrv4szRlcz8IaQu24oNQFBwbWCV1r8PIMpO\nB8/3FBX0k4WFf780pbdmPxTmRP9GqZ3WJeMH4Z5W3aNEHUVaqaVALiwuQYcF\ne/l+GZEy98XVxIZ80WinHiQ2SnRzqFZ3Yo5Tn2bjPDBtMnfICU7u/kGSb97g\nz2wMBtJar56E08kXMi5Xbv3E7URqp5qrXOLJnHi9bcv+rSTBXa8r1FNUUnIv\nQQtKXcLwFEzRsVn9OU4yJRNAbj/0fkh9T9ZwziKnPPI1E2A4datQ4Sz5MIG4\n7Wp1wORJ9WD0XPDG56ErpPD+tL6bFMutlZvG0R0nHKpv8VhtZ3rlvW6zjS04\nwMxeazLa+uVcv7LQIvfZHYGj+/0p9y26b5LTa/DxSD55ZnCKjZyhaE/YZp5h\nWBe0eWi5VfxMnYw3XFS2UjDhjARqwT0CwDhj3r5WBm2OPv2v1tbHH+c42bEm\nvOwNodzoA3Q1WgwL3gbrxwuehwQX8/q3T5HSKU4PMok5IA3arM0aFfyBt9be\nUFTQJgDjznpLXwGW26ZR5b3AWBNaY2u+r91SMUtiRUVUdiRmInXwJhMa7Djb\ncjSaZxGMFy4tUiQ1H1N0Ku+GYrHqLzHKes+enUYtFPkBj9fJ7/UTmvn2SlDW\nP+Q2\r\n=8NL8\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDJS9T/rRnD6zX58Iv1FZwR5TT3rwv1ZrWVoffZDUYGuAIhAIX3XpOCFJ+lZ1eKroMT2DyYm4UVRGM4I2opoiAu5IlU"}],"size":22196,"noattachment":false,"key":"/monocle-ts/-/monocle-ts-2.1.0.tgz"},"maintainers":[{"name":"gcanti","email":"giulio.canti@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/monocle-ts_2.1.0_1580406904916_0.540395760171053"},"_hasShrinkwrap":false,"readme":"[![build status](https://img.shields.io/travis/gcanti/monocle-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/monocle-ts)\n[![dependency status](https://img.shields.io/david/gcanti/monocle-ts.svg?style=flat-square)](https://david-dm.org/gcanti/monocle-ts)\n![npm downloads](https://img.shields.io/npm/dm/monocle-ts.svg)\n\n# Motivation\n\n(Adapted from [monocle site](https://www.optics.dev/Monocle/))\n\nModifying immutable nested object in JavaScript is verbose which makes code difficult to understand and reason about.\n\nLet's have a look at some examples:\n\n```ts\ninterface Street {\n  num: number\n  name: string\n}\ninterface Address {\n  city: string\n  street: Street\n}\ninterface Company {\n  name: string\n  address: Address\n}\ninterface Employee {\n  name: string\n  company: Company\n}\n```\n\nLet’s say we have an employee and we need to upper case the first character of his company street name. Here is how we\ncould write it in vanilla JavaScript\n\n```ts\nconst employee: Employee = {\n  name: 'john',\n  company: {\n    name: 'awesome inc',\n    address: {\n      city: 'london',\n      street: {\n        num: 23,\n        name: 'high street'\n      }\n    }\n  }\n}\n\nconst capitalize = (s: string): string => s.substring(0, 1).toUpperCase() + s.substring(1)\n\nconst employeeCapitalized = {\n  ...employee,\n  company: {\n    ...employee.company,\n    address: {\n      ...employee.company.address,\n      street: {\n        ...employee.company.address.street,\n        name: capitalize(employee.company.address.street.name)\n      }\n    }\n  }\n}\n```\n\nAs we can see copy is not convenient to update nested objects because we need to repeat ourselves. Let's see what could\nwe do with `monocle-ts`\n\n```ts\nimport { Lens } from 'monocle-ts'\n\nconst company = Lens.fromProp<Employee>()('company')\nconst address = Lens.fromProp<Company>()('address')\nconst street = Lens.fromProp<Address>()('street')\nconst name = Lens.fromProp<Street>()('name')\n```\n\n`compose` takes two `Lenses`, one from `A` to `B` and another one from `B` to `C` and creates a third `Lens` from `A` to\n`C`. Therefore, after composing `company`, `address`, `street` and `name`, we obtain a `Lens` from `Employee` to\n`string` (the street name). Now we can use this `Lens` issued from the composition to modify the street name using the\nfunction `capitalize`\n\n```ts\nconst capitalizeName = company.compose(address).compose(street).compose(name).modify(capitalize)\n\nassert.deepStrictEqual(capitalizeName(employee), employeeCapitalized)\n```\n\nYou can use the `fromPath` API to avoid some boilerplate\n\n```ts\nimport { Lens } from 'monocle-ts'\n\nconst name = Lens.fromPath<Employee>()(['company', 'address', 'street', 'name'])\n\nconst capitalizeName = name.modify(capitalize)\n\nassert.deepStrictEqual(capitalizeName(employee), employeeCapitalized) // true\n```\n\nHere `modify` lift a function `string => string` to a function `Employee => Employee`. It works but it would be clearer\nif we could zoom into the first character of a `string` with a `Lens`. However, we cannot write such a `Lens` because\n`Lenses` require the field they are directed at to be _mandatory_. In our case the first character of a `string` is\noptional as a `string` can be empty. So we need another abstraction that would be a sort of partial Lens, in\n`monocle-ts` it is called an `Optional`.\n\n```ts\nimport { Optional } from 'monocle-ts'\nimport { some, none } from 'fp-ts/Option'\n\nconst firstLetterOptional = new Optional<string, string>(\n  (s) => (s.length > 0 ? some(s[0]) : none),\n  (a) => (s) => (s.length > 0 ? a + s.substring(1) : s)\n)\n\nconst firstLetter = company.compose(address).compose(street).compose(name).asOptional().compose(firstLetterOptional)\n\nassert.deepStrictEqual(firstLetter.modify((s) => s.toUpperCase())(employee), employeeCapitalized)\n```\n\nSimilarly to `compose` for lenses, `compose` for optionals takes two `Optionals`, one from `A` to `B` and another from\n`B` to `C` and creates a third `Optional` from `A` to `C`. All `Lenses` can be seen as `Optionals` where the optional\nelement to zoom into is always present, hence composing an `Optional` and a `Lens` always produces an `Optional`.\n\n# TypeScript compatibility\n\nThe stable version is tested against TypeScript 3.5.2, but should run with TypeScript 2.8.0+ too\n\n| `monocle-ts` version | required `typescript` version |\n| -------------------- | ----------------------------- |\n| 2.0.x+               | 3.5+                          |\n| 1.x+                 | 2.8.0+                        |\n\n**Note**. If you are running `< typescript@3.0.1` you have to polyfill `unknown`.\n\nYou can use [unknown-ts](https://github.com/gcanti/unknown-ts) as a polyfill.\n\n# Documentation\n\n- [API Reference](https://gcanti.github.io/monocle-ts/)\n\n## Experimental modules (version `2.3+`)\n\nExperimental modules (\\*) are published in order to get early feedback from the community.\n\nThe experimental modules are **independent and backward-incompatible** with stable ones.\n\n(\\*) A feature tagged as _Experimental_ is in a high state of flux, you're at risk of it changing without notice.\n\nFrom `monocle@2.3+` you can use the following experimental modules:\n\n- `Iso`\n- `Lens`\n- `Prism`\n- `Optional`\n- `Traversal`\n- `At`\n- `Ix`\n\nwhich implement the same features contained in `index.ts` but are `pipe`-based instead of `class`-based.\n\nHere's the same examples with the new API\n\n```ts\ninterface Street {\n  num: number\n  name: string\n}\ninterface Address {\n  city: string\n  street: Street\n}\ninterface Company {\n  name: string\n  address: Address\n}\ninterface Employee {\n  name: string\n  company: Company\n}\n\nconst employee: Employee = {\n  name: 'john',\n  company: {\n    name: 'awesome inc',\n    address: {\n      city: 'london',\n      street: {\n        num: 23,\n        name: 'high street'\n      }\n    }\n  }\n}\n\nconst capitalize = (s: string): string => s.substring(0, 1).toUpperCase() + s.substring(1)\n\nconst employeeCapitalized = {\n  ...employee,\n  company: {\n    ...employee.company,\n    address: {\n      ...employee.company.address,\n      street: {\n        ...employee.company.address.street,\n        name: capitalize(employee.company.address.street.name)\n      }\n    }\n  }\n}\n\nimport * as assert from 'assert'\nimport * as L from 'monocle-ts/Lens'\nimport { pipe } from 'fp-ts/function'\n\nconst capitalizeName = pipe(\n  L.id<Employee>(),\n  L.prop('company'),\n  L.prop('address'),\n  L.prop('street'),\n  L.prop('name'),\n  L.modify(capitalize)\n)\n\nassert.deepStrictEqual(capitalizeName(employee), employeeCapitalized)\n\nimport * as O from 'monocle-ts/Optional'\nimport { some, none } from 'fp-ts/Option'\n\nconst firstLetterOptional: O.Optional<string, string> = {\n  getOption: (s) => (s.length > 0 ? some(s[0]) : none),\n  set: (a) => (s) => (s.length > 0 ? a + s.substring(1) : s)\n}\n\nconst firstLetter = pipe(\n  L.id<Employee>(),\n  L.prop('company'),\n  L.prop('address'),\n  L.prop('street'),\n  L.prop('name'),\n  L.composeOptional(firstLetterOptional)\n)\n\nassert.deepStrictEqual(\n  pipe(\n    firstLetter,\n    O.modify((s) => s.toUpperCase())\n  )(employee),\n  employeeCapitalized\n)\n```\n","publish_time":1580406905034,"_cnpm_publish_time":1580406905034,"dist-tags":{"latest":"2.3.13","next":"2.3.9"}}