Comparing version 0.0.5 to 0.0.6
@@ -19,21 +19,21 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
var code = elt.charCodeAt(0) | ||
if(code === PLUS) | ||
if (code === PLUS) | ||
return 62 // '+' | ||
if(code === SLASH) | ||
if (code === SLASH) | ||
return 63 // '/' | ||
if(code < NUMBER) | ||
if (code < NUMBER) | ||
return -1 //no match | ||
if(code < NUMBER + 10) | ||
if (code < NUMBER + 10) | ||
return code - NUMBER + 26 + 26 | ||
if(code < UPPER + 26) | ||
if (code < UPPER + 26) | ||
return code - UPPER | ||
if(code < LOWER + 26) | ||
if (code < LOWER + 26) | ||
return code - LOWER + 26 | ||
} | ||
function b64ToByteArray(b64) { | ||
var i, j, l, tmp, placeHolders, arr; | ||
function b64ToByteArray (b64) { | ||
var i, j, l, tmp, placeHolders, arr | ||
if (b64.length % 4 > 0) { | ||
throw 'Invalid string. Length must be a multiple of 4'; | ||
throw new Error('Invalid string. Length must be a multiple of 4') | ||
} | ||
@@ -50,10 +50,7 @@ | ||
// base64 is 4/3 + up to two characters of the original data | ||
arr = new Arr(b64.length * 3 / 4 - placeHolders); | ||
arr = new Arr(b64.length * 3 / 4 - placeHolders) | ||
// if there are placeholders, only get up to the last complete 4 chars | ||
l = placeHolders > 0 ? b64.length - 4 : b64.length | ||
l = placeHolders > 0 ? b64.length - 4 : b64.length; | ||
var L = 0 | ||
@@ -66,25 +63,25 @@ | ||
for (i = 0, j = 0; i < l; i += 4, j += 3) { | ||
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3)); | ||
push((tmp & 0xFF0000) >> 16); | ||
push((tmp & 0xFF00) >> 8); | ||
push(tmp & 0xFF); | ||
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3)) | ||
push((tmp & 0xFF0000) >> 16) | ||
push((tmp & 0xFF00) >> 8) | ||
push(tmp & 0xFF) | ||
} | ||
if (placeHolders === 2) { | ||
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4); | ||
push(tmp & 0xFF); | ||
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4) | ||
push(tmp & 0xFF) | ||
} else if (placeHolders === 1) { | ||
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2); | ||
push((tmp >> 8) & 0xFF); | ||
push(tmp & 0xFF); | ||
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2) | ||
push((tmp >> 8) & 0xFF) | ||
push(tmp & 0xFF) | ||
} | ||
return arr; | ||
return arr | ||
} | ||
function uint8ToBase64(uint8) { | ||
function uint8ToBase64 (uint8) { | ||
var i, | ||
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes | ||
output = "", | ||
temp, length; | ||
temp, length | ||
@@ -96,9 +93,9 @@ function encode (num) { | ||
function tripletToBase64 (num) { | ||
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F); | ||
}; | ||
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(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); | ||
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) | ||
output += tripletToBase64(temp) | ||
} | ||
@@ -109,22 +106,21 @@ | ||
case 1: | ||
temp = uint8[uint8.length - 1]; | ||
output += encode(temp >> 2); | ||
output += encode((temp << 4) & 0x3F); | ||
output += '=='; | ||
break; | ||
temp = uint8[uint8.length - 1] | ||
output += encode(temp >> 2) | ||
output += encode((temp << 4) & 0x3F) | ||
output += '==' | ||
break | ||
case 2: | ||
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]); | ||
output += encode(temp >> 10); | ||
output += encode((temp >> 4) & 0x3F); | ||
output += encode((temp << 2) & 0x3F); | ||
output += '='; | ||
break; | ||
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]) | ||
output += encode(temp >> 10) | ||
output += encode((temp >> 4) & 0x3F) | ||
output += encode((temp << 2) & 0x3F) | ||
output += '=' | ||
break | ||
} | ||
return output; | ||
return output | ||
} | ||
module.exports.toByteArray = b64ToByteArray; | ||
module.exports.fromByteArray = uint8ToBase64; | ||
}()); | ||
module.exports.toByteArray = b64ToByteArray | ||
module.exports.fromByteArray = uint8ToBase64 | ||
}()) |
@@ -5,3 +5,3 @@ { | ||
"description": "Base64 encoding/decoding in pure JS", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -1,4 +0,6 @@ | ||
Intro | ||
===== | ||
base64-js | ||
========= | ||
`base64-js` does basic base64 encoding/decoding in pure JS. | ||
[![build status](https://secure.travis-ci.org/beatgammit/base64-js.png)](http://travis-ci.org/beatgammit/base64-js) | ||
@@ -8,12 +10,23 @@ | ||
`base64-js` does basic base64 encoding/decoding in pure JS. Many browsers already have this functionality, but it is for text data, not all-purpose binary data. | ||
Many browsers already have base64 encoding/decoding functionality, but it is for text data, not all-purpose binary data. | ||
Sometimes encoding/decoding binary data in the browser is useful, and that is what this module does. | ||
API | ||
=== | ||
## install | ||
`base64-js` has two exposed functions, `toByteArray` and `fromByteArray`, which both take a single argument. | ||
With [npm](https://npmjs.org) do: | ||
* toByteArray- Takes a base64 string and returns a byte array | ||
* fromByteArray- Takes a byte array and returns a base64 string | ||
`npm install base64-js` | ||
## methods | ||
`var base64 = require('base64-js')` | ||
`base64` has two exposed functions, `toByteArray` and `fromByteArray`, which both take a single argument. | ||
* `toByteArray` - Takes a base64 string and returns a byte array | ||
* `fromByteArray` - Takes a byte array and returns a base64 string | ||
## license | ||
MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7791
31
0