{"_id":"@ama-team/optional","_rev":"11-c48c952367579e99200aaa1018c6a040","name":"@ama-team/optional","dist-tags":{"latest":"0.1.3"},"versions":{"0.1.0":{"name":"@ama-team/optional","version":"0.1.0","main":"dist/index.js","repository":{"type":"git","url":"git@github.com:ama-team/ts-optional.git"},"author":{"name":"AMA Team","email":"dev@amagroup.ru"},"license":"MIT","private":false,"files":["dist"],"scripts":{"compile":"tsc","test":"nyc mocha","report":"allure generate -o report/allure -- metadata/allure","coverage:publish":"cat report/coverage/lcov.info | coveralls"},"devDependencies":{"@types/chai":"^4.1.2","@types/mocha":"^2.2.48","@types/sinon":"^4.3.0","allure-commandline":"^2.5.0","chai":"^4.1.2","coveralls":"^3.0.0","mocha":"^5.0.4","mocha-allure-reporter":"^1.3.2","mocha-junit-reporter":"^1.17.0","mocha-multi-reporters":"^1.1.7","mocha-typescript":"^1.1.12","nyc":"^11.6.0","sinon":"^4.4.6","source-map-support":"^0.5.4","ts-node":"^5.0.1","tslint":"^5.9.1","typescript":"^2.7.2"},"nyc":{"extension":[".ts"],"include":["src/**/*.ts"],"exclude":["**/*.d.ts"],"reporter":"lcov","report-dir":"report/coverage","temp-directory":"metadata/nyc","all":true},"description":"[![npm](https://img.shields.io/npm/v/@ama-team/ts-optional.svg?style=flat-square)](https://www.npmjs.com/package/@ama-team/ts-optional) [![CircleCI/master](https://img.shields.io/circleci/project/github/ama-team/ts-optional/master.svg?style=flat-square)](","licenseText":"Copyright (c) 2018 AMA Team\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 furnished\nto 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\nTHE SOFTWARE.\n","_id":"@ama-team/optional@0.1.0","dist":{"shasum":"bb8ae71ac15bac7792e9ecbecad4253c51756dcd","tarball":"https://registry.npmjs.org/@ama-team/optional/-/optional-0.1.0.tgz","fileCount":11,"unpackedSize":13403,"integrity":"sha512-37ZaiJKo32izpOKYFfWvOWvdGvjtAP78Qfk33yrhC4yHQPw520GDVSICIFXP0Y7s/ZjbD3mxH9I5JZ/t5bDeRA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDx/ckQlRVHsAs+trLCuP17VHreTsfiy940llBA3gZqnAiBq/Gbn0X4KPO1KS2+ISAMJmPiqegGdbrKk9wctVcQ+9A=="}]},"maintainers":[{"name":"ama-team","email":"dev@amagroup.ru"}],"_npmUser":{"name":"ama-team","email":"dev@amagroup.ru"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/optional_0.1.0_1521564151556_0.09430102092281034"},"_hasShrinkwrap":false},"0.1.1":{"name":"@ama-team/optional","version":"0.1.1","main":"dist/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/ama-team/ts-optional.git"},"author":{"name":"AMA Team","email":"dev@amagroup.ru"},"license":"MIT","private":false,"files":["dist"],"scripts":{"compile":"tsc -d","test":"nyc mocha","report":"allure generate -o report/allure -- metadata/allure","coverage:publish":"cat report/coverage/lcov.info | coveralls"},"devDependencies":{"@types/chai":"^4.1.2","@types/mocha":"^2.2.48","@types/sinon":"^4.3.0","allure-commandline":"^2.5.0","chai":"^4.1.2","coveralls":"^3.0.0","mocha":"^5.0.4","mocha-allure-reporter":"^1.3.2","mocha-junit-reporter":"^1.17.0","mocha-multi-reporters":"^1.1.7","mocha-typescript":"^1.1.12","nyc":"^11.6.0","sinon":"^4.4.6","source-map-support":"^0.5.4","ts-node":"^5.0.1","tslint":"^5.9.1","typescript":"^2.7.2"},"nyc":{"extension":[".ts"],"include":["src/**/*.ts"],"exclude":["**/*.d.ts"],"reporter":"lcov","report-dir":"report/coverage","temp-directory":"metadata/nyc","all":true},"gitHead":"33e3d08699fcab228b81ecd0808d89b0f0075c28","description":"[![npm](https://img.shields.io/npm/v/@ama-team/optional.svg?style=flat-square)](https://www.npmjs.com/package/@ama-team/optional) [![CircleCI/master](https://img.shields.io/circleci/project/github/ama-team/ts-optional/master.svg?style=flat-square)](https:","bugs":{"url":"https://github.com/ama-team/ts-optional/issues"},"homepage":"https://github.com/ama-team/ts-optional#readme","_id":"@ama-team/optional@0.1.1","_shasum":"043f27d3d619b48f40a0ee85c0091c5bd572087d","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.13.1","_npmUser":{"name":"ama-team","email":"dev@amagroup.ru"},"dist":{"shasum":"043f27d3d619b48f40a0ee85c0091c5bd572087d","tarball":"https://registry.npmjs.org/@ama-team/optional/-/optional-0.1.1.tgz","fileCount":14,"unpackedSize":16597,"integrity":"sha512-2H5jUrUsT4SKhkDhBMHGeDsd2rtO1m72p7pVo8QPqfjFdwVh833xmCcV5GPW1fNLmvEkwXmXGwnA9ReR8WG13Q==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG7qHAajDI+UnMjZOiM6qwESgyuCQ/rWAuKBBKvU9aiIAiA+dB11hi8LCkpLH0qu/z/nNkObisZkxjilljFcWytJZA=="}]},"maintainers":[{"email":"ops@amagroup.ru","name":"ama-bot"},{"email":"dev@amagroup.ru","name":"ama-team"},{"email":"etki@etki.me","name":"etki"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/optional_0.1.1_1521910620686_0.9850701845647405"},"_hasShrinkwrap":false},"0.1.3":{"name":"@ama-team/optional","version":"0.1.3","main":"dist/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/ama-team/ts-optional.git"},"author":{"name":"AMA Team","email":"dev@amagroup.ru"},"license":"MIT","private":false,"files":["dist"],"scripts":{"compile":"tsc -d","test":"nyc mocha","report":"allure generate -o report/allure -- metadata/allure","coverage:publish":"cat report/coverage/lcov.info | coveralls"},"devDependencies":{"@types/chai":"^4.1.2","@types/mocha":"^2.2.48","@types/sinon":"^4.3.0","allure-commandline":"^2.5.0","chai":"^4.1.2","coveralls":"^3.0.0","mocha":"^5.0.4","mocha-allure-reporter":"^1.3.2","mocha-junit-reporter":"^1.17.0","mocha-multi-reporters":"^1.1.7","mocha-typescript":"^1.1.12","nyc":"^11.6.0","sinon":"^4.4.6","source-map-support":"^0.5.4","ts-node":"^5.0.1","tslint":"^5.9.1","typescript":"^2.7.2"},"nyc":{"extension":[".ts"],"include":["src/**/*.ts"],"exclude":["**/*.d.ts"],"reporter":"lcov","report-dir":"report/coverage","temp-directory":"metadata/nyc","all":true},"gitHead":"afbbac98615430f0fa6e37d5f52a4cce8a1a771b","description":"[![npm](https://img.shields.io/npm/v/@ama-team/optional.svg?style=flat-square)](https://www.npmjs.com/package/@ama-team/optional) [![CircleCI/master](https://img.shields.io/circleci/project/github/ama-team/ts-optional/master.svg?style=flat-square)](https:","bugs":{"url":"https://github.com/ama-team/ts-optional/issues"},"homepage":"https://github.com/ama-team/ts-optional#readme","_id":"@ama-team/optional@0.1.3","_npmVersion":"5.6.0","_nodeVersion":"9.5.0","_npmUser":{"name":"etki","email":"etki@etki.me"},"dist":{"integrity":"sha512-laTytNAO8YWXRVLaVbT0F5WynW2MX0CsHBX/esQ0IHdjDs1lUSZbE4NBEZ77WRe+t9FvjfwDcKjuR6XsrvMMfA==","shasum":"b841ef3759f06ace57a08e88da1cde8f92d09ac1","tarball":"https://registry.npmjs.org/@ama-team/optional/-/optional-0.1.3.tgz","fileCount":14,"unpackedSize":16919,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCGuT5VAWJdV66oZ5ImPbCqQG93cHiblK8CwQFvOObEwwIhALBlBy8Xpim3pTV9V/4GBkn+chELVH1PoTu20GqQzwPW"}]},"maintainers":[{"email":"ops@amagroup.ru","name":"ama-bot"},{"email":"dev@amagroup.ru","name":"ama-team"},{"email":"etki@etki.me","name":"etki"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/optional_0.1.3_1522331344860_0.17121738920435625"},"_hasShrinkwrap":false}},"time":{"created":"2018-03-20T16:42:31.503Z","0.1.0":"2018-03-20T16:42:31.605Z","modified":"2022-06-12T14:36:16.444Z","0.1.1":"2018-03-24T16:57:00.764Z","0.1.2":"2018-03-29T13:31:08.932Z","0.1.3":"2018-03-29T13:49:04.955Z"},"maintainers":[{"email":"dev@amagroup.ru","name":"ama-master"},{"email":"ops@amagroup.ru","name":"ama-bot"},{"email":"etki@etki.me","name":"etki"}],"description":"[![npm](https://img.shields.io/npm/v/@ama-team/optional.svg?style=flat-square)](https://www.npmjs.com/package/@ama-team/optional) [![CircleCI/master](https://img.shields.io/circleci/project/github/ama-team/ts-optional/master.svg?style=flat-square)](https:","repository":{"type":"git","url":"git+ssh://git@github.com/ama-team/ts-optional.git"},"author":{"name":"AMA Team","email":"dev@amagroup.ru"},"license":"MIT","readme":"# @ama-team/optional\n\n[![npm](https://img.shields.io/npm/v/@ama-team/optional.svg?style=flat-square)](https://www.npmjs.com/package/@ama-team/optional)\n[![CircleCI/master](https://img.shields.io/circleci/project/github/ama-team/ts-optional/master.svg?style=flat-square)](https://circleci.com/gh/ama-team/ts-optional/master)\n[![Coveralls/master](https://img.shields.io/coveralls/github/ama-team/ts-optional/master.svg?style=flat-square)](https://coveralls.io/github/ama-team/ts-optional?branch=master)\n[![Code Climate](https://img.shields.io/codeclimate/github/ama-team/ts-optional.svg?style=flat-square)](https://codeclimate.com/github/ama-team/ts-optional)\n\nThis module contains small class which is inspired by Java Optional and\nwas created to provide earier ways to work with possibly \nnulled/undefined values.\n\nThis library is heavily inspired by Optional from standard java library,\nbut doesn't follow it precisely. If you need exact port, there is\n[typescript-optional](https://www.npmjs.com/package/typescript-optional) \nimplementation.\n\n**Please note that this project follows semantic versioning approach and\nmay change API in between pre-1.0 minor releases.**\n\n## Installation\n\n```bash\nnpm i -S @ama-team/optional\n```\n\nor\n\n```bash\nyarn add @ama-team/optional\n```\n\n## Usage\n\n### 30-second start\n\n`Optional` is just a class that wraps possibly-null/undefined value and\napply operations to it if it is present. For example, you have a \nresponse from API that *optionally* has a metadata field with a flag \ndeeply buried inside the response:\n\n```\nuser:\n  id: 1\n  ...\n  metadata:\n    processors:\n      fraud:\n        fraudulent: true\n```\n\nInstead of checking presence of the fields, you can take a shortcut:\n\n```typescript\nimport {Optional} from '@ama-team/optional'\n\nconst fraudulent = Optional\n  .of(user)\n  .map(user => user.processors)\n  .map(processors => processors.fraud)\n  .map(metadata => metadata.fraudulent)\n  .orElse(false);\n```\n\nBingo, now you either have `true` or `false` without too much hassle.\nI'll show how to work with pipelines like the one above later.\n\n### API\n\nYou have three ways to create an optional:\n\n```typescript\nOptional.empty();\nOptional.of(value); // throws error if value is null / undefined\nOptional.ofNullable(value);\n```\n\nOptional has two properties and several methods to interact with current\nstate:\n\n```typescript\noptional.present; // boolean\noptional.empty; // also boolean\n\n// following methods returns Optional itself, allowing to chain methods\noptional.ifPresent(identity => {});\noptional.ifEmpty(() => {});\noptional.peek(identity => {}); // will substitute missing identity with null\noptional.on(identity => {}, () => {}); // will trigger one of those depending on identity presence \n```\n\nYou can also suggest a value for optional, which will be used as \nidentity if optional is empty:\n\n```typescript\noptional.rescue(value);\noptional.rescueWith(() => value);\n```\n\nThere are also several ways to retrieve identity or substitute it with \nvalue:\n\n```typescript\noptional.get(); // will throw TypeError on missing entity\noptional.orElse(value); // return identity or value\noptional.orElseGet(() => value); // return identity or producer result\noptional.orElseThrow(() => new Error()); // throw error using producer\n```\n\nAnd, finally, there are transformation methods:\n\n```typescript\n// all those methods aren't mutating optional itself - transformations \n// will be applied eagerly to construct new optional\noptional.map(identity => transform(identity));\noptional.flatMap(identity => Optional.of(identity)); // unwraps returned optional\noptional.filter(identity => true); // substitutes identity with null if filter doesn't return true\n```\n\n### Pipelines\n\nOptionals help with checking for null/undefined, but generally the \nexample above bloats code as well, and usually should be placed \notherwise. To do so, pipelines were introduced: encapsulated set of lazy\noperations over optional:\n\n```typescript\nimport {Pipeline} from '@ama-team/optional';\n\nconst extractor = Pipeline.create()\n  .map(user => user.processors)\n  .map(processors => processors.fraud)\n  .map(metadata => metadata.fraudulent);\n\n// ...\n\nconst fraudulent = extractor.apply(user).orElse(false);\n```\n\nPipelines are very simple, and currently they provide two methods to\nprocess value:\n\n- `.apply(value)`, which will return an Optional and will let you to use \n`.orElse()` or similar methods\n- `.transform(value)`, which will in fact just a shortcut for \n`.apply(value).orElse(null)`.\n\nAlso pipelines can be concatenated - you can use `.append(pipeline)`\nmethod for that, which will return you a new pipeline. \n\n## Contributing\n\nFeel free to send PR to `dev` branch.\n\n### `dev` (next release) branch state\n\n[![CircleCI/dev](https://img.shields.io/circleci/project/github/ama-team/ts-optional/dev.svg?style=flat-square)](https://circleci.com/gh/ama-team/ts-optional/dev)\n[![Coveralls/dev](https://img.shields.io/coveralls/github/ama-team/ts-optional/dev.svg?style=flat-square)](https://coveralls.io/github/ama-team/ts-optional?branch=dev)\n\n## Licensing\n\nMIT License / AMA Team / 2018\n","readmeFilename":"README.md","homepage":"https://github.com/ama-team/ts-optional#readme","bugs":{"url":"https://github.com/ama-team/ts-optional/issues"}}