Skip to content

Instantly share code, notes, and snippets.

@frostney
Created August 21, 2014 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frostney/644da01f725b8c72f3a2 to your computer and use it in GitHub Desktop.
Save frostney/644da01f725b8c72f3a2 to your computer and use it in GitHub Desktop.
Normalizing a path
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