url-query-builder
Advanced tools
Comparing version 1.0.8 to 1.0.9
70
index.js
/** | ||
* Merge two objects | ||
*/ | ||
function mergeObj(obj1, obj2) { | ||
var result = {}; | ||
for(var i in obj1) result[i] = obj1[i]; | ||
for(var j in obj2) result[j] = obj2[j]; | ||
return result; | ||
} | ||
/** | ||
* Parse queries | ||
@@ -8,20 +18,15 @@ * @param {Object|string} queries | ||
var parsedQueries = {}; | ||
var paramType = typeof queries; | ||
switch(paramType) { | ||
case "object": | ||
parsedQueries = queries; | ||
break; | ||
case "string": | ||
var queriesArray = queries.split("&"); | ||
for(var i = 0; i < queriesArray.length; i++) { | ||
var query = queriesArray[i].split("="); | ||
// check to valud data | ||
if(query.length == 2) | ||
parsedQueries[query[0]] = query[1]; | ||
} | ||
break; | ||
if(typeof queries === "string") { | ||
var queriesArray = queries.split("&"); | ||
for(var i = 0; i < queriesArray.length; i++) { | ||
var query = queriesArray[i].split("="); | ||
// check to valud data | ||
if(query.length == 2) | ||
parsedQueries[query[0]] = query[1]; | ||
} | ||
} else if(typeof (queries === "object") && queries) { // typeof null/undefined === "object" | ||
parsedQueries = queries; | ||
} | ||
default: break; | ||
} | ||
return parsedQueries; | ||
@@ -68,4 +73,3 @@ } | ||
var queriesFromParam = parseQueries(queries); | ||
for(var i in queriesFromParam) | ||
this.queries[i] = queriesFromParam[i]; | ||
this.queries = mergeObj(this.queries, queriesFromParam); | ||
@@ -79,5 +83,4 @@ | ||
var queries = '?'; | ||
for(var name in this.queries) { | ||
for(var name in this.queries) | ||
queries += (name + "=" + this.queries[name] + "&"); | ||
} | ||
@@ -102,2 +105,3 @@ return url + queries; | ||
delete this.queries[name]; | ||
return this; | ||
@@ -113,2 +117,3 @@ }; | ||
this.queries[name] = value.toString(); | ||
return this; | ||
@@ -123,17 +128,12 @@ }; | ||
URLQueryBuilder.prototype.add = function(name, value) { | ||
var paramType = typeof name; | ||
if(typeof name === "string") { | ||
this.queries[name] = value.toString(); | ||
} else if(typeof name === "object" && name) { | ||
var queries = name; | ||
for(var i in queries) | ||
this.add(i, queries[i]); | ||
} else { | ||
throw new Error("Param 'name' must be a string or an object"); | ||
} | ||
switch(paramType) { | ||
case "string": | ||
this.queries[name] = value.toString(); | ||
break; | ||
case "object": | ||
var queries = name; | ||
for(var i in queries) this.add(i, queries[i]); | ||
break; | ||
default: | ||
throw new Error("Param name must be a string or an object"); | ||
} | ||
return this; | ||
@@ -162,2 +162,2 @@ }; | ||
}; | ||
} | ||
} |
{ | ||
"name": "url-query-builder", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "Simple add/change/delete url query", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,2 @@ # URL Query Builder | ||
var URLQueryBuilder = require("url-query-builder"); | ||
// es6 way | ||
@@ -14,3 +13,3 @@ import URLQueryBuilder from "url-query-builder"; | ||
// create instance | ||
var q = new URLQueryBuilder("example.com"); | ||
var q = new URLQueryBuilder("example.com"); // => example.com? | ||
// with query in url | ||
@@ -21,19 +20,18 @@ var q1 = new URLQueryBuilder("example.com?a=b"); // => example.com?a=b& | ||
// or object | ||
var q3 = new URLQueryBuilder("example.com", { | ||
"name": "value", | ||
"name2": "value2" | ||
}); // => example.com?name=value&name2=value2& | ||
var q3 = new URLQueryBuilder("example.com", {a: "b"}); // => example.com?a=b& | ||
q.add("num1", 100) // => example.com?num1=100 | ||
.add("num2", 200) // => example.com?num1=100&num2=200& | ||
.change("num1", 150) // => example.com?num1=150&num2=200& | ||
.delete("num2") // => example.com?num1=100& | ||
.add("num3", "300"); // => example.com?num1=100&num3=300& | ||
.getUrl(); // => example.com?num1=100&num3=300& | ||
// add queries | ||
q.add("a", "b") // => example.com?a=b& | ||
q.add({a: "b"}) // => example.com?a=b& | ||
// change queries | ||
q.change("a","c") // => example.com?a=c& | ||
// delete queries | ||
q.delete("a") // => example.com? | ||
// reset queries | ||
q.reset().getUrl(); // => example.com? | ||
// or reset with new queries | ||
q.reset({name: 100}); // => example.com?name=100& | ||
q.reset(); // => example.com? | ||
q.reset({a: "b"}); // => example.com?a=b& | ||
``` | ||
@@ -40,0 +38,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9557
261
47
1