Skip to content

Commit 887c30a

Browse files
committed
Add dist files
1 parent 7ceb521 commit 887c30a

7 files changed

+243
-0
lines changed

dist/base64.cjs

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
(function (global, factory) {
2+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3+
typeof define === 'function' && define.amd ? define(factory) :
4+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.base64 = factory());
5+
})(this, (function () { 'use strict';
6+
7+
/* ------------------------------------------------------------------------------------
8+
9+
base64 - MIT License - Hexagon <[email protected]>
10+
11+
------------------------------------------------------------------------------------
12+
13+
License:
14+
15+
Copyright (c) 2021 Hexagon <[email protected]>
16+
17+
Permission is hereby granted, free of charge, to any person obtaining a copy
18+
of this software and associated documentation files (the "Software"), to deal
19+
in the Software without restriction, including without limitation the rights
20+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21+
copies of the Software, and to permit persons to whom the Software is
22+
furnished to do so, subject to the following conditions:
23+
The above copyright notice and this permission notice shall be included in
24+
all copies or substantial portions of the Software.
25+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31+
THE SOFTWARE.
32+
33+
------------------------------------------------------------------------------------ */
34+
35+
const
36+
// Regular base64 characters
37+
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
38+
39+
// Base64url characters
40+
charsUrl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
41+
42+
genLookup = (target) => {
43+
let lookupTemp = typeof Uint8Array === "undefined" ? [] : new Uint8Array(256);
44+
for (let i = 0; i < chars.length; i++) {
45+
lookupTemp[target.charCodeAt(i)] = i;
46+
}
47+
return lookupTemp;
48+
},
49+
50+
// Use a lookup table to find the index.
51+
lookup = genLookup(chars),
52+
lookupUrl = genLookup(charsUrl);
53+
54+
/**
55+
* @namespace base64
56+
*/
57+
let base64 = {};
58+
59+
/**
60+
* Convenience function for converting a base64 encoded string to an ArrayBuffer instance
61+
* @public
62+
*
63+
* @param {string} data - Base64 representation of data
64+
* @param {boolean} [urlMode] - If set to true, URL mode string will be expected
65+
* @returns {ArrayBuffer} - Decoded data
66+
*/
67+
base64.toArrayBuffer = (data, urlMode) => {
68+
let bufferLength = data.length * 0.75,
69+
len = data.length,
70+
i,
71+
p = 0,
72+
encoded1,
73+
encoded2,
74+
encoded3,
75+
encoded4;
76+
77+
if (data[data.length - 1] === "=") {
78+
bufferLength--;
79+
if (data[data.length - 2] === "=") {
80+
bufferLength--;
81+
}
82+
}
83+
84+
const
85+
arraybuffer = new ArrayBuffer(bufferLength),
86+
bytes = new Uint8Array(arraybuffer),
87+
target = urlMode ? lookupUrl : lookup;
88+
89+
for (i = 0; i < len; i += 4) {
90+
encoded1 = target[data.charCodeAt(i)];
91+
encoded2 = target[data.charCodeAt(i + 1)];
92+
encoded3 = target[data.charCodeAt(i + 2)];
93+
encoded4 = target[data.charCodeAt(i + 3)];
94+
95+
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
96+
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
97+
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
98+
}
99+
100+
return arraybuffer;
101+
102+
};
103+
104+
/**
105+
* Convenience function for converting base64 encoded string to an ArrayBuffer instance
106+
* @public
107+
*
108+
* @param {ArrayBuffer} arrBuf - ArrayBuffer to be encoded
109+
* @param {boolean} [urlMode] - If set to true, URL mode string will be returned
110+
* @returns {string} - Base64 representation of data
111+
*/
112+
base64.fromArrayBuffer = (arrBuf, urlMode) => {
113+
let bytes = new Uint8Array(arrBuf),
114+
i,
115+
len = bytes.length,
116+
result = "",
117+
target = urlMode ? charsUrl : chars;
118+
119+
for (i = 0; i < len; i += 3) {
120+
result += target[bytes[i] >> 2];
121+
result += target[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
122+
result += target[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
123+
result += target[bytes[i + 2] & 63];
124+
}
125+
126+
if (len % 3 === 2) {
127+
result = result.substring(0, result.length - 1) + (urlMode ? "" : "=");
128+
} else if (len % 3 === 1) {
129+
result = result.substring(0, result.length - 2) + (urlMode ? "" : "==");
130+
}
131+
132+
return result;
133+
};
134+
135+
/**
136+
* Convenience function for converting base64 to string
137+
* @public
138+
*
139+
* @param {string} str - Base64 encoded string to be decoded
140+
* @param {boolean} [urlMode] - If set to true, URL mode string will be expected
141+
* @returns {string} - Decoded string
142+
*/
143+
base64.toString = (str, urlMode) => {
144+
return new TextDecoder().decode(base64.toArrayBuffer(str, urlMode));
145+
};
146+
147+
/**
148+
* Convenience function for converting a javascript string to base64
149+
* @public
150+
*
151+
* @param {string} str - String to be converted to base64
152+
* @param {boolean} [urlMode] - If set to true, URL mode string will be returned
153+
* @returns {string} - Base64 encoded string
154+
*/
155+
base64.fromString = (str, urlMode) => {
156+
return base64.fromArrayBuffer(new TextEncoder().encode(str), urlMode);
157+
};
158+
159+
/**
160+
* Function to validate base64
161+
* @public
162+
* @param {string} encoded - Base64 or Base64url encoded data
163+
* @param {boolean} [urlMode] - If set to true, base64url will be expected
164+
* @returns {boolean} - Valid base64/base64url?
165+
*/
166+
base64.validate = (encoded, urlMode) => {
167+
168+
// Bail out if not string
169+
if (!(typeof encoded === "string" || encoded instanceof String)) {
170+
return false;
171+
}
172+
173+
// Go on validate
174+
try {
175+
if (urlMode) {
176+
return /^[-A-Za-z0-9\-_]*$/.test(encoded);
177+
} else {
178+
return /^[-A-Za-z0-9+/]*={0,3}$/.test(encoded);
179+
}
180+
} catch (_e) {
181+
return false;
182+
}
183+
};
184+
185+
base64.base64 = base64;
186+
187+
return base64;
188+
189+
}));

dist/base64.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/base64.min.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/base64.min.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",charsUrl="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",genLookup=target=>{let lookupTemp=typeof Uint8Array==="undefined"?[]:new Uint8Array(256);for(let i=0;i<chars.length;i++){lookupTemp[target.charCodeAt(i)]=i}return lookupTemp},lookup=genLookup(chars),lookupUrl=genLookup(charsUrl);let base64={};base64.toArrayBuffer=(data,urlMode)=>{let bufferLength=data.length*.75,len=data.length,i,p=0,encoded1,encoded2,encoded3,encoded4;if(data[data.length-1]==="="){bufferLength--;if(data[data.length-2]==="="){bufferLength--}}const arraybuffer=new ArrayBuffer(bufferLength),bytes=new Uint8Array(arraybuffer),target=urlMode?lookupUrl:lookup;for(i=0;i<len;i+=4){encoded1=target[data.charCodeAt(i)];encoded2=target[data.charCodeAt(i+1)];encoded3=target[data.charCodeAt(i+2)];encoded4=target[data.charCodeAt(i+3)];bytes[p++]=encoded1<<2|encoded2>>4;bytes[p++]=(encoded2&15)<<4|encoded3>>2;bytes[p++]=(encoded3&3)<<6|encoded4&63}return arraybuffer};base64.fromArrayBuffer=(arrBuf,urlMode)=>{let bytes=new Uint8Array(arrBuf),i,len=bytes.length,result="",target=urlMode?charsUrl:chars;for(i=0;i<len;i+=3){result+=target[bytes[i]>>2];result+=target[(bytes[i]&3)<<4|bytes[i+1]>>4];result+=target[(bytes[i+1]&15)<<2|bytes[i+2]>>6];result+=target[bytes[i+2]&63]}if(len%3===2){result=result.substring(0,result.length-1)+(urlMode?"":"=")}else if(len%3===1){result=result.substring(0,result.length-2)+(urlMode?"":"==")}return result};base64.toString=(str,urlMode)=>{return(new TextDecoder).decode(base64.toArrayBuffer(str,urlMode))};base64.fromString=(str,urlMode)=>{return base64.fromArrayBuffer((new TextEncoder).encode(str),urlMode)};base64.validate=(encoded,urlMode)=>{if(!(typeof encoded==="string"||encoded instanceof String)){return false}try{if(urlMode){return/^[-A-Za-z0-9\-_]*$/.test(encoded)}else{return/^[-A-Za-z0-9+/]*={0,3}$/.test(encoded)}}catch(_e){return false}};base64.base64=base64;export{base64,base64 as default};

dist/base64.min.mjs.map

+1
Original file line numberDiff line numberDiff line change

types/base64.d.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export default base64;
2+
export namespace base64 {
3+
/**
4+
* Convenience function for converting a base64 encoded string to an ArrayBuffer instance
5+
* @public
6+
*
7+
* @param {string} data - Base64 representation of data
8+
* @param {boolean} [urlMode] - If set to true, URL mode string will be expected
9+
* @returns {ArrayBuffer} - Decoded data
10+
*/
11+
export function toArrayBuffer(data: string, urlMode?: boolean): ArrayBuffer;
12+
/**
13+
* Convenience function for converting base64 encoded string to an ArrayBuffer instance
14+
* @public
15+
*
16+
* @param {ArrayBuffer} arrBuf - ArrayBuffer to be encoded
17+
* @param {boolean} [urlMode] - If set to true, URL mode string will be returned
18+
* @returns {string} - Base64 representation of data
19+
*/
20+
export function fromArrayBuffer(arrBuf: ArrayBuffer, urlMode?: boolean): string;
21+
/**
22+
* Convenience function for converting base64 to string
23+
* @public
24+
*
25+
* @param {string} str - Base64 encoded string to be decoded
26+
* @param {boolean} [urlMode] - If set to true, URL mode string will be expected
27+
* @returns {string} - Decoded string
28+
*/
29+
export function toString(str: string, urlMode?: boolean): string;
30+
/**
31+
* Convenience function for converting a javascript string to base64
32+
* @public
33+
*
34+
* @param {string} str - String to be converted to base64
35+
* @param {boolean} [urlMode] - If set to true, URL mode string will be returned
36+
* @returns {string} - Base64 encoded string
37+
*/
38+
export function fromString(str: string, urlMode?: boolean): string;
39+
/**
40+
* Function to validate base64
41+
* @public
42+
* @param {string} encoded - Base64 or Base64url encoded data
43+
* @param {boolean} [urlMode] - If set to true, base64url will be expected
44+
* @returns {boolean} - Valid base64/base64url?
45+
*/
46+
export function validate(encoded: string, urlMode?: boolean): boolean;
47+
export { base64 };
48+
}

types/base64.single.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default base64;
2+
import base64 from "./base64.js";

0 commit comments

Comments
 (0)