Skip to content

Commit

Permalink
extendquery-optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapro committed Jul 21, 2016
1 parent 0beef5c commit da3148b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion components/appState/uri/parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var _ = require('lodash');
var urlParse = require('url-parse');
var urlParse = require('url').parse;
var serializer = require('./serializer');

/**
Expand Down
63 changes: 32 additions & 31 deletions lib/stuff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @module stuff
*/

var querystring = require('querystring');
var _ = require('lodash');

/**
Expand Down Expand Up @@ -46,16 +47,16 @@ function logToChannel(channel, type, args) {
}
}

/**
* Форматирует строку.
*
* @example
* _.format('Hello %1 and %2', 'Vasya', 'Petya'); // => 'Hello Vasya and Petya'
*
* @param {string} str - Форматируемая строка.
* @param {...Array} values - Значения для форматирования.
* @returns {string} Отформатированная строка.
*/
/**
* Форматирует строку.
*
* @example
* _.format('Hello %1 and %2', 'Vasya', 'Petya'); // => 'Hello Vasya and Petya'
*
* @param {string} str - Форматируемая строка.
* @param {...Array} values - Значения для форматирования.
* @returns {string} Отформатированная строка.
*/
exports.format = function(str) {
var values = arguments;

Expand Down Expand Up @@ -182,31 +183,31 @@ exports.uri2state = function(uri) {
/**
* Extend query string
*
* @param [String] urlOld
* @param [String] urlNew
* @param [String] oldUrl
* @param [String] newUrl
* @returns {String}
*/
exports.extendQuery = function(urlOld, urlNew, forcedRenewParam) {
if (!urlOld && !urlNew) return '';
if (!urlOld) return urlNew;
if (!urlNew) return urlOld;

var urlParse = require('url-parse');
var newLocation = urlParse(urlNew, true);
var currentLocation = urlParse(urlOld, true);
exports.extendQuery = function(oldUrl, newUrl, forcedRenewParam) {
if (!oldUrl && !newUrl) return '';
if (!oldUrl) return newUrl;
if (!newUrl) return oldUrl;

var oldQueryIndex = oldUrl.indexOf('?');
var newQueryIndex = newUrl.indexOf('?');

var oldQuery = oldQueryIndex != -1 ? oldUrl.slice(oldQueryIndex + 1) : '';
var newQuery = newQueryIndex != -1 ? newUrl.slice(newQueryIndex + 1) : '';

var oldQueryParsed = oldQuery ? querystring.parse(oldQuery) : {};
var newQueryParsed = newQuery ? querystring.parse(newQuery) : {};

if (forcedRenewParam) {
delete currentLocation.query[forcedRenewParam];
delete oldQueryParsed[forcedRenewParam];
}

if (!newLocation.protocol) { // some of these params can be absent
newLocation.set('protocol', currentLocation.protocol);
}
if (!newLocation.host) {
newLocation.set('host', currentLocation.host);
}
if (!newLocation.hostname) {
newLocation.set('hostname', currentLocation.hostname);
}
var resQueryParsed = _.extend(oldQueryParsed, newQueryParsed);
var resBase = newQueryIndex != -1 ? newUrl.slice(0, newQueryIndex) : newUrl;
var resQuery = _.isEmpty(resQueryParsed) ? '' : '?' + querystring.stringify(resQueryParsed);

return newLocation.set('query', _.extend(currentLocation.query, newLocation.query)).toString();
return resBase + resQuery;
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"reqwest": "git://github.com/2gis/reqwest",
"setimmediate": "^1.0.2",
"ua-parser-js": "~0.7.9",
"url-parse": "~1.0.5",
"wrench": "~1.5.8",
"yargs": "^3.6.0"
},
Expand Down

0 comments on commit da3148b

Please sign in to comment.