-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (32 loc) · 830 Bytes
/
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
/**
* @param {string} path
* @return {string}
*/
var simplifyPath = function(path) {
if(path[path.length -1] === '/'){
path = path.slice(0, -1);
}
if(path[0] === '/'){
path = path.slice(1);
}
console.log(path);
let arr = path.split('/');
let ans = [];
for(var i = 0; i< arr.length; i++){
if( /^\.\.$/.test(arr[i])){
console.log(i);
if( !/^\.\.$/.test(ans[ans.length - 1]) && ans.length>0){
ans.pop();
}else{
// ans.push(arr[i]);
continue;
}
}else if(arr[i] === '.' || arr[i] ===''){
continue;
}else{
ans.push(arr[i])
}
}
ans = ans.join('/');
return ('/' + ans);
};