1.8.0 • Published 5 years ago

@angeligareta/npm-modules-angeligareta v1.8.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Eloquent JavaScript. Chapter 6: The Secret Life of Objects. Section Laying out a table

Author: Ángel Igareta

Build Status npm version

StretchCell

The aim of this practice is to implement a cell type named StretchCell(inner, width, height). It should wrap another cell (like UnderlinedCell does) and ensure that the resulting cell has at least the given width and height, even if the inner cell would naturally be smaller.

Constructor

In the constructor we initialize the inner cell, the height or the width of the StretchCell:

constructor (inner, width, height) {
    this.inner = inner;
    this.width = width;
    this.height = height;
  }

MinHeight and MinWidth

Those methods must return the maximum width or height between the inner cell or the ones passed by argument.

// Minimum width is the maximum between the text width and the one passed to the Constructor.
  minWidth() {
    return Math.max(this.inner.minWidth(), this.width);
  }

  // Minimum height is the maximum between the text height and the one passed to the Constructor.
  minHeight() {
      return Math.max(this.inner.minHeight(), this.height);
}

Draw

As there is an inner cell, we simply call it's draw method to draw it.

// Method that simply call the draw method at the inner cell.
  draw(width, height) {
    return this.inner.draw(width, height);
}

Exports

Finally it's important to remark the module.exports = StretchCell before the class.

Test StretchCell

For testing the stretchcell we used:

/**********************/
/*   StretchCell   */
/**********************/

var valueStretchCell = "StretchCell";
var expectedStretchCell = ['StretchCell    ', '               ' ];
var widthStretchCell = 15, heightStretchCell = 2;

describe("StretchCell", function() {
  it("Creation of StretchCell", function() {
    var stretchCell = new StretchCell(new TCell(valueStretchCell), widthStretchCell, heightStretchCell);

    assert(widthStretchCell === stretchCell.minWidth());
    assert(heightStretchCell === stretchCell.minHeight());
assert(
      JSON.stringify(stretchCell.draw(stretchCell.minWidth(), stretchCell.minHeight())) ==
      JSON.stringify(expectedStretchCell)
    );
  })
});
1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago