FreeDATA/gui/src/js/freedata.ts

56 lines
1.6 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;
};
}