-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavascriptTester.js
More file actions
98 lines (87 loc) · 3.27 KB
/
JavascriptTester.js
File metadata and controls
98 lines (87 loc) · 3.27 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
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
var JavascriptTester = function() {
};
JavascriptTester.prototype = {
testRegex : function(config, testStrings) {
var result = {
"type": "",
"resultList": [],
"exception": ""
};
try {
var flags = "";
if(config["global"])
flags += "g";
if(config["ignore"])
flags += "i";
if(config["multiline"])
flags += "m";
if(config["unicode"])
flags += "u";
if(config["sticky"])
flags += "y";
if(!config["test_type"])
throw new Error("'test_type' parameter doesn't exists.");
var regex = new RegExp(config["regex"], flags);
switch(config["test_type"]) {
case "match":
result["type"] = "MATCH";
testStrings.forEach(function(testString){
result["resultList"].push(regex.test(testString) ? true : false);
});
break;
case "group":
result["type"] = "GROUP";
result["columns"] = [];
var groupCount = (new RegExp(regex.toString() + '|')).exec('').length;
for(var i = 0; i < groupCount; i++) {
result["columns"].push("Group #" + i);
}
testStrings.forEach(function(testString) {
var groupsList = {"list": []};
var lastIndex = -1;
while((match = regex.exec(testString)) != null) {
if(lastIndex == match.lastIndex) {
break;
}
lastIndex = match.lastIndex;
var groups = [];
match.forEach(function(group){
groups.push(group);
});
groupsList["list"].push(groups);
}
if(groupsList["list"].length > 0) {
result["resultList"].push(groupsList);
} else {
result["resultList"].push(null);
}
});
break;
case "replace":
result["type"] = "STRING";
var replaceString = config["replace"];
testStrings.forEach(function(testString) {
result["resultList"].push(testString.replace(regex, replaceString));
});
break;
}
} catch(e) {
result["exception"] = e.message;
} finally {
return result;
}
}
};
var main = function(){
config = JSON.parse(process.argv[2]);
testStrings = JSON.parse(process.argv[3]);
var tester = new JavascriptTester();
result = tester.testRegex(config, testStrings);
console.log("##START_RESULT##");
console.log(JSON.stringify(result));
console.log("##END_RESULT##");
}
if (require.main === module) {
main();
}
module.exports = JavascriptTester;