splitargs.js
1.49 KB
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
/**
* Created by elgs on 7/2/14.
*/
(function () {
"use strict";
module.exports = function (input, sep, keepQuotes) {
var separator = sep || /\s/g;
var singleQuoteOpen = false;
var doubleQuoteOpen = false;
var tokenBuffer = [];
var ret = [];
var arr = input.split('');
for (var i = 0; i < arr.length; ++i) {
var element = arr[i];
var matches = element.match(separator);
if (element === "'" && !doubleQuoteOpen) {
if (keepQuotes === true) {
tokenBuffer.push(element);
}
singleQuoteOpen = !singleQuoteOpen;
continue;
} else if (element === '"' && !singleQuoteOpen) {
if (keepQuotes === true) {
tokenBuffer.push(element);
}
doubleQuoteOpen = !doubleQuoteOpen;
continue;
}
if (!singleQuoteOpen && !doubleQuoteOpen && matches) {
if (tokenBuffer.length > 0) {
ret.push(tokenBuffer.join(''));
tokenBuffer = [];
} else if (!!sep) {
ret.push(element);
}
} else {
tokenBuffer.push(element);
}
}
if (tokenBuffer.length > 0) {
ret.push(tokenBuffer.join(''));
} else if (!!sep) {
ret.push('');
}
return ret;
};
})();