Browse Source

Merge pull request #3 from feross/master

IE6-8 support
master
Jameson Little 11 years ago
parent
commit
4e049fc68f
  1. 39
      lib/b64.js
  2. 31
      test/runner.js

39
lib/b64.js

@ -15,7 +15,7 @@
// represent one byte
// if there is only one, then the three characters before it represent 2 bytes
// this is just a cheap hack to not do indexOf twice
placeHolders = b64.indexOf('=');
placeHolders = indexOf(b64, '=');
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0;
// base64 is 4/3 + up to two characters of the original data
@ -25,17 +25,17 @@
l = placeHolders > 0 ? b64.length - 4 : b64.length;
for (i = 0, j = 0; i < l; i += 4, j += 3) {
tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12) | (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]);
tmp = (indexOf(lookup, b64.charAt(i)) << 18) | (indexOf(lookup, b64.charAt(i + 1)) << 12) | (indexOf(lookup, b64.charAt(i + 2)) << 6) | indexOf(lookup, b64.charAt(i + 3));
arr.push((tmp & 0xFF0000) >> 16);
arr.push((tmp & 0xFF00) >> 8);
arr.push(tmp & 0xFF);
}
if (placeHolders === 2) {
tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4);
tmp = (indexOf(lookup, b64.charAt(i)) << 2) | (indexOf(lookup, b64.charAt(i + 1)) >> 4);
arr.push(tmp & 0xFF);
} else if (placeHolders === 1) {
tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4) | (lookup.indexOf(b64[i + 2]) >> 2);
tmp = (indexOf(lookup, b64.charAt(i)) << 10) | (indexOf(lookup, b64.charAt(i + 1)) << 4) | (indexOf(lookup, b64.charAt(i + 2)) >> 2);
arr.push((tmp >> 8) & 0xFF);
arr.push(tmp & 0xFF);
}
@ -50,7 +50,7 @@
temp, length;
function tripletToBase64 (num) {
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F];
return lookup.charAt(num >> 18 & 0x3F) + lookup.charAt(num >> 12 & 0x3F) + lookup.charAt(num >> 6 & 0x3F) + lookup.charAt(num & 0x3F);
};
// go through the array every three bytes, we'll deal with trailing stuff later
@ -63,15 +63,15 @@
switch (extraBytes) {
case 1:
temp = uint8[uint8.length - 1];
output += lookup[temp >> 2];
output += lookup[(temp << 4) & 0x3F];
output += lookup.charAt(temp >> 2);
output += lookup.charAt((temp << 4) & 0x3F);
output += '==';
break;
case 2:
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]);
output += lookup[temp >> 10];
output += lookup[(temp >> 4) & 0x3F];
output += lookup[(temp << 2) & 0x3F];
output += lookup.charAt(temp >> 10);
output += lookup.charAt((temp >> 4) & 0x3F);
output += lookup.charAt((temp << 2) & 0x3F);
output += '=';
break;
}
@ -82,3 +82,22 @@
module.exports.toByteArray = b64ToByteArray;
module.exports.fromByteArray = uint8ToBase64;
}());
function indexOf (arr, elt /*, from*/) {
var len = arr.length;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++) {
if ((typeof arr === 'string' && arr.charAt(from) === elt) ||
(typeof arr !== 'string' && arr[from] === elt)) {
return from;
}
}
return -1;
}

31
test/runner.js

@ -15,15 +15,15 @@
],
res;
res = checks.some(function (check) {
res = some(checks, function (check) {
var b64Str,
arr,
str;
b64Str = b64.fromByteArray(Array.prototype.map.call(check, function (char) { return char.charCodeAt(0); }));
b64Str = b64.fromByteArray(map(check, function (char) { return char.charCodeAt(0); }));
arr = b64.toByteArray(b64Str);
str = arr.map(function (byte) { return String.fromCharCode(byte); }).join('');
str = map(arr, function (byte) { return String.fromCharCode(byte); }).join('');
if (check !== str) {
console.log('Fail:', check);
console.log('Base64:', b64Str);
@ -37,3 +37,28 @@
console.log('All tests passed!');
}
}());
function some (arr, fun) {
for (var i = 0, len = arr.length; i < len; i++) {
if (i in arr && fun(arr[i], i, arr)) {
return true;
}
}
return false;
}
function map (arr, callback) {
var res = [];
for (var k = 0, len = arr.length; k < len; k++) {
if ((typeof arr === 'string' && !!arr.charAt(k))) {
var kValue = arr.charAt(k);
var mappedValue = callback(kValue, k, arr);
res[k] = mappedValue;
} else if (typeof arr !== 'string' && k in arr) {
var kValue = arr[k];
var mappedValue = callback(kValue, k, arr);
res[k] = mappedValue;
}
}
return res;
}
Loading…
Cancel
Save