{"name":"daggy","version":"0.0.1","description":"Library for creating tagged constructors.","main":"daggy.js","scripts":{},"repository":{"type":"git","url":"git://github.com/puffnfresh/daggy.git"},"keywords":["constructor"],"author":{"name":"Brian McKenna"},"license":"MIT","devDependencies":{"emu":"0.0.2"},"bugs":{"url":"https://github.com/puffnfresh/daggy/issues"},"_id":"daggy@0.0.1","dist":{"shasum":"ae9c932c9f7136e655a04afb21e8baadfd0d3707","tarball":"http://registry-npm.ywwl.com/daggy/download/daggy-0.0.1.tgz","integrity":"sha512-zMSxQ1O+9xx1CyvUP2Z2cdD+o6zB3nyInl+UcwTIq08gK704hLZkHK+ZU9boyrk8nBCIUxpZF7aXbys3BTr+rQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDdqFbMYhOcRTAHNzZemBHXXJSuUERHUbE4nnD/Xgz2XgIhAPK7VZjbTfSvyF2xU++63/ZtdgshlaAhIUV+KA9Sdec4"}],"size":3215,"noattachment":false,"key":"/daggy/-/daggy-0.0.1.tgz"},"_from":".","_npmVersion":"1.2.23","_npmUser":{"name":"puffnfresh","email":"brian@brianmckenna.org"},"maintainers":[{"name":"puffnfresh","email":"brian@brianmckenna.org"}],"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":1369925121546,"_cnpm_publish_time":1369925121546,"dist-tags":{"latest":"1.5.0"}}