Code coverage report for src\parser.js

Statements: 100% (26 / 26)      Branches: 100% (6 / 6)      Functions: 100% (10 / 10)      Lines: 100% (26 / 26)      Ignored: none     

All files » src/ » parser.js
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                      2 2 2                           1                 6 6                 6                   6 6 6 12 12 3     9     5                   9   8                     9   9 1     8 8   8                       8 8             5      
/**
 * An .env file content parser.
 */
export class Parser {
  /**
   * Parser function which can easily called to initialize the Parser and start
   * parsing the given string. It directly returns the resulting environment.
   *
   * @param content - The content string that will be parsed.
   */
  static parse(content: string): any {
    let parser = new Parser();
    parser.doParse(content);
    return parser.getEnv();
  }
 
  /**
   * Environment object containing the parsed key-value-pairs of this Parser
   * object.
   */
  env: any = {};
 
  /**
   * The number of lines parsed by this object.
   */
  lineNum: number = 0;
 
  constructor() {
  }
 
  /**
   * Parses the given content and returns the parsed environment object.
   *
   * @param content - content to parse
   */
  doParse(content: string): any {
    let lines = this.getLines(content);
    return this.parseContent(lines);
  }
 
  /**
   * Splits the given string by line breaks and returns the array of lines.
   *
   * @param content - content to split by line endings
   */
  getLines(content: string): string[] {
    return content.split('\n');
  }
 
  /**
   * Parses the given array of strings. If a line starts with a #, it will be
   * treated as a comment. Otherwise, the line will handed over to line parsing.
   *
   * @param lines - array of lines
   */
  parseContent(lines: string[]): any {
    this.env = {};
    this.lineNum = 0;
    for (let line of lines) {
      this.lineNum++;
      if (line.startsWith('#') || !line) {
        continue;
      }
 
      this.parseLine(line);
    }
 
    return this.env;
  }
 
  /**
   * Parses the given line and sets the resulting key-value pair as an
   * environment object property.
   *
   * @param line - the line reflecting a key-value pair
   */
  parseLine(line: string): void {
    let pair = this.parseKeyValuePair(line);
 
    this.env[pair.key] = pair.value;
  }
 
  /**
   * Splits the given line by = and verifies that a key and a value exist.
   * Returns the key-value pair as an object.
   *
   * @param line - key-value pair as a string
   * @throws ParserException
   */
  parseKeyValuePair(line: string): any {
    let pair = line.split('=');
 
    if (pair.length !== 2) {
      throw new Error('Could not parse key value pair from line "' + line + '"');
    }
 
    let key = pair[0];
    let value = this.stripComments(pair[1]);
 
    return {
      key: key,
      value: value
    };
  }
 
  /**
   * Strips comments from a value.
   *
   * @param value - the value to strip comments from
   */
  stripComments(value: string): string {
    let values = value.split(' #');
    return values[0].trim();
  }
 
  /**
   * Returns the environment object.
   */
  getEnv(): any {
    return this.env;
  }
}