VulpesOS/gaia/apps/calendar/js/utils/uri.js
OverSu b38dea0597 Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
2026-09-30 23:13:03 +02:00

58 lines
1.2 KiB
JavaScript
Executable file

define(function(require, exports) {
'use strict';
/**
* Get the port of the url.
* @param {string} url full url.
* @return {?number} port number, null if none found.
*/
exports.getPort = function(url) {
var parts = url.split(':');
if (parts.length < 2) {
return null;
}
// If we found a port and path, it's the last part
var candidate = parts[parts.length - 1];
parts = candidate.split('/');
// If we found a port, it's the first part
candidate = parts[0];
if (!isInteger(candidate)) {
return null;
}
return parseInt(candidate, 10);
};
/**
* Get the scheme of the url. Note that this only detects http and https.
* @param {string} url full url.
* @return {?string} uri scheme (ie http), null if none found.
*/
exports.getScheme = function(url) {
var parts = url.split(':');
if (parts.length < 2) {
return null;
}
// If we found a scheme, it's the first part
var candidate = parts[0];
if (candidate !== 'http' && candidate !== 'https') {
return null;
}
return candidate;
};
/**
* Decide whether or not this string represents an integer.
* @param {string} str some string.
* @param {boolean} whether or not str represents an integer.
*/
function isInteger(str) {
return (/^\d+$/).test(str);
}
});