Created
August 21, 2014 08:55
-
-
Save frostney/644da01f725b8c72f3a2 to your computer and use it in GitHub Desktop.
Normalizing a path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var normalize = function(path, delimiter) { | |
if (delimiter == null) { | |
delimiter = '/'; | |
} | |
var pathArr = path.split(delimiter); | |
var newPath = []; | |
for (var i = 0, j = pathArr.length; i < j; i++) { | |
(function(item) { | |
if (item) { | |
if (item === '..') { | |
return pathArr[i + 1] = ''; | |
} | |
if (item === '.') { | |
return; | |
} | |
newPath.push(item); | |
} | |
})(pathArr[i]); | |
} | |
return newPath.join(delimiter); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment