All files / lib/md read.js

88.46% Statements 46/52
70% Branches 14/20
100% Functions 9/9
88.46% Lines 46/52
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 124 125 126 127 128 129  1x 1x             4x 4x       2x   2x 2x 2x     2x 2x       2x       2x 2x             4x   4x       4x 4x       4x 2x 2x 2x   2x 1x 1x   1x     1x       2x 2x 2x 2x   2x 2x                           4x 4x 1x     3x       2x       2x 1x   1x       1x       1x 2x   2x       2x               1x        
'use strict';
const stream = require('stream');
const todo = require('../todo');
 
/*
 * PUBLIC API
 */
class SplitParagraphs extends stream.Transform {
  constructor() {
    super();
    this._buffer = '';
  }
 
  _transform(chunk, _, cb) {
    const array = (this._buffer + chunk.toString()).split('\n\n');
 
    while (array.length > 1) {
      const paragraph = array.shift();
      this.push(paragraph);
    }
 
    this._buffer = array[0];
    cb();
  }
 
  _flush(cb) {
    Iif (/^\s*$/.test(this._buffer)) {
      cb();
    }
    else {
      this.push(this._buffer.replace(/\n+$/, ''));
      cb();
    }
  }
}
 
class Deserialize extends stream.Writable {
  constructor(project) {
    super();
 
    Iif (!(project instanceof todo.Project)) {
      throw new TypeError('Expecting object of type todo.Project');
    }
 
    this._project = project;
    this._state = 0;
  }
 
  _write(chunk, _, cb) {
    if (this._state) {
      try {
        const [title, rest] = chunk.toString().split(/\n((.|\n)+)/);
        const [desc, items] = deserializeParagraph(rest);
 
        const list = new todo.List(deserializeTitle(title, 2), desc ? desc : '');
        deserializeItems(list, items);
        this._project.insertList(list);
 
        cb();
      }
      catch (err) {
        cb(err);
      }
    }
    else {
      try {
        const [title, desc] = chunk.toString().split(/\n((.|\n)+)/);
        this._project.title = deserializeTitle(title, 1);
        this._project.desc = desc;
 
        ++this._state;
        cb();
      }
      catch (err) {
        cb(err);
      }
    }
  }
}
 
 
/*
 * PRIVATE FUNCTIONS
 */
function deserializeTitle(string, level) {
  const re = new RegExp('^#{' + level + '}[^#]');
  if (!re.exec(string)) {
    throw new Error(`Expected an H${level} '${string}'`);
  }
 
  return string.substring(level);
}
 
function deserializeParagraph(string) {
  Iif (!string || string.length === 0) {
    return [undefined, undefined];
  }
 
  if (/^\s*[-*]/.test(string)) {
    return [undefined, string];
  }
  return string.split(/\n(\s*[-*](.|\n)*)/);
}
 
function deserializeItems(list, string) {
  Iif (!string) {
    return;
  }
 
  string.split('\n').forEach(item => {
    const match = item.match(/^\s*[-*]\s*\[( |x)\]\s*(.*)/i);
 
    Iif (!match) {
      throw new TypeError(`Invalid todo item '${item}'`);
    }
 
    list.insertItem(match[2], (match[1] !== ' '));
  });
}
 
 
/*
 * MODULE EXPORTS
 */
module.exports = {
  SplitParagraphs,
  Deserialize
};