FreeDATA/gui/src/js/freedata.ts

131 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-01-17 18:49:14 +00:00
const os = require("os");
const path = require("path");
2024-01-17 18:44:39 +00:00
/**
* Binary to ASCII replacement
* @param {string} data in normal/usual utf-8 format
* @returns base64 encoded string
*/
export function btoa_FD(data) {
2023-10-22 08:12:00 +00:00
//exports.btoa_FD = function (data) {
return Buffer.from(data, "utf-8").toString("base64");
2023-10-22 08:12:00 +00:00
}
/**
* ASCII to Binary replacement
* @param {string} data in base64 encoding
* @returns utf-8 normal/usual string
*/
export function atob_FD(data) {
2023-10-22 08:12:00 +00:00
//exports.atob_FD = function (data) {
return Buffer.from(data, "base64").toString("utf-8");
2023-10-22 08:12:00 +00:00
}
/**
* UTF8 to ASCII btoa
* @param {string} data in base64 encoding
* @returns base64 bota compatible data for use in browser
*/
export function atob(data) {
2023-10-22 08:12:00 +00:00
//exports.atob = function (data) {
return window.btoa(Buffer.from(data, "base64").toString("utf8"));
2023-10-22 08:12:00 +00:00
}
//https://medium.com/@asadise/sorting-a-json-array-according-one-property-in-javascript-18b1d22cd9e9
2023-11-04 18:56:25 +00:00
/**
* Sort a json collection by a property ascending
* @param {string} property property to sort on
* @returns sorted json collection
*/
export function sortByProperty(property) {
return function (a, b) {
if (a[property] > b[property]) return 1;
else if (a[property] < b[property]) return -1;
return 0;
};
}
//https://medium.com/@asadise/sorting-a-json-array-according-one-property-in-javascript-18b1d22cd9e9
2023-11-04 18:56:25 +00:00
/**
* Sort a json collection by a property descending
* @param {string} property property to sort on
* @returns sorted json collection
*/
export function sortByPropertyDesc(property) {
return function (a, b) {
if (a[property] < b[property]) return 1;
else if (a[property] > b[property]) return -1;
return 0;
};
2023-12-10 07:05:48 +00:00
}
/**
* Validate a call sign with ssid
* @param {string} callsign callsign to check
* @returns true or false if callsign appears to be valid with an SSID
*/
2023-12-10 08:01:20 +00:00
export function validateCallsignWithSSID(callsign: string) {
2024-02-21 15:55:45 +00:00
var patt = new RegExp("^[A-Za-z0-9]{1,7}-[0-9]{1,3}$");
2024-02-03 12:55:30 +00:00
callsign = callsign;
2023-12-10 08:01:20 +00:00
if (
callsign === undefined ||
callsign === "" ||
patt.test(callsign) === false
) {
console.error(
"Call sign given is not in correct format or missing; callsign passed is: " +
callsign,
);
2023-12-10 07:05:48 +00:00
return false;
2023-12-10 08:01:20 +00:00
}
2023-12-10 07:05:48 +00:00
return true;
2023-12-10 08:01:20 +00:00
}
2023-12-10 07:05:48 +00:00
/**
* Validate/check if a call sign has an SSID
* @param {string} callsign callsign to check
* @returns true or false if callsign appears to be valid without an SSID
*/
2023-12-10 08:01:20 +00:00
export function validateCallsignWithoutSSID(callsign: string) {
2024-02-21 15:55:45 +00:00
var patt = new RegExp("^[A-Za-z0-9]{1,7}$");
2023-12-10 08:01:20 +00:00
if (
callsign === undefined ||
callsign === "" ||
patt.test(callsign) === false
) {
console.error(
"Call sign given is not in correct format or missing; callsign passed is: " +
callsign,
);
2023-12-10 07:05:48 +00:00
return false;
2023-12-10 08:01:20 +00:00
}
2023-12-10 07:05:48 +00:00
return true;
2023-12-10 08:01:20 +00:00
}
2024-01-17 18:44:39 +00:00
2024-01-17 18:49:14 +00:00
export function getAppDataPath() {
const platform = os.platform();
let appDataPath;
2024-01-17 18:44:39 +00:00
2024-01-23 10:44:34 +00:00
// Check if running in GitHub Actions
const isGitHubActions = process.env.GITHUB_ACTIONS === "true";
2024-01-23 10:39:16 +00:00
if (isGitHubActions) {
return "/home/runner/work/FreeDATA/FreeDATA/gui/config";
}
2024-01-17 18:49:14 +00:00
switch (platform) {
case "darwin": // macOS
appDataPath = path.join(os.homedir(), "Library", "Application Support");
break;
case "win32": // Windows
appDataPath =
process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming");
break;
case "linux": // Linux
appDataPath = path.join(os.homedir(), ".config");
break;
default:
throw new Error("Unsupported platform");
}
2024-01-17 18:44:39 +00:00
2024-01-17 18:49:14 +00:00
return appDataPath;
}