-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathutilsprey.js
More file actions
66 lines (60 loc) · 1.92 KB
/
utilsprey.js
File metadata and controls
66 lines (60 loc) · 1.92 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
// eslint-disable-next-line consistent-return
const isBoolean = (type) => {
if (typeof type === 'string') {
const resp = type.trim().toLowerCase();
return resp === 'true';
}
if (typeof type === 'number' && type !== 1 && type !== 0) return false;
return Boolean(type);
};
const stringBooleanOrEmpty = (str) => {
// eslint-disable-next-line no-constant-condition
if (!str && str !== false) return '';
const stringStr = str.toString();
if (stringStr.localeCompare('Allow') === 0) return 'true';
if (stringStr.localeCompare('Deny') === 0) return 'false';
if (/^(true|false)$/.test(stringStr)) return stringStr;
return '';
};
const splitGfromString = (str) => {
const separatedG = str.split('g');
if (separatedG.length > 1) {
const regex = /-?\d+/g;
const matches = separatedG[1].match(regex);
if (!matches) return str;
return matches[0];
}
return str;
};
const getChannelDifFormat = (str) => {
let dataSeparator = '';
const splittedStr = str.split('(');
if (splittedStr.length > 1) {
const trimmedStr = splittedStr[0].trim();
dataSeparator = trimmedStr;
}
if (dataSeparator.localeCompare('') !== 0) {
return splitGfromString(dataSeparator);
}
return splitGfromString(str);
};
const getInformationChannel = (str) => {
const regex = /\d+(?= \(.*\))/;
const match = str.match(regex);
const number = match ? match[0] : '';
if (number.localeCompare('') === 0) {
return getChannelDifFormat(str);
}
return number;
};
const removeBackslash = (str) => {
let stringData = str;
if (typeof stringData === 'object') stringData = JSON.stringify(stringData);
return stringData.replace(/\\/g, '');
};
exports.getChannelDifFormat = getChannelDifFormat;
exports.getInformationChannel = getInformationChannel;
exports.isBoolean = isBoolean;
exports.stringBooleanOrEmpty = stringBooleanOrEmpty;
exports.removeBackslash = removeBackslash;
exports.splitGfromString = splitGfromString;