FreeDATA/gui/src/js/freedata.ts

100 lines
2.7 KiB
TypeScript
Raw Normal View History

/**
* 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) {
2023-12-10 07:05:48 +00:00
var patt = new RegExp("^[A-Z,a-z]+[0-9][A-Z,a-z]*-(1[0-5]|[0-9])$");
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) {
2023-12-10 07:05:48 +00:00
var patt = new RegExp("^[A-Za-z]+[0-9][A-Za-z]+$");
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
}