-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standardizer.js
72 lines (62 loc) · 1.52 KB
/
standardizer.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
const get = require('simple-get')
const url = 'https://standardizer.glitch.me'
const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
const version = url + '/version'
const lint = url + '/lint'
const fix = url + '/fix'
const headers = {
'user-agent': userAgent
}
module.exports = {
version: function (cb) {
const opts = {
method: 'GET',
url: version,
json: true,
headers
}
process(opts, function (err, versions) {
if (err) return cb(err)
const text = Object.keys(versions).reduce(function (p, c, i) {
p += i > 0 ? ', ' : ''
return p + c + '@' + versions[c]
}, '')
return cb(null, text)
})
},
lint: function (text, cb) {
if (!text) return cb(null, [])
const opts = {
method: 'POST',
url: lint,
json: true,
body: { text },
headers
}
process(opts, function (err, resp) {
if (err) return cb(err)
return cb(null, resp[0].messages)
})
},
fix: function (text, cb) {
if (!text) return cb(null, [])
const opts = {
method: 'POST',
url: fix,
json: true,
body: { text },
headers
}
process(opts, function (err, resp) {
if (err) return cb(err)
const fixedText = resp[0].output || text
return cb(null, fixedText)
})
}
}
function process (opts, cb) {
get.concat(opts, function (err, res, data) {
if (err) return cb(err)
cb(null, data)
})
}