forked from i-rocky/country-list-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (31 loc) · 753 Bytes
/
server.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
const country = require('./index');
module.exports = (req) => {
var cmd = url(req.url);
var fn = country[cmd.method];
var p = params(fn).map(nm => cmd[nm])
return fn.apply(country, p);
}
function url(s) {
var ret = {};
var [, args] = s.split('?');
if (!args) return ret;
var r = args.split(/[&=]/);
for (var i = 0; i < r.length; i += 2) {
ret[r[i]] = r[i+1];
}
return ret;
}
function params(f) {
var s = f.toString();
var re = [
/function\s+\w+\((.*?)\)/,
/function\s*\((.*?)\)/,
/\((.*?)\)\s*=>/,
/(\w+)\s*=>/
]
for (var i = 0; i < re.length; i++) {
var r = s.match(re[i]);
if (r) return r[1].split(',');
}
return [];
}