-
Notifications
You must be signed in to change notification settings - Fork 0
/
2json
executable file
·160 lines (142 loc) · 3.16 KB
/
2json
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
var eachline= require("eachline"),
optimist= require("optimist")
//// UTIL
var intTest= /^\-?([0-9]+)$/,
floatTest= /^\-?([0-9]+\.[0-9]*)$/
function printIt(v){
if(!v)
return "null"
var floatExec= floatTest.exec(v)
if(floatExec){
return parseFloat(floatExec[1])
}else{
var intExec= intTest.exec(v)
if(intExec)
return parseInt(intExec[1])
}
v= v.replace(/"/g, '\"')
return '"'+v+'"'
}
function trim(a){
for(var i = a.length-1; i > 0; --i){
if(!a[i])
continue
a= Array.prototype.splice.call(a,i+1)
break
}
return a
}
//// ARG PROCESSING
var argv = optimist
.usage("Usage: $0 [-w x,x,..,x] [-d '\s]'")
.options("w", {alias: "words", default: "-1"})
.options("d", {alias: "delim", default: "\\s"})
.options("c", {alias: "callback"})
.options("o", {alias: "object"})
.options("n", {alias: "newline", default: "false"})
.options("bare", {alias: "b"})
.options("slots", {alias: "s"})
.argv;
if(argv.bare && (argv.o || argv.c)){
console.error("Bare mode precludes encasing options")
process.exit(1)
}
var delim= argv.d,
data= "[^"+delim+"]",
newline= argv.n == true.toString() || argv.n == true
if(argv.w == "-1")
argv.w = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1"
var wordMatches= []
wordMatches= argv.w.toString().split(",").map(function(c,w){
if(!c)
throw "Invalid word expression- empty"
if(isNaN(c))
throw "Invalid word expression- not a number"
var expr= ["(?:",delim,"*("],
words= parseInt(c)
if(words == -1)
return
if(words < 1)
throw "Must have positive words"
for(var i= 0; i< words; ++i){
expr.push("(?:",i==0?"":(delim+"+"),data,"+)?")
}
expr.push("))?")
return expr.join("")
})
if(argv.slots){
argv.slots= argv.slots.split(",")
argv.slots.forEach(function(v,k){
argv.slots[k]= printIt(v)
})
}
if(argv.c){
if(argv.c === true || argv.c == "true"){
process.stdout.write("callback(")
}else{
process.stdout.write(argv.c+"(")
}
}
//// MAIN PROGRAM
if(argv.o){
process.stdout.write('{"'+argv.o+'":')
}
if(!argv.bare){
process.stdout.write("[")
}
var matcher= wordMatches.join(""),
expr= new RegExp(matcher),
started= false
process.stdin.pipe(eachline(function(line){
if(line == "")
return ""
var buf= [","]
if(!started){
started= true
buf.pop()
}else if(newline){
buf.push("\n")
}
var res= expr.exec(line)
trim(res)
if(!res){
buf.push("null")
}else if(res.length == 2){
buf.push(printIt(res[1]))
}else if(argv.slots){
var i = 1
buf.push("{", argv.slots[i-1], ":", printIt(res[i]))
for(++i; i-1 < argv.slots.length; ++i){
buf.push(",", argv.slots[i-1], ":", printIt(res[i]))
}
if(i < res.length){
buf.push(',"_extra":[', printIt(res[i]))
for(; ++i < res.length;){
buf.push(",",printIt(res[i]))
}
buf.push("]")
}
buf.push("}");
}else{
buf.push("[",printIt(res[1]))
for(var i= 2; i< res.length; ++i){
var r= res[i]
if(r)
buf.push(",",printIt(res[i]))
}
buf.push("]")
}
return buf.join("")
})).pipe(process.stdout)
process.stdin.on("end",function(){
if(!argv.bare){
process.stdout.write("]")
}
if(argv.o){
process.stdout.write("}")
}
if(argv.c){
process.stdout.write(")")
}
})