All files / lib whitespace.js

100% Statements 11/11
100% Branches 0/0
100% Functions 3/3
100% Lines 11/11
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 331x 1x 1x   1x               94x       94x 94x 94x 94x     79x         79x          
import peg from 'pegjs';
import fs from 'fs';
import path from 'path';
 
const options = {
  SPACE_CHAR: '\uEFEE',
  TAB_CHAR: '\uEFFE',
  NEWLINE_CHAR: '\uEFFF\n',
  RETURN_CHAR: '\uEEFF',
};
 
function read(filePath) {
  return fs.readFileSync(filePath, { encoding: 'utf8' });
}
 
function escape(template) {
  const pegPath = path.join(__dirname, './preprocessor.pegjs');
  const pegfile = read(pegPath);
  const preprocessor = peg.generate(pegfile);
  return preprocessor.parse(template, options);
}
function unescape(template) {
  const unescaped = template
    .replace(RegExp(options.SPACE_CHAR, ['g']), '\x20')
    .replace(RegExp(options.TAB_CHAR, ['g']), '\x09')
    .replace(RegExp(options.NEWLINE_CHAR, ['g']), '\x0A')
    .replace(RegExp(options.RETURN_CHAR, ['g']), '\x0D');
  return unescaped;
}
 
export { escape, unescape };