-
Notifications
You must be signed in to change notification settings - Fork 4
/
identifier.js
81 lines (72 loc) · 1.87 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use strict";
exports.isAbsolute = isAbsolute;
function isAbsolute(path) {
return (
path !== "" &&
path.lastIndexOf("./", 0) < 0 &&
path.lastIndexOf("../", 0) < 0
);
}
exports.isBare = isBare;
function isBare(id) {
var lastSlash = id.lastIndexOf("/");
return id.indexOf(".", lastSlash) < 0;
}
// TODO @user/name package names
exports.head = head;
function head(id) {
var firstSlash = id.indexOf("/");
if (firstSlash < 0) { return id; }
return id.slice(0, firstSlash);
}
exports.tail = tail;
function tail(id) {
var firstSlash = id.indexOf("/");
if (firstSlash < 0) { return ""; }
return id.slice(firstSlash + 1);
}
exports.extension = extension;
function extension(id) {
var lastSlash = id.lastIndexOf("/");
var lastDot = id.lastIndexOf(".");
if (lastDot <= lastSlash) { return ""; }
return id.slice(lastDot + 1);
}
exports.dirname = dirname;
function dirname(id) {
var lastSlash = id.lastIndexOf("/");
if (lastSlash < 0) {
return id;
}
return id.slice(0, lastSlash);
}
exports.basename = basename;
function basename(id) {
var lastSlash = id.lastIndexOf("/");
if (lastSlash < 0) {
return id;
}
return id.slice(lastSlash + 1);
}
exports.resolve = resolve;
function resolve(rel, abs) {
abs = abs || "";
var source = rel.split("/");
var target = [];
var parts;
if (source.length && source[0] === "." || source[0] === "..") {
parts = abs.split("/");
parts.pop();
source.unshift.apply(source, parts);
}
for (var index = 0; index < source.length; index++) {
if (source[index] === "..") {
if (target.length) {
target.pop();
}
} else if (source[index] !== "" && source[index] !== ".") {
target.push(source[index]);
}
}
return target.join("/");
}