SlexAxton/messageformat.js — pull request #45 by eemeli (2014-04-22 10:10:52+00:00)
Title: Make output code independent of all globals

This fixes the issues raised [here](https://github.com/SlexAxton/messageformat.js/pull/34#discussion-diff-11803289) by making the one-letter utility functions properly local to the output, rather than leaking into the global namespace.

This is done by adding a new function `MessageFormat.LocalFunctions()`, which returns said function declarations as a string that's included by `compile()` in `lib/messageformat.dev.js` as well as `build()` in `bin/messageformat.js`. In the former, the functions are included within the scope of the `new Function()` -generated function; in the latter, they're scoped within the wrapping anonymous function.

As a side product, `lib/messageformat.include.js` is now obsolete and hence removed; this is good because it means that the utility functions are only defined in one place.

This also enabled me to remove references to `MessageFormat.locale` from the generated code, as they're now wrapped up by `MessageFormat.LocalFunctions()`.

If this pull request is merged, `MessageFormat.prototype.compile()` will always return a function that's independent of all globals, including `MessageFormat` itself. However, to write that function to disk for later use, you'll need to also write the output of `MessageFormat.LocalFunctions()` to a namespace accessible from the same place, just like `build()` does.

While I was poking at the code, I also simplified things a bit by inlining the utility functions that are each only used once and could be replaced by single lines of code. As a result, `fallbackLocale`, `pluralFunc`, `SafeString` and `Utils` have been removed.