All files helper.ts

96.77% Statements 30/31
87.5% Branches 14/16
100% Functions 3/3
96.67% Lines 29/30

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  2x 2x 2x             2x 2x             2x 8x 8x 8x 13x 13x 3x 10x 2x 2x 8x 2x 6x 3x 3x 3x         8x           2x   2x   2x 1x 1x   1x     2x    
import { Day } from './types';
import * as keytar from 'keytar';
import { v4 as uuidv4 } from 'uuid';
import Conf from 'conf';
 
/**
 * Rounds number to specific digit.
 * @param number the number to round.
 * @param place the digit to round to.
 */
export function toPlace(number: number, place = 2) {
  return Math.round(number * Math.pow(10, place)) / Math.pow(10, place);
}
 
/**
 * Converts to Google recurrence days string array.
 * @param courseDays the CU days string
 */
export function toDays(courseDays: string): Day[] {
  const days: Day[] = [];
  const chars = courseDays.toLowerCase().split('');
  for (let i = 0; i < chars.length; i++) {
    const char = chars[i];
    if (char === 'm') {
      days.push('MO');
    } else if (char === 't' && i + 1 < chars.length && chars[i + 1] === 'h') {
      days.push('TH');
      i++;
    } else if (char === 't') {
      days.push('TU');
    } else if (char === 'w') {
      days.push('WE');
    } else Eif (char === 'f') {
      days.push('FR');
    } else {
      throw new TypeError(`Invalid course days "${courseDays}"`);
    }
  }
  return days;
}
 
/**
 * Setups the secured config, using the system's keyring.
 */
export async function setupConfig(): Promise<Conf> {
  // create config for app
  const foundPassword = await keytar.findPassword('cu-api');
  let configPassword: string;
  if (!foundPassword) {
    configPassword = uuidv4();
    keytar.setPassword('cu-api', 'cu-api', configPassword);
  } else {
    configPassword = foundPassword;
  }
 
  return new Conf({ encryptionKey: configPassword });
}