@frsource/base64 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 FRSOURCE - Let's shape your web
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ <p align="center">
2
+ <a href="https://www.npmjs.com/package/@frsource/base64">
3
+ <img src="https://img.shields.io/npm/v/@frsource/base64.svg" alt="NPM version badge">
4
+ </a>
5
+ <a href="https://www.npmjs.com/package/@frsource/base64">
6
+ <img src="https://img.shields.io/npm/dt/@frsource/base64.svg" alt="NPM total downloads badge">
7
+ </a>
8
+ <a href="https://github.com/semantic-release/semantic-release">
9
+ <img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-relase badge">
10
+ </a>
11
+ <a href="https://github.com/FRSOURCE/base64/blob/main/LICENSE">
12
+ <img src="https://img.shields.io/github/license/FRSOURCE/base64.svg" alt="license MIT badge">
13
+ </a>
14
+ </p>
15
+
16
+ # @frsource/base64
17
+
18
+ This javascript code is used to encode / decode data using base64 (this encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean). Script is fully compatible with UTF-8 encoding. You can use base64 encoded data as simple encryption mechanism.
19
+
20
+ If you plan using UTF-8 encoding in your project don’t forget to set the page encoding to UTF-8 (Content-Type meta tag).
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ npm i @frsource/base64
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```js
31
+ // Importing:
32
+ // typescript / ES6
33
+ import { encode, decode } from '@frsource/base64';
34
+
35
+ // legacy javscript
36
+ const { encode, decode } = require('@frsource/base64');
37
+
38
+
39
+ const encoded = encode('我喜欢解码lubię dekodować');
40
+ console.log(encoded); // output: 5oiR5Zac5qyi6Kej56CBbHViacSZIGRla29kb3dhxIc=
41
+ const decoded = decode(encoded);
42
+ console.log(encoded); // output: 我喜欢解码lubię dekodować
43
+ ```
44
+
45
+ ## References
46
+
47
+ Based on webtoolkit.info implementation.
48
+ Original source code available at: http://www.webtoolkit.info/javascript_base64.html
49
+
50
+ ## License
51
+
52
+ [MIT](https://opensource.org/licenses/MIT)
53
+
54
+ Copyright (c) 2022-present, Jakub FRS Freisler, [FRSOURCE](https://www.frsource.org/)
55
+
56
+ <p align="center">
57
+ <a href="https://www.frsource.org/" title="Click to visit FRSOURCE page!">
58
+ <img src="https://www.frsource.org/logo.jpg" alt="FRSOURCE logo" height="60px"/>
59
+ </a>
60
+ </p>
@@ -0,0 +1,2 @@
1
+ export function encode(input: string): string;
2
+ export function decode(input: string): string;
package/dist/index.js ADDED
@@ -0,0 +1,134 @@
1
+ /**
2
+ *
3
+ * Base64 encode / decode
4
+ * http://www.webtoolkit.info/
5
+ *
6
+ **/
7
+ const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
8
+ /**
9
+ * Encodes given string.
10
+ *
11
+ * @param {string} input - Text to encode
12
+ */
13
+
14
+ exports.encode = function (input) {
15
+ let output = "";
16
+ let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
17
+ let i = 0;
18
+ input = _utf8_encode(input);
19
+
20
+ 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;
28
+
29
+ if (isNaN(chr2)) {
30
+ enc3 = enc4 = 64;
31
+ } else if (isNaN(chr3)) {
32
+ enc4 = 64;
33
+ }
34
+
35
+ output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
36
+ }
37
+
38
+ return output;
39
+ };
40
+ /**
41
+ * Decodes given string.
42
+ *
43
+ * @param {string} input - Text to decode
44
+ */
45
+
46
+
47
+ exports.decode = function (input) {
48
+ let output = "";
49
+ let chr1, chr2, chr3;
50
+ let enc1, enc2, enc3, enc4;
51
+ let i = 0;
52
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
53
+
54
+ while (i < input.length) {
55
+ enc1 = _keyStr.indexOf(input.charAt(i++));
56
+ enc2 = _keyStr.indexOf(input.charAt(i++));
57
+ enc3 = _keyStr.indexOf(input.charAt(i++));
58
+ enc4 = _keyStr.indexOf(input.charAt(i++));
59
+ chr1 = enc1 << 2 | enc2 >> 4;
60
+ chr2 = (enc2 & 15) << 4 | enc3 >> 2;
61
+ chr3 = (enc3 & 3) << 6 | enc4;
62
+ output = output + String.fromCharCode(chr1);
63
+
64
+ if (enc3 != 64) {
65
+ output = output + String.fromCharCode(chr2);
66
+ }
67
+
68
+ if (enc4 != 64) {
69
+ output = output + String.fromCharCode(chr3);
70
+ }
71
+ }
72
+
73
+ return output = _utf8_decode(output);
74
+ };
75
+ /**
76
+ * (private method) does a UTF-8 encoding
77
+ *
78
+ * @private
79
+ * @param {string} string - Text to encode
80
+ */
81
+
82
+
83
+ function _utf8_encode(string) {
84
+ string = string.replace(/\r\n/g, "\n");
85
+ let utftext = "";
86
+
87
+ for (let n = 0; n < string.length; n++) {
88
+ const c = string.charCodeAt(n);
89
+
90
+ if (c < 128) {
91
+ utftext += String.fromCharCode(c);
92
+ } else if (c > 127 && c < 2048) {
93
+ utftext += String.fromCharCode(c >> 6 | 192) + String.fromCharCode(c & 63 | 128);
94
+ } else {
95
+ utftext += String.fromCharCode(c >> 12 | 224) + String.fromCharCode(c >> 6 & 63 | 128) + String.fromCharCode(c & 63 | 128);
96
+ }
97
+ }
98
+
99
+ return utftext;
100
+ }
101
+ /**
102
+ * (private method - do not use) does a UTF-8 decoding
103
+ *
104
+ * @private
105
+ * @param {string} utftext - UTF-8 text to dencode
106
+ */
107
+
108
+
109
+ function _utf8_decode(utftext) {
110
+ let string = "";
111
+ let i = 0;
112
+ let c = c1 = c2 = 0;
113
+
114
+ while (i < utftext.length) {
115
+ c = utftext.charCodeAt(i);
116
+
117
+ if (c < 128) {
118
+ string += String.fromCharCode(c);
119
+ i++;
120
+ } else if (c > 191 && c < 224) {
121
+ c2 = utftext.charCodeAt(i + 1);
122
+ string += String.fromCharCode((c & 31) << 6 | c2 & 63);
123
+ i += 2;
124
+ } else {
125
+ c2 = utftext.charCodeAt(i + 1);
126
+ c3 = utftext.charCodeAt(i + 2);
127
+ string += String.fromCharCode((c & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
128
+ i += 3;
129
+ }
130
+ }
131
+
132
+ return string;
133
+ }
134
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../index.js"],"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 */\nexports.encode = function (input) {\n let output = \"\";\n let chr1, chr2, chr3, enc1, enc2, enc3, enc4;\n let i = 0;\n\n input = _utf8_encode(input);\n\n while (i < input.length) {\n chr1 = input.charCodeAt(i++);\n chr2 = input.charCodeAt(i++);\n chr3 = input.charCodeAt(i++);\n\n enc1 = chr1 >> 2;\n enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);\n enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);\n 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 */\nexports.decode = function (input) {\n let output = \"\";\n let chr1, chr2, chr3;\n let enc1, enc2, enc3, enc4;\n let i = 0;\n\n input = input.replace(/[^A-Za-z0-9\\+\\/\\=]/g, \"\");\n\n while (i < input.length) {\n enc1 = _keyStr.indexOf(input.charAt(i++));\n enc2 = _keyStr.indexOf(input.charAt(i++));\n enc3 = _keyStr.indexOf(input.charAt(i++));\n enc4 = _keyStr.indexOf(input.charAt(i++));\n\n chr1 = (enc1 << 2) | (enc2 >> 4);\n chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);\n 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) {\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) {\n let string = \"\";\n let i = 0;\n let c = (c1 = c2 = 0);\n\n while (i < utftext.length) {\n c = utftext.charCodeAt(i);\n\n if (c < 128) {\n string += String.fromCharCode(c);\n i++;\n } else if (c > 191 && c < 224) {\n c2 = utftext.charCodeAt(i + 1);\n string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));\n i += 2;\n } else {\n c2 = utftext.charCodeAt(i + 1);\n 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","exports","encode","input","output","chr1","chr2","chr3","enc1","enc2","enc3","enc4","i","_utf8_encode","length","charCodeAt","isNaN","charAt","decode","replace","indexOf","String","fromCharCode","_utf8_decode","string","utftext","n","c","c1","c2","c3"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AAEA,MAAMA,OAAO,GACX,mEADF,CAAA;AAGA;AACA;AACA;AACA;AACA;;AACAC,OAAO,CAACC,MAAR,GAAiB,UAAUC,KAAV,EAAiB;EAChC,IAAIC,MAAM,GAAG,EAAb,CAAA;AACA,EAAA,IAAIC,IAAJ,EAAUC,IAAV,EAAgBC,IAAhB,EAAsBC,IAAtB,EAA4BC,IAA5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GAlB,MAAAA,CAAC,IAAI,CAAL,CAAA;AACD,KAAA;AACF,GAAA;;AACD,EAAA,OAAOY,MAAP,CAAA;AACD;;"}
package/dist/index.mjs ADDED
@@ -0,0 +1,134 @@
1
+ /**
2
+ *
3
+ * Base64 encode / decode
4
+ * http://www.webtoolkit.info/
5
+ *
6
+ **/
7
+ const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
8
+ /**
9
+ * Encodes given string.
10
+ *
11
+ * @param {string} input - Text to encode
12
+ */
13
+
14
+ exports.encode = function (input) {
15
+ let output = "";
16
+ let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
17
+ let i = 0;
18
+ input = _utf8_encode(input);
19
+
20
+ 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;
28
+
29
+ if (isNaN(chr2)) {
30
+ enc3 = enc4 = 64;
31
+ } else if (isNaN(chr3)) {
32
+ enc4 = 64;
33
+ }
34
+
35
+ output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
36
+ }
37
+
38
+ return output;
39
+ };
40
+ /**
41
+ * Decodes given string.
42
+ *
43
+ * @param {string} input - Text to decode
44
+ */
45
+
46
+
47
+ exports.decode = function (input) {
48
+ let output = "";
49
+ let chr1, chr2, chr3;
50
+ let enc1, enc2, enc3, enc4;
51
+ let i = 0;
52
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
53
+
54
+ while (i < input.length) {
55
+ enc1 = _keyStr.indexOf(input.charAt(i++));
56
+ enc2 = _keyStr.indexOf(input.charAt(i++));
57
+ enc3 = _keyStr.indexOf(input.charAt(i++));
58
+ enc4 = _keyStr.indexOf(input.charAt(i++));
59
+ chr1 = enc1 << 2 | enc2 >> 4;
60
+ chr2 = (enc2 & 15) << 4 | enc3 >> 2;
61
+ chr3 = (enc3 & 3) << 6 | enc4;
62
+ output = output + String.fromCharCode(chr1);
63
+
64
+ if (enc3 != 64) {
65
+ output = output + String.fromCharCode(chr2);
66
+ }
67
+
68
+ if (enc4 != 64) {
69
+ output = output + String.fromCharCode(chr3);
70
+ }
71
+ }
72
+
73
+ return output = _utf8_decode(output);
74
+ };
75
+ /**
76
+ * (private method) does a UTF-8 encoding
77
+ *
78
+ * @private
79
+ * @param {string} string - Text to encode
80
+ */
81
+
82
+
83
+ function _utf8_encode(string) {
84
+ string = string.replace(/\r\n/g, "\n");
85
+ let utftext = "";
86
+
87
+ for (let n = 0; n < string.length; n++) {
88
+ const c = string.charCodeAt(n);
89
+
90
+ if (c < 128) {
91
+ utftext += String.fromCharCode(c);
92
+ } else if (c > 127 && c < 2048) {
93
+ utftext += String.fromCharCode(c >> 6 | 192) + String.fromCharCode(c & 63 | 128);
94
+ } else {
95
+ utftext += String.fromCharCode(c >> 12 | 224) + String.fromCharCode(c >> 6 & 63 | 128) + String.fromCharCode(c & 63 | 128);
96
+ }
97
+ }
98
+
99
+ return utftext;
100
+ }
101
+ /**
102
+ * (private method - do not use) does a UTF-8 decoding
103
+ *
104
+ * @private
105
+ * @param {string} utftext - UTF-8 text to dencode
106
+ */
107
+
108
+
109
+ function _utf8_decode(utftext) {
110
+ let string = "";
111
+ let i = 0;
112
+ let c = c1 = c2 = 0;
113
+
114
+ while (i < utftext.length) {
115
+ c = utftext.charCodeAt(i);
116
+
117
+ if (c < 128) {
118
+ string += String.fromCharCode(c);
119
+ i++;
120
+ } else if (c > 191 && c < 224) {
121
+ c2 = utftext.charCodeAt(i + 1);
122
+ string += String.fromCharCode((c & 31) << 6 | c2 & 63);
123
+ i += 2;
124
+ } else {
125
+ c2 = utftext.charCodeAt(i + 1);
126
+ c3 = utftext.charCodeAt(i + 2);
127
+ string += String.fromCharCode((c & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
128
+ i += 3;
129
+ }
130
+ }
131
+
132
+ return string;
133
+ }
134
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../index.js"],"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 */\nexports.encode = function (input) {\n let output = \"\";\n let chr1, chr2, chr3, enc1, enc2, enc3, enc4;\n let i = 0;\n\n input = _utf8_encode(input);\n\n while (i < input.length) {\n chr1 = input.charCodeAt(i++);\n chr2 = input.charCodeAt(i++);\n chr3 = input.charCodeAt(i++);\n\n enc1 = chr1 >> 2;\n enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);\n enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);\n 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 */\nexports.decode = function (input) {\n let output = \"\";\n let chr1, chr2, chr3;\n let enc1, enc2, enc3, enc4;\n let i = 0;\n\n input = input.replace(/[^A-Za-z0-9\\+\\/\\=]/g, \"\");\n\n while (i < input.length) {\n enc1 = _keyStr.indexOf(input.charAt(i++));\n enc2 = _keyStr.indexOf(input.charAt(i++));\n enc3 = _keyStr.indexOf(input.charAt(i++));\n enc4 = _keyStr.indexOf(input.charAt(i++));\n\n chr1 = (enc1 << 2) | (enc2 >> 4);\n chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);\n 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) {\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) {\n let string = \"\";\n let i = 0;\n let c = (c1 = c2 = 0);\n\n while (i < utftext.length) {\n c = utftext.charCodeAt(i);\n\n if (c < 128) {\n string += String.fromCharCode(c);\n i++;\n } else if (c > 191 && c < 224) {\n c2 = utftext.charCodeAt(i + 1);\n string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));\n i += 2;\n } else {\n c2 = utftext.charCodeAt(i + 1);\n 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","exports","encode","input","output","chr1","chr2","chr3","enc1","enc2","enc3","enc4","i","_utf8_encode","length","charCodeAt","isNaN","charAt","decode","replace","indexOf","String","fromCharCode","_utf8_decode","string","utftext","n","c","c1","c2","c3"],"mappings":"AAAA;AACA;AACA;A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CAAA;MACAkB,EAAE,GAAGL,OAAO,CAACV,UAAR,CAAmBH,CAAC,GAAG,CAAvB,CAAL,CAAA;MACAY,MAAM,IAAIH,MAAM,CAACC,YAAP,CACP,CAACK,CAAC,GAAG,EAAL,KAAY,EAAb,GAAoB,CAACE,EAAE,GAAG,EAAN,KAAa,CAAjC,GAAuCC,EAAE,GAAG,EADpC,CAAV,CAAA;AAGAlB,MAAAA,CAAC,IAAI,CAAL,CAAA;AACD,KAAA;AACF,GAAA;;AACD,EAAA,OAAOY,MAAP,CAAA;AACD"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@frsource/base64",
3
+ "version": "1.0.0",
4
+ "description": "base64 encoder/decoder with UTF-8 support 📔",
5
+ "scripts": {
6
+ "build": "del-cli dist && microbundle index.js --target node -f cjs,modern",
7
+ "prettify": "prettier --write index.js",
8
+ "prettify:ci": "prettier --check index.js",
9
+ "prepack": "pnpm build",
10
+ "release": "semantic-release",
11
+ "release:ci": "pnpm release --yes",
12
+ "release:test": "pnpm release --no-git-tag-version --no-push --skip-npm"
13
+ },
14
+ "main": "dist/index.js",
15
+ "types": "dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./*": "./*.js"
23
+ },
24
+ "keywords": [
25
+ "base64",
26
+ "utf-8",
27
+ "encoder",
28
+ "decoder",
29
+ "webtoolkit",
30
+ "webtoolkit.base64"
31
+ ],
32
+ "author": "Jakub Freisler <jakub.freisler@frsource.org>",
33
+ "license": "MIT",
34
+ "devDependencies": {
35
+ "@semantic-release/commit-analyzer": "^9.0.2",
36
+ "@semantic-release/git": "^10.0.1",
37
+ "@semantic-release/npm": "^9.0.1",
38
+ "del-cli": "^5.0.0",
39
+ "microbundle": "^0.15.1",
40
+ "prettier": "^2.7.1",
41
+ "semantic-release": "^19.0.5"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "files": [
47
+ "dist",
48
+ "package.json",
49
+ "README.md",
50
+ "LICENSE"
51
+ ]
52
+ }