-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.js
36 lines (34 loc) · 1.2 KB
/
match.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
// compare a string to a string or a regex
function isMatch(r, s) {
console.log('matching', s, 'against',r);
// is r a regexp?
if (r[0] === '/' && r[r.length-1] === '/') {
r = new RegExp(r.substr(1, r.length-2));
var result = r.test(s);
if (result) console.log(s, 'matches', r);
return result;
}
return r == s;
}
// return the index of the reading list item that
// the student is currently looking at (the current url is passed as href)
module.exports = function(rl, href) {
for(var i = 0; i<rl.resources.length; ++i) {
var resource = rl.resources[i];
// check the pages first
if (resource.pages && resource.pages.length) {
for(var ii=0; ii<resource.pages.length; ++ii) {
var page = resource.pages[ii];
if (typeof page === 'string') {
if (isMatch(page, href)) return {step:i, page: ii};
} else {
if (isMatch(page.url, href)) return {step:i, page: ii};
}
}
}
// now check resource's entry
if (isMatch(resource.entry, href)) return {step: i};
}
// found no match
return null;
};