All files jsonc.ts

100% Statements 18/18
80% Branches 4/5
100% Functions 4/4
100% Lines 14/14

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 201x 1x   4x 4x 1x 4x   2x 2x 2x 2x 10x 2x     2x   1x  
import * as stripJsonComments from 'strip-json-comments';
import {JSONFile, JSONObject} from './json_file';
 
export class JSONC extends JSONFile {
  lineCommentPattern = /^(\s+)?\/\*|\*\/|\/\//;
  parse(content: string): JSONObject {
    return JSON.parse(stripJsonComments(content));
  }
  finalize(content: JSONObject, rawContent: string): string {
    const contentLines = this.stringify(content).split('\n');
    const rawContentLines = rawContent.split('\n');
    rawContentLines.forEach((line, i) => {
      if (line.match(this.lineCommentPattern)) {
        contentLines.splice(i - 1, 0, line);
      }
    });
    return contentLines.join('\n');
  }
}