Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug with trailing nulls; moved to pakmanager; fixed up some whi…

…tespace
  • Loading branch information...
commit 208250b97123c60b4a7fb098025582911b4bc33d 1 parent 5f273c7
@beatgammit authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+node_modules
+pakmanaged*
View
6 README.md
@@ -1,7 +1,7 @@
Intro
=====
-toDataURL takes whatever you give it (assuming it's supported) and turns it into something you can download.
+`toDataURL` takes whatever you give it (assuming it's supported) and turns it into something you can download.
Supported inputs are:
@@ -9,6 +9,8 @@ Supported inputs are:
* Array- takes Array of numbers and assumes they're bytes (converts to Uint8Array internally)
* Uint8Array- uses a home-grown base64 conversion function
+`toDataURL` works with pakmanager and probably any other CommonJS compliant browser package builders.
+
Exported Methods
----------------
@@ -18,4 +20,4 @@ Exported Methods
* download- optional; defaults to false; this will automagically download the data if true
* openDataURL- takes up to two arguments
* data- the dataURL output from toDataURL
- * name- name of the window to open it in (defaults to _self)
+ * name- name of the window to open it in (defaults to `_self`)
View
7 configure
@@ -1,8 +1,5 @@
#/bin/sh
-if [ ! -d "js" ]
-then
- mkdir js
-fi
cd js
-ender build jeesh toDataURL
+pakmanager build
+cd -
View
42 index.html
@@ -1,47 +1,7 @@
<html>
<head>
<title>Download Stuff</title>
-<script src='js/ender.js'></script>
-<script>
-(function () {
- var $ = require('ender');
-
- $.domReady(function () {
- $('button').bind('click', function () {
- var a = $.toDataURL($('textarea').val()),
- uint8, i;
-
- console.log('As string:');
- console.log('DataURL:', a);
- console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
- console.log('');
-
- a = Array.prototype.slice.call($('textarea').val()).map(function (item) {
- return item.charCodeAt(0);
- });
- a = $.toDataURL(a);
- console.log('As byte array:');
- console.log('DataURL:', a);
- console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
- console.log('');
-
- a = $('textarea').val();
- uint8 = new Uint8Array(a.length);
- for (i = uint8.length; i >= 0; i -= 1) {
- uint8[i] = a.charCodeAt(i);
- }
- a = $.toDataURL(uint8);
- console.log('As Uint8Array:');
- console.log('DataURL:', a);
- console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
-
- if (confirm ('Would you like to download your text?')) {
- $.openDataURL(a);
- }
- });
- });
-}());
-</script>
+<script src='js/pakmanaged.js'></script>
</head>
<body>
View
39 js/main.js
@@ -0,0 +1,39 @@
+(function () {
+ var $ = require('ender'),
+ toDataURL = require('../src/download');
+
+ $.domReady(function () {
+ $('button').bind('click', function () {
+ var a = $.toDataURL($('textarea').val()),
+ uint8, i;
+
+ console.log('As string:');
+ console.log('DataURL:', a);
+ console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
+ console.log('');
+
+ a = Array.prototype.slice.call($('textarea').val()).map(function (item) {
+ return item.charCodeAt(0);
+ });
+ a = $.toDataURL(a);
+ console.log('As byte array:');
+ console.log('DataURL:', a);
+ console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
+ console.log('');
+
+ a = $('textarea').val();
+ uint8 = new Uint8Array(a.length);
+ for (i = uint8.length; i >= 0; i -= 1) {
+ uint8[i] = a.charCodeAt(i);
+ }
+ a = $.toDataURL(uint8);
+ console.log('As Uint8Array:');
+ console.log('DataURL:', a);
+ console.log('Decoded text:', atob(a.match(/base64,(.*)/)[1]));
+
+ if (confirm ('Would you like to download your text?')) {
+ $.openDataURL(a);
+ }
+ });
+ });
+}());
View
13 js/package.json
@@ -0,0 +1,13 @@
+{
+ "author": "",
+ "name": "js",
+ "version": "0.0.0",
+ "main": "./main.js",
+ "dependencies": {
+ "qwery": "~2",
+ "bonzo": "*",
+ "bean": "*",
+ "domready": "*"
+ },
+ "devDependencies": {}
+}
View
8 package.json
@@ -2,12 +2,12 @@
"author": "T. Jameson Little <t.jameson.little@gmail.com>",
"name": "toDataURL",
"description": "Download something in the browser as a file",
- "keywords": ["ender", "dataURL", "download"],
- "version": "0.0.1",
+ "keywords": ["dataURL", "download"],
+ "version": "0.1.0",
"repository": {
- "type": "git",
+ "type": "git",
"url": "https://github.com/beatgammit/toDataURL.git"
},
"dependencies": {},
- "main": "src/download.js"
+ "main": "src/download.js"
}
View
185 src/download.js
@@ -1,90 +1,99 @@
(function (exports) {
- 'use strict';
-
- var lookup = [
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
- 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
- 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
- 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
- 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
- 'w', 'x', 'y', 'z', '0', '1', '2', '3',
- '4', '5', '6', '7', '8', '9', '+', '/'
- ];
-
- function uint8ToBase64(uint8) {
- var i,
- extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
- output = "",
- temp, length;
-
- function tripletToBase64 (num) {
- return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F];
- };
-
- // go through the array every three bytes, we'll deal with trailing stuff later
- for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
- temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
- output += tripletToBase64(temp);
- }
-
- // this prevents an ERR_INVALID_URL in Chrome (Firefox okay)
- switch (output.length % 4) {
- case 1:
- output += '=';
- break;
- case 2:
- output += '==';
- break;
- default:
- break;
- }
-
- return output;
- }
-
- function openDataURL(dataURI, name) {
- window.open(dataURI, name || '_self');
- }
-
- function createDataURL (data, mimeType, autoDownload) {
- var tmp, base64, dataURI, i;
-
- if (!data || (typeof data !== 'string' && !(data instanceof Array) && !(data instanceof Uint8Array))) {
- throw 'Data must be a non-empty string, Array, or Uint8Array';
- }
-
- if (typeof mimeType !== 'string') {
- autoDownload = mimeType;
- mimeType = 'application/octet-stream';
- }
- if (typeof autoDownload !== 'boolean') {
- autoDownload = false;
- }
-
- if (typeof data === 'string') {
- tmp = encodeURIComponent(data);
- tmp = unescape(tmp || data.toString());
- base64 = btoa(tmp);
- } else if (data instanceof Array) {
- tmp = new Uint8Array(data.length);
- for (i = data.length; i >= 0; i -= 1) {
- tmp[i] = data[i] & 0xFF;
- }
- base64 = uint8ToBase64(tmp);
- } else if (data instanceof Uint8Array) {
- base64 = uint8ToBase64(data);
- }
-
- dataURI = 'data:' + mimeType + ';base64,' + base64;
-
- if (autoDownload) {
- openDataURL(dataURI);
- }
-
- return dataURI;
- }
-
- exports.toDataURL = createDataURL;
- exports.openDataURL = openDataURL;
+ 'use strict';
+
+ var lookup = [
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/'
+ ];
+
+ function uint8ToBase64(uint8) {
+ var i,
+ extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
+ output = "",
+ temp, length;
+
+ function tripletToBase64 (num) {
+ return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F];
+ };
+
+ // go through the array every three bytes, we'll deal with trailing stuff later
+ for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
+ temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
+ output += tripletToBase64(temp);
+ }
+
+ switch (extraBytes) {
+ case 1:
+ output += 'AA';
+ break;
+ case 2:
+ output += 'A';
+ break;
+ }
+
+ // this prevents an ERR_INVALID_URL in Chrome (Firefox okay)
+ switch (output.length % 4) {
+ case 1:
+ output += '=';
+ break;
+ case 2:
+ output += '==';
+ break;
+ default:
+ break;
+ }
+
+ return output;
+ }
+
+ function openDataURL(dataURI, name) {
+ window.open(dataURI, name || '_self');
+ }
+
+ function createDataURL (data, mimeType, autoDownload) {
+ var tmp, base64, dataURI, i;
+
+ if (!data || (typeof data !== 'string' && !(data instanceof Array) && !(data instanceof Uint8Array))) {
+ throw 'Data must be a non-empty string, Array, or Uint8Array';
+ }
+
+ if (typeof mimeType !== 'string') {
+ autoDownload = mimeType;
+ mimeType = 'application/octet-stream';
+ }
+ if (typeof autoDownload !== 'boolean') {
+ autoDownload = false;
+ }
+
+ if (typeof data === 'string') {
+ tmp = encodeURIComponent(data);
+ tmp = unescape(tmp || data.toString());
+ base64 = btoa(tmp);
+ } else if (data instanceof Array) {
+ tmp = new Uint8Array(data.length);
+ for (i = data.length; i >= 0; i -= 1) {
+ tmp[i] = data[i] & 0xFF;
+ }
+ base64 = uint8ToBase64(tmp);
+ } else if (data instanceof Uint8Array) {
+ base64 = uint8ToBase64(data);
+ }
+
+ dataURI = 'data:' + mimeType + ';base64,' + base64;
+
+ if (autoDownload) {
+ openDataURL(dataURI);
+ }
+
+ return dataURI;
+ }
+
+ exports.toDataURL = createDataURL;
+ exports.openDataURL = openDataURL;
}(typeof module !== 'undefined' ? module.exports : window));
Please sign in to comment.
Something went wrong with that request. Please try again.