-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata-from-url.js
36 lines (33 loc) · 990 Bytes
/
data-from-url.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
var replacer = /\{([^\}]+)\}/g;
// Returns data from a url, given a fixtue URL. For example, given
// "todo/{id}" and "todo/5", it will return an object with an id property
// equal to 5.
module.exports = function dataFromUrl(fixtureUrl, url) {
if(!fixtureUrl) {
// if there's no url, it's a match
return {};
}
var order = [],
// Sanitizes fixture URL
fixtureUrlAdjusted = fixtureUrl.replace('.', '\\.')
.replace('?', '\\?'),
// Creates a regular expression out of the adjusted fixture URL and
// runs it on the URL we passed in.
res = new RegExp(fixtureUrlAdjusted.replace(replacer, function (whole, part) {
order.push(part);
return "([^\/]+)";
}) + "$")
.exec(url),
data = {};
// If there were no matches, return null;
if (!res) {
return null;
}
// Shift off the URL and just keep the data.
res.shift();
order.forEach( function (name) {
// Add data from regular expression onto data object.
data[name] = res.shift();
});
return data;
};