-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.js
57 lines (46 loc) · 1.26 KB
/
parser.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
;(function() {
function parse(url, config, callback) {
getPackageData(url, getDependencies)
function getDependencies(text) {
var packageData = config.parse(text)
var deps = filter(packageData[config.keys[0]], config.filter)
var devDeps = filter(packageData[config.keys[1]], config.filter)
callback(deps, devDeps, config)
}
}
function getPackageData(url, callback) {
var xhr = new XMLHttpRequest()
xhr.addEventListener('load', listener)
xhr.open('GET', url)
xhr.send()
function listener() {
var parser = new DOMParser()
var doc = parser.parseFromString(this.responseText, 'text/html')
var blob = doc.querySelector('.blob-wrapper > table')
callback(blob.textContent)
}
}
function filter(deps, configFilter) {
if (!configFilter) {
return deps
}
for (name in deps) {
if (!configFilter.test(name)) {
delete deps[name]
}
}
return deps
}
function json(text) {
return JSON.parse(text)
}
function toml(text) {
var nodes = window.toml.parse(text)
return window.toml.compile(nodes)
}
window.parse = parse
window.parser = {
json: json,
toml: toml
}
})()