@frsource/base64 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +18 -2
- package/dist/index.js +50 -49
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -49
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -11
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Base64 encode / decode
|
|
4
|
+
* http://www.webtoolkit.info/
|
|
5
|
+
*
|
|
6
|
+
**/
|
|
7
|
+
/**
|
|
8
|
+
* Encodes given string.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} input - Text to encode
|
|
11
|
+
*/
|
|
12
|
+
export declare const encode: (input: string) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Decodes given string.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} input - Text to decode
|
|
17
|
+
*/
|
|
18
|
+
export declare const decode: (input: string) => string;
|
package/dist/index.js
CHANGED
|
@@ -1,30 +1,29 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* Base64 encode / decode
|
|
4
|
-
* http://www.webtoolkit.info/
|
|
5
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Base64 encode / decode
|
|
4
|
+
* http://www.webtoolkit.info/
|
|
5
|
+
*
|
|
6
6
|
**/
|
|
7
7
|
const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
8
|
-
/**
|
|
9
|
-
* Encodes given string.
|
|
10
|
-
*
|
|
11
|
-
* @param {string} input - Text to encode
|
|
8
|
+
/**
|
|
9
|
+
* Encodes given string.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} input - Text to encode
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
const encode = input => {
|
|
15
15
|
let output = "";
|
|
16
|
-
let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
|
17
16
|
let i = 0;
|
|
18
17
|
input = _utf8_encode(input);
|
|
19
18
|
|
|
20
19
|
while (i < input.length) {
|
|
21
|
-
chr1 = input.charCodeAt(i++);
|
|
22
|
-
chr2 = input.charCodeAt(i++);
|
|
23
|
-
chr3 = input.charCodeAt(i++);
|
|
24
|
-
enc1 = chr1 >> 2;
|
|
25
|
-
enc2 = (chr1 & 3) << 4 | chr2 >> 4;
|
|
26
|
-
enc3 = (chr2 & 15) << 2 | chr3 >> 6;
|
|
27
|
-
enc4 = chr3 & 63;
|
|
20
|
+
const chr1 = input.charCodeAt(i++);
|
|
21
|
+
const chr2 = input.charCodeAt(i++);
|
|
22
|
+
const chr3 = input.charCodeAt(i++);
|
|
23
|
+
const enc1 = chr1 >> 2;
|
|
24
|
+
const enc2 = (chr1 & 3) << 4 | chr2 >> 4;
|
|
25
|
+
let enc3 = (chr2 & 15) << 2 | chr3 >> 6;
|
|
26
|
+
let enc4 = chr3 & 63;
|
|
28
27
|
|
|
29
28
|
if (isNaN(chr2)) {
|
|
30
29
|
enc3 = enc4 = 64;
|
|
@@ -37,28 +36,29 @@ exports.encode = function (input) {
|
|
|
37
36
|
|
|
38
37
|
return output;
|
|
39
38
|
};
|
|
40
|
-
/**
|
|
41
|
-
* Decodes given string.
|
|
42
|
-
*
|
|
43
|
-
* @param {string} input - Text to decode
|
|
39
|
+
/**
|
|
40
|
+
* Decodes given string.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} input - Text to decode
|
|
44
43
|
*/
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
exports.decode = function (input) {
|
|
45
|
+
const decode = input => {
|
|
48
46
|
let output = "";
|
|
49
|
-
let chr1, chr2, chr3;
|
|
50
|
-
let enc1, enc2, enc3, enc4;
|
|
51
47
|
let i = 0;
|
|
52
48
|
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
|
|
53
49
|
|
|
54
50
|
while (i < input.length) {
|
|
55
|
-
enc1 = _keyStr.indexOf(input.charAt(i++));
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
51
|
+
const enc1 = _keyStr.indexOf(input.charAt(i++));
|
|
52
|
+
|
|
53
|
+
const enc2 = _keyStr.indexOf(input.charAt(i++));
|
|
54
|
+
|
|
55
|
+
const enc3 = _keyStr.indexOf(input.charAt(i++));
|
|
56
|
+
|
|
57
|
+
const enc4 = _keyStr.indexOf(input.charAt(i++));
|
|
58
|
+
|
|
59
|
+
const chr1 = enc1 << 2 | enc2 >> 4;
|
|
60
|
+
const chr2 = (enc2 & 15) << 4 | enc3 >> 2;
|
|
61
|
+
const chr3 = (enc3 & 3) << 6 | enc4;
|
|
62
62
|
output = output + String.fromCharCode(chr1);
|
|
63
63
|
|
|
64
64
|
if (enc3 != 64) {
|
|
@@ -72,14 +72,13 @@ exports.decode = function (input) {
|
|
|
72
72
|
|
|
73
73
|
return output = _utf8_decode(output);
|
|
74
74
|
};
|
|
75
|
-
/**
|
|
76
|
-
* (private method) does a UTF-8 encoding
|
|
77
|
-
*
|
|
78
|
-
* @private
|
|
79
|
-
* @param {string} string - Text to encode
|
|
75
|
+
/**
|
|
76
|
+
* (private method) does a UTF-8 encoding
|
|
77
|
+
*
|
|
78
|
+
* @private
|
|
79
|
+
* @param {string} string - Text to encode
|
|
80
80
|
*/
|
|
81
81
|
|
|
82
|
-
|
|
83
82
|
function _utf8_encode(string) {
|
|
84
83
|
string = string.replace(/\r\n/g, "\n");
|
|
85
84
|
let utftext = "";
|
|
@@ -98,32 +97,31 @@ function _utf8_encode(string) {
|
|
|
98
97
|
|
|
99
98
|
return utftext;
|
|
100
99
|
}
|
|
101
|
-
/**
|
|
102
|
-
* (private method - do not use) does a UTF-8 decoding
|
|
103
|
-
*
|
|
104
|
-
* @private
|
|
105
|
-
* @param {string} utftext - UTF-8 text to dencode
|
|
100
|
+
/**
|
|
101
|
+
* (private method - do not use) does a UTF-8 decoding
|
|
102
|
+
*
|
|
103
|
+
* @private
|
|
104
|
+
* @param {string} utftext - UTF-8 text to dencode
|
|
106
105
|
*/
|
|
107
106
|
|
|
108
107
|
|
|
109
108
|
function _utf8_decode(utftext) {
|
|
110
109
|
let string = "";
|
|
111
110
|
let i = 0;
|
|
112
|
-
let c = c1 = c2 = 0;
|
|
113
111
|
|
|
114
112
|
while (i < utftext.length) {
|
|
115
|
-
c = utftext.charCodeAt(i);
|
|
113
|
+
const c = utftext.charCodeAt(i);
|
|
116
114
|
|
|
117
115
|
if (c < 128) {
|
|
118
116
|
string += String.fromCharCode(c);
|
|
119
117
|
i++;
|
|
120
118
|
} else if (c > 191 && c < 224) {
|
|
121
|
-
c2 = utftext.charCodeAt(i + 1);
|
|
119
|
+
const c2 = utftext.charCodeAt(i + 1);
|
|
122
120
|
string += String.fromCharCode((c & 31) << 6 | c2 & 63);
|
|
123
121
|
i += 2;
|
|
124
122
|
} else {
|
|
125
|
-
c2 = utftext.charCodeAt(i + 1);
|
|
126
|
-
c3 = utftext.charCodeAt(i + 2);
|
|
123
|
+
const c2 = utftext.charCodeAt(i + 1);
|
|
124
|
+
const c3 = utftext.charCodeAt(i + 2);
|
|
127
125
|
string += String.fromCharCode((c & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
|
|
128
126
|
i += 3;
|
|
129
127
|
}
|
|
@@ -131,4 +129,7 @@ function _utf8_decode(utftext) {
|
|
|
131
129
|
|
|
132
130
|
return string;
|
|
133
131
|
}
|
|
132
|
+
|
|
133
|
+
exports.decode = decode;
|
|
134
|
+
exports.encode = encode;
|
|
134
135
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../index.
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../index.ts"],"sourcesContent":["/**\n *\n * Base64 encode / decode\n * http://www.webtoolkit.info/\n *\n **/\n\nconst _keyStr =\n \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\";\n\n/**\n * Encodes given string.\n *\n * @param {string} input - Text to encode\n */\nexport const encode = (input: string) => {\n let output = \"\";\n let i = 0;\n\n input = _utf8_encode(input);\n\n while (i < input.length) {\n const chr1 = input.charCodeAt(i++);\n const chr2 = input.charCodeAt(i++);\n const chr3 = input.charCodeAt(i++);\n\n const enc1 = chr1 >> 2;\n const enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);\n let enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);\n let enc4 = chr3 & 63;\n\n if (isNaN(chr2)) {\n enc3 = enc4 = 64;\n } else if (isNaN(chr3)) {\n enc4 = 64;\n }\n\n output =\n output +\n _keyStr.charAt(enc1) +\n _keyStr.charAt(enc2) +\n _keyStr.charAt(enc3) +\n _keyStr.charAt(enc4);\n }\n\n return output;\n};\n\n/**\n * Decodes given string.\n *\n * @param {string} input - Text to decode\n */\nexport const decode = (input: string) => {\n let output = \"\";\n let i = 0;\n\n input = input.replace(/[^A-Za-z0-9\\+\\/\\=]/g, \"\");\n\n while (i < input.length) {\n const enc1 = _keyStr.indexOf(input.charAt(i++));\n const enc2 = _keyStr.indexOf(input.charAt(i++));\n const enc3 = _keyStr.indexOf(input.charAt(i++));\n const enc4 = _keyStr.indexOf(input.charAt(i++));\n\n const chr1 = (enc1 << 2) | (enc2 >> 4);\n const chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);\n const chr3 = ((enc3 & 3) << 6) | enc4;\n\n output = output + String.fromCharCode(chr1);\n\n if (enc3 != 64) {\n output = output + String.fromCharCode(chr2);\n }\n\n if (enc4 != 64) {\n output = output + String.fromCharCode(chr3);\n }\n }\n\n return (output = _utf8_decode(output));\n};\n\n/**\n * (private method) does a UTF-8 encoding\n *\n * @private\n * @param {string} string - Text to encode\n */\nfunction _utf8_encode(string: string) {\n string = string.replace(/\\r\\n/g, \"\\n\");\n let utftext = \"\";\n\n for (let n = 0; n < string.length; n++) {\n const c = string.charCodeAt(n);\n\n if (c < 128) {\n utftext += String.fromCharCode(c);\n } else if (c > 127 && c < 2048) {\n utftext +=\n String.fromCharCode((c >> 6) | 192) +\n String.fromCharCode((c & 63) | 128);\n } else {\n utftext +=\n String.fromCharCode((c >> 12) | 224) +\n String.fromCharCode(((c >> 6) & 63) | 128) +\n String.fromCharCode((c & 63) | 128);\n }\n }\n\n return utftext;\n}\n\n/**\n * (private method - do not use) does a UTF-8 decoding\n *\n * @private\n * @param {string} utftext - UTF-8 text to dencode\n */\nfunction _utf8_decode(utftext: string) {\n let string = \"\";\n let i = 0;\n\n while (i < utftext.length) {\n const c = utftext.charCodeAt(i);\n\n if (c < 128) {\n string += String.fromCharCode(c);\n i++;\n } else if (c > 191 && c < 224) {\n const c2 = utftext.charCodeAt(i + 1);\n string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));\n i += 2;\n } else {\n const c2 = utftext.charCodeAt(i + 1);\n const c3 = utftext.charCodeAt(i + 2);\n string += String.fromCharCode(\n ((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)\n );\n i += 3;\n }\n }\n return string;\n}\n"],"names":["_keyStr","encode","input","output","i","_utf8_encode","length","chr1","charCodeAt","chr2","chr3","enc1","enc2","enc3","enc4","isNaN","charAt","decode","replace","indexOf","String","fromCharCode","_utf8_decode","string","utftext","n","c","c2","c3"],"mappings":"AAAA;;;;;AAKI;AAEJ,MAAMA,OAAO,GACX,mEADF,CAAA;AAGA;;;;AAIG;;AACUC,MAAAA,MAAM,GAAIC,KAAD,IAAkB;EACtC,IAAIC,MAAM,GAAG,EAAb,CAAA;EACA,IAAIC,CAAC,GAAG,CAAR,CAAA;AAEAF,EAAAA,KAAK,GAAGG,YAAY,CAACH,KAAD,CAApB,CAAA;;AAEA,EAAA,OAAOE,CAAC,GAAGF,KAAK,CAACI,MAAjB,EAAyB;IACvB,MAAMC,IAAI,GAAGL,KA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|
const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
8
|
-
/**
|
|
9
|
-
* Encodes given string.
|
|
10
|
-
*
|
|
11
|
-
* @param {string} input - Text to encode
|
|
8
|
+
/**
|
|
9
|
+
* Encodes given string.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} input - Text to encode
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
const encode = input => {
|
|
15
15
|
let output = "";
|
|
16
|
-
let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
|
17
16
|
let i = 0;
|
|
18
17
|
input = _utf8_encode(input);
|
|
19
18
|
|
|
20
19
|
while (i < input.length) {
|
|
21
|
-
chr1 = input.charCodeAt(i++);
|
|
22
|
-
chr2 = input.charCodeAt(i++);
|
|
23
|
-
chr3 = input.charCodeAt(i++);
|
|
24
|
-
enc1 = chr1 >> 2;
|
|
25
|
-
enc2 = (chr1 & 3) << 4 | chr2 >> 4;
|
|
26
|
-
enc3 = (chr2 & 15) << 2 | chr3 >> 6;
|
|
27
|
-
enc4 = chr3 & 63;
|
|
20
|
+
const chr1 = input.charCodeAt(i++);
|
|
21
|
+
const chr2 = input.charCodeAt(i++);
|
|
22
|
+
const chr3 = input.charCodeAt(i++);
|
|
23
|
+
const enc1 = chr1 >> 2;
|
|
24
|
+
const enc2 = (chr1 & 3) << 4 | chr2 >> 4;
|
|
25
|
+
let enc3 = (chr2 & 15) << 2 | chr3 >> 6;
|
|
26
|
+
let enc4 = chr3 & 63;
|
|
28
27
|
|
|
29
28
|
if (isNaN(chr2)) {
|
|
30
29
|
enc3 = enc4 = 64;
|
|
@@ -37,28 +36,29 @@ exports.encode = function (input) {
|
|
|
37
36
|
|
|
38
37
|
return output;
|
|
39
38
|
};
|
|
40
|
-
/**
|
|
41
|
-
* Decodes given string.
|
|
42
|
-
*
|
|
43
|
-
* @param {string} input - Text to decode
|
|
39
|
+
/**
|
|
40
|
+
* Decodes given string.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} input - Text to decode
|
|
44
43
|
*/
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
exports.decode = function (input) {
|
|
45
|
+
const decode = input => {
|
|
48
46
|
let output = "";
|
|
49
|
-
let chr1, chr2, chr3;
|
|
50
|
-
let enc1, enc2, enc3, enc4;
|
|
51
47
|
let i = 0;
|
|
52
48
|
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
|
|
53
49
|
|
|
54
50
|
while (i < input.length) {
|
|
55
|
-
enc1 = _keyStr.indexOf(input.charAt(i++));
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
51
|
+
const enc1 = _keyStr.indexOf(input.charAt(i++));
|
|
52
|
+
|
|
53
|
+
const enc2 = _keyStr.indexOf(input.charAt(i++));
|
|
54
|
+
|
|
55
|
+
const enc3 = _keyStr.indexOf(input.charAt(i++));
|
|
56
|
+
|
|
57
|
+
const enc4 = _keyStr.indexOf(input.charAt(i++));
|
|
58
|
+
|
|
59
|
+
const chr1 = enc1 << 2 | enc2 >> 4;
|
|
60
|
+
const chr2 = (enc2 & 15) << 4 | enc3 >> 2;
|
|
61
|
+
const chr3 = (enc3 & 3) << 6 | enc4;
|
|
62
62
|
output = output + String.fromCharCode(chr1);
|
|
63
63
|
|
|
64
64
|
if (enc3 != 64) {
|
|
@@ -72,14 +72,13 @@ exports.decode = function (input) {
|
|
|
72
72
|
|
|
73
73
|
return output = _utf8_decode(output);
|
|
74
74
|
};
|
|
75
|
-
/**
|
|
76
|
-
* (private method) does a UTF-8 encoding
|
|
77
|
-
*
|
|
78
|
-
* @private
|
|
79
|
-
* @param {string} string - Text to encode
|
|
75
|
+
/**
|
|
76
|
+
* (private method) does a UTF-8 encoding
|
|
77
|
+
*
|
|
78
|
+
* @private
|
|
79
|
+
* @param {string} string - Text to encode
|
|
80
80
|
*/
|
|
81
81
|
|
|
82
|
-
|
|
83
82
|
function _utf8_encode(string) {
|
|
84
83
|
string = string.replace(/\r\n/g, "\n");
|
|
85
84
|
let utftext = "";
|
|
@@ -98,32 +97,31 @@ function _utf8_encode(string) {
|
|
|
98
97
|
|
|
99
98
|
return utftext;
|
|
100
99
|
}
|
|
101
|
-
/**
|
|
102
|
-
* (private method - do not use) does a UTF-8 decoding
|
|
103
|
-
*
|
|
104
|
-
* @private
|
|
105
|
-
* @param {string} utftext - UTF-8 text to dencode
|
|
100
|
+
/**
|
|
101
|
+
* (private method - do not use) does a UTF-8 decoding
|
|
102
|
+
*
|
|
103
|
+
* @private
|
|
104
|
+
* @param {string} utftext - UTF-8 text to dencode
|
|
106
105
|
*/
|
|
107
106
|
|
|
108
107
|
|
|
109
108
|
function _utf8_decode(utftext) {
|
|
110
109
|
let string = "";
|
|
111
110
|
let i = 0;
|
|
112
|
-
let c = c1 = c2 = 0;
|
|
113
111
|
|
|
114
112
|
while (i < utftext.length) {
|
|
115
|
-
c = utftext.charCodeAt(i);
|
|
113
|
+
const c = utftext.charCodeAt(i);
|
|
116
114
|
|
|
117
115
|
if (c < 128) {
|
|
118
116
|
string += String.fromCharCode(c);
|
|
119
117
|
i++;
|
|
120
118
|
} else if (c > 191 && c < 224) {
|
|
121
|
-
c2 = utftext.charCodeAt(i + 1);
|
|
119
|
+
const c2 = utftext.charCodeAt(i + 1);
|
|
122
120
|
string += String.fromCharCode((c & 31) << 6 | c2 & 63);
|
|
123
121
|
i += 2;
|
|
124
122
|
} else {
|
|
125
|
-
c2 = utftext.charCodeAt(i + 1);
|
|
126
|
-
c3 = utftext.charCodeAt(i + 2);
|
|
123
|
+
const c2 = utftext.charCodeAt(i + 1);
|
|
124
|
+
const c3 = utftext.charCodeAt(i + 2);
|
|
127
125
|
string += String.fromCharCode((c & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
|
|
128
126
|
i += 3;
|
|
129
127
|
}
|
|
@@ -131,4 +129,6 @@ function _utf8_decode(utftext) {
|
|
|
131
129
|
|
|
132
130
|
return string;
|
|
133
131
|
}
|
|
132
|
+
|
|
133
|
+
export { decode, encode };
|
|
134
134
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../index.
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../index.ts"],"sourcesContent":["/**\n *\n * Base64 encode / decode\n * http://www.webtoolkit.info/\n *\n **/\n\nconst _keyStr =\n \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\";\n\n/**\n * Encodes given string.\n *\n * @param {string} input - Text to encode\n */\nexport const encode = (input: string) => {\n let output = \"\";\n let i = 0;\n\n input = _utf8_encode(input);\n\n while (i < input.length) {\n const chr1 = input.charCodeAt(i++);\n const chr2 = input.charCodeAt(i++);\n const chr3 = input.charCodeAt(i++);\n\n const enc1 = chr1 >> 2;\n const enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);\n let enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);\n let enc4 = chr3 & 63;\n\n if (isNaN(chr2)) {\n enc3 = enc4 = 64;\n } else if (isNaN(chr3)) {\n enc4 = 64;\n }\n\n output =\n output +\n _keyStr.charAt(enc1) +\n _keyStr.charAt(enc2) +\n _keyStr.charAt(enc3) +\n _keyStr.charAt(enc4);\n }\n\n return output;\n};\n\n/**\n * Decodes given string.\n *\n * @param {string} input - Text to decode\n */\nexport const decode = (input: string) => {\n let output = \"\";\n let i = 0;\n\n input = input.replace(/[^A-Za-z0-9\\+\\/\\=]/g, \"\");\n\n while (i < input.length) {\n const enc1 = _keyStr.indexOf(input.charAt(i++));\n const enc2 = _keyStr.indexOf(input.charAt(i++));\n const enc3 = _keyStr.indexOf(input.charAt(i++));\n const enc4 = _keyStr.indexOf(input.charAt(i++));\n\n const chr1 = (enc1 << 2) | (enc2 >> 4);\n const chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);\n const chr3 = ((enc3 & 3) << 6) | enc4;\n\n output = output + String.fromCharCode(chr1);\n\n if (enc3 != 64) {\n output = output + String.fromCharCode(chr2);\n }\n\n if (enc4 != 64) {\n output = output + String.fromCharCode(chr3);\n }\n }\n\n return (output = _utf8_decode(output));\n};\n\n/**\n * (private method) does a UTF-8 encoding\n *\n * @private\n * @param {string} string - Text to encode\n */\nfunction _utf8_encode(string: string) {\n string = string.replace(/\\r\\n/g, \"\\n\");\n let utftext = \"\";\n\n for (let n = 0; n < string.length; n++) {\n const c = string.charCodeAt(n);\n\n if (c < 128) {\n utftext += String.fromCharCode(c);\n } else if (c > 127 && c < 2048) {\n utftext +=\n String.fromCharCode((c >> 6) | 192) +\n String.fromCharCode((c & 63) | 128);\n } else {\n utftext +=\n String.fromCharCode((c >> 12) | 224) +\n String.fromCharCode(((c >> 6) & 63) | 128) +\n String.fromCharCode((c & 63) | 128);\n }\n }\n\n return utftext;\n}\n\n/**\n * (private method - do not use) does a UTF-8 decoding\n *\n * @private\n * @param {string} utftext - UTF-8 text to dencode\n */\nfunction _utf8_decode(utftext: string) {\n let string = \"\";\n let i = 0;\n\n while (i < utftext.length) {\n const c = utftext.charCodeAt(i);\n\n if (c < 128) {\n string += String.fromCharCode(c);\n i++;\n } else if (c > 191 && c < 224) {\n const c2 = utftext.charCodeAt(i + 1);\n string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));\n i += 2;\n } else {\n const c2 = utftext.charCodeAt(i + 1);\n const c3 = utftext.charCodeAt(i + 2);\n string += String.fromCharCode(\n ((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)\n );\n i += 3;\n }\n }\n return string;\n}\n"],"names":["_keyStr","encode","input","output","i","_utf8_encode","length","chr1","charCodeAt","chr2","chr3","enc1","enc2","enc3","enc4","isNaN","charAt","decode","replace","indexOf","String","fromCharCode","_utf8_decode","string","utftext","n","c","c2","c3"],"mappings":"AAAA;;;;;AAKI;AAEJ,MAAMA,OAAO,GACX,mEADF,CAAA;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"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frsource/base64",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "base64 encoder/decoder with UTF-8 support 📔",
|
|
5
|
+
"repository": "https://github.com/FRSOURCE/base64.git",
|
|
6
|
+
"homepage": "http://www.webtoolkit.info/javascript_base64.html",
|
|
5
7
|
"scripts": {
|
|
6
|
-
"build": "del-cli dist && microbundle index.
|
|
7
|
-
"prettify": "prettier --write index.
|
|
8
|
-
"prettify:ci": "prettier --check index.
|
|
8
|
+
"build": "del-cli dist && microbundle index.ts --target node -f cjs,modern",
|
|
9
|
+
"prettify": "prettier --write index.ts",
|
|
10
|
+
"prettify:ci": "prettier --check index.ts",
|
|
9
11
|
"prepack": "pnpm build",
|
|
10
12
|
"release": "semantic-release",
|
|
11
13
|
"release:ci": "pnpm release --yes",
|
|
@@ -32,13 +34,13 @@
|
|
|
32
34
|
"author": "Jakub Freisler <jakub.freisler@frsource.org>",
|
|
33
35
|
"license": "MIT",
|
|
34
36
|
"devDependencies": {
|
|
35
|
-
"@semantic-release/commit-analyzer": "
|
|
36
|
-
"@semantic-release/git": "
|
|
37
|
-
"@semantic-release/npm": "
|
|
38
|
-
"del-cli": "
|
|
39
|
-
"microbundle": "
|
|
40
|
-
"prettier": "
|
|
41
|
-
"semantic-release": "
|
|
37
|
+
"@semantic-release/commit-analyzer": "9.0.2",
|
|
38
|
+
"@semantic-release/git": "10.0.1",
|
|
39
|
+
"@semantic-release/npm": "9.0.1",
|
|
40
|
+
"del-cli": "5.0.0",
|
|
41
|
+
"microbundle": "0.15.1",
|
|
42
|
+
"prettier": "2.7.1",
|
|
43
|
+
"semantic-release": "19.0.5"
|
|
42
44
|
},
|
|
43
45
|
"publishConfig": {
|
|
44
46
|
"access": "public"
|