1 /*
  2  * Path.js - minimal pure js implementation of the nodejs path module
  3  *
  4  * Copyright © 2015, JEDLSoft
  5  *
  6  * Licensed under the Apache License, Version 2.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at
  9  *
 10  *     http://www.apache.org/licenses/LICENSE-2.0
 11  *
 12  * Unless required by applicable law or agreed to in writing, software
 13  * distributed under the License is distributed on an "AS IS" BASIS,
 14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  *
 16  * See the License for the specific language governing permissions and
 17  * limitations under the License.
 18  */
 19 
 20 var Path = {
 21     /**
 22      * Return the parent directory of the given pathname
 23      * similar to the dirname shell function.
 24      * @static
 25      * @param {string} pathname path to check
 26      * @return {string} the parent dir of the given pathname
 27      */
 28     dirname: function(pathname) {
 29         if (!pathname) return pathname;
 30         pathname = Path.normalize(pathname);
 31         return (pathname === ".") ? ".." : Path.normalize(pathname + "/..");
 32     },
 33 
 34     /**
 35      * Return the normalized version of the given pathname. This
 36      * cleans up things like double directory separators and such.
 37      * @static
 38      * @param {string} pathname path to check
 39      * @return {string} the normalized version of the given pathname
 40      */
 41     normalize: function(pathname) {
 42         if (pathname) {
 43             pathname = pathname.replace(/\\/g, "/");
 44             pathname = pathname.replace(/\/\//g, "/");
 45             pathname = pathname.replace(/\/\//g, "/");
 46             pathname = pathname.replace(/\/[^/]*[^\./]\/\.\./g, "/.");
 47             pathname = pathname.replace(/^[^/]*[^\./]\/\.\./g, ".");
 48             pathname = pathname.replace(/\/\.\//g, "/");
 49             pathname = pathname.replace(/^\.\//, "");
 50             pathname = pathname.replace(/\/\//g, "/");
 51             pathname = pathname.replace(/\/\.$/, "");
 52             pathname = pathname.replace(/\/\//g, "/");
 53             if (pathname.length > 1) pathname = pathname.replace(/\/$/, "");
 54             if (pathname.length === 0) pathname = '.';
 55         }
 56         return pathname;
 57     },
 58 
 59     /**
 60      * Return a path that is the concatenation of all the of the arguments
 61      * which each name a path segment.
 62      * @static
 63      * @param {...string} var_args
 64      * @return {string} the concatenated pathname
 65      */
 66     join: function(var_args) {
 67         var arr = [];
 68         for (var i = 0; i < arguments.length; i++) {
 69             arr.push(arguments[i] && arguments[i].length > 0 ? arguments[i] : ".");
 70         }
 71         return Path.normalize(arr.join("/"));
 72     },
 73 
 74     /**
 75      * Return the base file name of the path. If the extension is given,
 76      * with or without the leading dot, then the extension is removed from
 77      * the base name.
 78      * @param {string} pathname the path to take the base name of
 79      * @param {string|undefined} extension the optional extension to remove
 80      * @return {string} the base name of the file without the extension
 81      */
 82     basename: function(pathname, extension) {
 83         var base = pathname;
 84         var slash = pathname.lastIndexOf("/");
 85         if (slash !== -1) {
 86             base = pathname.substring(slash+1);
 87         }
 88 
 89         if (extension) {
 90             var ext = extension[0] === "." ? extension : "." + extension;
 91             var index = base.lastIndexOf(ext);
 92             if (index > -1) {
 93                 base = base.substring(0, index);
 94             }
 95         }
 96 
 97         return base;
 98     }
 99 };
100 
101 module.exports = Path;
102