diff options
author | Filipa Lacerda <filipa@gitlab.com> | 2017-02-13 19:11:11 +0300 |
---|---|---|
committer | Filipa Lacerda <filipa@gitlab.com> | 2017-02-15 22:57:48 +0300 |
commit | 1285d629064abce3aee8faafaa57492da6f8f163 (patch) | |
tree | 29f6e73839bd7fadfd7f98a0223b955b2c0165c6 /app/assets/javascripts/lib | |
parent | 73accafe430f56cd3065774c6118de3db0a45734 (diff) |
Move change page param to utility function
Add tests
Adds folder name in the top of the table
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r-- | app/assets/javascripts/lib/utils/common_utils.js.es6 | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/common_utils.js.es6 b/app/assets/javascripts/lib/utils/common_utils.js.es6 index bcb3a706b51..276ff01ab89 100644 --- a/app/assets/javascripts/lib/utils/common_utils.js.es6 +++ b/app/assets/javascripts/lib/utils/common_utils.js.es6 @@ -241,5 +241,45 @@ acc[element] = DOMStringMapObject[element]; return acc; }, {}); + + /** + * Updates the search parameter of a URL given the parameter and values provided. + * + * If no search params are present we'll add it. + * If param for page is already present, we'll update it + * If there are params but not for the given one, we'll add it at the end. + * Returns the new search parameters. + * + * @param {String} param + * @param {Number|String|Undefined|Null} value + * @return {String} + */ + w.gl.utils.setParamInURL = (param, value) => { + let search; + + if (window.location.search.length === 0) { + search = `?${param}=${value}`; + } + + if (window.location.search.indexOf(param) !== -1) { + const regex = new RegExp(param + '=\\d'); + search = window.location.search.replace(regex, `${param}=${value}`); + } + + if (window.location.search.length && + window.location.search.indexOf(param) === -1) { + search = `${window.location.search}&${param}=${value}`; + } + + return search; + }; + + /** + * Converts permission provided as strings to booleans. + * + * @param {String} string + * @returns {Boolean} + */ + w.gl.utils.convertPermissionToBoolean = permission => permission === 'true'; })(window); }).call(this); |