-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (36 loc) · 948 Bytes
/
index.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
function isEmpty(object) {
for(var prop in object) { return!1; }
return!0;
}
function toQueryString(data) {
var queryString = '?';
for (var param in data) {
if (data.hasOwnProperty(param)) {
queryString += param + '=' + data[param] + '&';
}
}
return queryString.slice(0, queryString.length-1);
}
function composeUrl(urlString, data) {
if (typeof data != 'object') {
console.warn('data must be an object');
return false;
};
var queryString = {};
if (isEmpty(data)) {
return urlString;
} else {
for (var key in data) {
if (new RegExp('\/:'+key+'(\/|\.|$)').test(urlString)) {
urlString = urlString.replace(':'+key, data[key]);
} else {
queryString[key] = data[key];
}
}
if (!isEmpty(queryString)) {
urlString += toQueryString(queryString);
}
return urlString;
}
}
module.exports = composeUrl;