Unibabel JS: The Browserify-Ready Unibabel
A simple browserify style version of Unibabel, exported as a module instead of as a global object.
Currently does not support the add-in support for hex or base32, because I didn't need them when I made this, and its extendable format is a little unusual. So, PRs welcome.
var Unibabel =// Now you can use Unibabel like the original docs below.
The original Unibabel fails hard when loaded into node.js, but this one doesn't. That's because I unit-test some things that include this in a node environment, even when not using it. I think the hard failure was a little extreme.
To reiterate the error: You shouldn't use this in node.js, you have Buffers in node.js, and they do this all better.
Original Unibabel Docs
- TextEncoder / TextDecoder
- TextEncoderLite (based on Buffer)
- TextEncoderLite (based on text-encoding)
- Beatgammit's base64-js
Quick Note to Node.js Users
var buf = 'I ½ ♥ 💩' 'utf8';buf;buf;buf;buf;buf; // deprecated, do not use
// TypedArray <--> UTF8var uint8Array = Unibabel;var str = Unibabel;// TypedArray <--> Base64var base64 = Unibabelvar uint8Array = Unibabel
- utf8ToBuffer(utf8str) => array
- bufferToUtf8(array) => string
- utf8ToBase64(utf8str) => base64
- base64ToUtf8(base64) => string
- bufferToBase64(array) => base64
- base64ToBuffer(base64) => array
- hexToBuffer(hexstr) => array
- bufferToHex(array) => hexstr
- base32ToBuffer(b32str) => array
- bufferToBase32(array) => b32str
- utf8ToBinaryString(utf8str) => binstr
- binaryStringToUtf8(binstr) => utf8str
- bufferToBinaryString(buffer) => binstr
- binaryStringToBuffer(binstr) => array
// Base64var myArray = Unibabel; // "Base 64 \u2014 Mozilla Developer Network"var myBuffer = Unibabelbuffer; // "Base 64 \u2014 Mozilla Developer Network"console;// Crazy Unicodevar sMyInput = "I'm a ☢ ☃ that plays 𝄢 guitar and spea̧͈͖ks Ar̽̾̈́͒͑ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜!";var aMyUTF8Input = Unibabel;var sMyBase64 = Unibabel;;var aMyUTF8Output = Unibabel;var sMyOutput = Unibabel;;
unibabel.hex.jsare dual-licensed as Apache 2.0 and MIT.
unibabel.base32.jsis a modified version of thirty-two and is therefore licensed MIT.
Some parts of the code were taken from MDN, which Mozilla has licensed in the Public Domain, which means that I am at liberty to re-license my copy under the Apache 2 and MIT licenses.
The new implementation is binary compatible with node.js, TextEncoder, and other more-common UTF-8 encodings.
It is also based on DOM APIs which result in much less code and are still backwards compatible all the way back to IE6 (not on purpose, just that it happens to work).
This version was based on the work by good folks at the MDN, however, the UTF-8 conversion was not byte-compatible with other UTF-8 conversions (such as node.js and TextEncoder), so don't use it. See https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding