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