forked from montagejs/mr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
identifier.js
59 lines (53 loc) · 1.28 KB
/
identifier.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
55
56
57
58
// Resolves CommonJS module IDs (not paths)
exports.resolve = resolve;
function resolve(id, baseId) {
id = String(id);
var source = id.split("/");
var target = [];
if (source.length && source[0] === "." || source[0] === "..") {
var parts = baseId.split("/");
parts.pop();
source.unshift.apply(source, parts);
}
for (var i = 0, ii = source.length; i < ii; i++) {
/*jshint -W035 */
var part = source[i];
if (part === "" || part === ".") {
} else if (part === "..") {
if (target.length) {
target.pop();
}
} else {
target.push(part);
}
/*jshint +W035 */
}
return target.join("/");
}
exports.normalize = normalize;
function normalize(id) {
var match = /^(.*)\.js$/.exec(id);
if (match) {
id = match[1];
}
return id;
}
exports.extension = extension;
function extension(location) {
var match = /\.([^\/\.]+)$/.exec(location);
if (match) {
return match[1];
}
}
exports.isRelative = isRelative;
function isRelative(id) {
return /^\.\.?[/]/.test(id);
}
exports.split = split;
function split(id) {
var match = /([^\/]+)\/(.*)/.exec(id);
return {
name: match[1],
id: match[2]
};
}