From 8aa4c36bc91dd1efb152330afff98645bebcd12a Mon Sep 17 00:00:00 2001 From: Shankar Thiyagarajan Date: Fri, 4 Oct 2019 12:54:43 +0530 Subject: [PATCH 1/2] - ES6 Version support added. --- src/uri_helper_es6.js | 414 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 414 insertions(+) create mode 100644 src/uri_helper_es6.js diff --git a/src/uri_helper_es6.js b/src/uri_helper_es6.js new file mode 100644 index 0000000..dc77d5f --- /dev/null +++ b/src/uri_helper_es6.js @@ -0,0 +1,414 @@ +/*! + * JavaScript URI Query + * Simple Instance for access URI. + * + * Author : Shankar Thiyagarajan + * Email : shankarthiyagaraajan@gmail.com + * Github : https://github.com/shankarThiyagaraajan + * + * Source + * https://github.com/global-source/javascript_uri_query + * + * Site + * https://global-source.github.io/javascript_uri_query/ + * + * Copyright 2017-2019 + * + * Released under the MIT license + * https://github.com/global-source/javascript_uri_query/blob/master/LICENSE + * + */ + +class URI { + // To Get Parameter by its Name [ex. ?page=123, function('page') => 123] + get(name, url, default_result) { + + // Sanity check. + if (typeof url === 'undefined' && url) url = false; + if (typeof name === 'undefined' && name) name = false; + if (typeof default_result === 'undefined') default_result = false; + + // Default response. + var response; + + // Sanity check. + if (!url) { + + // If not valid, then update the active url. + url = window.location.href; + } + + // If filter name is not valid then return the default value. + if (!name) return default_result; + + name = name.replace(/[\[\]]/g, "\\$&"); + var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), + results = regex.exec(url); + if (!results) return default_result; + if (!results[2]) return ''; + response = decodeURIComponent(results[2].replace(/\+/g, " ")); + // If final response is not valid, then return the "default_result". + if (null === response) return default_result; + return response; + }; + + // To get an URI data by index. + getAll(byString) { + // Sanity check. + if (typeof byString === 'undefined') byString = false; + var queryDict; + queryDict = {}; + + if (byString) { + // Return param list as String. + return location.search.substr(1).split("&"); + } else { + // To loop the params. + location.search.substr(1).split("&").forEach(function (item) { + // If empty, then return. + if ('' !== item) { + item = item.split("="); + // Make Object of Params. + queryDict[item[0]] = item[1]; + } + }); + } + return queryDict; + }; + + // To clear all URI params. + clear() { + // To get all URI data's. + var list = getAll(); + + // Loop to remove all URI data's. + for (var i in list) { + remove(i, list[i], false); + } + + // Update URI status. + update(); + }; + + update() { + // Get active URL. + var href = window.location.href; + + // Check and remove "?" from URL. + if (href.indexOf('?') !== -1) { + + // Remove "?" from URL. + href = href.slice(0, href.indexOf('?')); + + // Update to URL. + window.history.pushState('', 'Title', href) + } + }; + + // To Remove Params by object or single. + remove(list, value, multiple) { + + // To Get list of Params. + var core_list = getAll(); + // Sanity check. + if('undefined' === core_list) core_list = 0; + // If no objects, then clear all data. + if (0 === objCount(core_list)) return update(); + + var isObject = true; + + var item; + var item_out; + var temp_obj = {}; + // To Check the type is Object or Not. + if (typeof list != 'object') isObject = false; + // If list is not object or string, then return false. + if (false === isObject && 'string' !== typeof list) return false; + // To Count the Params to check existence. + var count = objCount(core_list); + // If No params exist, then return false. + if (count <= 0) return false; + // Make Updated_list as Core List. + var updated_list = core_list; + // Remove stacked item from the index. + if (true === multiple && false === isObject) { + item = get(list); + item += ','; + if (-1 !== item.indexOf(',')) { + item = item.split(','); + // if ('object' === typeof item) { + delete item[item.indexOf(value)]; + // } + } + + if ('string' === typeof item) item = [item]; + + // Removing the empty values. + item = item.filter(function (i) { + return ('' !== i); + }); + + if (0 === item.length) remove(list); + + // Join to make string. + item_out = item.join(','); + temp_obj[list] = item_out; + // Update the resulted value value to URI. + add([temp_obj]); + } else { + if (true === isObject) { + // Generate Update List by eliminating the element list. + for (var i = 0; i < list.length; i++) { + // Remove the list of elements, one by one. + delete updated_list[list[i]]; + } + } else { + // Remove single index. + delete updated_list[list]; + } + + var newQuery = '?'; + // To Form New and Updated Query. + for (var i in updated_list) { + newQuery += i + '=' + updated_list[i] + '&'; + } + // Update the query output. + newQuery = newQuery.slice(0, -1); + // If output is empty, then set it into "?" + if ('' === newQuery) newQuery = '?'; + // To Update the URI. + window.history.pushState('', 'Title', newQuery); + } + }; + // To remove all params in URI. + removeAll(reset) { + var href = window.location.href; + if (-1 !== href.indexOf('?')) { + href = href.split('?'); + window.history.pushState('', 'Title', href[0]); + } + if (true === reset) window.location.reload(); + }; + // To Add Single Param To URI. + addNew = function(key, value){ + var temp_object; + // To Get list of Params. + var core_list = getAll(); + // Make Updated_list as Core List. + var updated_list = core_list; + + // If Key or Value is empty then return False. + if('' == value || '' == key) return false; + + // Append value to the List. + updated_list[key] = value; + + var newQuery = '?'; + // To Form New and Updated Query. + console.log(updated_list); + for (var i in updated_list) { + newQuery += i + '=' + updated_list[i] + '&'; + } + + // To Update the URI. + window.history.pushState('', 'Title', newQuery.slice(0, -1)); + + }; + // To Add list of Params To URI. + add(list, append) { + var temp_object; + // To Check the type is Object or Not. + if (typeof list != 'object') return false; + // To Get list of Params. + var core_list = getAll(); + // Make Updated_list as Core List. + var updated_list = core_list; + + // Append multiple value for single index. + if (true === append) { + var val; + var val_result; + for (var i in list) { + if (typeof list[i] === 'object') { + for (var k in list[i]) { + val_result = URI.get(k); + if (false !== val_result) { + temp_object = val_result.split(','); + // If already exists, then ignore. + if (-1 !== temp_object.indexOf(list[i][k])) continue; + val = val_result + ','; + // If not exist, then reset. + if (false === val_result || '' === val_result) { + val = ''; + } + // Append to the list. + val = val + list[i][k]; + } else { + val = list[i][k]; + } + updated_list[k] = val; + } + } + } + // Only one value per index. + } else { + // Loop with New Params. + for (var i in list) { + // Adding Params to Existing list. + for (var k in list[i]) { + // If empty, then remove + if ('' === list[i][k]) continue; + updated_list[k] = list[i][k]; + } + } + } + + var newQuery = '?'; + // To Form New and Updated Query. + console.log(updated_list); + for (var i in updated_list) { + newQuery += i + '=' + updated_list[i] + '&'; + } + + // To Update the URI. + window.history.pushState('', 'Title', newQuery.slice(0, -1)); + }; + append = function(key, value){ + var val; + var val_result; + val_result = URI.get(key); + // To Get list of Params. + var core_list = getAll(); + // Make Updated_list as Core List. + var updated_list = core_list; + if (false !== val_result) { + temp_object = val_result.split(','); + val = val_result + ','; + // If not exist, then reset. + if (false === val_result || '' === val_result) { + val = ''; + } + // Append to the list. + val = val + value; + } else { + val = value; + } + updated_list[key] = val; + + var newQuery = '?'; + // To Form New and Updated Query. + console.log(updated_list); + for (var i in updated_list) { + newQuery += i + '=' + updated_list[i] + '&'; + } + + // To Update the URI. + window.history.pushState('', 'Title', newQuery.slice(0, -1)); + }; + fixURI() { + var URI_items = getAll(); + removeAll(); + for (var i in URI_items) { + if ('undefined' !== URI_items[i]) { + add([{i: URI_items[i]}]); + } + } + }; + // To Go to Next Page. + nextPage() { + // To Get the actual value of "page" + var page = get('page'); + if (!page) { + page = 1; + } + page++; + // Update the Param "page" value. + replaceParam('page', page); + }; + // To Go back to Previous Page. + prevPage() { + // To Get the actual value of "page" + var page = get('page'); + // If "page" is not defined, then init with "1". + if (!page) { + page = 1; + } + + // Don't allow to remove if page index is "1". + if (page != 1) { + page--; + } + + // Update the Param "page" value. + replaceParam('page', page); + }; + + // Sanity check of param. + isParamExists(param) { + + // To extract the URI from URL. + var paramString = location.search.substr(1); + + // Return, param is exist or not. + return (paramString.indexOf(param + '=') !== -1); + }; + + // To Replace | Append | Create param. [DEPRECATED] + replaceParam(param, value) { + + // To extract the URI from URL. + var queryString = location.search.substr(1); + + // To Find the Matched element. + var total = queryString.match(/[a-z\d]+=[a-zA-Z\d]+/gi); + + // Form New Param with Value. + var newValue = param + '=' + value; + + // Form Old Param with Value. + var oldValue = param + '=' + get(param); + + // To Check have param or not. + if (objCount(total) > 0) { + + // To Check, whether param is exist or not. + if (isParamExists(param)) { + + // To Update the Value, If param already exist. + queryString = '?' + queryString.replace(oldValue, newValue); + } else { + + // To Create New Param, If not exist. + queryString = '?' + queryString + '&' + newValue; + } + } else { + + // To Create First param, If no param exist. + queryString = '?' + newValue; + } + + // To update to the URI. + window.history.pushState('', 'Title', queryString); + + return queryString; + }; + + // To Count number of object elements. + objCount(object) { + + // Initiating Length. + var length = 0; + + // Looping the Objects. + for (var key in object) { + + // Check with object existance. + if (object.hasOwnProperty(key)) { + ++length; + } + } + + // Total count of Objects. + return length; + } +}; From 8095f44b6e19085e2cc3efdf0317ae1316bb959e Mon Sep 17 00:00:00 2001 From: Shankar Thiyagarajan Date: Fri, 4 Oct 2019 12:58:37 +0530 Subject: [PATCH 2/2] - Improved suppor for ES6 --- src/uri_helper_es6.js | 100 +++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/src/uri_helper_es6.js b/src/uri_helper_es6.js index dc77d5f..0244430 100644 --- a/src/uri_helper_es6.js +++ b/src/uri_helper_es6.js @@ -29,7 +29,7 @@ class URI { if (typeof default_result === 'undefined') default_result = false; // Default response. - var response; + let response; // Sanity check. if (!url) { @@ -42,7 +42,7 @@ class URI { if (!name) return default_result; name = name.replace(/[\[\]]/g, "\\$&"); - var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), + let regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return default_result; if (!results[2]) return ''; @@ -56,7 +56,7 @@ class URI { getAll(byString) { // Sanity check. if (typeof byString === 'undefined') byString = false; - var queryDict; + let queryDict; queryDict = {}; if (byString) { @@ -79,10 +79,10 @@ class URI { // To clear all URI params. clear() { // To get all URI data's. - var list = getAll(); + let list = getAll(); // Loop to remove all URI data's. - for (var i in list) { + for (let i in list) { remove(i, list[i], false); } @@ -92,7 +92,7 @@ class URI { update() { // Get active URL. - var href = window.location.href; + let href = window.location.href; // Check and remove "?" from URL. if (href.indexOf('?') !== -1) { @@ -109,27 +109,27 @@ class URI { remove(list, value, multiple) { // To Get list of Params. - var core_list = getAll(); + let core_list = getAll(); // Sanity check. if('undefined' === core_list) core_list = 0; // If no objects, then clear all data. if (0 === objCount(core_list)) return update(); - var isObject = true; + let isObject = true; - var item; - var item_out; - var temp_obj = {}; + let item; + let item_out; + let temp_obj = {}; // To Check the type is Object or Not. if (typeof list != 'object') isObject = false; // If list is not object or string, then return false. if (false === isObject && 'string' !== typeof list) return false; // To Count the Params to check existence. - var count = objCount(core_list); + let count = objCount(core_list); // If No params exist, then return false. if (count <= 0) return false; // Make Updated_list as Core List. - var updated_list = core_list; + let updated_list = core_list; // Remove stacked item from the index. if (true === multiple && false === isObject) { item = get(list); @@ -158,7 +158,7 @@ class URI { } else { if (true === isObject) { // Generate Update List by eliminating the element list. - for (var i = 0; i < list.length; i++) { + for (let i = 0; i < list.length; i++) { // Remove the list of elements, one by one. delete updated_list[list[i]]; } @@ -167,9 +167,9 @@ class URI { delete updated_list[list]; } - var newQuery = '?'; + let newQuery = '?'; // To Form New and Updated Query. - for (var i in updated_list) { + for (let i in updated_list) { newQuery += i + '=' + updated_list[i] + '&'; } // Update the query output. @@ -182,7 +182,7 @@ class URI { }; // To remove all params in URI. removeAll(reset) { - var href = window.location.href; + let href = window.location.href; if (-1 !== href.indexOf('?')) { href = href.split('?'); window.history.pushState('', 'Title', href[0]); @@ -191,11 +191,11 @@ class URI { }; // To Add Single Param To URI. addNew = function(key, value){ - var temp_object; + let temp_object; // To Get list of Params. - var core_list = getAll(); + let core_list = getAll(); // Make Updated_list as Core List. - var updated_list = core_list; + let updated_list = core_list; // If Key or Value is empty then return False. if('' == value || '' == key) return false; @@ -203,10 +203,10 @@ class URI { // Append value to the List. updated_list[key] = value; - var newQuery = '?'; + let newQuery = '?'; // To Form New and Updated Query. console.log(updated_list); - for (var i in updated_list) { + for (let i in updated_list) { newQuery += i + '=' + updated_list[i] + '&'; } @@ -216,21 +216,21 @@ class URI { }; // To Add list of Params To URI. add(list, append) { - var temp_object; + let temp_object; // To Check the type is Object or Not. if (typeof list != 'object') return false; // To Get list of Params. - var core_list = getAll(); + let core_list = getAll(); // Make Updated_list as Core List. - var updated_list = core_list; + let updated_list = core_list; // Append multiple value for single index. if (true === append) { - var val; - var val_result; - for (var i in list) { + let val; + let val_result; + for (let i in list) { if (typeof list[i] === 'object') { - for (var k in list[i]) { + for (let k in list[i]) { val_result = URI.get(k); if (false !== val_result) { temp_object = val_result.split(','); @@ -253,9 +253,9 @@ class URI { // Only one value per index. } else { // Loop with New Params. - for (var i in list) { + for (let i in list) { // Adding Params to Existing list. - for (var k in list[i]) { + for (let k in list[i]) { // If empty, then remove if ('' === list[i][k]) continue; updated_list[k] = list[i][k]; @@ -263,10 +263,10 @@ class URI { } } - var newQuery = '?'; + let newQuery = '?'; // To Form New and Updated Query. console.log(updated_list); - for (var i in updated_list) { + for (let i in updated_list) { newQuery += i + '=' + updated_list[i] + '&'; } @@ -274,13 +274,13 @@ class URI { window.history.pushState('', 'Title', newQuery.slice(0, -1)); }; append = function(key, value){ - var val; - var val_result; + let val; + let val_result; val_result = URI.get(key); // To Get list of Params. - var core_list = getAll(); + let core_list = getAll(); // Make Updated_list as Core List. - var updated_list = core_list; + let updated_list = core_list; if (false !== val_result) { temp_object = val_result.split(','); val = val_result + ','; @@ -295,10 +295,10 @@ class URI { } updated_list[key] = val; - var newQuery = '?'; + let newQuery = '?'; // To Form New and Updated Query. console.log(updated_list); - for (var i in updated_list) { + for (let i in updated_list) { newQuery += i + '=' + updated_list[i] + '&'; } @@ -306,9 +306,9 @@ class URI { window.history.pushState('', 'Title', newQuery.slice(0, -1)); }; fixURI() { - var URI_items = getAll(); + let URI_items = getAll(); removeAll(); - for (var i in URI_items) { + for (let i in URI_items) { if ('undefined' !== URI_items[i]) { add([{i: URI_items[i]}]); } @@ -317,7 +317,7 @@ class URI { // To Go to Next Page. nextPage() { // To Get the actual value of "page" - var page = get('page'); + let page = get('page'); if (!page) { page = 1; } @@ -328,7 +328,7 @@ class URI { // To Go back to Previous Page. prevPage() { // To Get the actual value of "page" - var page = get('page'); + let page = get('page'); // If "page" is not defined, then init with "1". if (!page) { page = 1; @@ -347,7 +347,7 @@ class URI { isParamExists(param) { // To extract the URI from URL. - var paramString = location.search.substr(1); + let paramString = location.search.substr(1); // Return, param is exist or not. return (paramString.indexOf(param + '=') !== -1); @@ -357,16 +357,16 @@ class URI { replaceParam(param, value) { // To extract the URI from URL. - var queryString = location.search.substr(1); + let queryString = location.search.substr(1); // To Find the Matched element. - var total = queryString.match(/[a-z\d]+=[a-zA-Z\d]+/gi); + let total = queryString.match(/[a-z\d]+=[a-zA-Z\d]+/gi); // Form New Param with Value. - var newValue = param + '=' + value; + let newValue = param + '=' + value; // Form Old Param with Value. - var oldValue = param + '=' + get(param); + let oldValue = param + '=' + get(param); // To Check have param or not. if (objCount(total) > 0) { @@ -397,10 +397,10 @@ class URI { objCount(object) { // Initiating Length. - var length = 0; + let length = 0; // Looping the Objects. - for (var key in object) { + for (let key in object) { // Check with object existance. if (object.hasOwnProperty(key)) {