const _ = require('lodash');
const uuidV4 = require('uuid/v4');
const Sentence = require('./sentence');

class Text {
  constructor(text) {
    let paragraphs = _.chain(text).split('\n').compact()
      .map((text, index) => new Paragraph(text, index))
      .value();

    return paragraphs;
  }
}

let sentences = new WeakMap();

class Paragraph {
  constructor(text, index = 0) {
    this.id = uuidV4();
    this.index = index;
    this.text = text;
    this.parseSentence();
    return this;
  }

  get sentencesObject() {
    return sentences.get(this);
  }

  get sentences() {
    return _.map(sentences.get(this), 'id');
  }

  set sentences(s) {
    sentences.set(this, s);
  }

  parseSentence() {
    let {id: paragraphId, text} = this;
    this.sentences = _.chain(text).split('. ').compact()
      .map((text, index) =>
        new Sentence(paragraphId, text, index)
      ).value();
  }
}

module.exports = {Text, Paragraph};

const _ = require('lodash');
const uuidV4 = require('uuid/v4');
const Sentence = require('./sentence');

class Text {
  constructor(parent, text) {
    let paragraphs = _.chain(text).split('\n').compact()
      .map((text, index) => new Paragraph(parent, text, index))
      .value();

    return paragraphs;
  }
}

let sentences = new WeakMap();
let parent = new WeakMap();
class Paragraph {
  constructor($parent, text, index = 0) {
    parent.set(this, $parent);
    this.id = uuidV4();
    this.index = index;
    this.text = text;
    this.parseSentence();
    return this;
  }

  parent() {
    return parent.get(this);
  }

  findSentenceById(id) {
    return _.find(this.sentencesCollection, {id});
  }

  findSentenceByIndex(index) {
    return _.find(this.sentencesCollection, {index});
  }

  next() {
    let index = this.index;
    return this.parent().findParagraphByIndex(index + 1);
  }

  prev() {
    let index = this.index;
    return this.parent().findParagraphByIndex(index - 1);
  }

  get sentencesCollection() {
    return sentences.get(this);
  }

  get sentences() {
    return _.map(sentences.get(this), 'id');
  }

  set sentences(s) {
    sentences.set(this, s);
  }

  parseSentence() {
    let paragraph = this;
    let {text} = paragraph;
    this.sentences = _.chain(text).split('. ').compact()
      .map((text, index) =>
        new Sentence(paragraph, text, index)
      ).value();
  }
}

module.exports = {Text, Paragraph};

Add more method and refactor code.
Add more method and refactor code.

Constructor first argument is a parent text.
- parent() get paragraph parent which is a text.

Adding lookup methods, they are:
- findSentenceById() = find a sentence by its id in the sentencesCollection
- findSentenceByIndex = find a sentence by its index in the sentencesCollection

Navigation methods:
- prev() is for navigating paragraph to the previous paragraph in a text parent context.
- next() is for navigating paragraph to the next paragraph in a text parent context.

Also, rename sentencesObject to sentenceCollection. And pass paragraph to new Sentence rather than sentenceId.