-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
106 lines (96 loc) · 2.86 KB
/
lib.js
File metadata and controls
106 lines (96 loc) · 2.86 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// eslint-disable-next-line no-unused-vars, no-redeclare
function copyToClipboard(value) {
if (navigator && navigator.clipboard && navigator.clipboard.writeText && typeof value === 'string') {
return navigator.clipboard.writeText(value);
}
if (navigator && navigator.clipboard && navigator.clipboard.write && typeof value === 'object') {
return navigator.clipboard.write(value);
}
return Promise.reject('The Clipboard API is not available.');
}
// eslint-disable-next-line no-unused-vars, no-redeclare
function getDataUrlSize(dataUrl) {
const base64String = dataUrl.split(',')[1];
return ((base64String.length * 3 / 4 - (base64String.endsWith('==') ? 2 : 1)) / 1000).toFixed(2);
}
// eslint-disable-next-line no-unused-vars, no-redeclare
function pasteFromClipboard() {
if (navigator && navigator.clipboard && navigator.clipboard.readText) {
return navigator.clipboard.readText();
}
return Promise.reject('The Clipboard API is not available.');
}
// eslint-disable-next-line no-unused-vars, no-redeclare
function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
return resolve(reader.result);
};
reader.onerror = function (error) {
return reject(error);
};
});
}
// eslint-disable-next-line no-unused-vars, no-redeclare
function base64UrlEncode(str) {
return btoa(str)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
// eslint-disable-next-line no-redeclare
function base64UrlDecode(str) {
str = str
.replace(/-/g, '+')
.replace(/_/g, '/');
while (str.length % 4) {
str += '=';
}
return atob(str);
}
// eslint-disable-next-line no-unused-vars, no-redeclare
function isJWT(str) {
const parts = str.split('.');
if (parts.length === 3) {
try {
base64UrlDecode(parts[0]);
base64UrlDecode(parts[1]);
JSON.parse(base64UrlDecode(parts[1]));
return true;
} catch (e) {
return false;
}
}
return false;
}
// eslint-disable-next-line no-unused-vars, no-redeclare
function isJSON(str) {
if (str.startsWith('{') || str.startsWith('[')) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
return false;
}
// eslint-disable-next-line no-unused-vars, no-redeclare
function parseJwt(token) {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
// eslint-disable-next-line no-unused-vars, no-redeclare
function isBase64(str) {
try {
return str.length > 3 && !(str.length % 4) && atob(str) && true;
} catch (err) {
return false;
}
}