{"name":"daggy","description":"Library for creating tagged constructors.","repository":{"type":"git","url":"git+https://github.com/fantasyland/daggy.git"},"keywords":["constructor"],"author":{"name":"Brian McKenna"},"license":"MIT","dependencies":{"sanctuary-type-classes":"3.0.0","sanctuary-type-identifiers":"1.0.0"},"devDependencies":{"benchmark":"2.1.3","cli-table":"0.3.1","colors":"1.1.2","daggy":"1.0.0","eslint":"3.19.x","fantasy-combinators":"0.0.x","sanctuary-style":"0.5.x","standard":"8.6.0","tap":"10.0.0","xyz":"2.1.x"},"scripts":{"lint":"eslint --config node_modules/sanctuary-style/eslint-es3.json --env es3 --global define --global module --global require --global self -- src/daggy.js && standard bench/*.js test/*.js","release-major":"xyz --repo git@github.com:fantasyland/daggy.git --increment major","release-minor":"xyz --repo git@github.com:fantasyland/daggy.git --increment minor","release-patch":"xyz --repo git@github.com:fantasyland/daggy.git --increment patch","test":"tap -R tap test/*.js","check":"npm run lint && npm run test","bench":"node bench/"},"files":["src/*.js"],"main":"src/daggy.js","version":"1.2.0","gitHead":"6d49954d9918e78acefca765e95ea007a676b0a8","bugs":{"url":"https://github.com/fantasyland/daggy/issues"},"homepage":"https://github.com/fantasyland/daggy#readme","_id":"daggy@1.2.0","_shasum":"f0151fcffd58f1742d63d8c53a7556e84b258cb4","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.11.1","_npmUser":{"name":"davidchambers","email":"dc@davidchambers.me"},"dist":{"shasum":"f0151fcffd58f1742d63d8c53a7556e84b258cb4","tarball":"http://registry-npm.ywwl.com/daggy/download/daggy-1.2.0.tgz","integrity":"sha512-tyjrrmN4ytCOzA4o2WWatKaM1nnO7p8RNJBjjfq8+kvNJjW2z1X40mqNzbjeSlK4akfHE36OYT9sLfquX7jBSA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCID/dBuGdYIzd/HqsKygWcjKoYTDIWAxBhlmbpe3i3oWIAiA+2+7pjdN1hN5TnfqIicoEVlSzXgr/v++TJ80lc1ui3A=="}],"size":3482,"noattachment":false,"key":"/daggy/-/daggy-1.2.0.tgz"},"maintainers":[{"name":"davidchambers","email":"dc@davidchambers.me"},{"name":"drboolean","email":"brian@looprecur.com"},{"name":"jaforbes","email":"james.a.forbes@gmail.com"},{"name":"joneshf","email":"jones3.hardy@gmail.com"},{"name":"kennknowles","email":"kenn.knowles@gmail.com"},{"name":"mudge","email":"mudge@mudge.name"},{"name":"puffnfresh","email":"brian@brianmckenna.org"},{"name":"raimohanska","email":"juha.paananen@gmail.com"},{"name":"raynos","email":"raynos2@gmail.com"},{"name":"robotlolita","email":"quildreen@gmail.com"},{"name":"safareli","email":"i.safareli@gmail.com"},{"name":"simonrichardson","email":"stickupkid@gmail.com"},{"name":"trevorbasinger","email":"trevor.basinger@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/daggy-1.2.0.tgz_1501840798472_0.21467466582544148"},"directories":{},"readme":"# Daggy\n\n[![Build Status](https://img.shields.io/travis/fantasyland/daggy/master.svg)](https://travis-ci.org/fantasyland/daggy)\n[![npm](https://img.shields.io/npm/v/daggy.svg)](https://www.npmjs.com/package/daggy)\n\nLibrary for creating tagged constructors a.k.a. \"disjoint union types\" or \"sum types\".\n\n## Motivation\n\nJavaScript does not have first class support for sum types, but they can be imitated in a handful of different ways. Nevertheless, this imitation leads to excess boilerplate that can lead to extra work and the potential for errors:\n\n```javascript\nconst successCase = { success: true, items: [1, 2, 3] }\nconst failureCase = { success: false, error: 'There was a problem.' }\n\nfunction handleResult(result) {\n  if (result.success) {\n    console.log(result.items)\n  } else {\n    console.error(result.error)\n  }\n}\n```\n\nDaggy reduces the boilerplate needed to represent sum types in JavaScript:\n\n```javascript\nconst Result = daggy.taggedSum('Result', {\n  Success: ['items'],\n  Failure: ['error']\n})\n\nconst successCase = Result.Success([1, 2, 3])\nconst failureCase = Result.Failure('There was a problem.')\n\nfunction handleResult(result) {\n  result.cata({\n    Success: message => console.log(message),\n    Failure: error => console.error(error)\n  })\n}\n```\n\n## API\n\n### `daggy.tagged(typeName, fields)`\n\nCreates a new constructor with the given field names\n\n```javascript\nconst Point3D = daggy.tagged('Point3D', ['x', 'y', 'z'])\nPoint3D.toString() // 'Point3D'\nconst a = Point3D(1, 2, 3) // { x: 1, y: 2, z: 3 }\na.x == 1 && a.y == 2 && a.z == 3 // true\na.toString() // 'Point3D(1, 2, 3)'\nPoint3D.is(a) // true\nPoint3D.prototype.scale = function(n){\n  return Point3D(this.x * n, this.y * n, this.z * n)\n}\nconst b = a.scale(2) // { x: 2, y: 4, z: 6 }\nb.toString() // 'Point3D(2, 4, 6)'\nconst c = Point3D.from({y: 2, x: 1, z: 3}) // { x: 1, y: 2, z: 3 }\n```\n\n### `daggy.taggedSum(typeName, constructors)`\n\nReturns Type Representative containing constructors of for each key in `constructors` as a property. Allows `{TypeRep}.is` and `{TypeRep}.{Tag}.is` checks for values created by constructors.\n\n```javascript\nconst Option = daggy.taggedSum('Option', {\n  Some: ['x'],\n  None: [],\n})\nconst a = Option.Some(1) // { x: 1 }\na.toString() // 'Option.Some(1)'\nOption.Some.is(a) // true\nOption.is(a) // true\nOption.None.is(Option.None) // true\nOption.is(Option.None) // true\nOption.None.toString() // 'Option.None'\nOption.Some.toString() // 'Option.Some'\nOption.prototype.map = function (f) {\n  return this.cata({\n    Some: (x) => Option.Some(f(x)),\n    None: () => this,\n  })\n}\nconst b = a.map(x => x+1) // { x: 2 }\nb.toString() // 'Option.Some(2)'\nconst c = Option.Some.from({x: 1}) // { x: 1 }\n```\n","publish_time":1501840799323,"_cnpm_publish_time":1501840799323,"dist-tags":{"latest":"1.5.0"}}