forked from LittoCats/react-native-path
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (134 loc) · 3.2 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
/**
path.basename(path[, ext])
path.dirname(path)
path.extname(path)
path.format(pathObject)
path.isAbsolute(path)
path.join([...paths])
path.normalize(path)
path.parse(path)
path.relative(from, to)
path.resolve([...paths])
path.sep
*/
function _normalize(path) {
valid(path);
return `${path}/`.split(/\/+/g).reduce((previous, current) => {
if (!Array.isArray(previous)) previous = [previous];
if (current === '.') return previous;
if (current === '..') previous.pop();
else if (current.length) previous.push(current);
return previous;
}).join('/');
}
function _basename(path, ext) {
var basename = path.split(/\//g).pop();
if (ext) {
var tmp = basename.split(/\./g);
var _ext = tmp.pop();
if (ext === _ext || ext.slice(1) === _ext) {
return tmp.join('.')
}
}
return basename;
}
function _dirname(path) {
return path.split(/\//g).slice(0, -1).join('/')
}
function _extname(path) {
var tmp = path.replace(/^[\.]+/, '');
if (/\./.test(tmp)) return tmp.match(/\.[^.]*$/)[0];
return '';
}
function _format(options) {
var {
dir,
root,
base,
name,
ext
} = options;
var _dir = dir || root;
var _base = base || `${name || ''}${/^\./.test(ext) ? '' : '.'}${ext || ''}`;
return normalize(`${_dir}/${_base}`);
}
function _isAbsolute(path) {
return /^\//.test(path)
}
function _parse(path) {
var obj = {},
tmp;
var components = path.split(/\//g)
obj.base = components.pop();
obj.dir = components.join('/');
if (/^\//.test(obj.dir)) {
obj.root = '/';
}
if (obj.base != undefined) {
tmp = obj.base.replace(/^[\.]+/, '');
if (/\./.test(tmp)) {
obj.ext = tmp.match(/\.[^.]*$/)[0];
obj.name = obj.base.slice(0, -obj.ext.length);
} else {
obj.name = obj.base
}
} else {
delete obj.base
}
return obj;
}
function _resolve(segments) {
var flat = segments.reduce((previous, current) => {
if (!Array.isArray(previous)) previous = [previous];
if (Array.isArray(current)) Array.prototype.push.apply(previous, current);
Array.prototype.push.call(previous, current);
return previous;
}).reduce((previous, current) => {
if (/^\//.test(current)) return current;
return `${previous}/${current}`;
});
return _normalize(flat);
}
function _relative(base, path) {
base = base.split(/\//g);
path = path.split(/\//g);
while(base[0] === path[0]) {
base.shift();
path.shift();
}
return Array(base.length).fill('..').concat(path).join('/');
}
function valid(path) {
if (typeof path != 'string') {
throw new TypeError('path must be string.');
}
}
module.exports = exports = {
normalize(path) {
return _normalize(path);
},
basename(path, ext) {
return _basename(_normalize(path), ext);
},
dirname(path) {
return _dirname(_normalize(path));
},
extname(path) {
return _extname(_normalize(path))
},
format(options) {
return _format(options);
},
isAbsolute(path) {
return _isAbsolute(valid(path))
},
parse(path) {
return _parse(_normalize(path))
},
resolve() {
return _resolve.call({}, Array.prototype.slice.call(arguments, 0));
},
relative(base, path) {
return _relative(_normalize(base), _normalize(path))
}
}