add atob and btoa polyfills
This commit is contained in:
parent
f0ca4fc7c2
commit
1e47435058
1 changed files with 43 additions and 1 deletions
|
@ -2,6 +2,47 @@
|
||||||
|
|
||||||
Ox.polyfill = {};
|
Ox.polyfill = {};
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||||
|
/*@
|
||||||
|
Ox.polyfill.atob <f> see https://developer.mozilla.org/en/DOM/window.atob
|
||||||
|
> Ox.polyfill.atob('Cg==')
|
||||||
|
'\n'
|
||||||
|
> Ox.polyfill.atob('Zm9v')
|
||||||
|
'foo'
|
||||||
|
@*/
|
||||||
|
Ox.polyfill.atob = function(string) {
|
||||||
|
var binary = '', ret = '';
|
||||||
|
String(string).replace(/=/g, '').split('').forEach(function(char) {
|
||||||
|
binary += Ox.pad(digits.indexOf(char).toString(2), 'left', 6, '0');
|
||||||
|
});
|
||||||
|
while (binary.length >= 8) {
|
||||||
|
ret += Ox.char(parseInt(binary.slice(0, 8), 2));
|
||||||
|
binary = binary.slice(8);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
/*@
|
||||||
|
Ox.polyfill.btoa <f> see https://developer.mozilla.org/en/DOM/window.btoa
|
||||||
|
> Ox.polyfill.btoa('\n')
|
||||||
|
'Cg=='
|
||||||
|
> Ox.polyfill.btoa('foo')
|
||||||
|
'Zm9v'
|
||||||
|
@*/
|
||||||
|
Ox.polyfill.btoa = function(string) {
|
||||||
|
var binary = '', ret = '';
|
||||||
|
String(string).split('').forEach(function(char) {
|
||||||
|
binary += Ox.pad(char.charCodeAt(0).toString(2), 'left', 8, '0');
|
||||||
|
});
|
||||||
|
binary = Ox.pad(binary, Math.ceil(binary.length / 6) * 6, '0')
|
||||||
|
while (binary) {
|
||||||
|
ret += digits[parseInt(binary.slice(0, 6), 2)];
|
||||||
|
binary = binary.slice(6);
|
||||||
|
}
|
||||||
|
return Ox.pad(ret, Math.ceil(ret.length / 4) * 4, '=');
|
||||||
|
};
|
||||||
|
}());
|
||||||
|
|
||||||
(function(window) {
|
(function(window) {
|
||||||
/*@
|
/*@
|
||||||
Ox.polyfill.bind <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
|
Ox.polyfill.bind <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
|
||||||
|
@ -343,7 +384,8 @@ Ox.polyfill.trim = function() {
|
||||||
for (key in Ox.polyfill) {
|
for (key in Ox.polyfill) {
|
||||||
object = key == 'bind' ? Function.prototype
|
object = key == 'bind' ? Function.prototype
|
||||||
: key == 'isArray' ? Array
|
: key == 'isArray' ? Array
|
||||||
: key == 'JSON' || key == 'localStorage' ? window
|
: key == 'atob' || key == 'btoa'
|
||||||
|
|| key == 'JSON' || key == 'localStorage' ? window
|
||||||
: key == 'keys' ? Object
|
: key == 'keys' ? Object
|
||||||
: key == 'trim' ? String.prototype
|
: key == 'trim' ? String.prototype
|
||||||
: Array.prototype;
|
: Array.prototype;
|
||||||
|
|
Loading…
Reference in a new issue