2019-10-03 01:28:01 -06:00
|
|
|
|
# Unibabel
|
2015-05-18 12:04:35 -06:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
Minimalistic **Base64**, **TypedArrays**, and **UTF-8** / **Unicode** conversions in Browser (and Node) JavaScript.
|
|
|
|
|
|
Optional add-on support for **hex** and **base32**.
|
2015-05-18 12:04:35 -06:00
|
|
|
|
|
2015-06-10 17:59:01 -06:00
|
|
|
|
See <https://coolaj86.com/articles/base64-unicode-utf-8-javascript-and-you/>
|
2015-05-18 12:04:35 -06:00
|
|
|
|
|
2015-06-12 22:41:06 -06:00
|
|
|
|
See also
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
- [TextEncoder](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encode) / [TextDecoder](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/decode)
|
|
|
|
|
|
- [DateView](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView)
|
|
|
|
|
|
- [text-encoding](https://github.com/inexorabletash/text-encoding)
|
|
|
|
|
|
- [TextEncoderLite (based on Buffer)](https://github.com/coolaj86/TextEncoderLite/tree/litest)
|
|
|
|
|
|
- [TextEncoderLite (based on text-encoding)](https://github.com/coolaj86/TextEncoderLite/tree/lite)
|
|
|
|
|
|
- [Beatgammit's base64-js](https://github.com/beatgammit/base64-js)
|
2015-06-12 22:41:06 -06:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
# Example (Unicode to ArrayBuffer):
|
2015-11-17 13:17:52 -08:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
```js
|
|
|
|
|
|
var unicodeString = "I'm a ☢ ☃ that plays 𝄢 guitar and spea̧͈͖ks Ar̽̾̈́͒͑ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜!";
|
|
|
|
|
|
var buf = Unibabel.utf8ToBuffer(unicodeString);
|
|
|
|
|
|
```
|
2017-12-04 14:00:43 -07:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
## Supported Environments
|
2015-11-17 13:17:52 -08:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
- VanillaJS
|
|
|
|
|
|
- Node.js
|
|
|
|
|
|
- CommonJS
|
|
|
|
|
|
- WebPack
|
|
|
|
|
|
- ... everything?
|
|
|
|
|
|
|
|
|
|
|
|
# Browser Usage
|
2015-11-17 13:17:52 -08:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
## CDN
|
2017-12-04 14:00:43 -07:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
- <https://unpkg.com/unibabel@3.0.0/dist/unibabel.all.js>
|
|
|
|
|
|
- <https://unpkg.com/unibabel@3.0.0/dist/unibabel.all.min.js>
|
|
|
|
|
|
|
|
|
|
|
|
## Bower
|
2017-12-04 14:00:43 -07:00
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
bower install --save unibabel
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```html
|
2019-10-03 01:28:01 -06:00
|
|
|
|
<script src="/bower_components/unibabel/dist/unibabel.all.js"></script>
|
2017-12-04 14:00:43 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
# Node Usage
|
|
|
|
|
|
|
|
|
|
|
|
You don't _need_ this module, as you already have
|
|
|
|
|
|
[`Buffer`](https://nodejs.org/api/buffer.html) and [`thirty-two`](https://github.com/chrisumbel/thirty-two):
|
2017-12-04 14:00:43 -07:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
For example:
|
2017-12-04 14:00:43 -07:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
```javascript
|
|
|
|
|
|
var buf = Buffer.from('I ½ ♥ 💩', 'utf8');
|
|
|
|
|
|
buf.toString('hex');
|
|
|
|
|
|
buf.toString('base64');
|
|
|
|
|
|
buf.toString('ascii');
|
|
|
|
|
|
buf.toString('utf8');
|
|
|
|
|
|
buf.toString('binary'); // deprecated, do not use
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
However, you _can_ use it (perhaps for isomorphic js?)
|
|
|
|
|
|
|
|
|
|
|
|
# API
|
2015-05-20 02:53:52 -06:00
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
|
// TypedArray <--> UTF8
|
|
|
|
|
|
var uint8Array = Unibabel.strToUtf8Arr(str);
|
|
|
|
|
|
var str = Unibabel.utf8ArrToStr(uint8Array);
|
|
|
|
|
|
|
|
|
|
|
|
// TypedArray <--> Base64
|
2019-10-03 01:28:01 -06:00
|
|
|
|
var base64 = Unibabel.arrToBase64(uint8Array);
|
|
|
|
|
|
var uint8Array = Unibabel.base64ToArr(base64);
|
2015-05-20 02:53:52 -06:00
|
|
|
|
```
|
|
|
|
|
|
|
2015-06-10 17:59:01 -06:00
|
|
|
|
**Normal APIs**
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
`unibabel.js`
|
2015-11-17 13:52:54 -08:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
- utf8ToBuffer(utf8str) => array
|
|
|
|
|
|
- bufferToUtf8(array) => string
|
|
|
|
|
|
- utf8ToBase64(utf8str) => base64
|
|
|
|
|
|
- base64ToUtf8(base64) => string
|
|
|
|
|
|
- bufferToBase64(array) => base64
|
|
|
|
|
|
- base64ToBuffer(base64) => array
|
2015-06-10 17:59:01 -06:00
|
|
|
|
|
2015-06-12 22:41:06 -06:00
|
|
|
|
**Hex APIs**
|
|
|
|
|
|
|
|
|
|
|
|
`unibabel.hex.js`
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
- hexToBuffer(hexstr) => array
|
|
|
|
|
|
- bufferToHex(array) => hexstr
|
2015-06-12 22:41:06 -06:00
|
|
|
|
|
2015-10-22 15:13:46 -07:00
|
|
|
|
**Base32 APIs**
|
|
|
|
|
|
|
|
|
|
|
|
`unibabel.base32.js`
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
- base32ToBuffer(b32str) => array
|
|
|
|
|
|
- bufferToBase32(array) => b32str
|
2015-10-22 15:13:46 -07:00
|
|
|
|
|
2015-06-10 17:59:01 -06:00
|
|
|
|
**Helper APIs**
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
- utf8ToBinaryString(utf8str) => binstr
|
|
|
|
|
|
- binaryStringToUtf8(binstr) => utf8str
|
|
|
|
|
|
- bufferToBinaryString(buffer) => binstr
|
|
|
|
|
|
- binaryStringToBuffer(binstr) => array
|
2015-06-10 17:59:01 -06:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
# Examples
|
2015-05-18 12:11:00 -06:00
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
|
// Base64
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
var myArray = Unibabel.base64ToArr(
|
|
|
|
|
|
'QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw=='
|
|
|
|
|
|
); // "Base 64 \u2014 Mozilla Developer Network"
|
|
|
|
|
|
var myBuffer = Unibabel.base64ToArr(
|
|
|
|
|
|
'QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw=='
|
|
|
|
|
|
).buffer; // "Base 64 \u2014 Mozilla Developer Network"
|
2015-05-18 12:11:00 -06:00
|
|
|
|
|
|
|
|
|
|
console.log(myBuffer.byteLength);
|
|
|
|
|
|
|
|
|
|
|
|
// Crazy Unicode
|
|
|
|
|
|
|
|
|
|
|
|
var sMyInput = "I'm a ☢ ☃ that plays 𝄢 guitar and spea̧͈͖ks Ar̽̾̈́͒͑ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜!";
|
2015-05-20 02:53:52 -06:00
|
|
|
|
var aMyUTF8Input = Unibabel.strToUtf8Arr(sMyInput);
|
|
|
|
|
|
var sMyBase64 = Unibabel.arrToBase64(aMyUTF8Input);
|
2015-05-18 12:11:00 -06:00
|
|
|
|
|
|
|
|
|
|
alert(sMyBase64);
|
|
|
|
|
|
|
2015-05-20 02:53:52 -06:00
|
|
|
|
var aMyUTF8Output = Unibabel.base64ToArr(sMyBase64);
|
|
|
|
|
|
var sMyOutput = Unibabel.utf8ArrToStr(aMyUTF8Output);
|
2015-05-18 12:11:00 -06:00
|
|
|
|
|
|
|
|
|
|
alert(sMyOutput);
|
|
|
|
|
|
```
|
2015-05-18 12:04:35 -06:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
# License
|
2015-05-18 12:04:35 -06:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
- `index.js` and `unibabel.hex.js` are dual-licensed as Apache 2.0 and MIT.
|
|
|
|
|
|
- `unibabel.base32.js` is a modified version of [thirty-two](https://github.com/chrisumbel/thirty-two) and is therefore licensed MIT.
|
2015-10-22 15:13:46 -07:00
|
|
|
|
|
|
|
|
|
|
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.
|
2015-05-18 12:11:00 -06:00
|
|
|
|
|
|
|
|
|
|
See <https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses>
|
2015-06-10 17:59:01 -06:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
# ChangeLog
|
|
|
|
|
|
|
|
|
|
|
|
## v3.0.0
|
|
|
|
|
|
|
|
|
|
|
|
- Proper node support (`unibabel.node.js`)
|
|
|
|
|
|
- Various minor updates
|
2015-06-10 17:59:01 -06:00
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
## v2.1.0
|
2015-10-22 15:13:46 -07:00
|
|
|
|
|
|
|
|
|
|
Added `unibabel.base32.js`
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
## v2.0.0
|
2015-06-10 17:59:01 -06:00
|
|
|
|
|
|
|
|
|
|
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/>
|
|
|
|
|
|
|
2019-10-03 01:28:01 -06:00
|
|
|
|
## v1.0.0
|
2015-06-10 17:59:01 -06:00
|
|
|
|
|
|
|
|
|
|
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>
|