1 |
|
2 | !function(exports) {
|
3 | "use strict";
|
4 | var sep = exports.sep = "/"
|
5 | exports.dirname = dirname
|
6 | exports.normalize = normalize
|
7 | exports.relative = relative
|
8 | exports.resolve = resolve
|
9 |
|
10 | function dirname(path) {
|
11 | for (var skip = 0, i = path.length; i > 0; ) {
|
12 | if (path.charCodeAt(--i) !== 47) {
|
13 | if (skip === 0) skip = i
|
14 | } else if (skip > 0) break
|
15 | }
|
16 | return i > 0 ? path.slice(0, i) : path.charCodeAt(0) === 47 ? "/" : "."
|
17 | }
|
18 |
|
19 | function normalize(path) {
|
20 | var c
|
21 | , SLASH = 47
|
22 | , DOT = 46
|
23 | , res = path
|
24 | , abs = res.charCodeAt(0) == SLASH
|
25 | , len = res.length
|
26 | , i = len
|
27 | , cut = len
|
28 | , up = 0
|
29 | , last = SLASH
|
30 |
|
31 | for (; i > 0; ) {
|
32 | c = res.charCodeAt(--i)
|
33 | if (c === SLASH) {
|
34 | if (cut === 0) {
|
35 | cut = i
|
36 | } else if (last !== SLASH && up > 0) {
|
37 | up--
|
38 | }
|
39 | } else if (last === SLASH) {
|
40 | if (c === DOT) {
|
41 | if (i > 0) {
|
42 | c = res.charCodeAt(--i)
|
43 | if (c === DOT && (i === 0 || res.charCodeAt(i - 1) === SLASH)) {
|
44 | last = SLASH
|
45 | i--
|
46 | up++
|
47 | } else if (c !== SLASH) {
|
48 | cut = 0
|
49 | }
|
50 | continue
|
51 | } else {
|
52 | break
|
53 | }
|
54 | }
|
55 | if (up === 0 && cut > 0) {
|
56 | if (len === cut) {
|
57 | if (cut > i + 2) {
|
58 | res = res.slice(0, res.charCodeAt(len - 1) === SLASH ? i + 2 : i + 1)
|
59 | }
|
60 | } else if (cut > i + 1) {
|
61 | res = res.slice(0, i + 1) + res.slice(cut)
|
62 | }
|
63 | cut = 0
|
64 | }
|
65 | }
|
66 | last = c
|
67 | }
|
68 | if (cut > 0 && i === 0) {
|
69 | res = res.slice(abs ? cut : cut + 1)
|
70 | }
|
71 | return res
|
72 | }
|
73 |
|
74 | function relative(_from, _to) {
|
75 | if (_from === _to) return ""
|
76 |
|
77 | var last, code1, code2
|
78 | , from = normalize(clear(_from))
|
79 | , to = normalize(clear(_to))
|
80 | , i = 0
|
81 | , arr = []
|
82 |
|
83 | if (from === to) return ""
|
84 |
|
85 | for (; code1 === code2; ) {
|
86 | code1 = from.charCodeAt(i)
|
87 | if (code1 === 47) last = i
|
88 | code2 = to.charCodeAt(i++)
|
89 | }
|
90 | if (code1 === code1) {
|
91 | code1 = from.length
|
92 | for (i = last++; i < code1; ) {
|
93 | if (from.charCodeAt(i++) === 47) arr.push("..")
|
94 | }
|
95 | } else {
|
96 | last = i
|
97 | }
|
98 | if (code2 === code2) {
|
99 | arr.push(to.slice(last))
|
100 | }
|
101 |
|
102 | return arr.join(sep)
|
103 | }
|
104 |
|
105 | function resolve() {
|
106 | var args = arguments
|
107 | , i = args.length
|
108 | , out = []
|
109 |
|
110 | for (; i; ) {
|
111 | if ((out[i--] = clear(args[i])).charCodeAt(0) === 47) {
|
112 | out.splice(0, i + 1)
|
113 | i = 0
|
114 | }
|
115 | }
|
116 | if (!out[0]) out[0] = process.cwd()
|
117 |
|
118 | return normalize(out.join(sep))
|
119 | }
|
120 |
|
121 | function clear(path) {
|
122 | if (typeof path !== "string") {
|
123 | throw new TypeError("Path must be a string. Got " + typeof path)
|
124 | }
|
125 | for (var len = path.length - 1, i = len; path.charCodeAt(i) === 47; i--);
|
126 | return i !== len ? path.slice(0, i < 0 ? 1 : i + 1) : path
|
127 | }
|
128 | }(this)
|
129 |
|
130 |
|
131 |
|