/**
 * Append query to a url string.
 * @function lib.url.appendQuery
 * @param {string} urlString - Url string to append query.
 * @param {object} data - Query data.
 * @author Taka Okunishi
 *
 */

var qs = require('querystring'),
    url = require('url'),
    object = require('../object'),
    copy = object.copy;

module.exports = function (urlString, data) {
    var urlData = url.parse(urlString),
        queryData = qs.parse(urlData.query) || {};
    return url.format({
        protocol: urlData.protocol,
        auth: urlData.auth,
        host: urlData.host,
        hash: urlData.hash,
        pathname: urlData.pathname,
        search: qs.stringify(copy(data, queryData))
    });
};