Code coverage report for src/Common/Url/Parser.js

Statements: 98.21% (55 / 56)      Branches: 85.71% (24 / 28)      Functions: 100% (11 / 11)      Lines: 98.11% (52 / 53)      Ignored: none     

All files » src/Common/Url/ » Parser.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217      1         1   1       1                 90     63   63   63                       10 10                       4 4 4   2                       33   33     33   22     33                         16   16     16     16 16 16     16   16                       12   12   12   12                         6                               4 4   4   4       2 2 2 2     2     2                     2   2     2 2         3   2   2 2             2             1  
'use strict';
 
 
var _       = require("lodash"),
    Type    = require("../Type");
 
 
/// regular expression to get the baseUrl
var BaseUrlRegExStr = '^[a-z]+://[a-z.-]+(?::[0-9]+)?';
/// regular expression to get the protocol
var ProtocolRegExStr = '^([a-z]+)://';
 
 
 
var UrlParser = {
 
    /**
     * Normalize url
     *
     * @return{String} the current url
     */
    normalize: function(url) {
    
        if(!url) return "";
 
        /// replace all //// to a single slash
        url = url.replace(/\/+/g, "/");
        /// because previouly all // were replaced lets fix the protocol syntax (http://)
        url = url.replace(/:\//g, "://");
 
        return url;
 
    },
 
    /**
     * Test if the url is absolute
     * 
     * @return {Boolean}
     * 
     */
    isAbsolute: function(url){
 
        var regex = new RegExp(BaseUrlRegExStr, "i");
        return !! regex.exec(url);
 
    },
 
    /**
     * Gets the protocol of the url 
     * 
     * @param  {String} url
     * @return {String}
     */
    protocol: function(url){
 
        var regex = new RegExp(ProtocolRegExStr, "gi");
        var result = regex.exec(url);
        if(result) return result[1];
        
        return "";
 
    },
 
    /**
     * Returns the baseUrl for the given url
     * 
     * @param  {String} url
     * @return {String}
     */
    baseUrl: function(url){
 
        var regex = new RegExp(BaseUrlRegExStr, "i");
 
        var result = "",
            regexResult = regex.exec(url);
 
        if(regexResult){
            /// always add / at the end of baseUrl
            result = regexResult.shift() + "/";
        }
 
        return UrlParser.normalize(result);
 
    },
 
    /**
     * Get the path of the url
     * 
     * @param  {String} url
     * @return {String}
     */
    path: function(url){
 
        // sanitize input
        url = url || "";
 
        var baseUrl = UrlParser.baseUrl(url) || "";
 
        // remove the base url
        url = url.replace(baseUrl, "");
 
        // remove the last part of the url. this may be a "/"" or "filehandler"
        var urlParts = url.split("/");
        urlParts.pop();
        urlParts.push("");
        
        // join all
        url = urlParts.join("/");
 
        return UrlParser.normalize(url);
    },
 
    /**
     * Get the file name
     * 
     * @return {[type]} [description]
     * 
     */
    filename: function(url){
 
        // sanitize input
        url = url || "";
 
        var urlParts = url.split("/");
        
        var filename = urlParts.pop().replace(/\?.*$/, "");
        
        return filename;
 
    },
 
 
    /**
     * Gets the entire File Path
     * 
     * @param  {String} url
     * @return {String}
     */
    filepath: function(url){ 
 
        return UrlParser.normalize(
            UrlParser.baseUrl(url) + 
            UrlParser.path(url) + 
            UrlParser.filename(url));
 
    },
 
    /**
     * get's the query string part of the url
     *
     * @param{href} The full url or null to get the current
     * @param{separator} The url qs separator. Normally is ?
     * @return{String} The query string part of the url
     */
    queryString: function(href, separator) {
 
        href = href || "";
        separator = separator || '?';
 
        var hrefSplit = href.split(separator) || [];
        
        if(hrefSplit.length > 1){
            
            // query strin can be malformed like ?a=1&?b=2
            // we can fix this
            var qStringPart = hrefSplit.splice(1, hrefSplit.length-1);
            var qString = "";
            _.forEach(qStringPart, function(qs){
                qString += qs;
            });
 
            return qString;
        }
 
        return "";
    },
 
    /*
     * Parse query string from url
     *
     * @param{separator} The url qs separator. Normally is ?
     * @return An hash with all the query string key/value's
     */
    queryStringObj: function(href, separator) {
    
        href = href || "";
 
        var qString = UrlParser.queryString(href, separator),
            values = {};
 
        qString = decodeURI(qString || "");
        _.each(
            qString.split("&"),
            function(keyValue){
 
                // ignore if 
                if(!keyValue) return;
 
                var pair = keyValue.split("=");
                
                Eif(pair.length>1)
                    values[pair[0]] = pair[1];
                else
                    values[pair[0]] = true; // it's more easy to use on conditions
                    
 
            });
 
        return values;
    }
 
 
 
};
 
module.exports = UrlParser;