misc.js

/**
 * @file misc.js
 * @description This file contains versatile fonctions without a specific module appartenance.
 * 
 * Functions included in this file : 
 *   - **showToast**
 *   - **getMedian**
 *   - **formatFrequency**
 * 
 * @version 1.0.0
 * @since 2024-05-31
 * @author Owen Pichot
 * @license Public Domain
 */

/**
 * @module Misc
 */

/**
 * Displays a small popup message in the bottom of the screen.
 * 
 * @function showToast
 * @memberof module:Misc
 * @param {string} message Message you want to display within the popup. 
 * @param {string} status Style of the popup window :
 *  - **toast-success**
 *  - **toast-info**
 *  - **toast-error**
 */
function showToast(message, status) {
    let toast = document.getElementById("toast");
    toast.innerHTML = message;
    toast.className = "";
    toast.classList.add("show", status);
    setTimeout(function(){ toast.className = toast.className.replace("show", ""); }, 3000);
};

/**
 * This function returns the median value from an array.
 * @function getMedian
 * @memberof module:Misc
 * @param {number[]} array Array of numbers. 
 * @returns {number} The median value from the array.
 */
function getMedian(array){
    let total = 0;
    for (let i = 0; i < array.length; i++){
        total = total + array[i];
    }
    return total / array.length
};

/**
 * This function takes in a frequency in Hz and returns a more readable result in kHz or mHz.
 * @function formatFrequency
 * @memberof module:Misc
 * @param {number} hertz 
 * @returns {string} A string value containing the number and scale of the parameter passed.
 */
function formatFrequency(hertz) {
    if (hertz < 1e3) {
        return `${hertz} Hz`;
    } else if (hertz < 1e6) {
        return `${(hertz / 1e3).toFixed(1)} kHz`;
    } else if (hertz < 1e9) {
        return `${(hertz / 1e6).toFixed(1)} MHz`;
    }
};