Base64, TypedArrays, and UTF-8 / Unicode conversions in ES5.1 JavaScript (Browser and Node.js)
Go to file
AJ ONeal f041dcd8bb bump 2015-06-27 02:27:20 -06:00
.gitignore Initial commit 2015-05-18 12:02:27 -06:00
LICENSE Initial commit 2015-05-18 12:02:27 -06:00
README.md note base64-js 2015-06-12 22:45:14 -06:00
bower.json bump 2015-06-27 02:27:20 -06:00
index.html add some tests 2015-06-10 19:35:51 -06:00
index.js fix binaryStringToBuffer 2015-06-27 02:11:59 -06:00
node-test.js add some tests 2015-06-10 19:35:51 -06:00
test.js add some tests 2015-06-10 19:35:51 -06:00
unibabel-dom.js v2.0 with proper encoding 2015-06-10 17:59:01 -06:00
unibabel-mozilla.js move index to unibabel-mozilla 2015-06-10 17:45:19 -06:00
unibabel.hex.js move index to unibabel-mozilla 2015-06-10 17:45:19 -06:00

README.md

Unibabel

Base64, TypedArrays, and UTF-8 / Unicode conversions in Browser (and Node) JavaScript

See https://coolaj86.com/articles/base64-unicode-utf-8-javascript-and-you/

See also

API

// TypedArray <--> UTF8
var uint8Array = Unibabel.strToUtf8Arr(str);
var str = Unibabel.utf8ArrToStr(uint8Array);

// TypedArray <--> Base64
var base64 = Unibabel.arrToBase64(uint8Array)
var uint8Array = Unibabel.base64ToArr(base64)

Normal APIs

  • utf8ToBuffer(utf8str) => array

  • bufferToUtf8(array) => string

  • utf8ToBase64(utf8str) => base64

  • base64ToUtf8(base64) => string

  • bufferToBase64(array) => base64

  • base64ToBuffer(base64) => array

Hex APIs

unibabel.hex.js

  • hexToBuffer(hexstr) => array
  • bufferToHex(array) => hexstr

Helper APIs

  • utf8ToBinaryString(utf8str) => binstr
  • binaryStringToUtf8(binstr) => utf8str
  • bufferToBinaryString(buffer) => binstr
  • binaryStringToBuffer(binstr) => array

Examples

// Base64

var myArray = Unibabel.base64ToArr("QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw=="); // "Base 64 \u2014 Mozilla Developer Network"
var myBuffer = Unibabel.base64ToArr("QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw==").buffer; // "Base 64 \u2014 Mozilla Developer Network"

console.log(myBuffer.byteLength);

// Crazy Unicode

var sMyInput = "I'm a ☢ ☃ that plays 𝄢 guitar and spea̧͈͖ks Ar̽̾̈́͒͑ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜!";
var aMyUTF8Input = Unibabel.strToUtf8Arr(sMyInput);
var sMyBase64 = Unibabel.arrToBase64(aMyUTF8Input);

alert(sMyBase64);

var aMyUTF8Output = Unibabel.base64ToArr(sMyBase64);
var sMyOutput = Unibabel.utf8ArrToStr(aMyUTF8Output);

alert(sMyOutput);

License

Mozilla has licensed this code in the Public Domain, which means that I am at liberty to re-license my copy under the Apache 2, which is something that, general speaking, your legal department will feel more comfortable with.

See https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses

ChangeLog

v2.0.0

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).

See https://coolaj86.com/articles/base64-unicode-utf-8-javascript-and-you/

v1.0.0

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