url-util.js 2.64 KB
Newer Older
Chok's avatar
Chok committed
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
module.exports = function init(jsUtil) {
  return {
    parseUrl: parseUrl,
    appendQueryParamsString: appendQueryParamsString,
    serializeQueryParams: serializeQueryParams
  }

  function parseUrl(url) {
    var match = url.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);

    return match && {
        protocol: match[1],
        host: match[2],
        hostname: match[3],
        port: match[4] || '',
        pathname: match[5],
        search: match[6],
        hash: match[7]
    }
  }

  function appendQueryParamsString(url, params) {
    if (!url.length || !params.length) {
      return url;
    }

    var parsed = parseUrl(url);

    return parsed.protocol
      + '//'
      + parsed.host
      + parsed.pathname
      + (parsed.search.length ? parsed.search + '&' + params : '?' + params)
      + parsed.hash;
  }

  function serializeQueryParams(params, encode) {
    return serializeObject('', params, encode);
  }

  function serializeObject(parentKey, object, encode) {
    var parts = [];

    for (var key in object) {
      if (!object.hasOwnProperty(key)) {
        continue;
      }

      var identifier = parentKey.length ? parentKey + '[' + key + ']' : key;

      if (jsUtil.getTypeOf(object[key]) === 'Array') {
        parts.push(serializeArray(identifier, object[key], encode));
        continue;
      } else if (jsUtil.getTypeOf(object[key]) === 'Object') {
        parts.push(serializeObject(identifier, object[key], encode));
        continue;
      }

      parts.push(serializeIdentifier(parentKey, key, encode) + '=' + serializeValue(object[key], encode));
    }

    return parts.join('&');
  }

  function serializeArray(parentKey, array, encode) {
    var parts = [];

    for (var i = 0; i < array.length; ++i) {
      if (jsUtil.getTypeOf(array[i]) === 'Array') {
        parts.push(serializeArray(parentKey + '[]', array[i], encode));
        continue;
      } else if (jsUtil.getTypeOf(array[i]) === 'Object') {
        parts.push(serializeObject(parentKey + '[]' + array[i], encode));
        continue;
      }

      parts.push(serializeIdentifier(parentKey, '', encode) + '=' + serializeValue(array[i], encode));
    }

    return parts.join('&');
  }

  function serializeIdentifier(parentKey, key, encode) {
    if (!parentKey.length) {
      return encode ? encodeURIComponent(key) : key;
    }

    if (encode) {
      return encodeURIComponent(parentKey) + '[' + encodeURIComponent(key) + ']';
    } else {
      return parentKey + '[' + key + ']';
    }
  }

  function serializeValue(value, encode) {
    if (encode) {
      return encodeURIComponent(value);
    } else {
      return value;
    }
  }
};