Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

url-query-builder

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

url-query-builder - npm Package Compare versions

Comparing version 1.0.6 to 1.0.7

198

index.js
/**
* Parse queries
* @param {Object|string} queries
* @return {Object} parsed queries
*/
function parseQueries (queries) {
var parsedQueries = {};
var paramType = typeof queries;
switch(paramType) {
case "undefined": break; // should not throw error
case "object":
parsedQueries = queries;
break;
case "string":
var queriesArray = queries.split("&");
for(var i = 0; i < queriesArray.length; i++) {
var query = queriesArray[i].split("=");
parsedQueries[query[0]] = query[1];
}
break;
default:
throw new Error("param queries must have a string or an object type");
}
return parsedQueries;
}
/**
* Parse queries from inital url string
* @param {string} url
*/
function parseQueriesFromUrl(url) {
var paramType = typeof url;
var queries = {};
switch(paramType) {
case "undefined": break;
case "string":
var queries = url.split("?")[1];
queries = parseQueries(queries);
break;
default:
throw new Error("param url must be a string or undefined");
}
return queries;
}
/**
* get a clear url without query
* @param {string} url
* @return {string} url without query string
*/
function getClearUrl(url) {
var paramType = typeof url;
var clearedUrl = '';
switch(paramType) {
case "undefined": break;
case "string":
clearedUrl = url.split("?")[0];
break;
default:
throw new Error("param url must be a string or undefined");
}
return clearedUrl;
}
/**
* @constructor

@@ -6,19 +75,18 @@ * @param {string} url

*/
module.exports = function URLQueryBuilder (url, queries) {
this.url = getClearUrl(url);
this.query = parseQueriesFromUrl(url);
this.queries = parseQueriesFromUrl(url);
var queriesFromParam = parseQueries(queries);
for(var i in queriesFromParam)
this.query[i] = queriesFromParam[i];
this.queries[i] = queriesFromParam[i];
/**
* Get a current url with queries
* Get a current url with queries
*/
URLQueryBuilder.prototype.getUrl = function() {
var url = this.url + "?";
for(var name in this.query) {
url += name + "=" + this.query[name] + "&";
for(var name in this.queries) {
url += name + "=" + this.queries[name] + "&";
}

@@ -28,12 +96,21 @@

};
/**
* Delete query by name
* @param {string} name, query that will be deleted
* Get clear url without queries
*/
URLQueryBuilder.prototype.getClearUrl = function() {
return this.url;
};
/**
* Delete query by name
* @param {string} name, query that will be deleted
*/
URLQueryBuilder.prototype.delete = function(name) {
if(!this.query[name])
if(!this.queries[name])
throw new Error("Can't delete. Query: '" + name + "' not exists");
delete this.query[name];
delete this.queries[name];
return this;

@@ -43,11 +120,11 @@ };

/**
* Change query by name
* @param {string} name, query what will be changed
* @param {string|number} value, new value for query
* Change query by name
* @param {string} name, query what will be changed
* @param {string|number} value, new value for query
*/
URLQueryBuilder.prototype.change = function(name, value) {
if(!this.query[name])
if(!this.queries[name])
throw new Error("Can't change. Query: '" + name + "' not exists");
this.query[name] = value.toString();
this.queries[name] = value.toString();
return this;

@@ -57,70 +134,43 @@ };

/**
* Add new query
* @param {string} name, name of new query
* @param {string|number} value, value for new query
* Add new query
* @param {string} name, name of new query
* @param {string|number} value, value for new query
*/
URLQueryBuilder.prototype.add = function(name, value) {
if(typeof name == 'string') {
if(this.query[name])
throw new Error("Can't add. Query: '" + name + "' already exists");
this.query[name] = value.toString();
} else if(typeof name == 'object') {
var queries = name;
for(var i in queries) {
this.add(i, queries[i]);
}
}
return this;
};
var paramType = typeof name;
switch(paramType) {
case "string":
if(this.queries[name])
throw new Error("Can't add. Query: '" + name + "' already exists");
this.queries[name] = value.toString();
break;
case "object":
var queries = name;
for(var i in queries)
this.add(i, queries[i]);
break;
/**
* Parse queries
* @param {Object|string} queries
* @return {Object} parsed queries
*/
function parseQueries (queries) {
var parsedQueries = {};
var queriesType = typeof queries;
default:
throw new Error("Unknow type of param name. Type must be a string or an object")
}
if(typeof queries == 'undefined') {
return parsedQueries;
} else if(typeof queries == "object") {
parsedQueries = queries;
} else if(typeof queries == "string") {
var queriesArray = queries.split("&");
for(var i = 0; i < queriesArray.length; i++) {
var query = queriesArray[i].split("=");
parsedQueries[query[0]] = query[1];
}
} else {
throw new Error("param queries must have a string or an object type");
}
return this;
};
return parsedQueries;
}
function parseQueriesFromUrl(url) {
if(!url) return {};
if(typeof url != 'string')
throw new Error("param url must be a string or undefined");
var queries = url.split("?")[1];
queries = parseQueries(queries);
return queries;
}
/**
* get a clear url without query
*/
function getClearUrl(url) {
if(!url) return '';
if(typeof url != 'string')
throw new Error("param url must be a string or undefined");
* Clear query string
* @param {string|object} queries
*/
URLQueryBuilder.prototype.reset = function(queries) {
this.queries = {};
if(typeof queries != 'undefined') {
var queriesFromParam = parseQueries(queries);
this.add(queriesFromParam);
}
return url.split("?")[0];
}
return this;
};
}
{
"name": "url-query-builder",
"version": "1.0.6",
"version": "1.0.7",
"description": "Simple add/change/delete url query",

@@ -11,3 +11,3 @@ "main": "index.js",

"type": "git",
"url": "git+https://github.com/Thisman/UrlQueryBuilder.git"
"url": "git+https://github.com/Thisman/url-query-builder.git"
},

@@ -24,5 +24,5 @@ "keywords": [

"bugs": {
"url": "https://github.com/Thisman/UrlQueryBuilder/issues"
"url": "https://github.com/Thisman/url-query-builder/issues"
},
"homepage": "https://github.com/Thisman/UrlQueryBuilder#readme"
"homepage": "https://github.com/Thisman/url-query-builder#readme"
}

@@ -14,6 +14,11 @@ # URL Query Builder

var q = new URLQueryBuilder("example.com");
// with query in url
var q1 = new URLQueryBuilder("example.com?name=value"); // => example.com?name=value&
// with initial query, can be string
var q1 = new URLQueryBuilder("example.com", "name=value"); // => example.com?name=value&
var q2 = new URLQueryBuilder("example.com", "name=value"); // => example.com?name=value&
// or object
var q2 = new URLQueryBuilder("example.com", {
var q3 = new URLQueryBuilder("example.com", {
"name": "value",

@@ -29,4 +34,9 @@ "name2": "value2"

.add("num3", "300"); // => example.com?num1=100&num3=300&
.getUrl(); // => example.com?num1=100&num3=300&
q.getUrl() // => example.com?num1=100&num3=300&
// reset queries
q.reset().getUrl(); // => example.com?
// or reset with new queries
q.reset({name: 100}); // => example.com?name=100&
```

@@ -40,5 +50,6 @@

## Test
- use *mocha.js* and *should.js*
- *mocha.js*
- *should.js*
```
npm run test
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc