url-query-builder
Advanced tools
Comparing version 1.1.1 to 2.0.0
268
index.js
@@ -1,177 +0,151 @@ | ||
/** | ||
* 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]; | ||
"use strict"; | ||
return result; | ||
} | ||
/** | ||
* Parse queries | ||
* @param {Object|string} queries | ||
* @return {Object} parsed queries | ||
*/ | ||
function parseQueries (queries) { | ||
var parsedQueries = {}; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
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; | ||
} | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
return parsedQueries; | ||
} | ||
/** | ||
* Parse queries from inital url string | ||
* @param {string} url | ||
*/ | ||
function parseQueriesFromUrl(url) { | ||
var queries = {}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
if(typeof url === 'string') { | ||
queries = url.split("?")[1]; | ||
queries = parseQueries(queries); | ||
} | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
return queries; | ||
} | ||
/** | ||
* get a clear url without query | ||
* @param {string} url | ||
* @return {string} url without query string | ||
*/ | ||
function getClearUrl(url) { | ||
var clearedUrl = ''; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
if(typeof url === 'string') | ||
clearedUrl = url.split("?")[0]; | ||
var URLQueryBuilder = function () { | ||
function URLQueryBuilder(url, queries) { | ||
_classCallCheck(this, URLQueryBuilder); | ||
return clearedUrl; | ||
} | ||
this.url = URLQueryBuilder.getClearUrl(url); | ||
this.queries = Object.assign(URLQueryBuilder.parseQueriesFromUrl(url), URLQueryBuilder.parseQueries(queries)); | ||
} | ||
_createClass(URLQueryBuilder, [{ | ||
key: "getUrl", | ||
value: function getUrl() { | ||
var url = this.url; | ||
var queries = this.queries; | ||
/** | ||
* check is certain value is null | ||
* @param {any} value | ||
* return {boolean} true if value is null | ||
*/ | ||
var queriesStr = ''; | ||
for (var prop in queries) { | ||
var value = queries[prop]; | ||
if (value != undefined || value != null) { | ||
queriesStr += prop + "=" + value + "&"; | ||
}; | ||
} | ||
queriesStr = queriesStr.slice(0, -1); | ||
return url + "?" + queriesStr; | ||
} | ||
}, { | ||
key: "getClearUrl", | ||
value: function getClearUrl() { | ||
return this.url; | ||
} | ||
}, { | ||
key: "delete", | ||
value: function _delete(name) { | ||
delete this.queries[name]; | ||
/** | ||
* @constructor | ||
* @param {string} url | ||
* @param {string|object|undefined} queries | ||
*/ | ||
return this; | ||
} | ||
}, { | ||
key: "change", | ||
value: function change(name, value) { | ||
this.queries[name] = value; | ||
/** | ||
* TODO | ||
* deep parse object/arrays | ||
* es6 syntax | ||
*/ | ||
module.exports = function URLQueryBuilder (url, queries) { | ||
this.url = getClearUrl(url); | ||
this.queries = parseQueriesFromUrl(url); | ||
return this; | ||
} | ||
}, { | ||
key: "add", | ||
value: function add() { | ||
var name = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var value = arguments[1]; | ||
var queriesFromParam = parseQueries(queries); | ||
this.queries = mergeObj(this.queries, queriesFromParam); | ||
if (typeof name === "string") { | ||
this.queries[name] = value; | ||
} else if ((typeof name === "undefined" ? "undefined" : _typeof(name)) === "object" && name != null) { | ||
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"); | ||
} | ||
/** | ||
* Get a current url with queries | ||
*/ | ||
URLQueryBuilder.prototype.getUrl = function() { | ||
var url = this.url; | ||
var queries = '?'; | ||
for(var name in this.queries) { | ||
var query = this.queries[name] | ||
if( | ||
query == undefined|| | ||
query == null | ||
) continue; | ||
return this; | ||
} | ||
}, { | ||
key: "reset", | ||
value: function reset(queries) { | ||
this.queries = URLQueryBuilder.parseQueries(queries); | ||
queries += (name + "=" + query + "&"); | ||
return this; | ||
} | ||
return url + queries; | ||
}; | ||
}, { | ||
key: "has", | ||
value: function has(name) { | ||
return name in this.queries; | ||
} | ||
}]); | ||
/** | ||
* Get clear url without queries | ||
*/ | ||
URLQueryBuilder.prototype.getClearUrl = function() { | ||
return URLQueryBuilder; | ||
}(); | ||
return this.url; | ||
}; | ||
exports.default = URLQueryBuilder; | ||
/** | ||
* Delete query by name | ||
* @param {string} name, query that will be deleted | ||
*/ | ||
URLQueryBuilder.prototype.delete = function(name) { | ||
delete this.queries[name]; | ||
return this; | ||
}; | ||
URLQueryBuilder.getClearUrl = function (url) { | ||
var clearedUrl = ''; | ||
/** | ||
* 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) { | ||
this.queries[name] = value; | ||
if (typeof url === 'string') { | ||
var _url$split = url.split("?"); | ||
return this; | ||
}; | ||
var _url$split2 = _slicedToArray(_url$split, 1); | ||
/** | ||
* 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") { | ||
this.queries[name] = value; | ||
} 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"); | ||
} | ||
clearedUrl = _url$split2[0]; | ||
} | ||
return this; | ||
}; | ||
return clearedUrl; | ||
}; | ||
URLQueryBuilder.parseQueriesFromUrl = function (url) { | ||
var queries = {}; | ||
/** | ||
* Clear query string | ||
* @param {string|object} queries | ||
*/ | ||
URLQueryBuilder.prototype.reset = function(queries) { | ||
this.queries = parseQueries(queries); | ||
if (typeof url === 'string') { | ||
var _url$split3 = url.split("?"); | ||
return this; | ||
}; | ||
var _url$split4 = _slicedToArray(_url$split3, 2); | ||
queries = _url$split4[1]; | ||
/** | ||
* Check if queries has specific query | ||
* @return {boolean} true if has, false if not | ||
*/ | ||
URLQueryBuilder.prototype.has = function(name) { | ||
queries = URLQueryBuilder.parseQueries(queries); | ||
} | ||
return (name in this.queries); | ||
}; | ||
} | ||
return queries; | ||
}; | ||
URLQueryBuilder.parseQueries = function () { | ||
var queries = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var parsedQueries = {}; | ||
var typeOfQueries = typeof queries === "undefined" ? "undefined" : _typeof(queries); | ||
if (typeOfQueries === "string") { | ||
var queriesArray = queries.split("&"); | ||
for (var i = 0; i < queriesArray.length; i++) { | ||
var _queriesArray$i$split = queriesArray[i].split("="); | ||
var _queriesArray$i$split2 = _slicedToArray(_queriesArray$i$split, 2); | ||
var prop = _queriesArray$i$split2[0]; | ||
var value = _queriesArray$i$split2[1]; | ||
parsedQueries[prop] = value; | ||
} | ||
} else if (typeOfQueries === "object" && queries != null) { | ||
parsedQueries = queries; | ||
} | ||
return parsedQueries; | ||
}; |
{ | ||
"name": "url-query-builder", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Simple add/change/delete url query", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha", | ||
"testDev": "mocha -w", | ||
"dev": "babel --watch ./src/index.js -o ./index.js", | ||
"build": "babel ./src/index.js -o ./index.js" | ||
}, | ||
@@ -13,2 +16,9 @@ "repository": { | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.0.0", | ||
"babel-core": "^6.5.2", | ||
"babel-plugin-remove-comments": "^2.0.0", | ||
"babel-preset-es2015": "^6.5.0", | ||
"babel-preset-stage-0": "^6.5.0" | ||
}, | ||
"keywords": [ | ||
@@ -15,0 +25,0 @@ "url", |
@@ -7,3 +7,3 @@ # URL Query Builder | ||
// nodejs style | ||
var URLQueryBuilder = require("url-query-builder"); | ||
var URLQueryBuilder = require("url-query-builder").default; | ||
// es6 way | ||
@@ -15,15 +15,15 @@ import URLQueryBuilder from "url-query-builder"; | ||
// with query in url | ||
var q1 = new URLQueryBuilder("example.com?a=b"); // => example.com?a=b& | ||
var q1 = new URLQueryBuilder("example.com?a=b"); // => example.com?a=b | ||
// with query by param, can be string | ||
var q2 = new URLQueryBuilder("example.com", "a=b"); // => example.com?a=b& | ||
var q2 = new URLQueryBuilder("example.com", "a=b"); // => example.com?a=b | ||
// or object | ||
var q3 = new URLQueryBuilder("example.com", {a: "b"}); // => example.com?a=b& | ||
var q3 = new URLQueryBuilder("example.com", {a: "b"}); // => example.com?a=b | ||
// add queries | ||
q.add("a", "b") // => example.com?a=b& | ||
q.add({a: "b"}) // => example.com?a=b& | ||
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& | ||
q.change("a","c") // => example.com?a=c | ||
@@ -35,3 +35,3 @@ // delete queries | ||
q.reset(); // => example.com? | ||
q.reset({a: "b"}); // => example.com?a=b& | ||
q.reset({a: "b"}); // => example.com?a=b | ||
``` | ||
@@ -41,10 +41,3 @@ | ||
``` | ||
npm i url-query-builder | ||
npm i url-query-builder --save | ||
``` | ||
## Test | ||
- *mocha.js* | ||
- *should.js* | ||
``` | ||
npm run test | ||
``` |
Sorry, the diff of this file is not supported yet
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
7434
5
5
118
40
1