Skip to content

Commit

Permalink
Update route param
Browse files Browse the repository at this point in the history
  • Loading branch information
rayc2045 committed Apr 30, 2024
1 parent 0625809 commit 21ee4da
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,27 @@ const utils = {
setTimeout(resolve, delay * 1000);
});
},
getUrlParam(url = window.location.href) {
const urlSearch = url.split('?')[1];
const urlSearchParams = new URLSearchParams(urlSearch);
const param = Object.fromEntries(urlSearchParams.entries());
Object.keys(param).forEach(key => {
const items = param[key].split(' ');
if (items.length === 1) {
if (!items[0].length) return (param[key] = null);
return (param[key] = items[0]);
}
param[key] = items;
});
return param;
getSearchQuery(url = window.location.href) {
if (!url.includes('?')) return null;
const query = {};
url
.split('?')[1]
.split('&')
.filter(p => p.length)
.forEach(param => {
const [prop, value] = param.split('=');
if (!prop) return null;
if (query.hasOwnProperty(prop)) return; // duplicates not set again
if (!value) return (query[prop] = true); // if no value, set 'true' as default
if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')
return (query[prop] = value.toLowerCase() === 'true');
const values = value
.split('+')
.filter(v => v.length)
.map(v => (isNaN(+v) ? v : +v));
query[prop] = values.length > 1 ? values : values[0];
});
return query;
},
async scrollToTop() {
scrollTo({ top: 0, behavior: prefer.motion ? 'smooth' : 'auto' });
Expand Down Expand Up @@ -180,7 +188,7 @@ const router = {
);
},
get currentParam() {
const param = utils.getUrlParam();
const param = { ...utils.getSearchQuery() };
if (!this.currentRoute.path.includes(':')) return param;
const routePathFrags = this.currentRoute.path.split('/'),
currentPathFrags = this.currentPath.split('/');
Expand Down

0 comments on commit 21ee4da

Please sign in to comment.