Introduce some functional programming techniques

This commit is contained in:
Dolan
2018-11-02 02:51:57 +00:00
parent 9cfd835171
commit 7980f14efb
108 changed files with 749 additions and 659 deletions

View File

@ -3,19 +3,19 @@ import { Level } from "./level";
import { MultiLevelType } from "./multi-level-type";
interface IAbstractNumberingAttributesProperties {
abstractNumId?: number;
restartNumberingAfterBreak?: number;
readonly abstractNumId?: number;
readonly restartNumberingAfterBreak?: number;
}
class AbstractNumberingAttributes extends XmlAttributeComponent<IAbstractNumberingAttributesProperties> {
protected xmlKeys = {
protected readonly xmlKeys = {
abstractNumId: "w:abstractNumId",
restartNumberingAfterBreak: "w15:restartNumberingAfterBreak",
};
}
export class AbstractNumbering extends XmlComponent {
public id: number;
public readonly id: number;
constructor(id: number) {
super("w:abstractNum");

View File

@ -1,16 +1,27 @@
import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
import * as paragraph from "../paragraph/formatting";
import {
Alignment,
AlignmentOptions,
Indent,
ISpacingProperties,
KeepLines,
KeepNext,
LeftTabStop,
MaxRightTabStop,
Spacing,
ThematicBreak,
} from "../paragraph/formatting";
import { ParagraphProperties } from "../paragraph/properties";
import * as formatting from "../paragraph/run/formatting";
import { RunProperties } from "../paragraph/run/properties";
interface ILevelAttributesProperties {
ilvl?: number;
tentative?: number;
readonly ilvl?: number;
readonly tentative?: number;
}
class LevelAttributes extends XmlAttributeComponent<ILevelAttributesProperties> {
protected xmlKeys = {
protected readonly xmlKeys = {
ilvl: "w:ilvl",
tentative: "w15:tentative",
};
@ -190,57 +201,57 @@ export class LevelBase extends XmlComponent {
// --------------------- Paragraph formatting ------------------------ //
public center(): Level {
this.addParagraphProperty(new paragraph.Alignment("center"));
this.addParagraphProperty(new Alignment(AlignmentOptions.CENTER));
return this;
}
public left(): Level {
this.addParagraphProperty(new paragraph.Alignment("left"));
this.addParagraphProperty(new Alignment(AlignmentOptions.LEFT));
return this;
}
public right(): Level {
this.addParagraphProperty(new paragraph.Alignment("right"));
this.addParagraphProperty(new Alignment(AlignmentOptions.RIGHT));
return this;
}
public justified(): Level {
this.addParagraphProperty(new paragraph.Alignment("both"));
this.addParagraphProperty(new Alignment(AlignmentOptions.BOTH));
return this;
}
public thematicBreak(): Level {
this.addParagraphProperty(new paragraph.ThematicBreak());
this.addParagraphProperty(new ThematicBreak());
return this;
}
public maxRightTabStop(): Level {
this.addParagraphProperty(new paragraph.MaxRightTabStop());
this.addParagraphProperty(new MaxRightTabStop());
return this;
}
public leftTabStop(position: number): Level {
this.addParagraphProperty(new paragraph.LeftTabStop(position));
this.addParagraphProperty(new LeftTabStop(position));
return this;
}
public indent(attrs: object): Level {
this.addParagraphProperty(new paragraph.Indent(attrs));
this.addParagraphProperty(new Indent(attrs));
return this;
}
public spacing(params: paragraph.ISpacingProperties): Level {
this.addParagraphProperty(new paragraph.Spacing(params));
public spacing(params: ISpacingProperties): Level {
this.addParagraphProperty(new Spacing(params));
return this;
}
public keepNext(): Level {
this.addParagraphProperty(new paragraph.KeepNext());
this.addParagraphProperty(new KeepNext());
return this;
}
public keepLines(): Level {
this.addParagraphProperty(new paragraph.KeepLines());
this.addParagraphProperty(new KeepLines());
return this;
}
}

View File

@ -13,15 +13,15 @@ class AbstractNumId extends XmlComponent {
}
interface INumAttributesProperties {
numId: number;
readonly numId: number;
}
class NumAttributes extends XmlAttributeComponent<INumAttributesProperties> {
protected xmlKeys = { numId: "w:numId" };
protected readonly xmlKeys = { numId: "w:numId" };
}
export class Num extends XmlComponent {
public id: number;
public readonly id: number;
constructor(numId: number, abstractNumId: number) {
super("w:num");
@ -41,12 +41,12 @@ export class Num extends XmlComponent {
}
}
class LevelOverrideAttributes extends XmlAttributeComponent<{ ilvl: number }> {
protected xmlKeys = { ilvl: "w:ilvl" };
class LevelOverrideAttributes extends XmlAttributeComponent<{ readonly ilvl: number }> {
protected readonly xmlKeys = { ilvl: "w:ilvl" };
}
export class LevelOverride extends XmlComponent {
private lvl?: LevelForOverride;
private readonly lvl: LevelForOverride;
constructor(private readonly levelNum: number, start?: number) {
super("w:lvlOverride");
@ -54,23 +54,18 @@ export class LevelOverride extends XmlComponent {
if (start !== undefined) {
this.root.push(new StartOverride(start));
}
this.lvl = new LevelForOverride(this.levelNum);
this.root.push(this.lvl);
}
public get Level(): LevelForOverride {
let lvl: LevelForOverride;
if (!this.lvl) {
lvl = new LevelForOverride(this.levelNum);
this.root.push(lvl);
this.lvl = lvl;
} else {
lvl = this.lvl;
}
return lvl;
return this.lvl;
}
}
class StartOverrideAttributes extends XmlAttributeComponent<{ val: number }> {
protected xmlKeys = { val: "w:val" };
class StartOverrideAttributes extends XmlAttributeComponent<{ readonly val: number }> {
protected readonly xmlKeys = { val: "w:val" };
}
class StartOverride extends XmlComponent {

View File

@ -378,14 +378,69 @@ describe("concrete numbering", () => {
it("sets a new override level for the given level number", () => {
concreteNumbering.overrideLevel(3);
const tree = new Formatter().format(concreteNumbering);
expect(tree["w:num"]).to.include({ "w:lvlOverride": [{ _attr: { "w:ilvl": 3 } }] });
expect(tree["w:num"]).to.include({
"w:lvlOverride": [
{
_attr: {
"w:ilvl": 3,
},
},
{
"w:lvl": [
{
_attr: {
"w:ilvl": 3,
"w15:tentative": 1,
},
},
{
"w:pPr": [],
},
{
"w:rPr": [],
},
],
},
],
});
});
it("sets the startOverride element if start is given", () => {
concreteNumbering.overrideLevel(1, 9);
const tree = new Formatter().format(concreteNumbering);
expect(tree["w:num"]).to.include({
"w:lvlOverride": [{ _attr: { "w:ilvl": 1 } }, { "w:startOverride": [{ _attr: { "w:val": 9 } }] }],
"w:lvlOverride": [
{
_attr: {
"w:ilvl": 1,
},
},
{
"w:startOverride": [
{
_attr: {
"w:val": 9,
},
},
],
},
{
"w:lvl": [
{
_attr: {
"w:ilvl": 1,
"w15:tentative": 1,
},
},
{
"w:pPr": [],
},
{
"w:rPr": [],
},
],
},
],
});
});

View File

@ -5,6 +5,7 @@ import { AbstractNumbering } from "./abstract-numbering";
import { Num } from "./num";
export class Numbering extends XmlComponent {
// tslint:disable-next-line:readonly-keyword
private nextId: number;
private readonly abstractNumbering: XmlComponent[] = [];