blue-button/bluebutton.js — pull request #44 by robdodson (2013-07-01 01:41:13+00:00)
Title: Add support for Node.js

Fixes #41 by adding support for Node.js.

**This PR isn't ready to merge yet but I wanted to go ahead and open it up so folks can mess around with it.**

TODO:

- [ ] add unit tests for node
- [ ] add unit tests for AMD
- [ ] cleanup the UMD task so it doesn't have such ugly formatting
- [ ] ???
- [ ] profit

I added [grunt-umd](https://github.com/alexlawrence/grunt-umd) which puts a [Universal Module Definition](https://github.com/umdjs/umd) wrapper around bluebutton.js. This means it can be loaded as a CommonJS module in Node, an AMD module with RequireJS or in a regular script tag which exports a browser global (like it always has). When the module thinks it's in node land it uses [jsdom](https://github.com/tmpvar/jsdom) to parse the XML and simulate DOM methods.

The jasmine tests continue to pass so everything that used to work is still intact :+1:

I still have a fair bit of work to do but if you want to try it out you can [clone my fork](https://github.com/robdodson/bluebutton.js) and run the following from the command line:

```
git checkout node
npm install
bower install
node demo.js
```

the output should look like this:

```
starting bluebutton...
Error: date is not a string // don't worry about these warnings, they've always been there
Error: date is not a string
Error: date is not a string
Error: date is not a string
Error: date is not a string
Error: date is not a string
Error: date is not a string
Error: date is not a string
Error: date is not a string
Error: date is not a string
Error: date is not a string
bb returned...
{ type: 'ccda', json: [Function] } // logging bb.document() to verify everything's working
```

I realize the UMD wrapper and my checks inside of core.js look redundant but I'm not sure what's the best way to clean that up, or if it should be cleaned up at all. The UMD wrapper is applied after everything is concatenated so we *could* just write our own wrapper starting at core.js and ending at whatever is the last file to be concatenated.. or we can have the somewhat more verbose version we have here. I'm cool with either.