All files / lib/md md.js

85.71% Statements 18/21
100% Branches 0/0
72.73% Functions 8/11
85.71% Lines 18/21
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  1x 1x 1x 1x           4x 4x 4x 4x   4x 2x       1x 1x         3x 3x 1x   1x       1x             1x        
'use strict';
const fs = require('fs');
const todo = require('../todo');
const mdReader = require('./read');
const MdWriter = require('./write');
 
/*
 * PUBLIC API
 */
function readFile(filename) {
  return new Promise((resolve, reject) => {
    const project = new todo.Project();
    const splitter = new mdReader.SplitParagraphs();
    const deserializer = new mdReader.Deserialize(project);
 
    fs.createReadStream(filename, 'utf8')
        .on('error', err => reject(err))
      .pipe(splitter)
        .on('error', err => reject(err))
      .pipe(deserializer)
        .on('error', err => reject(err))
        .on('finish', () => resolve(project));
  });
}
 
function writeFile(filename, object) {
  return new Promise((resolve, reject) => {
    const dataStream = new MdWriter(object);
    const fileStream = fs.createWriteStream(filename);
 
    dataStream
      .on('error', err => reject(err))
    .pipe(fileStream)
      .on('error', err => reject(err))
      .on('finish', () => resolve());
  });
}
 
/*
 * MODULE EXPORTS
 */
module.exports = {
  readFile,
  writeFile
};