{"size": 1520, "ext": "js", "max_stars_repo_path": "src/vigenere-cipher.js", "max_stars_repo_name": "Elena228/basic-js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/vigenere-cipher.js", "max_issues_repo_name": "Elena228/basic-js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/vigenere-cipher.js", "max_forks_repo_name": "Elena228/basic-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.1428571429, "max_line_length": 86, "alphanum_fraction": 0.5427631579}
const CustomError = require("../extensions/custom-error");

class VigenereCipheringMachine {

  constructor(type = true) {
    this.typeOfMachine = type;
  }

  encrypt(message, key) {
    if ((message === undefined) || (key === undefined)) {
    throw new Error('');
    }
    let result = [];
    let strArr = message.toUpperCase().split('');
    let keyArr = key.toUpperCase().split('');
    let i = 0;
    let arg;
    strArr.forEach(element => {
      if (/[A-Z]/.test(element)) {
        arg = ((element.codePointAt(0)-65) + (keyArr[i].codePointAt(0)-65)) % 26;
        i = (i < key.length - 1)? (i + 1): 0;
        result.push(String.fromCodePoint(arg + 65));
      }
      else {
        result.push(element);
      }
    }); 
    return this.typeOfMachine? result.join(''): result.reverse().join('');
  }   
  
  
  decrypt(message, key) {
    if ((message === undefined) || (key === undefined)) {
      throw new Error('');
      } 
    let result = [];
    let strArr = message.split('');
    let keyArr = key.toUpperCase().split('');
    let i = 0;
    let arg;
    strArr.forEach(element => {
      if (/[A-Z]/.test(element)) {
        arg = ((element.codePointAt(0)-65) - (keyArr[i].codePointAt(0)-65) + 26) % 26;
        i = (i < key.length - 1)? (i + 1): 0;
        result.push(String.fromCodePoint(arg + 65));
      }
      else {
        result.push(element);
      }
    }); 
    return this.typeOfMachine? result.join(''): result.reverse().join('');
  }
}

module.exports = VigenereCipheringMachine;
