Jameson Little
11 years ago
5 changed files with 88 additions and 82 deletions
@ -1,20 +1,34 @@ |
|||
{ |
|||
"author": "T. Jameson Little <t.jameson.little@gmail.com>", |
|||
"name": "base64-js", |
|||
"description": "Base64 encoding/decoding in pure JS", |
|||
"version": "0.0.4", |
|||
"repository": { |
|||
"type": "git", |
|||
"url": "git://github.com/beatgammit/base64-js.git" |
|||
}, |
|||
"main": "lib/b64.js", |
|||
"scripts": { |
|||
"test": "cd test; node runner.js; cd -" |
|||
}, |
|||
"engines": { |
|||
"node": ">= 0.4" |
|||
}, |
|||
"license": "MIT", |
|||
"dependencies": {}, |
|||
"devDependencies": {} |
|||
"author": "T. Jameson Little <t.jameson.little@gmail.com>", |
|||
"name": "base64-js", |
|||
"description": "Base64 encoding/decoding in pure JS", |
|||
"version": "0.0.4", |
|||
"repository": { |
|||
"type": "git", |
|||
"url": "git://github.com/beatgammit/base64-js.git" |
|||
}, |
|||
"main": "lib/b64.js", |
|||
"scripts": { |
|||
"test": "tape test/*.js" |
|||
}, |
|||
"testling": { |
|||
"files": "test/*.js", |
|||
"browsers": [ |
|||
"ie/6..latest", |
|||
"chrome/4..latest", |
|||
"firefox/3..latest", |
|||
"safari/5.1..latest", |
|||
"opera/11.0..latest", |
|||
"iphone/6", |
|||
"ipad/6" |
|||
] |
|||
}, |
|||
"engines": { |
|||
"node": ">= 0.4" |
|||
}, |
|||
"license": "MIT", |
|||
"dependencies": {}, |
|||
"devDependencies": { |
|||
"tape": "~2.3.2" |
|||
} |
|||
} |
|||
|
@ -0,0 +1,52 @@ |
|||
var test = require('tape'), |
|||
b64 = require('../lib/b64'), |
|||
checks = [ |
|||
'a', |
|||
'aa', |
|||
'aaa', |
|||
'hi', |
|||
'hi!', |
|||
'hi!!', |
|||
'sup', |
|||
'sup?', |
|||
'sup?!' |
|||
], |
|||
res; |
|||
|
|||
test('convert to base64 and back', function (t) { |
|||
t.plan(checks.length); |
|||
|
|||
for (var i = 0; i < checks.length; i++) { |
|||
var check = checks[i], |
|||
b64Str, |
|||
arr, |
|||
str; |
|||
|
|||
b64Str = b64.fromByteArray(map(check, function (char) { return char.charCodeAt(0); })); |
|||
|
|||
arr = b64.toByteArray(b64Str); |
|||
str = map(arr, function (byte) { return String.fromCharCode(byte); }).join(''); |
|||
|
|||
t.equal(check, str, 'Checked ' + check); |
|||
} |
|||
|
|||
}); |
|||
|
|||
function map (arr, callback) { |
|||
var res = [], |
|||
kValue, |
|||
mappedValue; |
|||
|
|||
for (var k = 0, len = arr.length; k < len; k++) { |
|||
if ((typeof arr === 'string' && !!arr.charAt(k))) { |
|||
kValue = arr.charAt(k); |
|||
mappedValue = callback(kValue, k, arr); |
|||
res[k] = mappedValue; |
|||
} else if (typeof arr !== 'string' && k in arr) { |
|||
kValue = arr[k]; |
|||
mappedValue = callback(kValue, k, arr); |
|||
res[k] = mappedValue; |
|||
} |
|||
} |
|||
return res; |
|||
} |
@ -1,64 +0,0 @@ |
|||
(function () { |
|||
'use strict'; |
|||
|
|||
var b64 = require('../lib/b64'), |
|||
checks = [ |
|||
'a', |
|||
'aa', |
|||
'aaa', |
|||
'hi', |
|||
'hi!', |
|||
'hi!!', |
|||
'sup', |
|||
'sup?', |
|||
'sup?!' |
|||
], |
|||
res; |
|||
|
|||
res = some(checks, function (check) { |
|||
var b64Str, |
|||
arr, |
|||
str; |
|||
|
|||
b64Str = b64.fromByteArray(map(check, function (char) { return char.charCodeAt(0); })); |
|||
|
|||
arr = b64.toByteArray(b64Str); |
|||
str = map(arr, function (byte) { return String.fromCharCode(byte); }).join(''); |
|||
if (check !== str) { |
|||
console.log('Fail:', check); |
|||
console.log('Base64:', b64Str); |
|||
return true; |
|||
} |
|||
}); |
|||
|
|||
if (res) { |
|||
console.log('Test failed'); |
|||
} else { |
|||
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…
Reference in new issue