-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
57 lines (51 loc) · 1.51 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
55
56
57
let parseCommand = function parseCommand(command, msg) {
const words = splitString(msg)
console.log(words)
return {
"command": command,
"action": words[0].toLowerCase(),
"name": words[1].toLowerCase(),
"parameter": words[2]
}
}
let splitString = function splitString(s) {
let clean = naiveClean(s)
//finds the tokens in a string
let matches = clean.match(/[\""].+?[\""]|[^ ]+/g)
//console.log(matches)
//but surrounding quotes are still there so remove
if (matches) {
for (var i=0, len=matches.length; i<len; i++) {
matches[i] = matches[i].replace(/^"(.*)"$/, '$1')
}
}
return matches
}
function naiveClean(s) {
return s.replace(/\"\"/g,"\"")
}
let cleanQuotes = function cleanQuotes(s) {
clean = s
if (clean.startsWith("\"") && clean.endsWith("\"")) {
clean = clean.substr(1).slice(0,-1)
}
if (clean.startsWith("\'") && clean.endsWith("\'")) {
clean = clean.substr(1).slice(0,-1)
}
return clean
}
let urlString = function urlString(url, query) {
let b64 = Buffer.from(query).toString('base64')
//console.log(b64);
return url + b64;
}
let isStart = function isStart(action) { return action.toLowerCase() === 'on' || action.toLowerCase() === 'start'}
let isStop = function isStop(action) { return action.toLowerCase() === 'off' || action.toLowerCase() === 'stop'}
module.exports = {
parseCommand: parseCommand,
splitString: splitString,
cleanQuotes: cleanQuotes,
urlString: urlString,
isStart: isStart,
isStop: isStop
}