-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
248 lines (194 loc) · 4.32 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
'use strict'
// Flags Characters
// 0 1 2 3 4 5
// ------------------------------------------------------------------------
// \ ' " normal space \n
// e,sq n/a n/a n/a n/a n/a n/a
// 0 ue,sq a \ suq a " a + a _ EOF
// 1 e,dq a \,ue a \',ue a ",ue a \+,ue a \_,ue ue
// 2 ue,dq e a ' duq a + a _ EOF
// 3 e,uq a \,ue a \',ue a \",ue a \+,ue a _,ue ue
// 4 ue,uq e sq dq a + tp EOF
const MATRIX = {
// object is more readable than multi-dim array.
0: [a, suq, a, a, a, EOF],
1: [eaue, aue, eaue, aue, aue, ue],
2: [e, a, duq, a, a, EOF],
3: [eaue, aue, aue, aue, eaue, ue],
4: [e, sq, dq, a, tp, EOF]
}
// - a: add
// - e: turn on escape mode
// - ue: turn off escape mode
// - q: turn on quote mode
// - sq: single quoted
// - dq: double quoted
// - uq: turn off quote mode
// - tp: try to push if there is something in the stash
// - EOF: end of file(input)
let escaped = false // 1
let single_quoted = false // 2
let double_quoted = false // 4
let ended = false
const FLAGS = {
2: 0,
5: 1,
4: 2,
1: 3,
0: 4
}
function y () {
let sum = 0
if (escaped) {
sum ++
}
if (single_quoted) {
sum += 2
}
if (double_quoted) {
sum += 4
}
return FLAGS[sum]
}
const BACK_SLASH = '\\'
const SINGLE_QUOTE = "'"
const DOUBLE_QUOTE = '"'
const WHITE_SPACE = ' '
const LINE_FEED = '\n'
function x () {
return c in CHARS
? CHARS[c]
: CHARS.NORMAL
}
const CHARS = {
[BACK_SLASH]: 0,
[SINGLE_QUOTE]: 1,
[DOUBLE_QUOTE]: 2,
NORMAL: 3,
[WHITE_SPACE]: 4,
[LINE_FEED]: 5
}
let c = ''
let stash = ''
let ret = []
function reset () {
escaped = false
single_quoted = false
double_quoted = false
ended = false
c = ''
stash = ''
ret.length = 0
}
function a () {
stash += c
}
function sq () {
single_quoted = true
}
function suq () {
single_quoted = false
}
function dq () {
double_quoted = true
}
function duq () {
double_quoted = false
}
function e () {
escaped = true
}
function ue () {
escaped = false
}
// add a backslash and a normal char, and turn off escaping
function aue () {
stash += BACK_SLASH + c
escaped = false
}
// add a escaped char and turn off escaping
function eaue () {
stash += c
escaped = false
}
// try to push
function tp () {
if (stash) {
ret.push(stash)
stash = ''
}
}
function EOF () {
ended = true
}
function split (str) {
if (typeof str !== 'string') {
type_error(`\`str\` must be a string. Received ${str}`, 'NON_STRING')
}
reset()
const length = str.length
let i = -1
while (++ i < length) {
c = str[i]
MATRIX[y()][x()]()
if (ended) {
break
}
}
if (single_quoted) {
error('unmatched single quote', 'UNMATCHED_SINGLE')
}
if (double_quoted) {
error('unmatched double quote', 'UNMATCHED_DOUBLE')
}
if (escaped) {
error('unexpected end with \\', 'ESCAPED_EOF')
}
tp()
return ret
}
function error (message, code) {
const err = new Error(message)
err.code = code
throw err
}
function type_error (message, code) {
const err = new TypeError(message)
err.code = code
throw err
}
const REGEX_NEED_QUOTE = /\s|"|'/
const CLI_LINE_FEED = '\\\n'
const LF = Symbol.for('argv-split:LF')
function join(args, {
quote = DOUBLE_QUOTE
} = {}) {
if (![SINGLE_QUOTE, DOUBLE_QUOTE].includes(quote)) {
type_error(
`\`options.quote\` must be either ${SINGLE_QUOTE} or ${DOUBLE_QUOTE}. Received ${quote}` , 'INVALID_QUOTE'
)
}
const process_arg = arg => {
if (!REGEX_NEED_QUOTE.test(arg)) {
return arg
}
if (!arg.includes(quote)) {
return quote + arg + quote
}
// escape quote
const escaped = arg.replace(new RegExp(quote, 'g'), `\\${quote}`)
return quote + escaped + quote
}
let joined = ''
for (const arg of args) {
if (arg === LF) {
joined += CLI_LINE_FEED
continue
}
joined += process_arg(arg) + WHITE_SPACE
}
return joined.trimRight()
}
module.exports = split
split.join = join
split.LF = LF