123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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;
- }
- }
- };
|