This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vurl.js
52 lines (46 loc) · 1.39 KB
/
vurl.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
//vurl by Alan-Liang
/*Usage:
*add:
vurl.add({path:'/path',func:function(req,resp){}});
or
vurl.add=({regexp:/^\/path$/,func:function(req,resp){}});
*remove:
vurl.remove(data); //data is the original version of vurl.add
*query:
vurl.query('/data');
*debug:
vurl.debug = true;
*/
//list of VURLs, private
var list = [];
//debug argument
this.debug = false;
//add
this.add=function (a) {
this.debug && console.log(a);
var flag = false;
for (var i = 0; i < list.length; i++)
if (a && list[i].path === a.path && list[i].func === a.func)
// there is one, so don't add it again
flag = true;
if (!flag && a && (a.path!==undefined || a.regexp!==undefined) && a.func)
// correct argument
list.push(a);
};
//remove
this.remove = function (a) {
if (a) for (var i = 0; i < list.length; i++)
if (((list[i].path == a.path) || (list[i].regexp && list[i].regexp.test(a))) && list[i].func == a.func)
// yes! got it, then remove it!
list[i] = {};
};
//query
this.query = function (a) {
this.debug && console.log(a);
for (var i = 0; i < list.length; i++)
if ((list[i].regexp && list[i].regexp.test(a)) || list[i].path == a)
// yes! got it!
return list[i].func;
// no,return undefined.
return undefined;
};