-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
65 lines (59 loc) · 1.5 KB
/
tools.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
function Tools(){}
/// to recursively parse strings with arrays
Tools.parse_array_str = function(str){
i = 0;
start = 0;
open_count = 0;
closed_count = 0;
var arr = [];
for (var i = 0; i < str.length; i++){
var has_inner_arrays = false;
if (str[i] === "["){
open_count++;
if (open_count === 1)
start = i;
if (open_count > 1)
has_inner_arrays = true;
}
else if (str[i] === "]")
closed_count++;
if ((closed_count !== 0) && (open_count === closed_count)){
if (has_inner_arrays)
arr.push(parse_array_str(str.substring(start + 1, i)));
else {
// arr.push(str.substring(start + 1, i).split(','));
arr.push(str.substring(start + 1, i));
}
open_count = 0;
closed_count = 0;
}
}
return arr;
};
Tools.smart_split = function(str){
i = 0;
start = 0;
open_count = 0;
closed_count = 0;
var arr = [];
for (var i = 0; i < str.length; i++){
if (str[i] === "[") {
open_count++;
}
else if (str[i] === "]")
closed_count++;
else if (str[i] === "," && open_count === closed_count) {
arr.push(str.substring(start, i));
start = i + 1;
}
}
arr.push(str.substring(start));
return arr;
}
Tools.removeBraces = function(str){
return str.replace(/[\]\[]{1,}/gi, "");
};
Tools.removeOuterBraces = function(str){
var str = str.match(/\[.+\]/)[0];
return str.substring(1, str.length - 1)
}