raptor
Version:
RaptorJS provides an AMD module loader that works in Node, Rhino and the web browser. It also includes various sub-modules to support building optimized web applications.
32 lines (27 loc) • 1.04 kB
JavaScript
define(
"raptor/paths",
function(require, exports, module) {
"use strict";
return {
/**
* Resolves a possibly path relative to a directory to an absolute path.
*/
resolve: function(dirPath, relativePath) {
if (relativePath.charAt(0) === '/') {
return relativePath;
}
var parts = relativePath.split('/'),
pathParts = dirPath.split('/');
for (var i=0, len=parts.length; i<len; i++) {
var part = parts[i];
if (part === '..') {
pathParts.splice(pathParts.length-1, 1); //Remove the last element
}
else if (part !== '.') {
pathParts.push(part);
}
}
return pathParts.join('/');
}
};
});