forked from jonschlinkert/relative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (130 loc) · 3.11 KB
/
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
var isObject = require('isobject');
var path = require('path');
var fs = require('fs');
/**
* Expose `relative`
*/
module.exports = relative;
/**
* Get the relative path from `a` to `b`.
*/
function relative(a, b, stat) {
if (typeof a !== 'string') {
throw new TypeError('relative expects a string.');
}
if (a == '' && !b) return a;
var len = arguments.length;
if (len === 1) {
b = a; a = process.cwd(); stat = null;
}
if (len === 2 && typeof b === 'boolean') {
b = a; a = process.cwd(); stat = true;
}
if (len === 2 && typeof b === 'object') {
stat = b; b = a; a = process.cwd();
}
var origB = b;
// see if a slash exists before normalizing
var slashA = endsWith(a, '/');
var slashB = endsWith(b, '/');
a = unixify(a);
b = unixify(b);
// if `a` had a slash, add it back
if (slashA) { a = a + '/'; }
if (isFile(a, stat)) {
a = path.dirname(a);
}
var res = path.relative(a, b);
if (res === '') {
return '.';
}
// if `b` originally had a slash, and the path ends
// with `b` missing a slash, then re-add the slash.
var noslash = trimEnd(origB, '/');
if (slashB && (res === noslash || endsWith(res, noslash))) {
res = res + '/';
}
return res;
}
/**
* Get the path relative to the given base path.
*/
relative.toBase = function toBase(base, fp) {
base = unixify(base);
fp = unixify(fp);
var res = fp.slice(base.length);
if (res.charAt(0) === '/') {
res = res.slice(1);
}
return res;
};
/**
* Normalize slashes in paths to unix slashes. This is necessary since
* paths are not calculated the same by node.js when
* windows paths are used.
*/
function unixify(str) {
return str.replace(/[\\\/]+/g, '/');
}
/**
* Remove the given character from the path.
*/
function trimEnd(fp, ch) {
return fp.slice(0, fp.length - ch.length);
}
/**
* Does the path end with the given `ch`aracter?
*/
function endsWith(fp, ch) {
return fp.slice(-ch.length)[0] === ch;
}
/**
* If `fp` exists, return a `stat` object,
* otherwise return `null`.
*/
function tryStats(fp) {
try {
return fs.statSync(fp);
} catch(err) {}
return null;
}
/**
* Returns true if `fp` is a directory. To use a `fs`
* stat object to actually check the file system,
* either pass `true` as the second arg, or pass your
* own stat object as the second arg.
*
* @param {String} `fp`
* @param {Boolean|Object} `stat`
* @return {Boolean}
*/
function isDir(fp, stat) {
if (endsWith(fp, '/')) {
return true;
}
if (stat === null) {
// try to get the directory info if it hasn't been done yet
// to ensure directories containing dots are well handle
stat = tryStats(fp);
}
if (isObject(stat) && typeof stat.isDirectory === 'function') {
return stat.isDirectory();
}
var segs = fp.split('/');
var last = segs[segs.length - 1];
if (last && last.indexOf('.') !== -1) {
return false;
}
return true;
}
/**
* Return true if `fp` looks like a file, or
* actually is a file if fs.stat is used.
*/
function isFile(fp, stat) {
if (stat === true) {
stat = tryStats(fp);
}
return !isDir(fp, stat);
}