Browse Source

Merge pull request #10 from jessetane/decode-url-safe

Decode url-safe style base64
master
Feross Aboukhadijeh 10 years ago
parent
commit
c92cd303a8
  1. 8
      lib/b64.js
  2. 18
      test/url-safe.js

8
lib/b64.js

@ -12,12 +12,16 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var NUMBER = '0'.charCodeAt(0)
var LOWER = 'a'.charCodeAt(0)
var UPPER = 'A'.charCodeAt(0)
var PLUS_URL_SAFE = '-'.charCodeAt(0)
var SLASH_URL_SAFE = '_'.charCodeAt(0)
function decode (elt) {
var code = elt.charCodeAt(0)
if (code === PLUS)
if (code === PLUS ||
code === PLUS_URL_SAFE)
return 62 // '+'
if (code === SLASH)
if (code === SLASH ||
code === SLASH_URL_SAFE)
return 63 // '/'
if (code < NUMBER)
return -1 //no match

18
test/url-safe.js

@ -0,0 +1,18 @@
var test = require('tape'),
b64 = require('../lib/b64');
test('decode url-safe style base64 strings', function (t) {
var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff];
var actual = b64.toByteArray('//++/++/++//');
for (var i = 0; i < actual.length; i++) {
t.equal(actual[i], expected[i])
}
actual = b64.toByteArray('__--_--_--__');
for (var i = 0; i < actual.length; i++) {
t.equal(actual[i], expected[i])
}
t.end();
});
Loading…
Cancel
Save