-
Notifications
You must be signed in to change notification settings - Fork 16
/
re-tree.js
90 lines (82 loc) · 2.35 KB
/
re-tree.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
/* global window */
/* global angular */
/* global module */
(function (module, window, angular) {
"use strict";
function test(string, regex) {
if (typeof regex === 'string' || regex instanceof String) {
regex=new RegExp(regex);
}
if (regex instanceof RegExp) {
return regex.test(string);
}
else if (regex && Array.isArray(regex.and)) {
return regex.and.every(function (item) {
return test(string, item);
});
}
else if (regex && Array.isArray(regex.or)) {
return regex.or.some(function (item) {
return test(string, item);
});
}
else if (regex && regex.not) {
return !test(string, regex.not);
}
else {
return false;
}
}
function exec(string, regex) {
if (typeof regex === 'string' || regex instanceof String) {
regex=new RegExp(regex);
}
if (regex instanceof RegExp) {
return regex.exec(string);
}
else if (regex && Array.isArray(regex)) {
return regex.reduce(function (res, item) {
return (!!res) ? res : exec(string, item);
}, null);
}
else if (regex && Array.isArray(regex.and)) {
if(test(string,regex))
{
return exec(string,regex.and);
}
else
{
return null;
}
}
else if (regex && Array.isArray(regex.or)) {
return exec(string,regex.or);
}
else if (regex && regex.not) {
return !exec(string, regex.not)?[]:null;
}
else {
return null;
}
}
if (!!angular) {
angular.module("reTree", []).factory("reTree", [function () {
return {
test: test,
exec: exec
};
}]);
}
if (!!window) {
window.reTree = {
test: test,
exec: exec
};
}
if (!!module && !!(module.exports)) {
module.exports = {
test: test,
exec: exec
};
}
})(typeof module === 'undefined' ? null : module, typeof window === 'undefined' ? null : window, typeof angular === 'undefined' ? null : angular);