@superhuman/fast64
Advanced tools
Comparing version 0.7.0 to 0.7.3
215
index.js
@@ -26,132 +26,121 @@ /* | ||
var B64 = { | ||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', | ||
lookup: null, | ||
rlookup: null, | ||
encode: function (s) { | ||
/* jshint bitwise:false */ | ||
var buffer = typeof s === 'string' ? B64.toUtf8(s) : s, | ||
position = -1, | ||
result, | ||
len, | ||
nan0, nan1, nan2; | ||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', | ||
// rlookup was created using LookupFactory.buildRlookup(false) | ||
rlookup: [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 61], | ||
// rlookup was created using LookupFactory.buildRlookup(true) | ||
urlrlookup: [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 61], | ||
// lookup was created using LookupFactory.buildLookup() | ||
lookup: [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, 62, null, 62, null, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, null, null, null, 64, null, null, null, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, null, null, null, null, 63, null, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, null, null], | ||
if (!B64.rlookup) { | ||
B64.rlookup = new Array(125); | ||
len = B64.alphabet.length; | ||
while (++position < len) | ||
B64.rlookup[position] = B64.alphabet.charCodeAt(position); | ||
position = -1; | ||
} | ||
encode: function (s, options) { | ||
var buffer = typeof s === 'string' ? B64.toUtf8(s) : s | ||
var position = -1 | ||
var len | ||
var result | ||
var rlookup | ||
var nan0, nan1, nan2 | ||
len = buffer.length; | ||
len = buffer.length | ||
result = new Uint8Array(new ArrayBuffer(Math.ceil(len / 3) * 4)); | ||
var i = 0; | ||
result = new Uint8Array(new ArrayBuffer(Math.ceil(len / 3) * 4)) | ||
rlookup = B64.rlookup | ||
if (options && options.url) { | ||
// spot the subtle difference :) | ||
result = new Uint8Array(new ArrayBuffer(Math.ceil(4 * len / 3))) | ||
rlookup = B64.urlrlookup | ||
} | ||
while (++position < len) { | ||
nan0 = buffer[position]; | ||
nan1 = buffer[++position]; | ||
result[i++] = B64.rlookup[nan0 >> 2]; | ||
result[i++] = B64.rlookup[((nan0 & 3) << 4) | (nan1 >> 4)]; | ||
if (isNaN(nan1)) { | ||
result[i++] = B64.rlookup[64]; | ||
result[i++] = B64.rlookup[64]; | ||
} else { | ||
nan2 = buffer[++position]; | ||
result[i++] = B64.rlookup[((nan1 & 15) << 2) | (nan2 >> 6)]; | ||
result[i++] = B64.rlookup[(isNaN(nan2)) ? 64 : nan2 & 63]; | ||
} | ||
} | ||
return new TextDecoder("utf8").decode(result); | ||
}, | ||
decode: function (s, options) { | ||
/* jshint bitwise:false */ | ||
s = s.replace(/\s/g, ''); | ||
if (s.length % 4) { | ||
throw new Error('InvalidLengthError: decode failed: The string to be decoded is not the correct length for a base64 encoded string.'); | ||
} | ||
if(/[^A-Za-z0-9+\/\-_=]/g.test(s)) { | ||
throw new Error('InvalidCharacterError: decode failed: The string contains characters invalid in a base64 encoded string.'); | ||
} | ||
var i = 0 | ||
while (++position < len) { | ||
nan0 = buffer[position] | ||
nan1 = buffer[++position] | ||
result[i++] = rlookup[nan0 >> 2] | ||
result[i++] = rlookup[((nan0 & 3) << 4) | (nan1 >> 4)] | ||
if (isNaN(nan1)) { | ||
result[i++] = rlookup[64] | ||
result[i++] = rlookup[64] | ||
} else { | ||
nan2 = buffer[++position] | ||
result[i++] = rlookup[((nan1 & 15) << 2) | (nan2 >> 6)] | ||
result[i++] = rlookup[(isNaN(nan2)) ? 64 : nan2 & 63] | ||
} | ||
} | ||
return B64.decodeCommon(s, options); | ||
}, | ||
toUtf8: function (s) { | ||
return new TextEncoder("utf8").encode(s); | ||
}, | ||
decodeCommon: function (s, options) { | ||
/* jshint bitwise:false */ | ||
var position = -1, | ||
array = new ArrayBuffer(s.length / 4 * 3), | ||
len, buffer = new Uint8Array(array), | ||
i = 0, | ||
enc0, enc1, enc2, enc3; | ||
return new TextDecoder('utf8').decode(result) | ||
}, | ||
if (!B64.lookup) { | ||
len = B64.alphabet.length; | ||
B64.lookup = new Array(125); | ||
B64.lookup['-'.charCodeAt(0)] = 62; | ||
B64.lookup['_'.charCodeAt(0)] = 63; | ||
while (++position < len) | ||
B64.lookup[B64.alphabet.charCodeAt(position)] = position; | ||
position = -1; | ||
} | ||
decode: function (s, options) { | ||
s = s.replace(/\s/g, '') | ||
if (s.length % 4) { | ||
throw new Error('InvalidLengthError: decode failed: The string to be decoded is not the correct length for a base64 encoded string.') | ||
} | ||
if (/[^A-Za-z0-9+\/\-_=]/g.test(s)) { // eslint-disable-line | ||
throw new Error('InvalidCharacterError: decode failed: The string contains characters invalid in a base64 encoded string.') | ||
} | ||
return B64.decodeCommon(s, options) | ||
}, | ||
len = s.length; | ||
while (++position < len) { | ||
enc0 = B64.lookup[s.charCodeAt(position)]; | ||
enc1 = B64.lookup[s.charCodeAt(++position)]; | ||
buffer[i++] = (enc0 << 2) | (enc1 >> 4); | ||
enc2 = B64.lookup[s.charCodeAt(++position)]; | ||
if (enc2 === 64) | ||
break; | ||
buffer[i++] = ((enc1 & 15) << 4) | (enc2 >> 2); | ||
enc3 = B64.lookup[s.charCodeAt(++position)]; | ||
if (enc3 === 64) | ||
break; | ||
buffer[i++] = ((enc2 & 3) << 6) | enc3; | ||
} | ||
toUtf8: function (s) { | ||
return new TextEncoder('utf8').encode(s) | ||
}, | ||
var uint8Array = new Uint8Array(array, 0, i); | ||
decodeCommon: function (s, options) { | ||
var position = -1 | ||
var array = new ArrayBuffer(s.length / 4 * 3) | ||
var len | ||
var buffer = new Uint8Array(array) | ||
var i = 0 | ||
var enc0, enc1, enc2, enc3 | ||
if (options && options.uint8Array) { | ||
return uint8Array; | ||
} | ||
len = s.length | ||
while (++position < len) { | ||
enc0 = B64.lookup[s.charCodeAt(position)] | ||
enc1 = B64.lookup[s.charCodeAt(++position)] | ||
buffer[i++] = (enc0 << 2) | (enc1 >> 4) | ||
enc2 = B64.lookup[s.charCodeAt(++position)] | ||
if (enc2 === 64) { break } | ||
buffer[i++] = ((enc1 & 15) << 4) | (enc2 >> 2) | ||
enc3 = B64.lookup[s.charCodeAt(++position)] | ||
if (enc3 === 64) { break } | ||
buffer[i++] = ((enc2 & 3) << 6) | enc3 | ||
} | ||
return new TextDecoder("utf8").decode(uint8Array); | ||
var uint8Array = new Uint8Array(array, 0, i) | ||
if (options && options.uint8Array) { | ||
return uint8Array | ||
} | ||
}; | ||
return new TextDecoder('utf8').decode(uint8Array) | ||
} | ||
} | ||
var B64url = { | ||
decode: function(input, options) { | ||
// Pad out with standard base64 required padding characters | ||
var pad = input.length % 4; | ||
if(pad) { | ||
if(pad === 1) { | ||
throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding'); | ||
} | ||
input += new Array(5-pad).join('='); | ||
} | ||
decode: function (input, options) { | ||
// Pad out with standard base64 required padding characters | ||
var pad = input.length % 4 | ||
if (pad) { | ||
if (pad === 1) { | ||
throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding') | ||
} | ||
input += new Array(5 - pad).join('=') | ||
} | ||
if(/[^A-Za-z0-9\-_=]/g.test(input)) { | ||
throw new Error('InvalidCharacterError: urldecode failed: The string contains characters invalid in a base64 encoded string.'); | ||
} | ||
if (/[^A-Za-z0-9\-_=]/g.test(input)) { | ||
throw new Error('InvalidCharacterError: urldecode failed: The string contains characters invalid in a base64 encoded string.') | ||
} | ||
return B64.decodeCommon(input, options); | ||
}, | ||
return B64.decodeCommon(input, options) | ||
}, | ||
encode: function(input) { | ||
var output = B64.encode(input); | ||
return output | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.split('=', 1)[0]; | ||
} | ||
}; | ||
encode: function (input) { | ||
return B64.encode(input, {url: true}) | ||
} | ||
} | ||
module.exports = { | ||
decode: B64.decode, | ||
encode: B64.encode, | ||
urldecode: B64url.decode, | ||
urlencode: B64url.encode, | ||
}; | ||
decode: B64.decode, | ||
encode: B64.encode, | ||
urldecode: B64url.decode, | ||
urlencode: B64url.encode | ||
} |
{ | ||
"name": "@superhuman/fast64", | ||
"version": "0.7.0", | ||
"version": "0.7.3", | ||
"license": "Apache", | ||
"repository": "https://github.com/ConradIrwin/fast64", | ||
"main": "index.js", | ||
"description":"The fastest in-browser base64 library", | ||
"description": "The fastest in-browser base64 library", | ||
"devDependencies": { | ||
"chai": "^3.4.1", | ||
"mocha": "^2.3.4" | ||
"mocha": "^10.1.0" | ||
}, | ||
"standard": { | ||
"ignore": [], | ||
"globals": [ | ||
"Base64", | ||
"TextDecoder", | ||
"TextEncoder", | ||
"context", | ||
"describe", | ||
"expect", | ||
"it", | ||
"performance" | ||
] | ||
} | ||
} |
@@ -50,3 +50,3 @@ | ||
npm install | ||
open test/test.html | ||
open test/index.html | ||
``` |
168
test/test.js
@@ -0,93 +1,115 @@ | ||
describe('Fast64', function () { | ||
let html = '<div id="mocha"><ul id="mocha-stats"><li class="progress"><canvas width="80" height="80" style="width: 40px; height: 40px;"></canvas></li><li class="passes"><a href="javascript:void(0);">passes:</a> <em>2</em></li><li class="failures"><a href="javascript:void(0);">failures:</a> <em>0</em></li><li class="duration">duration: <em>0.02</em>s</li></ul><ul id="mocha-report"><li class="suite"><h1><a href="/0/js/fast64/test/index.html?grep=decode">decode</a></h1><ul><li class="test pass fast"><h2>should work on simple cases<span class="duration">1ms</span> <a href="/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20simple%20cases" class="replay">ā£</a></h2><pre style="display: none;"><code>expect(Base<span class="number">64</span>.encode("hi")).to.equal("aGk=");</code></pre></li><li class="test pass fast"><h2>should work on emoji<span class="duration">0ms</span> <a href="/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20emoji" class="replay">ā£</a></h2><pre style="display: none;"><code>expect(Base<span class="number">64</span>.encode("š")).to.equal("<span class="number">8</span>J+Skw==");</code></pre></li></ul></li></ul></div>\n\n<script>\n mocha.run();\n</script>\n\n' | ||
let encoded = 'PGRpdiBpZD0ibW9jaGEiPjx1bCBpZD0ibW9jaGEtc3RhdHMiPjxsaSBjbGFzcz0icHJvZ3Jlc3MiPjxjYW52YXMgd2lkdGg9IjgwIiBoZWlnaHQ9IjgwIiBzdHlsZT0id2lkdGg6IDQwcHg7IGhlaWdodDogNDBweDsiPjwvY2FudmFzPjwvbGk+PGxpIGNsYXNzPSJwYXNzZXMiPjxhIGhyZWY9ImphdmFzY3JpcHQ6dm9pZCgwKTsiPnBhc3Nlczo8L2E+IDxlbT4yPC9lbT48L2xpPjxsaSBjbGFzcz0iZmFpbHVyZXMiPjxhIGhyZWY9ImphdmFzY3JpcHQ6dm9pZCgwKTsiPmZhaWx1cmVzOjwvYT4gPGVtPjA8L2VtPjwvbGk+PGxpIGNsYXNzPSJkdXJhdGlvbiI+ZHVyYXRpb246IDxlbT4wLjAyPC9lbT5zPC9saT48L3VsPjx1bCBpZD0ibW9jaGEtcmVwb3J0Ij48bGkgY2xhc3M9InN1aXRlIj48aDE+PGEgaHJlZ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VhbCgiYUdrPSIpOzwvY29kZT48L3ByZT48L2xpPjxsaSBjbGFzcz0idGVzdCBwYXNzIGZhc3QiPjxoMj5zaG91bGQgd29yayBvbiBlbW9qaTxzcGFuIGNsYXNzPSJkdXJhdGlvbiI-MG1zPC9zcGFuPiA8YSBocmVmPSIvMC9qcy9mYXN0NjQvdGVzdC9pbmRleC5odG1sP2dyZXA9ZGVjb2RlJTIwc2hvdWxkJTIwd29yayUyMG9uJTIwZW1vamkiIGNsYXNzPSJyZXBsYXkiPuKAozwvYT48L2gyPjxwcmUgc3R5bGU9ImRpc3BsYXk6IG5vbmU7Ij48Y29kZT5leHBlY3QoQmFzZTxzcGFuIGNsYXNzPSJudW1iZXIiPjY0PC9zcGFuPi5lbmNvZGUoIvCfkpMiKSkudG8uZXF1YWwoIjxzcGFuIGNsYXNzPSJudW1iZXIiPjg8L3NwYW4-SitTa3c9PSIpOzwvY29kZT48L3ByZT48L2xpPjwvdWw-PC9saT48L3VsPjwvZGl2PgoKPHNjcmlwdD4KICBtb2NoYS5ydW4oKTsKPC9zY3JpcHQ-Cgo' | ||
describe("encode", function () { | ||
describe('encode', function () { | ||
it('should work on simple cases', function () { | ||
expect(Base64.encode('hi')).to.equal('aGk=') | ||
}) | ||
it('should work on emoji', function () { | ||
expect(Base64.encode('š')).to.equal('8J+Skw==') | ||
}) | ||
it("should work for long strings of HTML", function () { | ||
expect(Base64.encode("<div id=\"mocha\"><ul id=\"mocha-stats\"><li class=\"progress\"><canvas width=\"80\" height=\"80\" style=\"width: 40px; height: 40px;\"></canvas></li><li class=\"passes\"><a href=\"javascript:void(0);\">passes:</a> <em>2</em></li><li class=\"failures\"><a href=\"javascript:void(0);\">failures:</a> <em>0</em></li><li class=\"duration\">duration: <em>0.02</em>s</li></ul><ul id=\"mocha-report\"><li class=\"suite\"><h1><a href=\"/0/js/fast64/test/index.html?grep=decode\">decode</a></h1><ul><li class=\"test pass fast\"><h2>should work on simple cases<span class=\"duration\">1ms</span> <a href=\"/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20simple%20cases\" class=\"replay\">ā£</a></h2><pre style=\"display: none;\"><code>expect(Base<span class=\"number\">64</span>.encode(\"hi\")).to.equal(\"aGk=\");</code></pre></li><li class=\"test pass fast\"><h2>should work on emoji<span class=\"duration\">0ms</span> <a href=\"/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20emoji\" class=\"replay\">ā£</a></h2><pre style=\"display: none;\"><code>expect(Base<span class=\"number\">64</span>.encode(\"š\")).to.equal(\"<span class=\"number\">8</span>J+Skw==\");</code></pre></li></ul></li></ul></div>\n\n<script>\n mocha.run();\n</script>\n\n")).to.equal("PGRpdiBpZD0ibW9jaGEiPjx1bCBpZD0ibW9jaGEtc3RhdHMiPjxsaSBjbGFzcz0icHJvZ3Jlc3MiPjxjYW52YXMgd2lkdGg9IjgwIiBoZWlnaHQ9IjgwIiBzdHlsZT0id2lkdGg6IDQwcHg7IGhlaWdodDogNDBweDsiPjwvY2FudmFzPjwvbGk+PGxpIGNsYXNzPSJwYXNzZXMiPjxhIGhyZWY9ImphdmFzY3JpcHQ6dm9pZCgwKTsiPnBhc3Nlczo8L2E+IDxlbT4yPC9lbT48L2xpPjxsaSBjbGFzcz0iZmFpbHVyZXMiPjxhIGhyZWY9ImphdmFzY3JpcHQ6dm9pZCgwKTsiPmZhaWx1cmVzOjwvYT4gPGVtPjA8L2VtPjwvbGk+PGxpIGNsYXNzPSJkdXJhdGlvbiI+ZHVyYXRpb246IDxlbT4wLjAyPC9lbT5zPC9saT48L3VsPjx1bCBpZD0ibW9jaGEtcmVwb3J0Ij48bGkgY2xhc3M9InN1aXRlIj48aDE+PGEgaHJlZj0iLzAvanMvZmFzdDY0L3Rlc3QvaW5kZXguaHRtbD9ncmVwPWRlY29kZSI+ZGVjb2RlPC9hPjwvaDE+PHVsPjxsaSBjbGFzcz0idGVzdCBwYXNzIGZhc3QiPjxoMj5zaG91bGQgd29yayBvbiBzaW1wbGUgY2FzZXM8c3BhbiBjbGFzcz0iZHVyYXRpb24iPjFtczwvc3Bhbj4gPGEgaHJlZj0iLzAvanMvZmFzdDY0L3Rlc3QvaW5kZXguaHRtbD9ncmVwPWRlY29kZSUyMHNob3VsZCUyMHdvcmslMjBvbiUyMHNpbXBsZSUyMGNhc2VzIiBjbGFzcz0icmVwbGF5Ij7igKM8L2E+PC9oMj48cHJlIHN0eWxlPSJkaXNwbGF5OiBub25lOyI+PGNvZGU+ZXhwZWN0KEJhc2U8c3BhbiBjbGFzcz0ibnVtYmVyIj42NDwvc3Bhbj4uZW5jb2RlKCJoaSIpKS50by5lcXVhbCgiYUdrPSIpOzwvY29kZT48L3ByZT48L2xpPjxsaSBjbGFzcz0idGVzdCBwYXNzIGZhc3QiPjxoMj5zaG91bGQgd29yayBvbiBlbW9qaTxzcGFuIGNsYXNzPSJkdXJhdGlvbiI+MG1zPC9zcGFuPiA8YSBocmVmPSIvMC9qcy9mYXN0NjQvdGVzdC9pbmRleC5odG1sP2dyZXA9ZGVjb2RlJTIwc2hvdWxkJTIwd29yayUyMG9uJTIwZW1vamkiIGNsYXNzPSJyZXBsYXkiPuKAozwvYT48L2gyPjxwcmUgc3R5bGU9ImRpc3BsYXk6IG5vbmU7Ij48Y29kZT5leHBlY3QoQmFzZTxzcGFuIGNsYXNzPSJudW1iZXIiPjY0PC9zcGFuPi5lbmNvZGUoIvCfkpMiKSkudG8uZXF1YWwoIjxzcGFuIGNsYXNzPSJudW1iZXIiPjg8L3NwYW4+SitTa3c9PSIpOzwvY29kZT48L3ByZT48L2xpPjwvdWw+PC9saT48L3VsPjwvZGl2PgoKPHNjcmlwdD4KICBtb2NoYS5ydW4oKTsKPC9zY3JpcHQ+Cgo="); | ||
it('should work for long strings of HTML', function () { | ||
expect(Base64.encode(html)).to.equal(encoded) | ||
}) | ||
}); | ||
it("should work on simple cases", function () { | ||
expect(Base64.encode("hi")).to.equal("aGk="); | ||
}); | ||
it("should work on emoji", function () { | ||
expect(Base64.encode("š")).to.equal("8J+Skw=="); | ||
}); | ||
it('should encode uint8 arrays', function () { | ||
expect(Base64.encode(new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x00]))).to.equal('iVBORwA=') | ||
}) | ||
}); | ||
describe("decode", function () { | ||
it("should work on simple cases", function () { | ||
expect(Base64.decode("aGk=")).to.equal("hi"); | ||
}); | ||
it('should be atleast 100% faster than encodeUsingBTOA', function () { | ||
let start = performance.now() | ||
let i = 0 | ||
while (i < 1000) { | ||
expect(Base64.encode(html)).to.equal(encoded) | ||
i++ | ||
} | ||
let startIntermediate = performance.now() | ||
i = 0 | ||
while (i < 1000) { | ||
expect(window.encodeUsingBTOA(html)).to.equal(encoded) | ||
i++ | ||
} | ||
let encodeTime = startIntermediate - start | ||
let encodeUsingBTOATime = performance.now() - startIntermediate | ||
expect(encodeUsingBTOATime / 2).to.be.above(encodeTime) | ||
}) | ||
}) | ||
it("should work on emoji", function () { | ||
expect(Base64.decode("8J+Skw==")).to.equal("š"); | ||
}); | ||
describe('decode', function () { | ||
it('should work on simple cases', function () { | ||
expect(Base64.decode('aGk=')).to.equal('hi') | ||
}) | ||
it("should work for long strings of HTML", function () { | ||
expect(Base64.decode( | ||
"PGRpdiBpZD0ibW9jaGEiPjx1bCBpZD0ibW9jaGEtc3RhdHMiPjxsaSBjbGFzcz0icHJvZ3Jlc3MiPjxjYW52YXMgd2lkdGg9IjgwIiBoZWlnaHQ9IjgwIiBzdHlsZT0id2lkdGg6IDQwcHg7IGhlaWdodDogNDBweDsiPjwvY2FudmFzPjwvbGk+PGxpIGNsYXNzPSJwYXNzZXMiPjxhIGhyZWY9ImphdmFzY3JpcHQ6dm9pZCgwKTsiPnBhc3Nlczo8L2E+IDxlbT4yPC9lbT48L2xpPjxsaSBjbGFzcz0iZmFpbHVyZXMiPjxhIGhyZWY9ImphdmFzY3JpcHQ6dm9pZCgwKTsiPmZhaWx1cmVzOjwvYT4gPGVtPjA8L2VtPjwvbGk+PGxpIGNsYXNzPSJkdXJhdGlvbiI+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8L3NwYW4+SitTa3c9PSIpOzwvY29kZT48L3ByZT48L2xpPjwvdWw+PC9saT48L3VsPjwvZGl2PgoKPHNjcmlwdD4KICBtb2NoYS5ydW4oKTsKPC9zY3JpcHQ+Cgo=" | ||
)).to.equal( | ||
"<div id=\"mocha\"><ul id=\"mocha-stats\"><li class=\"progress\"><canvas width=\"80\" height=\"80\" style=\"width: 40px; height: 40px;\"></canvas></li><li class=\"passes\"><a href=\"javascript:void(0);\">passes:</a> <em>2</em></li><li class=\"failures\"><a href=\"javascript:void(0);\">failures:</a> <em>0</em></li><li class=\"duration\">duration: <em>0.02</em>s</li></ul><ul id=\"mocha-report\"><li class=\"suite\"><h1><a href=\"/0/js/fast64/test/index.html?grep=decode\">decode</a></h1><ul><li class=\"test pass fast\"><h2>should work on simple cases<span class=\"duration\">1ms</span> <a href=\"/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20simple%20cases\" class=\"replay\">ā£</a></h2><pre style=\"display: none;\"><code>expect(Base<span class=\"number\">64</span>.encode(\"hi\")).to.equal(\"aGk=\");</code></pre></li><li class=\"test pass fast\"><h2>should work on emoji<span class=\"duration\">0ms</span> <a href=\"/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20emoji\" class=\"replay\">ā£</a></h2><pre style=\"display: none;\"><code>expect(Base<span class=\"number\">64</span>.encode(\"š\")).to.equal(\"<span class=\"number\">8</span>J+Skw==\");</code></pre></li></ul></li></ul></div>\n\n<script>\n mocha.run();\n</script>\n\n"); | ||
}); | ||
it('should work on emoji', function () { | ||
expect(Base64.decode('8J+Skw==')).to.equal('š') | ||
}) | ||
it("should return Uint8Array if option param uint8Array is passed with value true", function () { | ||
result = Base64.decode("aGk=", {uint8Array:true}) | ||
expect(result instanceof Uint8Array).to.equal(true); | ||
expect(result[0]).to.equal(104); | ||
expect(result[1]).to.equal(105); | ||
}); | ||
}); | ||
it('should work for long strings of HTML', function () { | ||
expect(Base64.decode(encoded)).to.equal(html) | ||
}) | ||
describe("urldecode", function () { | ||
it("should work on simple cases", function () { | ||
expect(Base64.urldecode("aGk")).to.equal("hi"); | ||
}); | ||
it('should return Uint8Array if option param uint8Array is passed with value true', function () { | ||
let result = Base64.decode('aGk=', {uint8Array: true}) | ||
expect(result instanceof Uint8Array).to.equal(true) | ||
expect(result[0]).to.equal(104) | ||
expect(result[1]).to.equal(105) | ||
}) | ||
it("should work on emoji", function () { | ||
expect(Base64.urldecode("8J-Skw")).to.equal("š"); | ||
}); | ||
it('should be atleast 100% faster than decodeUsingATOB', function () { | ||
let start = performance.now() | ||
let i = 0 | ||
while (i < 1000) { | ||
expect(Base64.decode(encoded)).to.equal(html) | ||
i++ | ||
} | ||
let startIntermediate = performance.now() | ||
i = 0 | ||
while (i < 1000) { | ||
expect(window.decodeUsingATOB(encoded)).to.equal(html) | ||
i++ | ||
} | ||
let decodeTime = startIntermediate - start | ||
let decodeUsingATOBTime = performance.now() - startIntermediate | ||
expect(decodeUsingATOBTime / 2).to.be.above(decodeTime) | ||
}) | ||
}) | ||
it("should work for long strings of HTML", function () { | ||
expect(Base64.urldecode( | ||
"PGRpdiBpZD0ibW9jaGEiPjx1bCBpZD0ibW9jaGEtc3RhdHMiPjxsaSBjbGFzcz0icHJvZ3Jlc3MiPjxjYW52YXMgd2lkdGg9IjgwIiBoZWlnaHQ9IjgwIiBzdHlsZT0id2lkdGg6IDQwcHg7IGhlaWdodDogNDBweDsiPjwvY2FudmFzPjwvbGk-PGxpIGNsYXNzPSJwYXNzZXMiPjxhIGhyZWY9ImphdmFzY3JpcHQ6dm9pZCgwKTsiPnBhc3Nlczo8L2E-IDxlbT4yPC9lbT48L2xpPjxsaSBjbGFzcz0iZmFpbHVyZXMiPjxhIGhyZWY9ImphdmFzY3JpcHQ6dm9pZCgwKTsiPmZhaWx1cmVzOjwvYT4gPGVtPjA8L2VtPjwvbGk-PGxpIGNsYXNzPSJkdXJhdGlvbiI-ZHVyYXRpb246IDxlbT4wLjAyPC9lbT5zPC9saT48L3VsPjx1bC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Ww-PC9saT48L3VsPjwvZGl2PgoKPHNjcmlwdD4KICBtb2NoYS5ydW4oKTsKPC9zY3JpcHQ-Cgo" | ||
)).to.equal( | ||
"<div id=\"mocha\"><ul id=\"mocha-stats\"><li class=\"progress\"><canvas width=\"80\" height=\"80\" style=\"width: 40px; height: 40px;\"></canvas></li><li class=\"passes\"><a href=\"javascript:void(0);\">passes:</a> <em>2</em></li><li class=\"failures\"><a href=\"javascript:void(0);\">failures:</a> <em>0</em></li><li class=\"duration\">duration: <em>0.02</em>s</li></ul><ul id=\"mocha-report\"><li class=\"suite\"><h1><a href=\"/0/js/fast64/test/index.html?grep=decode\">decode</a></h1><ul><li class=\"test pass fast\"><h2>should work on simple cases<span class=\"duration\">1ms</span> <a href=\"/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20simple%20cases\" class=\"replay\">ā£</a></h2><pre style=\"display: none;\"><code>expect(Base<span class=\"number\">64</span>.encode(\"hi\")).to.equal(\"aGk=\");</code></pre></li><li class=\"test pass fast\"><h2>should work on emoji<span class=\"duration\">0ms</span> <a href=\"/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20emoji\" class=\"replay\">ā£</a></h2><pre style=\"display: none;\"><code>expect(Base<span class=\"number\">64</span>.encode(\"š\")).to.equal(\"<span class=\"number\">8</span>J+Skw==\");</code></pre></li></ul></li></ul></div>\n\n<script>\n mocha.run();\n</script>\n\n"); | ||
describe('urlencode', function () { | ||
it('should work on simple cases', function () { | ||
expect(Base64.urlencode('hi')).to.equal('aGk') | ||
}) | ||
}); | ||
it('should work on emoji', function () { | ||
expect(Base64.urlencode('š')).to.equal('8J-Skw') | ||
}) | ||
it("should return Uint8Array if option param uint8Array is passed with value true", function () { | ||
result = Base64.urldecode("aGk", {uint8Array:true}) | ||
expect(result instanceof Uint8Array).to.equal(true); | ||
expect(result[0]).to.equal(104); | ||
expect(result[1]).to.equal(105); | ||
}); | ||
}); | ||
it('should work for long strings of HTML', function () { | ||
expect(Base64.urlencode(html)).to.equal(urlencoded) | ||
}) | ||
}) | ||
describe("urlencode", function () { | ||
describe('urldecode', function () { | ||
it('should work on simple cases', function () { | ||
expect(Base64.urldecode('aGk')).to.equal('hi') | ||
}) | ||
it("should work for long strings of HTML", function () { | ||
expect(Base64.urlencode( | ||
"<div id=\"mocha\"><ul id=\"mocha-stats\"><li class=\"progress\"><canvas width=\"80\" height=\"80\" style=\"width: 40px; height: 40px;\"></canvas></li><li class=\"passes\"><a href=\"javascript:void(0);\">passes:</a> <em>2</em></li><li class=\"failures\"><a href=\"javascript:void(0);\">failures:</a> <em>0</em></li><li class=\"duration\">duration: <em>0.02</em>s</li></ul><ul id=\"mocha-report\"><li class=\"suite\"><h1><a href=\"/0/js/fast64/test/index.html?grep=decode\">decode</a></h1><ul><li class=\"test pass fast\"><h2>should work on simple cases<span class=\"duration\">1ms</span> <a href=\"/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20simple%20cases\" class=\"replay\">ā£</a></h2><pre style=\"display: none;\"><code>expect(Base<span class=\"number\">64</span>.encode(\"hi\")).to.equal(\"aGk=\");</code></pre></li><li class=\"test pass fast\"><h2>should work on emoji<span class=\"duration\">0ms</span> <a href=\"/0/js/fast64/test/index.html?grep=decode%20should%20work%20on%20emoji\" class=\"replay\">ā£</a></h2><pre style=\"display: none;\"><code>expect(Base<span class=\"number\">64</span>.encode(\"š\")).to.equal(\"<span class=\"number\">8</span>J+Skw==\");</code></pre></li></ul></li></ul></div>\n\n<script>\n mocha.run();\n</script>\n\n" | ||
it('should work on emoji', function () { | ||
expect(Base64.urldecode('8J-Skw')).to.equal('š') | ||
}) | ||
it('should work for long strings of HTML', function () { | ||
expect(Base64.urldecode(urlencoded)).to.equal(html) | ||
}) | ||
)).to.equal( | ||
"PGRpdiBpZD0ibW9jaGEiPjx1bCBpZD0ibW9jaGEtc3RhdHMiPjxsaSBjbGFzcz0icHJvZ3Jlc3MiPjxjYW52YXMgd2lkdGg9IjgwIiBoZWlnaHQ9IjgwIiBzdHlsZ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mPSIvMC9qcy9mYXN0NjQvdGVzdC9pbmRleC5odG1sP2dyZXA9ZGVjb2RlJTIwc2hvdWxkJTIwd29yayUyMG9uJTIwZW1vamkiIGNsYXNzPSJyZXBsYXkiPuKAozwvYT48L2gyPjxwcmUgc3R5bGU9ImRpc3BsYXk6IG5vbmU7Ij48Y29kZT5leHBlY3QoQmFzZTxzcGFuIGNsYXNzPSJudW1iZXIiPjY0PC9zcGFuPi5lbmNvZGUoIvCfkpMiKSkudG8uZXF1YWwoIjxzcGFuIGNsYXNzPSJudW1iZXIiPjg8L3NwYW4-SitTa3c9PSIpOzwvY29kZT48L3ByZT48L2xpPjwvdWw-PC9saT48L3VsPjwvZGl2PgoKPHNjcmlwdD4KICBtb2NoYS5ydW4oKTsKPC9zY3JpcHQ-Cgo" | ||
); | ||
}); | ||
it("should work on simple cases", function () { | ||
expect(Base64.urlencode("hi")).to.equal("aGk"); | ||
}); | ||
it("should work on emoji", function () { | ||
expect(Base64.urlencode("š")).to.equal("8J-Skw"); | ||
}); | ||
}); | ||
it('should return Uint8Array if option param uint8Array is passed with value true', function () { | ||
let result = Base64.urldecode('aGk', {uint8Array: true}) | ||
expect(result instanceof Uint8Array).to.equal(true) | ||
expect(result[0]).to.equal(104) | ||
expect(result[1]).to.equal(105) | ||
}) | ||
}) | ||
}) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
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
7
316
19508
2