{"_id":"@blainehansen/monads","_rev":"3-f4561e39da6899c895fbd8fdf46934bb","name":"@blainehansen/monads","dist-tags":{"latest":"1.0.2"},"versions":{"1.0.0":{"name":"@blainehansen/monads","version":"1.0.0","description":"`Result` and `Maybe` types and accompanying `macro-ts` helper macros that allow a safe, functional way of dealing with errors and nullable values.","main":"dist/lib/index.js","typings":"dist/lib/index.d.ts","keywords":["monad","rust","functional","result","maybe","option","macro","macro-ts","question","operator","try","exception"],"scripts":{"dev":"macro-ts --dev run lib/index.ts","test":"mocha -r @blainehansen/macro-ts/register lib/**/*.test.ts macros/**/*.test.ts","check":"macro-ts --dev check","build":"./build.sh","preversion":"npm test && npm run build","postversion":"npm publish --access public"},"author":{"name":"Blaine Hansen","email":"faichenshing@gmail.com","url":"http://blainehansen.me/"},"repository":{"type":"git","url":"git+https://github.com/blainehansen/monads.git"},"license":"MIT","devDependencies":{"@blainehansen/macro-ts":"^0.1.2","@types/chai":"^4.2.12","@types/mocha":"^8.0.3","chai":"^4.2.0","mocha":"^8.1.1","typescript":"^4.0.2"},"gitHead":"7ceb575728db307f81833ebbed46d5e700743d16","bugs":{"url":"https://github.com/blainehansen/monads/issues"},"homepage":"https://github.com/blainehansen/monads#readme","_id":"@blainehansen/monads@1.0.0","_nodeVersion":"14.8.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-LSrYCQnMVx8Bf1IaQo03tLlRSdNYC9VO1Rdm0j55CafRSEpLe/ktz+chRmcD7NSajsAFDUZVJHZGOPpqg/LACQ==","shasum":"ffd097e1d4a817f23a3cda931454e9d65527501c","tarball":"https://registry.npmjs.org/@blainehansen/monads/-/monads-1.0.0.tgz","fileCount":17,"unpackedSize":53621,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfRbfXCRA9TVsSAnZWagAAdYcP/Rbte1I4EWneiEPxoMZd\nqrIDD3CLxIFZj8VgXQzsTf84d2JF0IlqnQqQqkSEKtZa0ksMEehjm7G3z6IF\ngkh06DEL2tkgZJkfLgkmxplSJ7ob4uDJ0u8D241HeNjLcr4NTOqVLyhYGBxA\n8rCUUtklIBo/EGSr/deRiZyV8Mm75rMexWo4vPDJPnhKh6Yjb2ntAUNd3xAH\nIxguBbFr5gEgC6u+5PLi9Nwu+VNaPnaQwxFhHpKrxM7mssHDuFAnrH/YRMmV\n6FjuetVutWLb6jdtb4v2PafFOd9FJeEsZFX/algynPNhOuVCBdJWovPE7V7n\nmewcJoRCRdD5yAEHmvPEGpzDLPesy8Mlfa21gqoIN/GXxOg5OA3BKuj9BB5i\nxAJgy4fFRpbap978E829pT4M1WEshvcxorcCqks8HcFwdu4Q0JCaO6JJOmuz\nT/0iVjmLu8B2GMb4Kv5JXn09jV/jTVDgP2ZauTM1bvkHdDru7nNC0+Od+bdZ\ngNC0bpymzVj5GxaUikOWSjvz8eNLOB9elxoSnmdR73QTw6Kf2NyeWaBSvPTO\nJWrB4Tulam8q+VNKKOroMZFgfWXlDM90oabDnDdxeLV5bK3/5Cc9dLsNGLjX\nSonk80g+uZtcBQ8ZZjoRPZM/F1+tcQFgK3uI8VcdqrX6BXGSoK5RjuZ/wkI7\nU0vb\r\n=9sli\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC8GJBg4n7mACrMpKKnYH8/ReNWGuLOyS0Enf+BktPX0QIhALTfrpIULwVAiQDRNIxIlDHWT4nbMnLDkuMEnPgrANDL"}]},"maintainers":[{"name":"blainehansen","email":"faichenshing@gmail.com"}],"_npmUser":{"name":"blainehansen","email":"faichenshing@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/monads_1.0.0_1598404566859_0.011169002458463195"},"_hasShrinkwrap":false},"1.0.1":{"name":"@blainehansen/monads","version":"1.0.1","description":"`Result` and `Maybe` types and accompanying `macro-ts` helper macros that allow a safe, functional way of dealing with errors and nullable values.","main":"dist/lib/index.js","typings":"dist/lib/index.d.ts","keywords":["monad","rust","functional","result","maybe","option","macro","macro-ts","question","operator","try","exception"],"scripts":{"dev":"macro-ts --dev run lib/index.ts","test":"mocha -r @blainehansen/macro-ts/register lib/**/*.test.ts macros/**/*.test.ts","check":"macro-ts --dev check","build":"./build.sh","preversion":"npm test && npm run build","postversion":"git push origin master && git push origin master --tags && npm publish --access public"},"author":{"name":"Blaine Hansen","email":"faichenshing@gmail.com","url":"http://blainehansen.me/"},"repository":{"type":"git","url":"git+https://github.com/blainehansen/monads.git"},"license":"MIT","devDependencies":{"@blainehansen/macro-ts":"^0.1.2","@types/chai":"^4.2.12","@types/mocha":"^8.0.3","chai":"^4.2.0","mocha":"^8.1.1","typescript":"^4.0.2"},"gitHead":"5f7b1b771240b5ac93227e1e149f6eba52c33683","bugs":{"url":"https://github.com/blainehansen/monads/issues"},"homepage":"https://github.com/blainehansen/monads#readme","_id":"@blainehansen/monads@1.0.1","_nodeVersion":"14.8.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-eIjpI/vE5pUp3xZHIdUu2So0Ho1AZqBjYT7Q6d6t9p66ugf0+NlESwBdBr+JF9iPHiu8rx0nxiqP6mXYGu33MQ==","shasum":"6e105cff1cfbfbfe2a3d0ac985e51e1899362d8f","tarball":"https://registry.npmjs.org/@blainehansen/monads/-/monads-1.0.1.tgz","fileCount":17,"unpackedSize":53725,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfRbjLCRA9TVsSAnZWagAALqoP/0i5DqnWHe/EdlNpEqPj\nd1A9SJ9LKZs6c/fc854R2DBhTZRStcmDW/xleBu601K0FxcZOgI6z0kjvViG\nEOKiotQOL+xtxi+LVL9x9z2DMjhZ0M7JJJx0UW1SR5so6VvdViWJzwPsCpGi\n8Jb/+I4lfo63JEUSvYOVqHOWskIn7i+5LgYzUqAgIDpMHMEcfpHo1MHucutB\nMwPaGxRu5MfGfNZUtDY4hNTpjouF2AVOEBzz9Pcd+rmQ95JktMZU/UW5l8u3\nj/3m7szM4SVM7o68AQmsyiIFevWOSnXSxnRdCSCdplpsBajm2ntz/fDBmv5i\nVXt1s/OOJYAbRS8pkfmLAqWZFQvdmWT3gbYydd3nFGbEAVXr85kAe7HswjhZ\nUykMofs1LHtTHBy6BX2Ir4DhC8FBubTpZaMfz1H2T3g5B1TJQgVkcEk1v4pW\nz1ymuU2drzYfmJWNHaggTVD6hjh/yX7yTphwKlfcN0tDbO+V+3vPPA+WbLdC\nBVA5bc1geJ5mTC4/tnK97Zt/Kwk/CHJPfuc8rMRMWomL1UyIzXovHqibAKye\nWYJC+6rZCuCXvhAT5xIQp3UZrz48W3kyHFxSHA+o/UOnKoIyb41iIj864JCX\nAkZUgSQRbjB3KqTrXuNP7euBe3CyTP6NAQYaIO8Q06dYFXVBo723EdLNBb/c\n2+BN\r\n=ge6b\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHjDg7OkErMauLiiIAnrwZjGVXeo7gQGpHPpUV471z5FAiEAw2RUs6fbEy1JCXB07OTGvyRQPTwU5lxH4h27Y197ze0="}]},"maintainers":[{"name":"blainehansen","email":"faichenshing@gmail.com"}],"_npmUser":{"name":"blainehansen","email":"faichenshing@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/monads_1.0.1_1598404810735_0.9243796749718549"},"_hasShrinkwrap":false},"1.0.2":{"name":"@blainehansen/monads","version":"1.0.2","description":"`Result` and `Maybe` types and accompanying `macro-ts` helper macros that allow a safe, functional way of dealing with errors and nullable values.","main":"./dist/lib/index.js","typings":"./dist/lib/index.d.ts","keywords":["monad","rust","functional","result","maybe","option","macro","macro-ts","question","operator","try","exception"],"scripts":{"dev":"macro-ts --dev run lib/index.ts","test":"mocha -r @blainehansen/macro-ts/register lib/**/*.test.ts macros/**/*.test.ts","check":"macro-ts --dev check","build":"./build.sh","preversion":"npm test && npm run build","postversion":"git push origin master && git push origin master --tags && npm publish --access public"},"author":{"name":"Blaine Hansen","email":"faichenshing@gmail.com","url":"http://blainehansen.me/"},"repository":{"type":"git","url":"git+https://github.com/blainehansen/monads.git"},"license":"MIT","devDependencies":{"@blainehansen/macro-ts":"^0.1.5","@types/chai":"^4.2.12","@types/mocha":"^8.0.3","chai":"^4.2.0","mocha":"^8.1.1","typescript":"^4.0.2"},"gitHead":"d0f1c8f6f706c384262afa7518080b2aaada2ec2","bugs":{"url":"https://github.com/blainehansen/monads/issues"},"homepage":"https://github.com/blainehansen/monads#readme","_id":"@blainehansen/monads@1.0.2","_nodeVersion":"14.8.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-OpCb7xTOd75/EWlvEmRoP5+B1aAmoeXjEplHL8X++G/6z9NUGsed01hsRSjlGICkbdnOvr5XnQ5MQ2rs00HZIQ==","shasum":"5cc05647df153d4a847a10fd28ec3ae64da9a12f","tarball":"https://registry.npmjs.org/@blainehansen/monads/-/monads-1.0.2.tgz","fileCount":17,"unpackedSize":55319,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfSIcqCRA9TVsSAnZWagAAk1AP/2C+hYoNo5Wvny3fVXum\nsKL04KbLHjx65JPXF802amfAHMsimm9DjubbCJsM/DTF+qRgrB1PANlb0zZN\n598IkitwEEiD1TUjCAUeb1d8rpdHJZvsSta3bElHTC89yvdNpCvq5G3Q6GHo\nylfIiG1JUnAZwoAHrBKi/u8ZGRdszrKS2czob6QhvZZhNsa7bd5VDpOgAiBT\nBUB+oqHwaqIgZx9bd2STLq/5lqgwjCKAs1cA78P9SExfPJb9lIlSFDSpJiuP\nD2glja1c/SdJ/J0u5pEtneLUU3GOvIpBn5Pjqvl4Tbk01hGxJxDiL3HjsRmY\nZo+TclRvD3+O1un59CYBQboauWw88IR2HMkOxHnhXoA536TTp9eoAC5xqm6E\nlp4U4h/3T69sSw4RhE8fYN0hOXAwT35WzCvuZmcFt64Q8DzuR5d/zxuclKKF\ndRSdMoSe+2kL8xJdlrDqoLQFPhByGiolCyC+ncuOva3lZ+/6kzIgluTZHZKf\nx/WERah0sTfpzaMRH9j1EOPTKS/tCo3fbbq9R4kP0ThLc6lCpKjUb9ohkS6C\nVotyfPUsitF+rlOY1WQrEt7RnCgmtw8ey+FYEo6k2C93olzI4mH7u+QMn6kj\nsjIXvKh0GJiFuQjJWyYbSDHTEl8s0Xo0QtmV0LghyBbEc4/TiwFP9jMBZW8j\nIsKz\r\n=lxG6\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCqM2D8dTh0er9I7cNhvYPvwujgJqDLZfA54WehNu544gIgMWymBqoNH+1Jz2vce8SCDFwyoj4L8/88Xrbso8h/3OY="}]},"maintainers":[{"name":"blainehansen","email":"faichenshing@gmail.com"}],"_npmUser":{"name":"blainehansen","email":"faichenshing@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/monads_1.0.2_1598588714316_0.7997578889893631"},"_hasShrinkwrap":false}},"time":{"created":"2020-08-26T01:16:06.800Z","1.0.0":"2020-08-26T01:16:06.955Z","modified":"2022-04-04T19:23:24.055Z","1.0.1":"2020-08-26T01:20:10.949Z","1.0.2":"2020-08-28T04:25:14.451Z"},"maintainers":[{"name":"blainehansen","email":"faichenshing@gmail.com"}],"description":"`Result` and `Maybe` types and accompanying `macro-ts` helper macros that allow a safe, functional way of dealing with errors and nullable values.","homepage":"https://github.com/blainehansen/monads#readme","keywords":["monad","rust","functional","result","maybe","option","macro","macro-ts","question","operator","try","exception"],"repository":{"type":"git","url":"git+https://github.com/blainehansen/monads.git"},"author":{"name":"Blaine Hansen","email":"faichenshing@gmail.com","url":"http://blainehansen.me/"},"bugs":{"url":"https://github.com/blainehansen/monads/issues"},"license":"MIT","readme":"# `@blainehansen/monads`\n\n> `Result` and `Maybe` types and accompanying `macro-ts` helper macros that allow a safe, functional way of dealing with errors and nullable values.\n\nThese types wrap successful or unsuccessful values in a way that allows very convenient chaining and recombination. Ever wanted to do a task on a value only if it is actually there? Or ever wanted to do some task with a bunch of values that all individually could fail or not? Are you sick of having to catch exceptions all the time that come out of nowhere and make your software buggy and unreliable? This library is for you.\n\n## [`type Result<T, E = string> = Ok<T> | Err<E>`](./lib/result.md)\n\nThis type is a much safer and more predictable alternative to exceptions. Since a `Result` type can either be a successful `Ok` or a failed `Err`, it allows very granular control of how and when errors are dealt with, without threatening to crash programs and cause bad behavior.\n\nSince a function that could fail can choose to return a `Result` rather than throwing an exception, this creates a clear contract between the caller and the callee, and requires the caller to make some intelligent decision about how to deal with that error, rather than being surprised with an exception.\n\n**Full `Result` docs [here](./lib/result.md)**\n\n```ts\nimport { Result, Ok, Err } from '@blainehansen/monads'\n\nconst a = Ok(5)\n  // perform some infallible operation on a successful value\n  .change(number => number + 1)\n  // perform some *fallible* operation on a successful value\n  .tryChange(number => number % 2 === 0 ? Ok(number + 1) : Err(\"number wasn't even\"))\n\na === Ok(7)\n\nconst b = Err(\"starting off bad...\")\n  .change(number => number + 1)\n  .tryChange(number => number % 2 === 0 ? Ok(number + 1) : Err(\"number wasn't even\"))\n\nb === Err(\"starting off bad...\")\n\nconst c = Ok(6)\n  .change(number => number + 1)\n  .tryChange(number => number % 2 === 0 ? Ok(number + 1) : Err(\"number wasn't even\"))\n\nc === Err(\"number wasn't even\")\n\n\nconst combineOk = Ok(1).join(Ok(2), Ok(3))\n  .combine((one, two, three) => one + two + three)\n\ncombineOk === Ok(6)\n\nconst combineErr = Ok(1).join(Err(\"two failed...\"), Ok(3))\n  .combine((one, two, three) => one + two + three)\n\ncombineErr === Err(\"two failed...\")\n```\n\n\n## [`type Maybe<T> = Some(T) | None`](./lib/maybe.md)\n\nThis type is an alternative to `null | undefined`, but it allows for chaining and joining in a functional style.\n\n**Full `Maybe` docs [here](./lib/maybe.md)**\n\n```ts\nimport { Maybe, Some, None } from '@blainehansen/monads'\n\nconst a = Some(5)\n  // perform some infallible operation on a successful value\n  .change(number => number + 1)\n  // perform some *fallible* operation on a successful value\n  .tryChange(number => number % 2 === 0 ? Some(number + 1) : None)\n\na === Some(7)\n\nconst b = None\n  .change(number => number + 1)\n  .tryChange(number => number % 2 === 0 ? Some(number + 1) : None)\n\nb === None\n\n\nconst combineSome = Some(1).join(Some(2), Some(3))\n  .combine((one, two, three) => one + two + three)\n\ncombineSome === Some(6)\n\nconst combineNone = Some(1).join(None, Some(3))\n  .combine((one, two, three) => one + two + three)\n\ncombineNone === None\n```\n\n\n## `ok!!` and `some!!` helper macros.\n\nThis library includes two [`macro-ts`](https://github.com/blainehansen/macro-ts) helper macros that implement behavior equivalent to [Rust's short-circuiting question mark operator](https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/the-question-mark-operator-for-easier-error-handling.html).\n\n```ts\nfunction addResults(a: Result<number>, b: Result<number>) {\n  // these macros will expand to code equivalent to this:\n  // if (a.isErr()) return Err(a.error)\n  // if (b.isErr()) return Err(b.error)\n  // const sum = a.value + b.value\n  const sum = ok!!(a) + ok!!(b)\n  return Ok(sum)\n}\naddResults(Ok(1), Ok(1)) === Ok(2)\naddResults(Ok(1), Err('boo')) === Err('boo')\n\nfunction printDollarMaybes(...maybes: Maybe<number>[]) {\n  // the `ok` and `some` macros\n  // can be used on arbitrarily complex expressions\n  const amounts = some!!(Maybe.all(maybes)).map(a => `$${a.toFixed(2)}`)\n  return Some(amounts.join(', '))\n}\nprintDollarMaybes(Some(4.3), Some(9)) === Some('$4.30, $9.00')\nprintDollarMaybes(Some(4.3), None, Some(9)) === None\n```\n\nTo use the macros, follow the directions to set up your project with [`macro-ts`](https://github.com/blainehansen/macro-ts), and then import and include the macros:\n\n```ts\nimport { ok, some } from '@blainehansen/monads'\n\nexport const macros = {\n  ok, some,\n  // ... any other macros you're using ...\n}\n```\n","readmeFilename":"README.md"}