1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-23 07:26:40 +02:00
keeweb/app/scripts/util/formatting/url-format.js

42 lines
1007 B
JavaScript
Raw Normal View History

2019-09-15 08:11:11 +02:00
const UrlFormat = {
2016-03-14 06:04:55 +01:00
multiSlashRegex: /\/{2,}/g,
2020-03-21 12:08:27 +01:00
lastPartRegex: /[\/\\]?[^\/\\]+$/,
2016-04-03 14:56:38 +02:00
kdbxEndRegex: /\.kdbx$/i,
2016-03-14 06:04:55 +01:00
2019-08-18 10:17:09 +02:00
getDataFileName(url) {
2017-01-31 07:50:28 +01:00
const ix = url.lastIndexOf('/');
2016-03-12 17:49:52 +01:00
if (ix >= 0) {
url = url.substr(ix + 1);
}
url = url.replace(/\?.*/, '').replace(/\.kdbx/i, '');
return url;
2016-03-14 06:04:55 +01:00
},
2019-08-18 10:17:09 +02:00
isKdbx(url) {
2016-04-03 14:56:38 +02:00
return url && this.kdbxEndRegex.test(url);
},
2019-08-18 10:17:09 +02:00
fixSlashes(url) {
2016-03-14 06:04:55 +01:00
return url.replace(this.multiSlashRegex, '/');
},
2019-08-18 10:17:09 +02:00
fileToDir(url) {
2017-04-16 17:00:35 +02:00
return url.replace(this.lastPartRegex, '') || '/';
},
makeUrl(base, args) {
const queryString = Object.entries(args)
.map(([key, value]) => key + '=' + encodeURIComponent(value))
.join('&');
return base + '?' + queryString;
},
buildFormData(params) {
return Object.entries(params)
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
.join('&');
2016-03-12 17:49:52 +01:00
}
};
2019-09-15 14:16:32 +02:00
export { UrlFormat };