-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase64.js
More file actions
57 lines (49 loc) · 1.57 KB
/
base64.js
File metadata and controls
57 lines (49 loc) · 1.57 KB
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
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
52
53
54
55
56
57
(function (context) {
// -----------------------------------------------------------------------------
'use strict';
var id = 'base64';
var dependencies = typeof Buffer === 'function' ? [] : ['./codec'];
function factory(codec) {
// cf. btoa
var encode = codec && codec.encodeString || function (b) {
return new Buffer(b).toString('base64');
};
// cf. atob
var decode = codec && codec.decodeAsString || function (a) {
return '' + new Buffer(a.replace(/-/g, '+').replace(/_/g, '/'), 'base64');
};
function niEncode(b) {
return urlEncode(b).replace(/=+$/g, '');
}
function urlEncode(b) {
return encode(b).replace(/\+/g, '-').replace(/\//g, '_');
}
return {
encode: encode,
decode: decode,
ni: {
encode: niEncode
},
url: {
encode: urlEncode
}
};
}
// -----------------------------------------------------------------------------
var n = dependencies.length;
var o = 'object';
var r = /([^-_\s])[-_\s]+([^-_\s])/g;
function s(m, a, b) { return a + b.toUpperCase(); }
context = typeof global === o ? global : typeof window === o ? window : context;
if (typeof define === 'function' && define.amd) {
define(dependencies, function () {
return factory.apply(context, [].slice.call(arguments));
});
} else if (typeof module === o && module.exports) {
for (; n--;) { dependencies[n] = require(dependencies[n]); }
module.exports = factory.apply(context, dependencies);
} else {
for (; n--;) { dependencies[n] = context[dependencies[n]]; }
context[id.replace(r, s)] = factory.apply(context, dependencies);
}
}(this));