diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 12:09:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 12:09:06 +0300 |
commit | 7e8278c0f46cf6058efad5afd0aef177977bd663 (patch) | |
tree | 7ac46710921145bb782bcb208ea896e1548b168b /app/assets/javascripts/lib/utils | |
parent | bbf6581214128ae12a6ff32f66a0d03ee57a2e91 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/lib/utils')
-rw-r--r-- | app/assets/javascripts/lib/utils/url_utility.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/url_utility.js b/app/assets/javascripts/lib/utils/url_utility.js index d48678c21f6..202363a1dda 100644 --- a/app/assets/javascripts/lib/utils/url_utility.js +++ b/app/assets/javascripts/lib/utils/url_utility.js @@ -1,6 +1,14 @@ const PATH_SEPARATOR = '/'; const PATH_SEPARATOR_LEADING_REGEX = new RegExp(`^${PATH_SEPARATOR}+`); const PATH_SEPARATOR_ENDING_REGEX = new RegExp(`${PATH_SEPARATOR}+$`); +const SHA_REGEX = /[\da-f]{40}/gi; + +// Reset the cursor in a Regex so that multiple uses before a recompile don't fail +function resetRegExp(regex) { + regex.lastIndex = 0; /* eslint-disable-line no-param-reassign */ + + return regex; +} // Returns a decoded url parameter value // - Treats '+' as '%20' @@ -128,6 +136,20 @@ export function doesHashExistInUrl(hashName) { return hash && hash.includes(hashName); } +export function urlContainsSha({ url = String(window.location) } = {}) { + return resetRegExp(SHA_REGEX).test(url); +} + +export function getShaFromUrl({ url = String(window.location) } = {}) { + let sha = null; + + if (urlContainsSha({ url })) { + [sha] = url.match(resetRegExp(SHA_REGEX)); + } + + return sha; +} + /** * Apply the fragment to the given url by returning a new url string that includes * the fragment. If the given url already contains a fragment, the original fragment @@ -154,6 +176,16 @@ export function visitUrl(url, external = false) { } } +export function updateHistory({ state = {}, title = '', url, replace = false, win = window } = {}) { + if (win.history) { + if (replace) { + win.history.replaceState(state, title, url); + } else { + win.history.pushState(state, title, url); + } + } +} + export function refreshCurrentPage() { visitUrl(window.location.href); } @@ -282,3 +314,7 @@ export const setUrlParams = (params, url = window.location.href, clearParams = f }; export const escapeFileUrl = fileUrl => encodeURIComponent(fileUrl).replace(/%2F/g, '/'); + +export function urlIsDifferent(url, compare = String(window.location)) { + return url !== compare; +} |