-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgeneral.js
110 lines (99 loc) · 2.47 KB
/
general.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function getDownloadWebsitePrefix(internalUrl = false){
if (internalUrl) return "https://flippermaker.github.io/download.html";
return "https://dev.flpr.app/s";
}
function cleanString(inputString) {
return inputString.toString().replaceAll(/\s/g, "");
}
function replaceSpace(inputString, replacement) {
return inputString.toString().replaceAll(/\s/g, replacement);
}
function urlEncodeSpace(inputString) {
return replaceSpace(inputString, "+");
}
function pad(num, size) {
num = cleanString(num);
while (num.length < size) num = "0" + num;
return num;
}
function range(size, startAt = 0) {
return [...Array(size).keys()].map(i => i + startAt);
}
function intToHex(inputNumber) {
var hexString = parseInt(inputNumber, 10).toString(16);
return hexString.toString().toUpperCase();
}
function hexToInt(inputHex) {
var hexString = parseInt(inputHex, 16);
return hexString.toString();
}
function intToBin(inputNumber) {
var binString = parseInt(inputNumber, 10).toString(2);
return binString.toString();
}
function binToInt(inputBin) {
var binString = parseInt(inputBin, 2);
return binString.toString();
}
function binToHex(inputBin) {
return intToHex(binToInt(inputBin)).toUpperCase();
}
function hexToBin(inputHex) {
return intToBin(hexToInt(inputHex));
}
function findParity(parity, bin) {
const arr = bin.toString().split("");
let countOnes = 0;
let res = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] == 1) {
countOnes += 1;
}
}
if (parity == "even") {
if (countOnes % 2 == 0) {
res = 0;
} else {
res = 1;
}
} else {
if (countOnes % 2 !== 0) {
res = 0;
} else {
res = 1;
}
}
return res;
}
function allNumeric(inputText) {
return /^\d+$/.test(inputText);
}
function allAlphaNumericUnderscore(inputText) {
return /^[A-Za-z_0-9]+$/.test(inputText);
}
function allHex(inputText) {
return /^[A-Fa-f0-9]+$/.test(inputText);
}
function allBinary(inputText) {
return /^[0-1]+$/.test(inputText);
}
function splitIntoPairs(inputString) {
return inputString.match(/(..?)/g);
}
function dictionaryKeySearch(inputDictionary, inputSearchWord){
var ret = null;
for (let i in inputDictionary) {
if(inputSearchWord.toString().trim().toLowerCase() == i.toString().trim().toLowerCase()){
ret = inputDictionary[i];
break;
}
}
return ret;
}
function dictionaryToArray(inputDictionary){
var ret = [];
for (let i in inputDictionary) {
ret.push([i, inputDictionary[i]]);
}
return ret;
}