{"_id":"@briandamaged/dispatcher","_rev":"1-ab65d74c9a54feaf029feed4553d4bf3","name":"@briandamaged/dispatcher","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@briandamaged/dispatcher","version":"1.0.0","description":"Command Dispatcher","main":"src/index.js","scripts":{"test":"mocha --recursive ./tests/unit","publish":"npm publish --access=public"},"repository":{"type":"git","url":"git+ssh://git@github.com/briandamaged/js-dispatcher.git"},"keywords":["Dispatcher","Function"],"author":{"name":"Brian Lauber"},"license":"ISC","bugs":{"url":"https://github.com/briandamaged/js-dispatcher/issues"},"homepage":"https://github.com/briandamaged/js-dispatcher#readme","devDependencies":{"chai":"^4.2.0","mocha":"^6.2.0"},"gitHead":"6b37c4f54a456c123f5e66a371b34f127395f8fe","_id":"@briandamaged/dispatcher@1.0.0","_npmVersion":"5.6.0","_nodeVersion":"8.11.2","_npmUser":{"name":"briandamaged","email":"constructible.truth@gmail.com"},"dist":{"integrity":"sha512-HT1J8XgKdQb7fsOR7RGrMEwo3wp/fva9OE0YcC6GfP4HX/eP0yklNvXYdZ9fYE/JbbSB0ABEzXM+a60k5jaJeA==","shasum":"1d4b1be69d7e267e791f11135cba4aca2171ff70","tarball":"https://registry.npmjs.org/@briandamaged/dispatcher/-/dispatcher-1.0.0.tgz","fileCount":4,"unpackedSize":6131,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdawCZCRA9TVsSAnZWagAA5QwP/RkYxof/DmNA/SdON+yW\nlDD6uGKkBXuupXnI+qzsNiosWqViNM45i9utyrYZBxqqXv8hvbKf22VwQs9m\nqzwoqkuXk8Vfit8CQm+WTaWptqyFwpwVoH2LBENu1Gxzj5W7iDgHiktNWAP+\nf3J4k5SRz7B/v0ho26sy9Y+HXMag/N1Uv6lsTujuloCSmPE0WFWZ6DMk4OaU\n2D51VZOORjPiHM7t+YCADn4ywNuq1CS9fpHa9k7CAJ+cnToLaskXx/M6SBv6\n56rGCABxxQf4YcZenLSGH6MSDXuHGecMjmM6uUu3JhGujbdx+YCwAVsZJzrs\nQ+VqrEDACnNTuRKcgVmO4xicDA8yV8S/e506RjsLbCoFswUl8piaO5/ZFZ43\ndkZ4EXGkV1QHjBgyYArRRQqXpG4th2n6MExGKGztsaawO8vTR0GEn00f0zIz\nfr8ymEkF9UUrk0fC5TO/0JaDVktPSu8WCoxAvpo2sLo1jc6ydF3M9fWGDVSK\nZQpZNuV7ijW3XYnBYwQrc48QFiurwi9suB599dHCntm49ZzY4SDUpy8vBXbt\n8Sb2R9u/d18LH/j6MeOlAaOmXUKdNYoLgpmzeILupVdAAivMqyct55UJNrFx\nzs1hWgeZ5EoX1464h/Tn7IkYVAcCBV9Q6nqU8hz+OBIHGPyhobpFoQ5LyFJi\nvWSL\r\n=ot6G\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIH38rh8zUaVKyXNVl4IFjF/3AhBqrPL2ExMWFQ7uKCrzAiEAguFHdlPJlq1ZeWsZHPb0Oea+vVj3pkGPTvwzLL9TtXw="}]},"maintainers":[{"name":"briandamaged","email":"constructible.truth@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dispatcher_1.0.0_1567293593136_0.6460760461180981"},"_hasShrinkwrap":false}},"time":{"created":"2019-08-31T23:19:52.919Z","1.0.0":"2019-08-31T23:19:53.258Z","modified":"2022-04-04T20:31:58.753Z"},"maintainers":[{"name":"briandamaged","email":"constructible.truth@gmail.com"}],"description":"Command Dispatcher","homepage":"https://github.com/briandamaged/js-dispatcher#readme","keywords":["Dispatcher","Function"],"repository":{"type":"git","url":"git+ssh://git@github.com/briandamaged/js-dispatcher.git"},"author":{"name":"Brian Lauber"},"bugs":{"url":"https://github.com/briandamaged/js-dispatcher/issues"},"license":"ISC","readme":"# js-dispatcher #\n\nFlexible Conditional Logic\n\n## Usage ##\n\n### The Basics ###\n\nHere's an example of a programmatically constructed `if... else if... else` statement:\n\n```javascript\nconst {\n  Dispatcher,\n  IF, RETURN,\n} = require('js-dispatcher');\n\n\nconst d = Dispatcher();\n\n// Setup our Dispatcher rules:\nd.use(IF((x, y)=> x < y, (x, y)=> x + y));\nd.use(IF((x, y)=> x > y, (x, y)=> x * y));\nd.otherwise((x, y)=> 0);\n\n\n// Alright, let's put it to the test:\nconsole.log(d(3, 4));   // Output: 7\nconsole.log(d(4, 3));   // Output: 12\nconsole.log(d(3, 3));   // Output: 0\n```\n\nNotice that `d.otherwise((x, y)=> 0)` always just returns the value `0`.  Since this is a common pattern, you can also write the statement as:\n\n```javascript\nd.otherwise(RETURN(0));\n```\n\n### Advanced Stuff ###\n\nThe `IF(condition, handler)` function is just a factory for producing a very common type of `RULE_FUNCTION`.  Specifically, the `RULE_FUNCTION` has the form:\n\n```javascript\nfunction _if(...args) {\n  if(condition(...args)) {\n    return handler;\n  }\n}\n```\n\nAs you can see, it evaluates the `condition(..)`.  If this condition returns something truthy, then it returns the associated `handler(..)` function.  Otherwise, it returns `undefined`.  This protocol informs the `Dispatcher` about whether or not the `RULE_FUNCTION` is able to handle the input.\n\nMost of the time, you don't need to worry about this level of detail, and you can just rely upon the `IF(..)` factory.  But, on rare occassions, both the `RULE_FUNCTION` and the `handler(..)` function rely upon the same computationally-expensive operation.  In these cases, you can write your own `RULE_FUNCTION` by hand to minimize computational costs.  For example:\n\n```javascript\nfunction customRule(...args) {\n  const expensive = someExpensiveCalculation(...args);\n\n  if(someCondition(expensive)) {\n    function handler() {\n      return doSomethingWith(expensive);\n    }\n\n    return handler;\n  }\n}\n\n\nd.use(customRule);\n```\n\n","readmeFilename":"README.md"}