Compare commits
	
		
			No commits in common. "master" and "v2.0.1" have entirely different histories.
		
	
	
		
	
		
							
								
								
									
										32
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								LICENSE
									
									
									
									
									
								
							@ -1,32 +1,3 @@
 | 
				
			|||||||
At your option you may choose either of the following licenses:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  * The MIT License (MIT)
 | 
					 | 
				
			||||||
  * The Apache License 2.0 (Apache-2.0)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
The MIT License (MIT)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Copyright (c) 2015 AJ ONeal
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					 | 
				
			||||||
of this software and associated documentation files (the "Software"), to deal
 | 
					 | 
				
			||||||
in the Software without restriction, including without limitation the rights
 | 
					 | 
				
			||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					 | 
				
			||||||
copies of the Software, and to permit persons to whom the Software is
 | 
					 | 
				
			||||||
furnished to do so, subject to the following conditions:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
The above copyright notice and this permission notice shall be included in all
 | 
					 | 
				
			||||||
copies or substantial portions of the Software.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					 | 
				
			||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					 | 
				
			||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					 | 
				
			||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					 | 
				
			||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					 | 
				
			||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
					 | 
				
			||||||
SOFTWARE.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                                 Apache License
 | 
					                                 Apache License
 | 
				
			||||||
                           Version 2.0, January 2004
 | 
					                           Version 2.0, January 2004
 | 
				
			||||||
                        http://www.apache.org/licenses/
 | 
					                        http://www.apache.org/licenses/
 | 
				
			||||||
@ -215,7 +186,7 @@ SOFTWARE.
 | 
				
			|||||||
      same "printed page" as the copyright notice for easier
 | 
					      same "printed page" as the copyright notice for easier
 | 
				
			||||||
      identification within third-party archives.
 | 
					      identification within third-party archives.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   Copyright 2015 AJ ONeal
 | 
					   Copyright {yyyy} {name of copyright owner}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   Licensed under the Apache License, Version 2.0 (the "License");
 | 
					   Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
   you may not use this file except in compliance with the License.
 | 
					   you may not use this file except in compliance with the License.
 | 
				
			||||||
@ -228,3 +199,4 @@ SOFTWARE.
 | 
				
			|||||||
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
   See the License for the specific language governing permissions and
 | 
					   See the License for the specific language governing permissions and
 | 
				
			||||||
   limitations under the License.
 | 
					   limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										62
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								README.md
									
									
									
									
									
								
							@ -1,7 +1,7 @@
 | 
				
			|||||||
Unibabel
 | 
					Unibabel
 | 
				
			||||||
========
 | 
					==========
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Minimalistic **Base64**, **TypedArrays**, and **UTF-8** / **Unicode** conversions in Browser (and Node) JavaScript. Optional add-on support for **hex** and **base32**.
 | 
					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 <https://coolaj86.com/articles/base64-unicode-utf-8-javascript-and-you/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -14,41 +14,6 @@ See also
 | 
				
			|||||||
  * [TextEncoderLite (based on text-encoding)](https://github.com/coolaj86/TextEncoderLite/tree/lite)
 | 
					  * [TextEncoderLite (based on text-encoding)](https://github.com/coolaj86/TextEncoderLite/tree/lite)
 | 
				
			||||||
  * [Beatgammit's base64-js](https://github.com/beatgammit/base64-js)
 | 
					  * [Beatgammit's base64-js](https://github.com/beatgammit/base64-js)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Are you in the right place?
 | 
					 | 
				
			||||||
------------------------
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Dear Node.js Users:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
You SHOULD NOT use this module. You already have [`Buffer`](https://nodejs.org/api/buffer.html) and [`thirty-two`](https://github.com/chrisumbel/thirty-two):
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```javascript
 | 
					 | 
				
			||||||
var buf = new Buffer('I ½ ♥ 💩', 'utf8');
 | 
					 | 
				
			||||||
buf.toString('hex');
 | 
					 | 
				
			||||||
buf.toString('base64');
 | 
					 | 
				
			||||||
buf.toString('ascii');
 | 
					 | 
				
			||||||
buf.toString('utf8');
 | 
					 | 
				
			||||||
buf.toString('binary'); // deprecated, do not use
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Install
 | 
					 | 
				
			||||||
-------
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
You just include the `index.js` in a lovely script tag.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```bash
 | 
					 | 
				
			||||||
bower install --save unibabel
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```html
 | 
					 | 
				
			||||||
<script src="/bower_components/unibabel/index.js"></script>
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Dear WebPack / Broccoli, Gulp / Grunt Users:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
I don't know how your build systems work these days,
 | 
					 | 
				
			||||||
but Unibabel is exported as `Unibabel`
 | 
					 | 
				
			||||||
and you can access it as `window.Unibabel`.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
API
 | 
					API
 | 
				
			||||||
===
 | 
					===
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -64,12 +29,12 @@ var uint8Array = Unibabel.base64ToArr(base64)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
**Normal APIs**
 | 
					**Normal APIs**
 | 
				
			||||||
 | 
					
 | 
				
			||||||
`index.js`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
* utf8ToBuffer(utf8str) => array
 | 
					* utf8ToBuffer(utf8str) => array
 | 
				
			||||||
* bufferToUtf8(array) => string
 | 
					* bufferToUtf8(array) => string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* utf8ToBase64(utf8str) => base64
 | 
					* utf8ToBase64(utf8str) => base64
 | 
				
			||||||
* base64ToUtf8(base64) => string
 | 
					* base64ToUtf8(base64) => string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* bufferToBase64(array) => base64
 | 
					* bufferToBase64(array) => base64
 | 
				
			||||||
* base64ToBuffer(base64) => array
 | 
					* base64ToBuffer(base64) => array
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -80,13 +45,6 @@ var uint8Array = Unibabel.base64ToArr(base64)
 | 
				
			|||||||
* hexToBuffer(hexstr) => array
 | 
					* hexToBuffer(hexstr) => array
 | 
				
			||||||
* bufferToHex(array) => hexstr
 | 
					* bufferToHex(array) => hexstr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
**Base32 APIs**
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
`unibabel.base32.js`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
* base32ToBuffer(b32str) => array
 | 
					 | 
				
			||||||
* bufferToBase32(array) => b32str
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
**Helper APIs**
 | 
					**Helper APIs**
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* utf8ToBinaryString(utf8str) => binstr
 | 
					* utf8ToBinaryString(utf8str) => binstr
 | 
				
			||||||
@ -122,22 +80,14 @@ alert(sMyOutput);
 | 
				
			|||||||
License
 | 
					License
 | 
				
			||||||
=======
 | 
					=======
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* `index.js` and `unibabel.hex.js` are dual-licensed as Apache 2.0 and MIT.
 | 
					Mozilla has licensed this code in the Public Domain, which means that I am at liberty to re-license my copy
 | 
				
			||||||
* `unibabel.base32.js` is a modified version of [thirty-two](https://github.com/chrisumbel/thirty-two) and is therefore licensed MIT.
 | 
					under the Apache 2, which is something that, general speaking, your legal department will feel more comfortable with.
 | 
				
			||||||
 | 
					 | 
				
			||||||
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.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
See <https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses>
 | 
					See <https://developer.mozilla.org/en-US/docs/MDN/About#Copyrights_and_licenses>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ChangeLog
 | 
					ChangeLog
 | 
				
			||||||
====
 | 
					====
 | 
				
			||||||
 | 
					
 | 
				
			||||||
v2.1.0
 | 
					 | 
				
			||||||
------
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Added `unibabel.base32.js`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
v2.0.0
 | 
					v2.0.0
 | 
				
			||||||
------
 | 
					------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										15
									
								
								bower.json
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								bower.json
									
									
									
									
									
								
							@ -1,9 +1,10 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "unibabel",
 | 
					  "name": "unibabel",
 | 
				
			||||||
  "main": "index.js",
 | 
					  "main": "index.js",
 | 
				
			||||||
  "homepage": "https://git.coolaj86.com/coolaj86/unibabel.js",
 | 
					  "version": "2.0.1",
 | 
				
			||||||
 | 
					  "homepage": "https://github.com/coolaj86/unibabel-js",
 | 
				
			||||||
  "authors": [
 | 
					  "authors": [
 | 
				
			||||||
    "AJ ONeal <coolaj86@gmail.com>"
 | 
					    "AJ ONeal <awesome@coolaj86.com>"
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
  "description": "Convert between UTF8, TypedArrays, and Base64",
 | 
					  "description": "Convert between UTF8, TypedArrays, and Base64",
 | 
				
			||||||
  "moduleType": [
 | 
					  "moduleType": [
 | 
				
			||||||
@ -11,21 +12,15 @@
 | 
				
			|||||||
    "node"
 | 
					    "node"
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
  "keywords": [
 | 
					  "keywords": [
 | 
				
			||||||
    "ascii",
 | 
					 | 
				
			||||||
    "binary",
 | 
					 | 
				
			||||||
    "utf8",
 | 
					    "utf8",
 | 
				
			||||||
    "utf-8",
 | 
					    "utf-8",
 | 
				
			||||||
    "ArrayBuffer",
 | 
					    "ArrayBuffer",
 | 
				
			||||||
    "TypedArrays",
 | 
					    "TypedArrays",
 | 
				
			||||||
    "TypedArray",
 | 
					 | 
				
			||||||
    "Uint",
 | 
					    "Uint",
 | 
				
			||||||
    "Uint8Array",
 | 
					 | 
				
			||||||
    "Base64",
 | 
					    "Base64",
 | 
				
			||||||
    "b64",
 | 
					    "b64"
 | 
				
			||||||
    "Base32",
 | 
					 | 
				
			||||||
    "b32"
 | 
					 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
  "license": "Apache-2.0",
 | 
					  "license": "Apache2",
 | 
				
			||||||
  "ignore": [
 | 
					  "ignore": [
 | 
				
			||||||
    "**/.*",
 | 
					    "**/.*",
 | 
				
			||||||
    "node_modules",
 | 
					    "node_modules",
 | 
				
			||||||
 | 
				
			|||||||
@ -1,12 +0,0 @@
 | 
				
			|||||||
<html>
 | 
					 | 
				
			||||||
<head>
 | 
					 | 
				
			||||||
  <meta charset="UTF-8">
 | 
					 | 
				
			||||||
</head>
 | 
					 | 
				
			||||||
<body>
 | 
					 | 
				
			||||||
  This is for testing. Look in the console.
 | 
					 | 
				
			||||||
  <script src="../index.js"></script>
 | 
					 | 
				
			||||||
  <script src="../unibabel.hex.js"></script>
 | 
					 | 
				
			||||||
  <script src="../unibabel.base32.js"></script>
 | 
					 | 
				
			||||||
  <script src="./test.js"></script>
 | 
					 | 
				
			||||||
</body>
 | 
					 | 
				
			||||||
</html>
 | 
					 | 
				
			||||||
@ -1,8 +0,0 @@
 | 
				
			|||||||
var base32 = require('thirty-two');
 | 
					 | 
				
			||||||
var str = "I ½ ♥ 𩶘";
 | 
					 | 
				
			||||||
var buf = new Buffer(str, 'utf8');
 | 
					 | 
				
			||||||
console.log('charLen', 7);
 | 
					 | 
				
			||||||
console.log('byteLen', buf.byteLength, JSON.stringify(buf.toString('utf8')));
 | 
					 | 
				
			||||||
var b32 = base32.encode(buf); // to base32
 | 
					 | 
				
			||||||
console.log('encoded', b32.toString('utf8'));
 | 
					 | 
				
			||||||
console.log('decoded', base32.decode(b32).toString('utf8'));
 | 
					 | 
				
			||||||
@ -1,95 +0,0 @@
 | 
				
			|||||||
(function () {
 | 
					 | 
				
			||||||
'use strict';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var Unibabel = window.Unibabel;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//UTF-8
 | 
					 | 
				
			||||||
var pass = true;
 | 
					 | 
				
			||||||
var references = {
 | 
					 | 
				
			||||||
  string: "I ½ ♥ 𩶘"
 | 
					 | 
				
			||||||
, array: [ 73, 32, 194, 189, 32, 226, 153, 165, 32, 240, 169, 182, 152 ]
 | 
					 | 
				
			||||||
, hex: "4920c2bd20e299a520f0a9b698"
 | 
					 | 
				
			||||||
, base64: "SSDCvSDimaUg8Km2mA=="
 | 
					 | 
				
			||||||
, base32: 'JEQMFPJA4KM2KIHQVG3JQ==='
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
references.buffer = new Uint8Array(references.array);
 | 
					 | 
				
			||||||
var binrefs = {
 | 
					 | 
				
			||||||
  // Note that the binary string "ÿâó<86>Î<93>k" can't be serialized to text
 | 
					 | 
				
			||||||
  array: [ 255, 226, 26, 243, 134, 206, 147, 107 ]
 | 
					 | 
				
			||||||
, hex: "ffe21af386ce936b"
 | 
					 | 
				
			||||||
, base64: "/+Ia84bOk2s="
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
binrefs.buffer = new Uint8Array(binrefs.array);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var str = references.string;
 | 
					 | 
				
			||||||
var buf = Unibabel.utf8ToBuffer(references.string);
 | 
					 | 
				
			||||||
var base64 = Unibabel.bufferToBase64(references.buffer);
 | 
					 | 
				
			||||||
var hex = Unibabel.bufferToHex(references.buffer);
 | 
					 | 
				
			||||||
var b32 = Unibabel.bufferToBase32(references.buffer);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function buffersAreEqual(buf1, buf2) {
 | 
					 | 
				
			||||||
  if (buf1.length !== buf2.length) {
 | 
					 | 
				
			||||||
    return false;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  return Array.prototype.every.call(buf1, function (byte, i) {
 | 
					 | 
				
			||||||
    if (byte === buf2[i]) {
 | 
					 | 
				
			||||||
      return true;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// TODO compare buffer
 | 
					 | 
				
			||||||
if (!buffersAreEqual(buf, references.array)) {
 | 
					 | 
				
			||||||
  pass = false;
 | 
					 | 
				
			||||||
  console.warn('[FAIL] utf8 -> buffer', buf);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
if (base64 !== references.base64) {
 | 
					 | 
				
			||||||
  pass = false;
 | 
					 | 
				
			||||||
  console.warn('[FAIL] utf8 -> base64', base64);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
if (hex !== references.hex) {
 | 
					 | 
				
			||||||
  pass = false;
 | 
					 | 
				
			||||||
  console.warn('[FAIL] utf8 -> hex', hex);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// binary
 | 
					 | 
				
			||||||
var bytes = binrefs.array;
 | 
					 | 
				
			||||||
buf = new Uint8Array(bytes);
 | 
					 | 
				
			||||||
str = Unibabel.bufferToBinaryString(buf);
 | 
					 | 
				
			||||||
base64 = Unibabel.bufferToBase64(buf);
 | 
					 | 
				
			||||||
hex = Unibabel.bufferToHex(buf);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// This can't be properly tested because binary strings can't be parsed
 | 
					 | 
				
			||||||
// if (str !== "ÿâóÎk") {
 | 
					 | 
				
			||||||
//   pass = false;
 | 
					 | 
				
			||||||
//   console.log('[FAIL] binary -> str', str);
 | 
					 | 
				
			||||||
// }
 | 
					 | 
				
			||||||
if (binrefs.base64 !== base64) {
 | 
					 | 
				
			||||||
  pass = false;
 | 
					 | 
				
			||||||
  console.warn('[FAIL] binary -> base64', base64);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
if (binrefs.hex !== hex) {
 | 
					 | 
				
			||||||
  pass = false;
 | 
					 | 
				
			||||||
  console.warn('[FAIL] binary -> hex', hex);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// Base32
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
b32 = Unibabel.bufferToBase32(references.buffer);
 | 
					 | 
				
			||||||
if (references.base32 !== b32) {
 | 
					 | 
				
			||||||
  pass = false;
 | 
					 | 
				
			||||||
  console.warn('[FAIL] binary -> base32', references.base32, '!==', b32);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
buf = Unibabel.base32ToBuffer(references.base32);
 | 
					 | 
				
			||||||
if (!buffersAreEqual(buf, references.buffer)) {
 | 
					 | 
				
			||||||
  pass = false;
 | 
					 | 
				
			||||||
  console.warn('[FAIL] base32 -> binary', references.buffer, '!==', buf);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if (pass) {
 | 
					 | 
				
			||||||
  console.info('[PASS] :-D');
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}());
 | 
					 | 
				
			||||||
							
								
								
									
										8
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					<html>
 | 
				
			||||||
 | 
					  <script src="./index.js"></script>
 | 
				
			||||||
 | 
					  <script src="./unibabel.hex.js"></script>
 | 
				
			||||||
 | 
					  <script src="./test.js"></script>
 | 
				
			||||||
 | 
					<body>
 | 
				
			||||||
 | 
					  This is for testing. Look in the console.
 | 
				
			||||||
 | 
					</body>
 | 
				
			||||||
 | 
					</html>
 | 
				
			||||||
							
								
								
									
										12
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								index.js
									
									
									
									
									
								
							@ -1,4 +1,4 @@
 | 
				
			|||||||
(function (exports) {
 | 
					(function () {
 | 
				
			||||||
'use strict';
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function utf8ToBinaryString(str) {
 | 
					function utf8ToBinaryString(str) {
 | 
				
			||||||
@ -6,7 +6,7 @@ function utf8ToBinaryString(str) {
 | 
				
			|||||||
  // replaces any uri escape sequence, such as %0A,
 | 
					  // replaces any uri escape sequence, such as %0A,
 | 
				
			||||||
  // with binary escape, such as 0x0A
 | 
					  // with binary escape, such as 0x0A
 | 
				
			||||||
  var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
 | 
					  var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
 | 
				
			||||||
    return String.fromCharCode(parseInt(p1, 16));
 | 
					    return String.fromCharCode('0x' + p1);
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return binstr;
 | 
					  return binstr;
 | 
				
			||||||
@ -25,7 +25,7 @@ function utf8ToBase64(str) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
function binaryStringToUtf8(binstr) {
 | 
					function binaryStringToUtf8(binstr) {
 | 
				
			||||||
  var escstr = binstr.replace(/(.)/g, function (m, p) {
 | 
					  var escstr = binstr.replace(/(.)/g, function (m, p) {
 | 
				
			||||||
    var code = p.charCodeAt(0).toString(16).toUpperCase();
 | 
					    var code = p.charCodeAt(p).toString(16).toUpperCase();
 | 
				
			||||||
    if (code.length < 2) {
 | 
					    if (code.length < 2) {
 | 
				
			||||||
      code = '0' + code;
 | 
					      code = '0' + code;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -63,7 +63,7 @@ function bufferToBase64(arr) {
 | 
				
			|||||||
function binaryStringToBuffer(binstr) {
 | 
					function binaryStringToBuffer(binstr) {
 | 
				
			||||||
  var buf;
 | 
					  var buf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if ('undefined' !== typeof Uint8Array) {
 | 
					  if ('undefined' === typeof Uint8Array) {
 | 
				
			||||||
    buf = new Uint8Array(binstr.length);
 | 
					    buf = new Uint8Array(binstr.length);
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    buf = [];
 | 
					    buf = [];
 | 
				
			||||||
@ -82,7 +82,7 @@ function base64ToBuffer(base64) {
 | 
				
			|||||||
  return buf;
 | 
					  return buf;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.Unibabel = {
 | 
					window.Unibabel = {
 | 
				
			||||||
  utf8ToBinaryString: utf8ToBinaryString
 | 
					  utf8ToBinaryString: utf8ToBinaryString
 | 
				
			||||||
, utf8ToBuffer: utf8ToBuffer
 | 
					, utf8ToBuffer: utf8ToBuffer
 | 
				
			||||||
, utf8ToBase64: utf8ToBase64
 | 
					, utf8ToBase64: utf8ToBase64
 | 
				
			||||||
@ -101,4 +101,4 @@ exports.Unibabel = {
 | 
				
			|||||||
, base64ToArr: base64ToBuffer
 | 
					, base64ToArr: base64ToBuffer
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}('undefined' !== typeof exports && exports || 'undefined' !== typeof window && window || global));
 | 
					}());
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										19
									
								
								node.js
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								node.js
									
									
									
									
									
								
							@ -1,19 +0,0 @@
 | 
				
			|||||||
'use strict';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
console.warn("Please don't use Unibabel in node.js. If you think you really think you have a valid use case please report it at https://git.coolaj86.com/coolaj86/unibabel.js/issues/new");
 | 
					 | 
				
			||||||
throw new Error("[unibabel] you're doing it wrong");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
var data = 'I ½ ♥ 💩';
 | 
					 | 
				
			||||||
var encoding = 'utf8';
 | 
					 | 
				
			||||||
var buf = new Buffer(data, encoding);
 | 
					 | 
				
			||||||
buf.toString('hex');
 | 
					 | 
				
			||||||
buf.toString('base64');
 | 
					 | 
				
			||||||
buf.toString('ascii');
 | 
					 | 
				
			||||||
buf.toString('utf8');
 | 
					 | 
				
			||||||
buf.toString('binary'); // deprecated, do not use
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var Base32 = require('thirty-two');
 | 
					 | 
				
			||||||
var b32 = Base32.encode(buf);
 | 
					 | 
				
			||||||
Base32.decode(buf);
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
							
								
								
									
										37
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								package.json
									
									
									
									
									
								
							@ -1,37 +0,0 @@
 | 
				
			|||||||
{
 | 
					 | 
				
			||||||
  "name": "unibabel",
 | 
					 | 
				
			||||||
  "version": "2.1.8",
 | 
					 | 
				
			||||||
  "description": "Base64, TypedArrays, and UTF-8 / Unicode conversions in Browser (and Node) JavaScript",
 | 
					 | 
				
			||||||
  "main": "node.js",
 | 
					 | 
				
			||||||
  "browser": {
 | 
					 | 
				
			||||||
    "./node.js": "./index.js"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "scripts": {
 | 
					 | 
				
			||||||
    "test": "echo \"Error: no test specified\" && exit 1"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "repository": {
 | 
					 | 
				
			||||||
    "type": "git",
 | 
					 | 
				
			||||||
    "url": "https://git.coolaj86.com/coolaj86/unibabel.js.git"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "keywords": [
 | 
					 | 
				
			||||||
    "ascii",
 | 
					 | 
				
			||||||
    "binary",
 | 
					 | 
				
			||||||
    "utf8",
 | 
					 | 
				
			||||||
    "utf-8",
 | 
					 | 
				
			||||||
    "ArrayBuffer",
 | 
					 | 
				
			||||||
    "TypedArrays",
 | 
					 | 
				
			||||||
    "TypedArray",
 | 
					 | 
				
			||||||
    "Uint",
 | 
					 | 
				
			||||||
    "Uint8Array",
 | 
					 | 
				
			||||||
    "Base64",
 | 
					 | 
				
			||||||
    "b64",
 | 
					 | 
				
			||||||
    "Base32",
 | 
					 | 
				
			||||||
    "b32"
 | 
					 | 
				
			||||||
  ],
 | 
					 | 
				
			||||||
  "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
 | 
					 | 
				
			||||||
  "license": "(MIT OR Apache-2.0)",
 | 
					 | 
				
			||||||
  "bugs": {
 | 
					 | 
				
			||||||
    "url": "https://git.coolaj86.com/coolaj86/unibabel.js/issues"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "homepage": "https://git.coolaj86.com/coolaj86/unibabel.js#readme"
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										66
									
								
								test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								test.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,66 @@
 | 
				
			|||||||
 | 
					(function () {
 | 
				
			||||||
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var Unibabel = window.Unibabel;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//UTF-8
 | 
				
			||||||
 | 
					var pass = true;
 | 
				
			||||||
 | 
					var str = "I ½ ♥ 𩶘";
 | 
				
			||||||
 | 
					var buf = Unibabel.utf8ToBuffer(str);
 | 
				
			||||||
 | 
					var base64 = Unibabel.bufferToBase64(buf);
 | 
				
			||||||
 | 
					var hex = Unibabel.bufferToHex(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function buffersAreEqual(buf1, buf2) {
 | 
				
			||||||
 | 
					  if (buf1.length !== buf2.length) {
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return Array.prototype.every.call(buf1, function (byte, i) {
 | 
				
			||||||
 | 
					    if (byte === buf2[i]) {
 | 
				
			||||||
 | 
					      return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					console.log('buffer', buf);
 | 
				
			||||||
 | 
					// TODO compare buffer
 | 
				
			||||||
 | 
					var reference = [ 73, 32, 194, 189, 32, 226, 153, 165, 32, 240, 169, 182, 152 ];
 | 
				
			||||||
 | 
					if (!buffersAreEqual(buf, reference)) {
 | 
				
			||||||
 | 
					  pass = false;
 | 
				
			||||||
 | 
					  console.log('[FAIL] utf8 -> buffer', buf);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					if (base64 !== "SSDCvSDimaUg8Km2mA==") {
 | 
				
			||||||
 | 
					  pass = false;
 | 
				
			||||||
 | 
					  console.log('[FAIL] utf8 -> base64', base64);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					if (hex !== "4920c2bd20e299a520f0a9b698") {
 | 
				
			||||||
 | 
					  pass = false;
 | 
				
			||||||
 | 
					  console.log('[FAIL] utf8 -> hex', hex);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// binary
 | 
				
			||||||
 | 
					var bytes = [ 255, 226, 26, 243, 134, 206, 147, 107 ];
 | 
				
			||||||
 | 
					buf = new Uint8Array(bytes);
 | 
				
			||||||
 | 
					str = Unibabel.bufferToBinaryString(buf);
 | 
				
			||||||
 | 
					base64 = Unibabel.bufferToBase64(buf);
 | 
				
			||||||
 | 
					hex = Unibabel.bufferToHex(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// This can't be properly tested because binary strings can't be parsed
 | 
				
			||||||
 | 
					// if (str !== "ÿâóÎk") {
 | 
				
			||||||
 | 
					//   pass = false;
 | 
				
			||||||
 | 
					//   console.log('[FAIL] binary -> str', str);
 | 
				
			||||||
 | 
					// }
 | 
				
			||||||
 | 
					if ("/+Ia84bOk2s=" !== base64) {
 | 
				
			||||||
 | 
					  pass = false;
 | 
				
			||||||
 | 
					  console.log('[FAIL] binary -> base64', base64);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					if (hex !== "ffe21af386ce936b") {
 | 
				
			||||||
 | 
					  pass = false;
 | 
				
			||||||
 | 
					  console.log('[FAIL] binary -> hex', hex);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (pass) {
 | 
				
			||||||
 | 
					  console.log('[PASS] :-D');
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}());
 | 
				
			||||||
							
								
								
									
										1
									
								
								unibabel-dom.js
									
									
									
									
									
										Symbolic link
									
								
							
							
						
						
									
										1
									
								
								unibabel-dom.js
									
									
									
									
									
										Symbolic link
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					index.js
 | 
				
			||||||
@ -1,142 +0,0 @@
 | 
				
			|||||||
/*
 | 
					 | 
				
			||||||
Copyright (c) 2011, Chris Umbel
 | 
					 | 
				
			||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					 | 
				
			||||||
of this software and associated documentation files (the "Software"), to deal
 | 
					 | 
				
			||||||
in the Software without restriction, including without limitation the rights
 | 
					 | 
				
			||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					 | 
				
			||||||
copies of the Software, and to permit persons to whom the Software is
 | 
					 | 
				
			||||||
furnished to do so, subject to the following conditions:
 | 
					 | 
				
			||||||
The above copyright notice and this permission notice shall be included in
 | 
					 | 
				
			||||||
all copies or substantial portions of the Software.
 | 
					 | 
				
			||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					 | 
				
			||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					 | 
				
			||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					 | 
				
			||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					 | 
				
			||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					 | 
				
			||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					 | 
				
			||||||
THE SOFTWARE.
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
(function (exports) {
 | 
					 | 
				
			||||||
'use strict';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var charTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
 | 
					 | 
				
			||||||
var byteTable = [
 | 
					 | 
				
			||||||
    0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
 | 
					 | 
				
			||||||
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 | 
					 | 
				
			||||||
    0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
 | 
					 | 
				
			||||||
    0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
 | 
					 | 
				
			||||||
    0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
 | 
					 | 
				
			||||||
    0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
 | 
					 | 
				
			||||||
    0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
 | 
					 | 
				
			||||||
    0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
 | 
					 | 
				
			||||||
    0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
 | 
					 | 
				
			||||||
    0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff
 | 
					 | 
				
			||||||
];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function quintetCount(buff) {
 | 
					 | 
				
			||||||
    var quintets = Math.floor(buff.length / 5);
 | 
					 | 
				
			||||||
    return buff.length % 5 === 0 ? quintets: quintets + 1;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
exports.bufferToBase32 = function(plain) {
 | 
					 | 
				
			||||||
    // plain MUST come in either as Array or Uint8Array
 | 
					 | 
				
			||||||
    if('undefined' !== typeof Uint8Array) {
 | 
					 | 
				
			||||||
        if (!(plain instanceof Uint8Array)){
 | 
					 | 
				
			||||||
            plain = new Uint8Array(plain);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    var i = 0;
 | 
					 | 
				
			||||||
    var j = 0;
 | 
					 | 
				
			||||||
    var shiftIndex = 0;
 | 
					 | 
				
			||||||
    var digit = 0;
 | 
					 | 
				
			||||||
    var encoded = new Array(quintetCount(plain) * 8);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* byte by byte isn't as pretty as quintet by quintet but tests a bit
 | 
					 | 
				
			||||||
        faster. will have to revisit. */
 | 
					 | 
				
			||||||
    while(i < plain.length) {
 | 
					 | 
				
			||||||
        var current = plain[i];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if(shiftIndex > 3) {
 | 
					 | 
				
			||||||
            digit = current & (0xff >> shiftIndex);
 | 
					 | 
				
			||||||
            shiftIndex = (shiftIndex + 5) % 8;
 | 
					 | 
				
			||||||
            digit = (digit << shiftIndex) | ((i + 1 < plain.length) ?
 | 
					 | 
				
			||||||
                plain[i + 1] : 0) >> (8 - shiftIndex);
 | 
					 | 
				
			||||||
            i++;
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            digit = (current >> (8 - (shiftIndex + 5))) & 0x1f;
 | 
					 | 
				
			||||||
            shiftIndex = (shiftIndex + 5) % 8;
 | 
					 | 
				
			||||||
            if(shiftIndex === 0) { i++; }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        encoded[j] = charTable[digit];
 | 
					 | 
				
			||||||
        j++;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for(i = j; i < encoded.length; i++) {
 | 
					 | 
				
			||||||
        encoded[i] = '=';
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return encoded.join('');
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
exports.base32ToBuffer = function(encoded) {
 | 
					 | 
				
			||||||
    var shiftIndex = 0;
 | 
					 | 
				
			||||||
    var plainDigit = 0;
 | 
					 | 
				
			||||||
    var plainChar;
 | 
					 | 
				
			||||||
    var plainPos = 0;
 | 
					 | 
				
			||||||
    var len = Math.ceil(encoded.length * 5 / 8);
 | 
					 | 
				
			||||||
    var decoded;
 | 
					 | 
				
			||||||
    encoded = encoded.split('').map(function (ch) {
 | 
					 | 
				
			||||||
      return ch.charCodeAt(0);
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
    if('undefined' !== typeof Uint8Array) {
 | 
					 | 
				
			||||||
        encoded = new Uint8Array(encoded);
 | 
					 | 
				
			||||||
        decoded = new Uint8Array(len);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        decoded = new Array(len);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* byte by byte isn't as pretty as octet by octet but tests a bit
 | 
					 | 
				
			||||||
        faster. will have to revisit. */
 | 
					 | 
				
			||||||
    for(var i = 0; i < encoded.length; i++) {
 | 
					 | 
				
			||||||
        if(encoded[i] === 0x3d){ //'='
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        var encodedByte = encoded[i] - 0x30;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if(encodedByte < byteTable.length) {
 | 
					 | 
				
			||||||
            plainDigit = byteTable[encodedByte];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if(shiftIndex <= 3) {
 | 
					 | 
				
			||||||
                shiftIndex = (shiftIndex + 5) % 8;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if(shiftIndex === 0) {
 | 
					 | 
				
			||||||
                    plainChar |= plainDigit;
 | 
					 | 
				
			||||||
                    decoded[plainPos] = plainChar;
 | 
					 | 
				
			||||||
                    plainPos++;
 | 
					 | 
				
			||||||
                    plainChar = 0;
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    plainChar |= 0xff & (plainDigit << (8 - shiftIndex));
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                shiftIndex = (shiftIndex + 5) % 8;
 | 
					 | 
				
			||||||
                plainChar |= 0xff & (plainDigit >>> shiftIndex);
 | 
					 | 
				
			||||||
                decoded[plainPos] = plainChar;
 | 
					 | 
				
			||||||
                plainPos++;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                plainChar = 0xff & (plainDigit << (8 - shiftIndex));
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            throw new Error('Invalid input - it is not base32 encoded string');
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (decoded.slice) { // Array or TypedArray
 | 
					 | 
				
			||||||
      return decoded.slice(0, plainPos);
 | 
					 | 
				
			||||||
    } else { // Mobile Safari TypedArray
 | 
					 | 
				
			||||||
      return new Uint8Array(Array.prototype.slice.call(decoded, 0, plainPos));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}(window.Unibabel || window));
 | 
					 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user