cms/app-client/mirage/serializers/utils.js
changeset 324 92fc9d077f95
child 543 aaaf9b0b09f6
equal deleted inserted replaced
323:47f0611cc57d 324:92fc9d077f95
       
     1 // taken from http://blog.stevenlevithan.com/archives/parseuri
       
     2 // parseUri 1.2.2
       
     3 // (c) Steven Levithan <stevenlevithan.com>
       
     4 // MIT License
       
     5 import _ from 'lodash';
       
     6 
       
     7 const PARSEURI_DEFAULT_OPTIONS = {
       
     8   strictMode: false,
       
     9   key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
       
    10   q: {
       
    11     name:   "queryKey",
       
    12     parser: /(?:^|&)([^&=]*)=?([^&]*)/g
       
    13   },
       
    14   parser: {
       
    15     strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
       
    16     loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
       
    17   }
       
    18 };
       
    19 
       
    20 export default {
       
    21   parseUri : function (str, options) {
       
    22     var o = _.defaultsDeep(options || {}, PARSEURI_DEFAULT_OPTIONS);
       
    23     var	m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
       
    24         uri = {},
       
    25         i   = 14;
       
    26 
       
    27     while (i--) { uri[o.key[i]] = m[i] || ""; }
       
    28 
       
    29     uri[o.q.name] = {};
       
    30     uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
       
    31       if ($1) { uri[o.q.name][$1] = $2; }
       
    32     });
       
    33 
       
    34     return uri;
       
    35   },
       
    36   mergeUri : function(uriParts) {
       
    37     let res = "";
       
    38     if(uriParts.protocol) {
       
    39       res += uriParts.protocol+":";
       
    40     }
       
    41     if(uriParts.host) {
       
    42       res += "//" + uriParts.host;
       
    43     }
       
    44     if(uriParts.port) {
       
    45       res += ":" + uriParts.port;
       
    46     }
       
    47     if(uriParts.path) {
       
    48       res += uriParts.path;
       
    49     }
       
    50     if(uriParts.queryKey) {
       
    51       res += "?" + _.map(uriParts.queryKey, function(v,k) {
       
    52         return k + "=" + v;
       
    53       }).join("&");
       
    54     }
       
    55 
       
    56     return res;
       
    57   }
       
    58 };