{"_id":"@baconsfoster/matches-with","_rev":"2-57be15185f5d29ee9465e56a72a6aa95","name":"@baconsfoster/matches-with","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@baconsfoster/matches-with","version":"0.1.0","description":"Simple pattern matching","main":"lib/index.js","repository":{"type":"git","url":"git+ssh://git@github.com/baconsfoster/matchesWith.git"},"scripts":{"test":"ts-node src/test.ts","build":"tsc"},"author":{"name":"ragnaro"},"license":"MIT","devDependencies":{"ts-node":"^8.0.2","typescript":"^3.3.1"},"private":false,"gitHead":"233c4a16a70a961cd2a8720b0bd92d100e961384","bugs":{"url":"https://github.com/baconsfoster/matchesWith/issues"},"homepage":"https://github.com/baconsfoster/matchesWith#readme","_id":"@baconsfoster/matches-with@0.1.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.0","_npmUser":{"name":"ragnaro","email":"ragnar@appleriver.online"},"dist":{"integrity":"sha512-0cab38YwSJqaJBWgeszA79x0LBn+J8eWPJ+ko7LN8in5rpo554Ng/ZG4iaRtQEj4619plAuh14octMC0X/AchQ==","shasum":"cfab822e72fae2ad74f2c1f1bea638a1b033ae26","tarball":"https://registry.npmjs.org/@baconsfoster/matches-with/-/matches-with-0.1.0.tgz","fileCount":8,"unpackedSize":15257,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcc1PTCRA9TVsSAnZWagAAyc8P/0JOlXaPgkbcxOyvhrIv\n7JMs0Wf1908SzLoKEZzlparSzxKBHHlAeOt/TeNIKczliZSk7yzrxROJqn8+\nys0ivfptB7/EjKI7Ojt5Xfi1vT8Ex2lSjMQzzNfuZCylXhKXGkyVWp2qilgc\nGpSIU4eEyYM9OSrFSI3Lcs9tnCe7wSqS+5zK5uwb2PPeKrMbaSMaRq/YUUnm\nCB+8F1Q7dCh19O9eGaRaKi4OF5cR/Pojl5KWXE7VUFv8DkfYQppGrHjvYvMe\nKPmvoBq97AM/eGgW7pdM1hMrC+2/htMBYpfS0DDombwXOIjIQNnqBs2XgQkd\nV+6/RmQBCJi9dIt0PJDhXPphaMMpgZ8fUqnuWY38845BmCOVITJ91TmtnCyn\n1FNVQesI3KHsLyV4HQnt701ojQMma62JF5iPHo+dKidAanvgQ9z4s7kIliVA\nLsAeng/XvNsJRffQeqL6Wgi7klvJ/zIQ1gS5X6qxaLYdpL/odpnMObWX6FNb\n0iTnukzp1+TiPpM2V/qDsttZa0icn4d4ehj/1TpMkO/3SKVx9QTxXkvq2bPp\n8hUfKnU6wiO4IUFxVdOusGciFqRVB45Lr5axIDpbsp1yyTdakMlwbvJhZROF\nMFPBW+rH19fWMsxw3FEfmDf6vFoDs5uFZLPkRgdbMNYm60ZqMMRcXKSnihp1\n6Bc8\r\n=kafe\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFOvwyw4ThH4uv5GbrL5C4LoAZiUoBu+glXnRGZ/rLKVAiBnn3PfTs5uBO003p7iWbVFACVHlW/i5D/rAoeTAa0Dwg=="}]},"maintainers":[{"name":"epfremmer","email":"epfremmer@gmail.com"},{"name":"ragnaro","email":"ragnar@appleriver.online"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/matches-with_0.1.0_1551061970841_0.03001096673619119"},"_hasShrinkwrap":false}},"time":{"created":"2019-02-25T02:32:50.653Z","0.1.0":"2019-02-25T02:32:51.005Z","modified":"2022-04-04T17:29:42.651Z"},"maintainers":[{"email":"jason@spradlin.dev","name":"jason.spradlin"},{"email":"epfremmer@gmail.com","name":"epfremmer"},{"email":"ragnar@appleriver.online","name":"ragnaro"}],"description":"Simple pattern matching","homepage":"https://github.com/baconsfoster/matchesWith#readme","repository":{"type":"git","url":"git+ssh://git@github.com/baconsfoster/matchesWith.git"},"author":{"name":"ragnaro"},"bugs":{"url":"https://github.com/baconsfoster/matchesWith/issues"},"license":"MIT","readme":"# MatchesWith\nA simple pattern matching library\n\n```\nyarn add @baconsfoster/matches-with\n\n// or\n\nnpm install --save @baconsfoster/matches-with\n```\n\n## Overview\n\n Provides:\n\n * `matchesWith` : the pattern matching function\n * `MatchesWith` : A class with both a static and instance member assigned the pattern matching function\n * `Any`         : A default-case predicate\n * `isInstanceOf`: A function that returns a predicate for a given class constructor\n * `isTypeOf`    : A function that returns a predicate for a given string based on the `typeof` operator\n\n ### Examples:\n\n ```javascript\n class ErrorA{}\n class ErrorB{}\n\n const isErrorA = isInstanceOf(ErrorA);\n const isErrorB = isInstanceOf(ErrorB);\n\n const isNumber = isTypeOf('number');\n const isBoolean = isTypeOf('boolean');\n\ntry {\n  await someFunctionThatCouldThrowManyDifferentErrors();\n  await someOtherFunctionThatMightThrowAStringOrNumber();\n} catch (e) {\n  matchesWith(e, [\n    [isErrorA, () => console.log('e is of type ErrorA')],\n    [isErrorB, () => console.log('e is of type ErrorB')],\n    [isNumber, () => console.log(`e is a number: ${e})`],\n    [isBoolean, () => console.log('who throws booleans?')],\n    [Any, () => console.log('this is the generic fallback handler')]\n  ]);\n}\n ```\n\n Note that the order of predicates matters:\n\n ```javascript\nmatchesWith(val, [\n  [Any, () => console.log('`Any` matches anything, so the rest aren\\'t tested')],\n  [somethingElse, () => console.log('can never be reached')]\n])\n ```\n\n Note that `matchesWith` returns the value of invoking the matched clause. If no cases are matched, NO_MATCH is returned instead.\n\n ```javascript\nimport { matchesWith, isTypeof, NO_MATCH } from '@baconsfoster/matchesWith';\n\nconst isString = isTypeOf('string');\n\nconst x = matchesWith(true, [\n  [isString, () => 'a string was matched']\n]);\n\nx === NO_MATCH; // true\n ```\n\n ## Inheriting and Mixing In matching\n\n `MatchesWith` is a class that can be utilized for inheritance or mixing in. Examples:\n\n ```javascript\nclass BaseClass extends MatchesWith {}\n\nBaseClass.matchesWith(someVal, [\n  // ... predicate clauses\n]);\n\nconst test = new BaseClass();\n\ntest.matchesWith([\n  // predicate clauses tested against the 'test' variable (i.e. self)\n]);\n ```\n\n This is mostly handy where you are operating on a sum type- say a value that is one of several child classes. For example:\n\n ```javascript\nclass BaseClass extends MatchesWith{}\nclass ChildA extends BaseClass{}\nclass ChildB extends BaseClass{}\n\nfunction returnChildAOrB() { /* returns either an instance of ChildA or ChildB */}\n\nconst test = returnChildAOrB();\ntest.matchesWith(test, [\n  [isChildA, () => console.log('got child a')],\n  [isChildB, () => console.log('got child b')]\n]);\n ```\n\n### TypeScript Caveats and Using as a Mixin\n\nRather than inheriting from MatchesWith, which may not be practical, you can use `implements` in TypeScript\nto treat the class's public members as an interface; from there, you just copy them over. Example:\n\n```javascript\nclass B implements MatchesWith {\n  static matchesWith = MatchesWith.matchesWith;\n\n  matchesWith = MatchesWith.prototype.matchesWith;\n}\n```\n\nThis way, you can get both a parent from a different inheritance chain while \"mixing in\" the MatchesWith\nbehavior, if so desired.\n","readmeFilename":"README.md"}