forked from CubeCoders/GenericConfigGen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.js
59 lines (54 loc) · 2.19 KB
/
common.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
String.prototype.contains = function (contains) { return this.indexOf(contains) > -1; };
Array.prototype.contains = function (contains) { return this.indexOf(contains) > -1; };
String.prototype.isEmptyOrWhitespace = function () { return this.match(/^\s*$/); };
String.prototype.pad = function (size) {
var s = String(this);
if (typeof (size) !== "number") { size = 2; }
while (s.length < size) { s = "0" + s; }
return s;
};
Number.prototype.pad = String.prototype.pad;
if (!String.prototype.format) {
String.prototype.format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
if (!String.prototype.template) {
String.prototype.template = function (obj) {
return this.replace(/{{\$(.+?)}}/g, function (match, field) {
return typeof obj[field] != 'undefined'
? obj[field]
: match
;
});
};
}
function WildcardToRegex(pattern) {
if (pattern == null) { return null; }
var escapeReplace = function (data, original, replacement) {
var searchRegex = new RegExp("\\\\+\\" + original);
var newRegex = data.replace(searchRegex, function (match) {
var count = match.length - 1;
var halfCount = Math.floor(count / 2);
var newSlashes = Array(halfCount).join("\\");
var result = newSlashes + ((halfCount % 2 === 0) ? replacement : original);
return result;
});
return newRegex;
};
var toRegex = function (pattern, starMatchesEmpty) {
var reg = "^" + pattern.replace(/([.*+?^${}()|[\]/\\])/g, "\\$1") + "$";
reg = reg.replace(/\d+/g, "\\d+"); // replace all numbers with \d+
reg = reg.replace(/\s+/g, "\\s+"); // replace all numbers with \d+
reg = reg.replace(/\\\*\\\{(\w+)\\\}/g, "(?<$1>.+)"); // replace *{} with named capture group
reg = reg.replace(/\(\?<misc>\.\+\)/g, ".*"); // replace *{} with named capture group
return reg;
};
return toRegex(pattern, false);
}