Turn methods into "add()"

This commit is contained in:
Dolan
2019-06-25 23:17:56 +01:00
parent 3ef8f5311d
commit e2574ec23b
55 changed files with 253 additions and 262 deletions

View File

@ -37,7 +37,7 @@ export class Document extends XmlComponent {
this.root.push(this.body);
}
public addParagraph(paragraph: Paragraph): Document {
public add(paragraph: Paragraph | Table): Document {
this.body.push(paragraph);
return this;
}
@ -47,11 +47,6 @@ export class Document extends XmlComponent {
return this;
}
public addTable(table: Table): Document {
this.body.push(table);
return this;
}
public get Body(): Body {
return this.body;
}

View File

@ -80,20 +80,18 @@ describe("File", () => {
});
});
describe("#addParagraph", () => {
it("should call the underlying document's addParagraph", () => {
describe("#add", () => {
it("should call the underlying document's add a Paragraph", () => {
const file = new File();
const spy = sinon.spy(file.Document, "addParagraph");
const spy = sinon.spy(file.Document, "add");
file.add(new Paragraph({}));
expect(spy.called).to.equal(true);
});
});
describe("#add", () => {
it("should call the underlying document's addTable", () => {
it("should call the underlying document's add when adding a Table", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addTable");
const spy = sinon.spy(wrapper.Document, "add");
wrapper.add(
new Table({
rows: 1,
@ -104,6 +102,17 @@ describe("File", () => {
expect(spy.called).to.equal(true);
});
it("should call the underlying document's add when adding an Image (paragraph)", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "add");
// tslint:disable-next-line:no-any
wrapper.add(new Paragraph(""));
expect(spy.called).to.equal(true);
});
});
describe("#add", () => {
it("should call the underlying document's addTableOfContents", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addTableOfContents");
@ -111,22 +120,13 @@ describe("File", () => {
expect(spy.called).to.equal(true);
});
it("should call the underlying document's addImage", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addParagraph");
// tslint:disable-next-line:no-any
wrapper.add(new Paragraph(""));
expect(spy.called).to.equal(true);
});
});
describe("#createImage", () => {
it("should call the underlying document's createImage", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Media, "addMedia");
const wrapperSpy = sinon.spy(wrapper.Document, "addParagraph");
const wrapperSpy = sinon.spy(wrapper.Document, "add");
wrapper.createImage("");
expect(spy.called).to.equal(true);

View File

@ -114,11 +114,11 @@ export class File {
public add(item: Paragraph | Table | TableOfContents): File {
if (item instanceof Paragraph) {
this.document.addParagraph(item);
this.document.add(item);
}
if (item instanceof Table) {
this.document.addTable(item);
this.document.add(item);
}
if (item instanceof TableOfContents) {
@ -136,7 +136,7 @@ export class File {
): Paragraph {
const image = Media.addImage(this, buffer, width, height, drawingOptions);
const paragraph = new Paragraph(image);
this.document.addParagraph(paragraph);
this.document.add(paragraph);
return paragraph;
}

View File

@ -20,7 +20,7 @@ export class Footnote extends XmlComponent {
);
}
public addParagraph(paragraph: Paragraph): void {
public add(paragraph: Paragraph): void {
paragraph.addRunToFront(new FootnoteRefRun());
this.root.push(paragraph);
}

View File

@ -37,7 +37,7 @@ export class FootNotes extends XmlComponent {
);
const begin = new Footnote(-1, FootnoteType.SEPERATOR);
begin.addParagraph(
begin.add(
new Paragraph({
spacing: {
after: 0,
@ -49,7 +49,7 @@ export class FootNotes extends XmlComponent {
this.root.push(begin);
const spacing = new Footnote(0, FootnoteType.CONTINUATION_SEPERATOR);
spacing.addParagraph(
spacing.add(
new Paragraph({
spacing: {
after: 0,
@ -63,7 +63,7 @@ export class FootNotes extends XmlComponent {
public createFootNote(paragraph: Paragraph): void {
const footnote = new Footnote(this.currentId);
footnote.addParagraph(paragraph);
footnote.add(paragraph);
this.root.push(footnote);
this.currentId++;

View File

@ -22,13 +22,9 @@ export class TableCell extends XmlComponent {
this.root.push(this.properties);
}
public addParagraph(content: Paragraph): TableCell {
this.root.push(content);
return this;
}
public add(item: Paragraph | Table): TableCell {
this.root.push(item);
public addTable(content: Table): TableCell {
this.root.push(content);
return this;
}
@ -36,7 +32,7 @@ export class TableCell extends XmlComponent {
// Cells must end with a paragraph
if (!(this.root[this.root.length - 1] instanceof Paragraph)) {
const para = new Paragraph({});
this.addParagraph(para);
this.add(para);
}
return super.prepForXml();
}

View File

@ -163,19 +163,19 @@ describe("Table", () => {
table
.getRow(0)
.getCell(0)
.addParagraph(new Paragraph("A1"));
.add(new Paragraph("A1"));
table
.getRow(0)
.getCell(1)
.addParagraph(new Paragraph("B1"));
.add(new Paragraph("B1"));
table
.getRow(1)
.getCell(0)
.addParagraph(new Paragraph("A2"));
.add(new Paragraph("A2"));
table
.getRow(1)
.getCell(1)
.addParagraph(new Paragraph("B2"));
.add(new Paragraph("B2"));
const tree = new Formatter().format(table);
const cell = (c) => ({
"w:tc": [
@ -221,10 +221,10 @@ describe("Table", () => {
rows: 2,
columns: 2,
});
table.getCell(0, 0).addParagraph(new Paragraph("A1"));
table.getCell(0, 1).addParagraph(new Paragraph("B1"));
table.getCell(1, 0).addParagraph(new Paragraph("A2"));
table.getCell(1, 1).addParagraph(new Paragraph("B2"));
table.getCell(0, 0).add(new Paragraph("A1"));
table.getCell(0, 1).add(new Paragraph("B1"));
table.getCell(1, 0).add(new Paragraph("A2"));
table.getCell(1, 1).add(new Paragraph("B2"));
const tree = new Formatter().format(table);
const cell = (c) => ({
"w:tc": [
@ -295,7 +295,7 @@ describe("Table", () => {
rows: 1,
columns: 1,
});
parentTable.getCell(0, 0).addTable(
parentTable.getCell(0, 0).add(
new Table({
rows: 1,
columns: 1,
@ -322,7 +322,7 @@ describe("Table", () => {
rows: 1,
columns: 1,
});
parentTable.getCell(0, 0).addParagraph(new Paragraph("Hello"));
parentTable.getCell(0, 0).add(new Paragraph("Hello"));
const tree = new Formatter().format(parentTable);
expect(tree)
.to.have.property("w:tbl")