-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b2d9ca
commit fea4753
Showing
2 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import routes from './routes.json' assert {type: 'json'}; | ||
|
||
var appUrl = document.head.querySelector('meta[name="app-url"]').content; | ||
|
||
if (!String.prototype.endsWith) { | ||
String.prototype.endsWith = function (search, this_len) { | ||
if (this_len === undefined || this_len > this.length) { | ||
this_len = this.length; | ||
} | ||
return this.substring(this_len - search.length, this_len) === search; | ||
}; | ||
} | ||
|
||
export function route(name, params) { | ||
if (routes[name] === undefined) { | ||
console.error('Unknown route ', name); | ||
return false; | ||
} | ||
|
||
if (params === null || typeof params !== 'object') { | ||
return appUrl + '/' + routes[name]; | ||
} | ||
|
||
var path = routes[name] | ||
.split('/') | ||
.map(function (part) { | ||
if (part === null) { | ||
return ''; | ||
} | ||
|
||
if (part[0] != '{') { | ||
return part; | ||
} | ||
|
||
var param = part.match(/[^{\}]+/g)[0]; | ||
|
||
if (param.endsWith('?')) { | ||
param = param.slice(0, -1); | ||
} | ||
|
||
var value = params['' + param]; | ||
|
||
delete params['' + param]; | ||
|
||
return value; | ||
}) | ||
.join('/') | ||
.replace(/\/+$/g, ''); | ||
|
||
var query = Object.keys(params) | ||
.map(function (key) { | ||
return key + '=' + params[key]; | ||
}) | ||
.join('&'); | ||
|
||
return query ? appUrl + '/' + path + '?' + encodeURI(query) : appUrl + '/' + path; | ||
} |