-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (54 loc) · 1.65 KB
/
index.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
//Dependencies
const Fs = require("fs")
//Variables
var Self = {}
//Functions
Self.get_root = function(data_eachline){
const the_root = data_eachline[i].slice(0, data_eachline[i].indexOf(">"))
return the_root
}
Self.pretty_root_data = function(rd){
var results = ""
rd = rd.replace(/\w+>/, "")
for( i in rd.split("\n") ){
if(!results.length){
results = rd.split("\n")[i].replace(/\s/g, "")
}else{
results += `\n${rd.split("\n")[i].replace(/\s/g, "")}`
}
}
return results
}
Self.parse = function(data){
var upper_root = ""
var upper_root_data = ""
var results = {}
const data_eachline = data.split("<")
for( i in data_eachline ){
if(data_eachline[i].indexOf(">") != -1 && data_eachline[i].indexOf("/") == -1){
const root = Self.get_root(data_eachline)
const root_data = Self.pretty_root_data(data_eachline[i])
if(root_data && JSON.stringify(upper_root_data).indexOf(root) == -1){
results[root] = root_data
}else if(root_data && upper_root && JSON.stringify(upper_root_data).indexOf(root) != -1){
if(!results[upper_root]){
results[upper_root] = {}
}
results[upper_root][root] = root_data
}else{
upper_root = root
upper_root_data = data_eachline.slice(i, data_eachline.indexOf(`/${root}>\n`))
}
}
}
return results
}
//Main
function parse(jtml){
const parsed_data = Self.parse(jtml)
return parsed_data
}
//Exporter
module.exports = {
parse: parse
}