-
Notifications
You must be signed in to change notification settings - Fork 42
/
dutils.js
54 lines (40 loc) · 1.09 KB
/
dutils.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
var dutils = {};
dutils.conf = {};
dutils.urls = function(){
function _get_path(name, kwargs, urls)
{
var path = urls[name] || false;
if (!path)
{
throw('URL not found for view: ' + name);
}
var _path = path;
var key;
for (key in kwargs)
{
if (kwargs.hasOwnProperty(key)) {
if (!path.match('<' + key +'>'))
{
throw(key + ' does not exist in ' + _path);
}
path = path.replace('<' + key +'>', kwargs[key]);
}
}
var re = new RegExp('<[a-zA-Z0-9-_]{1,}>', 'g');
var missing_args = path.match(re);
if (missing_args)
{
throw('Missing arguments (' + missing_args.join(", ") + ') for url ' + _path);
}
return path;
}
return {
resolve: function(name, kwargs, urls) {
if (!urls)
{
urls = dutils.conf.urls || {};
}
return _get_path(name, kwargs, urls);
}
};
}();