Introduce some functional programming techniques
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
class AliasAttributes extends XmlAttributeComponent<{ alias: string }> {
|
||||
protected xmlKeys = { alias: "w:val" };
|
||||
class AliasAttributes extends XmlAttributeComponent<{ readonly alias: string }> {
|
||||
protected readonly xmlKeys = { alias: "w:val" };
|
||||
}
|
||||
|
||||
export class Alias extends XmlComponent {
|
||||
|
@ -1,14 +1,10 @@
|
||||
// http://officeopenxml.com/WPfieldInstructions.php
|
||||
import { SpaceType } from "file/space-type";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
import { ITableOfContentsOptions } from "./table-of-contents-properties";
|
||||
|
||||
enum SpaceType {
|
||||
DEFAULT = "default",
|
||||
PRESERVE = "preserve",
|
||||
}
|
||||
|
||||
class TextAttributes extends XmlAttributeComponent<{ space: SpaceType }> {
|
||||
protected xmlKeys = { space: "xml:space" };
|
||||
class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
|
||||
protected readonly xmlKeys = { space: "xml:space" };
|
||||
}
|
||||
|
||||
export class FieldInstruction extends XmlComponent {
|
||||
|
@ -1,6 +1,6 @@
|
||||
export class StyleLevel {
|
||||
public styleName: string;
|
||||
public level: number;
|
||||
public readonly styleName: string;
|
||||
public readonly level: number;
|
||||
|
||||
constructor(styleName: string, level: number) {
|
||||
this.styleName = styleName;
|
||||
@ -22,13 +22,13 @@ export interface ITableOfContentsOptions {
|
||||
* The identifier designated by text in this switch's field-argument corresponds to the caption label.
|
||||
* Use captionLabelIncludingNumbers (\c) to build a table of captions with labels and numbers.
|
||||
*/
|
||||
captionLabel?: string;
|
||||
readonly captionLabel?: string;
|
||||
|
||||
/**
|
||||
* \b option - Includes entries only from the portion of the document marked by
|
||||
* the bookmark named by text in this switch's field-argument.
|
||||
*/
|
||||
entriesFromBookmark?: string;
|
||||
readonly entriesFromBookmark?: string;
|
||||
|
||||
/**
|
||||
* \c option - Includes figures, tables, charts, and other items that are numbered
|
||||
@ -36,24 +36,24 @@ export interface ITableOfContentsOptions {
|
||||
* field-argument, which corresponds to the caption label, shall match the identifier in the
|
||||
* corresponding SEQ field.
|
||||
*/
|
||||
captionLabelIncludingNumbers?: string;
|
||||
readonly captionLabelIncludingNumbers?: string;
|
||||
|
||||
/**
|
||||
* \d option - When used with \s, the text in this switch's field-argument defines
|
||||
* the separator between sequence and page numbers. The default separator is a hyphen (-).
|
||||
*/
|
||||
sequenceAndPageNumbersSeparator?: string;
|
||||
readonly sequenceAndPageNumbersSeparator?: string;
|
||||
|
||||
/**
|
||||
* \f option - Includes only those TC fields whose identifier exactly matches the
|
||||
* text in this switch's field-argument (which is typically a letter).
|
||||
*/
|
||||
tcFieldIdentifier?: string;
|
||||
readonly tcFieldIdentifier?: string;
|
||||
|
||||
/**
|
||||
* \h option - Makes the table of contents entries hyperlinks.
|
||||
*/
|
||||
hyperlink?: boolean;
|
||||
readonly hyperlink?: boolean;
|
||||
|
||||
/**
|
||||
* \l option - Includes TC fields that assign entries to one of the levels specified
|
||||
@ -61,14 +61,14 @@ export interface ITableOfContentsOptions {
|
||||
* where startLevel and endLevel are integers, and startLevel has a value equal-to or less-than endLevel.
|
||||
* TC fields that assign entries to lower levels are skipped.
|
||||
*/
|
||||
tcFieldLevelRange?: string;
|
||||
readonly tcFieldLevelRange?: string;
|
||||
|
||||
/**
|
||||
* \n option - Without field-argument, omits page numbers from the table of contents.
|
||||
* Page numbers are omitted from all levels unless a range of entry levels is specified by
|
||||
* text in this switch's field-argument. A range is specified as for \l.
|
||||
*/
|
||||
pageNumbersEntryLevelsRange?: string;
|
||||
readonly pageNumbersEntryLevelsRange?: string;
|
||||
|
||||
/**
|
||||
* \o option - Uses paragraphs formatted with all or the specified range of builtin
|
||||
@ -77,20 +77,20 @@ export interface ITableOfContentsOptions {
|
||||
* to the style with a style ID of HeadingX (e.g. 1 corresponds to Heading1).
|
||||
* If no heading range is specified, all heading levels used in the document are listed.
|
||||
*/
|
||||
headingStyleRange?: string;
|
||||
readonly headingStyleRange?: string;
|
||||
|
||||
/**
|
||||
* \p option - Text in this switch's field-argument specifies a sequence of characters
|
||||
* that separate an entry and its page number. The default is a tab with leader dots.
|
||||
*/
|
||||
entryAndPageNumberSeparator?: string;
|
||||
readonly entryAndPageNumberSeparator?: string;
|
||||
|
||||
/**
|
||||
* \s option - For entries numbered with a SEQ field (§17.16.5.56), adds a prefix to the page number.
|
||||
* The prefix depends on the type of entry. text in this switch's field-argument shall match the
|
||||
* identifier in the SEQ field.
|
||||
*/
|
||||
seqFieldIdentifierForPrefix?: string;
|
||||
readonly seqFieldIdentifierForPrefix?: string;
|
||||
|
||||
/**
|
||||
* \t field-argument Uses paragraphs formatted with styles other than the built-in heading styles.
|
||||
@ -98,25 +98,25 @@ export interface ITableOfContentsOptions {
|
||||
* with each doublet being a comma-separated set of style name and table of content level.
|
||||
* \t can be combined with \o.
|
||||
*/
|
||||
stylesWithLevels?: StyleLevel[];
|
||||
readonly stylesWithLevels?: StyleLevel[];
|
||||
|
||||
/**
|
||||
* \u Uses the applied paragraph outline level.
|
||||
*/
|
||||
useAppliedParagraphOutlineLevel?: boolean;
|
||||
readonly useAppliedParagraphOutlineLevel?: boolean;
|
||||
|
||||
/**
|
||||
* \w Preserves tab entries within table entries.
|
||||
*/
|
||||
preserveTabInEntries?: boolean;
|
||||
readonly preserveTabInEntries?: boolean;
|
||||
|
||||
/**
|
||||
* \x Preserves newline characters within table entries.
|
||||
*/
|
||||
preserveNewLineInEntries?: boolean;
|
||||
readonly preserveNewLineInEntries?: boolean;
|
||||
|
||||
/**
|
||||
* \z Hides tab leader and page numbers in web page view (§17.18.102).
|
||||
*/
|
||||
hideTabAndPageNumbersInWebView?: boolean;
|
||||
readonly hideTabAndPageNumbersInWebView?: boolean;
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import { expect } from "chai";
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { TableOfContents } from "./table-of-contents";
|
||||
import { ITableOfContentsOptions, StyleLevel } from "./table-of-contents-properties";
|
||||
import { StyleLevel } from "./table-of-contents-properties";
|
||||
|
||||
describe("Table of Contents", () => {
|
||||
describe("#constructor", () => {
|
||||
@ -14,28 +14,28 @@ describe("Table of Contents", () => {
|
||||
});
|
||||
|
||||
it("should construct a TOC with all the options and alias", () => {
|
||||
const props: ITableOfContentsOptions = {};
|
||||
|
||||
props.captionLabel = "A";
|
||||
props.entriesFromBookmark = "B";
|
||||
props.captionLabelIncludingNumbers = "C";
|
||||
props.sequenceAndPageNumbersSeparator = "D";
|
||||
props.tcFieldIdentifier = "F";
|
||||
props.hyperlink = true;
|
||||
props.tcFieldLevelRange = "L";
|
||||
props.pageNumbersEntryLevelsRange = "N";
|
||||
props.headingStyleRange = "O";
|
||||
props.entryAndPageNumberSeparator = "P";
|
||||
props.seqFieldIdentifierForPrefix = "S";
|
||||
|
||||
const styles = new Array<StyleLevel>();
|
||||
styles.push(new StyleLevel("SL", 1));
|
||||
styles.push(new StyleLevel("SL", 2));
|
||||
props.stylesWithLevels = styles;
|
||||
props.useAppliedParagraphOutlineLevel = true;
|
||||
props.preserveTabInEntries = true;
|
||||
props.preserveNewLineInEntries = true;
|
||||
props.hideTabAndPageNumbersInWebView = true;
|
||||
|
||||
const props = {
|
||||
captionLabel: "A",
|
||||
entriesFromBookmark: "B",
|
||||
captionLabelIncludingNumbers: "C",
|
||||
sequenceAndPageNumbersSeparator: "D",
|
||||
tcFieldIdentifier: "F",
|
||||
hyperlink: true,
|
||||
tcFieldLevelRange: "L",
|
||||
pageNumbersEntryLevelsRange: "N",
|
||||
headingStyleRange: "O",
|
||||
entryAndPageNumberSeparator: "P",
|
||||
seqFieldIdentifierForPrefix: "S",
|
||||
stylesWithLevels: styles,
|
||||
useAppliedParagraphOutlineLevel: true,
|
||||
preserveTabInEntries: true,
|
||||
preserveNewLineInEntries: true,
|
||||
hideTabAndPageNumbersInWebView: true,
|
||||
};
|
||||
|
||||
const toc = new TableOfContents("Summary", props);
|
||||
const tree = new Formatter().format(toc);
|
||||
|
Reference in New Issue
Block a user