-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.js
85 lines (67 loc) · 2.03 KB
/
utility.js
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
import { decrypt } from "./encryp-decrypt.js";
export const colorText = (text, colorId) => {
// red = 31
// green = 32
// yellow = 33
// blue = 34
// magenta = 35
// cyan = 36
// white = 37
// reset color = 0
return `\x1b[${colorId}m${text}\x1b[0m`;
};
export const addLabel = (config, encrypted_label) => {
const labels = config.get("labels") ? config.get("labels") : [];
labels.push(encrypted_label);
config.set("labels", labels);
};
export const removeLabel = (config, encrypted_label) => {
let labels = config.get("labels");
labels = labels.filter((label) => label != encrypted_label);
config.set("labels", labels);
};
export const isLabelExists = (config, encrypted_label) => {
const labels = config.get("labels");
if (!labels) {
return false;
}
for (let i = 0; i < labels.length; i++) {
if (labels[i] === encrypted_label) return true;
}
return false;
};
export const listLabels = (config, pass) => {
const encrypted_labels = config.get("labels");
if (!encrypted_labels) return [];
let decrypted_labels = [];
for (let i = 0; i < encrypted_labels.length; i++) {
const decryptedLabel = decrypt(encrypted_labels[i], pass);
decrypted_labels.push({
key: String.fromCharCode(i + 97),
value: decryptedLabel,
});
}
return decrypted_labels;
};
// The below function is for generating a random value in the range [min, max)
const getRandom = (min, max) => {
// here min is included and max is excluded
return Math.floor(Math.random() * (max - min) + min);
};
// The below function is for generating a random password
export const generate = () => {
let pass = "";
for (let i = 1; i <= 14; i++) {
const random = getRandom(1, 5);
if (random == 1) {
pass += getRandom(0, 10);
} else if (random == 2) {
pass += String.fromCharCode(getRandom(65, 91));
} else if (random == 3) {
pass += String.fromCharCode(getRandom(97, 123));
} else if (random == 4) {
pass += String.fromCharCode(getRandom(33, 48));
}
}
return pass;
};