Created
October 16, 2014 09:15
-
-
Save adoc/ab5176b145f8b45b494a to your computer and use it in GitHub Desktop.
join.js - Direct port of Python std posixpath.join. (Thanks Mr. Peterson!)
This file contains 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
// src: http://stackoverflow.com/a/646643 | |
// Add `startsWith` and `endsWith` to the String prototype. | |
if (typeof String.prototype.startsWith != 'function') { | |
String.prototype.startsWith = function (str){ | |
return this.slice(0, str.length) == str; | |
}; | |
} | |
if (typeof String.prototype.endsWith != 'function') { | |
String.prototype.endsWith = function (str){ | |
return this.slice(-str.length) == str; | |
}; | |
} | |
function path_join(a) { | |
/* Direct port of Python std posixpath.join. | |
src: https://hg.python.org/cpython/file/v2.7.3/Lib/posixpath.py:60 | |
*/ | |
var path = a; | |
for(var i=1; i<arguments.length; i++) { | |
var b = arguments[i]; | |
if(b.startsWith('/')) { | |
path = b; | |
} else if (path == '' || path.endsWith('/')) { | |
path = path.concat(b); | |
} else { | |
path = path.concat('/' + b); | |
} | |
} | |
return path; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment