-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
54 lines (49 loc) · 1.64 KB
/
utils.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
function parseIniString(data) {
let regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([^=]+?)\s*=\s*(.*?)\s*$/,
comment: /^\s*;.*$/
};
let value = {};
let lines = data.split(/[\r\n]+/);
let section = null;
lines.forEach(line => {
if (regex.comment.test(line)) {
} else if (regex.param.test(line)) {
const match = line.match(regex.param);
if (section) {
value[section][match[1]] = match[2];
} else {
value[match[1]] = match[2];
}
} else if (regex.section.test(line)) {
const match = line.match(regex.section);
value[match[1]] = {};
section = match[1];
} else if (line.length == 0 && section) {
section = null;
}
});
return value;
}
function stringifyProfileCreds(profileCreds) {
return Object.entries(profileCreds).reduce((acc, [key, val]) => acc + '[' + key + ']\n' + stringifyCreds(val) + '\n', '');
}
function stringifyCreds(creds) {
return Object.entries(creds).reduce((acc, [key, val]) => acc + key + ' = ' + val + '\n', '');
}
const toTitleCaseLowers =
'a an and as at but by for for from in into near nor of on onto or the to with'.split(
' ',
);
function toTitleCase(str) {
const lowers = toTitleCaseLowers.map(lower => '\\s' + lower + '\\s');
const regexString = '(?!' + lowers.join('|') + '})\\w\\S*';
const regex = new RegExp(regexString, 'g');
return str.replace(
regex,
txt =>
txt.charAt(0).toUpperCase() +
txt.substr(1).replace(/[A-Z]/g, word => ' ' + word),
);
}