All files / src utility.ts

100% Statements 74/74
100% Branches 13/13
100% Functions 3/3
100% Lines 74/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 761x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 2x 2x 5x 5x 5x 5x 7x 32x 32x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 18x 18x 6x 18x 3x 3x 9x 6x 6x 1x    
/**
 * Generates a random One-Time Password (OTP) of a specified length.
 *
 * This utility function generates a random OTP using digits from 0 to 9. The length of the OTP is determined by the 
 * provided `num` parameter. The OTP length must be at least 4 digits.
 *
 * @param {number} num - The length of the OTP to generate. Must be a positive integer greater than or equal to 4.
 * @returns {string} A string representing the randomly generated OTP with the specified length.
 *
 * @example
 * const otp = generateOTP(6);
 * console.log(otp); // Example Output: "483920"
 *
 * @throws {Error} Will throw an error if `num` is less than 4 or not a positive integer.
 */
export function generateOTP(num: number): string {
    if (num < 4 || !Number.isInteger(num)) {
        throw new Error("The length of the OTP must be a positive integer greater than or equal to 4.");
    }
 
    const digits = "0123456789";
    let OTP = "";
 
    for (let i = 0; i < num; i++) {
        OTP += digits[Math.floor(Math.random() * 10)];
    }
 
    return OTP;
}
 
 
/**
 * A class to manage form data for database queries, maintaining a specific order of the values.
 * 
 * This class stores the form data in a `data` object, where keys represent field names, 
 * and the values are the corresponding values of the fields. The `order` array is used to 
 * ensure that the values are returned in a specific order.
 */
export class PgFormData {
    private data: Record<string, any>;
    private order: string[];
  
    /**
     * Creates an instance of the PgFormData class.
     *
     * @param {object} data - The form data where keys are field names and values are the field values.
     * @param {string[]} order - The order in which the values should be returned.
     */
    constructor(data: object, order: string[]) {
      this.data = data;
      this.order = order;
    }
  
    /**
     * Returns the form values in the specified order for database query purposes.
     * 
     * If a field's value is an object, it will be converted to a JSON string.
     * If a field's value is `undefined`, it will be replaced with `null`.
     *
     * @returns {Array<any>} - The form values in the specified order, with `null` for undefined values and 
     *                          JSON strings for objects.
     */
    get values(): Array<any> {
      return this.order.map((key) => {
        const value = this.data[key];
        if (value === undefined) {
          return null;
        } else if (value !== null && typeof value === 'object') {
          return JSON.stringify(value); // Stringify objects
        }
        return value;
      });
    }
  }