Change all project enums to objects with as const (#2445)

* feat: change all enums to as const objects

* Add word to dictionary

---------

Co-authored-by: Dolan Miu <dolan_miu@hotmail.com>
This commit is contained in:
Stepan Svechnikov
2023-12-22 10:25:00 +09:00
committed by GitHub
parent fd1ea5b4dc
commit a756a7697c
60 changed files with 790 additions and 666 deletions

View File

@ -19,41 +19,47 @@ import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
// <xsd:enumeration value="right"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum AlignmentType {
/** Align Start */
START = "start",
/** Align Center */
CENTER = "center",
/** End */
END = "end",
/** Justified */
BOTH = "both",
/** Medium Kashida Length */
MEDIUM_KASHIDA = "mediumKashida",
/** Distribute All Characters Equally */
DISTRIBUTE = "distribute",
/** Align to List Tab */
NUM_TAB = "numTab",
/** Widest Kashida Length */
HIGH_KASHIDA = "highKashida",
/** Low Kashida Length */
LOW_KASHIDA = "lowKashida",
/** Thai Language Justification */
THAI_DISTRIBUTE = "thaiDistribute",
/** Align Left */
LEFT = "left",
/** Align Right */
RIGHT = "right",
/** Justified */
JUSTIFIED = "both",
}
export class AlignmentAttributes extends XmlAttributeComponent<{ readonly val: AlignmentType }> {
/* eslint-disable @typescript-eslint/naming-convention */
export const AlignmentType = {
/** Align Start */
START: "start",
/** Align Center */
CENTER: "center",
/** End */
END: "end",
/** Justified */
BOTH: "both",
/** Medium Kashida Length */
MEDIUM_KASHIDA: "mediumKashida",
/** Distribute All Characters Equally */
DISTRIBUTE: "distribute",
/** Align to List Tab */
NUM_TAB: "numTab",
/** Widest Kashida Length */
HIGH_KASHIDA: "highKashida",
/** Low Kashida Length */
LOW_KASHIDA: "lowKashida",
/** Thai Language Justification */
THAI_DISTRIBUTE: "thaiDistribute",
/** Align Left */
LEFT: "left",
/** Align Right */
RIGHT: "right",
/** Justified */
JUSTIFIED: "both",
} as const;
/* eslint-enable */
export class AlignmentAttributes extends XmlAttributeComponent<{
readonly val: (typeof AlignmentType)[keyof typeof AlignmentType];
}> {
protected readonly xmlKeys = { val: "w:val" };
}
export class Alignment extends XmlComponent {
public constructor(type: AlignmentType) {
public constructor(type: (typeof AlignmentType)[keyof typeof AlignmentType]) {
super("w:jc");
this.root.push(new AlignmentAttributes({ val: type }));
}

View File

@ -2,14 +2,14 @@
import { Attributes, XmlComponent } from "@file/xml-components";
import { Run } from "../run";
enum BreakType {
COLUMN = "column",
PAGE = "page",
const BreakType = {
COLUMN: "column",
PAGE: "page",
// textWrapping breaks are the default and already exposed via the "Run" class
}
} as const;
class Break extends XmlComponent {
public constructor(type: BreakType) {
public constructor(type: (typeof BreakType)[keyof typeof BreakType]) {
super("w:br");
this.root.push(
new Attributes({

View File

@ -1,17 +1,19 @@
// http://officeopenxml.com/WPspacing.php
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
export enum LineRuleType {
AT_LEAST = "atLeast",
EXACTLY = "exactly",
EXACT = "exact",
AUTO = "auto",
}
export const LineRuleType = {
// eslint-disable-next-line @typescript-eslint/naming-convention
AT_LEAST: "atLeast",
EXACTLY: "exactly",
EXACT: "exact",
AUTO: "auto",
} as const;
export interface ISpacingProperties {
readonly after?: number;
readonly before?: number;
readonly line?: number;
readonly lineRule?: LineRuleType;
readonly lineRule?: (typeof LineRuleType)[keyof typeof LineRuleType];
readonly beforeAutoSpacing?: boolean;
readonly afterAutoSpacing?: boolean;
}

View File

@ -1,14 +1,14 @@
import { Attributes, XmlComponent } from "@file/xml-components";
export enum HeadingLevel {
HEADING_1 = "Heading1",
HEADING_2 = "Heading2",
HEADING_3 = "Heading3",
HEADING_4 = "Heading4",
HEADING_5 = "Heading5",
HEADING_6 = "Heading6",
TITLE = "Title",
}
export const HeadingLevel = {
HEADING_1: "Heading1",
HEADING_2: "Heading2",
HEADING_3: "Heading3",
HEADING_4: "Heading4",
HEADING_5: "Heading5",
HEADING_6: "Heading6",
TITLE: "Title",
} as const;
export class Style extends XmlComponent {
public constructor(styleId: string) {

View File

@ -2,9 +2,9 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
export interface TabStopDefinition {
readonly type: TabStopType;
readonly position: number | TabStopPosition;
readonly leader?: LeaderType;
readonly type: (typeof TabStopType)[keyof typeof TabStopType];
readonly position: number | (typeof TabStopPosition)[keyof typeof TabStopPosition];
readonly leader?: (typeof LeaderType)[keyof typeof LeaderType];
}
export class TabStop extends XmlComponent {
@ -17,34 +17,35 @@ export class TabStop extends XmlComponent {
}
}
export enum TabStopType {
LEFT = "left",
RIGHT = "right",
CENTER = "center",
BAR = "bar",
CLEAR = "clear",
DECIMAL = "decimal",
END = "end",
NUM = "num",
START = "start",
}
export const TabStopType = {
LEFT: "left",
RIGHT: "right",
CENTER: "center",
BAR: "bar",
CLEAR: "clear",
DECIMAL: "decimal",
END: "end",
NUM: "num",
START: "start",
} as const;
export enum LeaderType {
DOT = "dot",
HYPHEN = "hyphen",
MIDDLE_DOT = "middleDot",
NONE = "none",
UNDERSCORE = "underscore",
}
export const LeaderType = {
DOT: "dot",
HYPHEN: "hyphen",
// eslint-disable-next-line @typescript-eslint/naming-convention
MIDDLE_DOT: "middleDot",
NONE: "none",
UNDERSCORE: "underscore",
} as const;
export enum TabStopPosition {
MAX = 9026,
}
export const TabStopPosition = {
MAX: 9026,
} as const;
export class TabAttributes extends XmlAttributeComponent<{
readonly val: TabStopType;
readonly val: (typeof TabStopType)[keyof typeof TabStopType];
readonly pos: string | number;
readonly leader?: LeaderType;
readonly leader?: (typeof LeaderType)[keyof typeof LeaderType];
}> {
protected readonly xmlKeys = { val: "w:val", pos: "w:pos", leader: "w:leader" };
}

View File

@ -3,43 +3,44 @@ import { HorizontalPositionAlign, VerticalPositionAlign } from "@file/shared/ali
import { HeightRule } from "@file/table";
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
export enum DropCapType {
NONE = "none",
DROP = "drop",
MARGIN = "margin",
}
export const DropCapType = {
NONE: "none",
DROP: "drop",
MARGIN: "margin",
} as const;
export enum FrameAnchorType {
MARGIN = "margin",
PAGE = "page",
TEXT = "text",
}
export const FrameAnchorType = {
MARGIN: "margin",
PAGE: "page",
TEXT: "text",
} as const;
export enum FrameWrap {
AROUND = "around",
AUTO = "auto",
NONE = "none",
NOT_BESIDE = "notBeside",
THROUGH = "through",
TIGHT = "tight",
}
export const FrameWrap = {
AROUND: "around",
AUTO: "auto",
NONE: "none",
// eslint-disable-next-line @typescript-eslint/naming-convention
NOT_BESIDE: "notBeside",
THROUGH: "through",
TIGHT: "tight",
} as const;
interface IBaseFrameOptions {
readonly anchorLock?: boolean;
readonly dropCap?: DropCapType;
readonly dropCap?: (typeof DropCapType)[keyof typeof DropCapType];
readonly width: number;
readonly height: number;
readonly wrap?: FrameWrap;
readonly wrap?: (typeof FrameWrap)[keyof typeof FrameWrap];
readonly lines?: number;
readonly anchor: {
readonly horizontal: FrameAnchorType;
readonly vertical: FrameAnchorType;
readonly horizontal: (typeof FrameAnchorType)[keyof typeof FrameAnchorType];
readonly vertical: (typeof FrameAnchorType)[keyof typeof FrameAnchorType];
};
readonly space?: {
readonly horizontal: number;
readonly vertical: number;
};
readonly rule?: HeightRule;
readonly rule?: (typeof HeightRule)[keyof typeof HeightRule];
}
export interface IXYFrameOptions extends IBaseFrameOptions {
@ -51,8 +52,8 @@ export interface IXYFrameOptions extends IBaseFrameOptions {
export interface IAlignmentFrameOptions extends IBaseFrameOptions {
readonly alignment: {
readonly x: HorizontalPositionAlign;
readonly y: VerticalPositionAlign;
readonly x: (typeof HorizontalPositionAlign)[keyof typeof HorizontalPositionAlign];
readonly y: (typeof VerticalPositionAlign)[keyof typeof VerticalPositionAlign];
};
}
@ -62,20 +63,20 @@ export type IFrameOptions = IXYFrameOptions | IAlignmentFrameOptions;
export class FramePropertiesAttributes extends XmlAttributeComponent<{
readonly anchorLock?: boolean;
readonly dropCap?: DropCapType;
readonly dropCap?: (typeof DropCapType)[keyof typeof DropCapType];
readonly width: number;
readonly height: number;
readonly x?: number;
readonly y?: number;
readonly wrap?: FrameWrap;
readonly wrap?: (typeof FrameWrap)[keyof typeof FrameWrap];
readonly lines?: number;
readonly anchorHorizontal?: FrameAnchorType;
readonly anchorVertical?: FrameAnchorType;
readonly anchorHorizontal?: (typeof FrameAnchorType)[keyof typeof FrameAnchorType];
readonly anchorVertical?: (typeof FrameAnchorType)[keyof typeof FrameAnchorType];
readonly spaceHorizontal?: number;
readonly spaceVertical?: number;
readonly rule?: HeightRule;
readonly alignmentX?: HorizontalPositionAlign;
readonly alignmentY?: VerticalPositionAlign;
readonly rule?: (typeof HeightRule)[keyof typeof HeightRule];
readonly alignmentX?: (typeof HorizontalPositionAlign)[keyof typeof HorizontalPositionAlign];
readonly alignmentY?: (typeof VerticalPositionAlign)[keyof typeof VerticalPositionAlign];
}> {
protected readonly xmlKeys = {
anchorLock: "w:anchorLock",

View File

@ -5,10 +5,10 @@ import { uniqueId } from "@util/convenience-functions";
import { ParagraphChild } from "../paragraph";
import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink-attributes";
export enum HyperlinkType {
INTERNAL = "INTERNAL",
EXTERNAL = "EXTERNAL",
}
export const HyperlinkType = {
INTERNAL: "INTERNAL",
EXTERNAL: "EXTERNAL",
} as const;
export class ConcreteHyperlink extends XmlComponent {
public readonly linkId: string;
@ -39,7 +39,12 @@ export class InternalHyperlink extends ConcreteHyperlink {
}
export class ExternalHyperlink extends XmlComponent {
public constructor(public readonly options: { readonly children: readonly ParagraphChild[]; readonly link: string }) {
public constructor(
public readonly options: {
readonly children: readonly ParagraphChild[];
readonly link: string;
},
) {
super("w:externalHyperlink");
}
}

View File

@ -18,7 +18,7 @@ import { OutlineLevel } from "./links";
import { IRunOptions, RunProperties } from ".";
export interface ILevelParagraphStylePropertiesOptions {
readonly alignment?: AlignmentType;
readonly alignment?: (typeof AlignmentType)[keyof typeof AlignmentType];
readonly thematicBreak?: boolean;
readonly contextualSpacing?: boolean;
readonly rightTabStop?: number;
@ -47,7 +47,7 @@ export interface IParagraphStylePropertiesOptions extends ILevelParagraphStylePr
export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOptions {
readonly border?: IBordersOptions;
readonly heading?: HeadingLevel;
readonly heading?: (typeof HeadingLevel)[keyof typeof HeadingLevel];
readonly bidirectional?: boolean;
readonly pageBreakBefore?: boolean;
readonly tabStops?: readonly TabStopDefinition[];

View File

@ -1,11 +1,11 @@
import { Attributes, XmlComponent } from "@file/xml-components";
export enum EmphasisMarkType {
DOT = "dot",
}
export const EmphasisMarkType = {
DOT: "dot",
} as const;
export abstract class BaseEmphasisMark extends XmlComponent {
protected constructor(emphasisMarkType: EmphasisMarkType) {
protected constructor(emphasisMarkType: (typeof EmphasisMarkType)[keyof typeof EmphasisMarkType]) {
super("w:em");
this.root.push(
new Attributes({
@ -16,7 +16,7 @@ export abstract class BaseEmphasisMark extends XmlComponent {
}
export class EmphasisMark extends BaseEmphasisMark {
public constructor(emphasisMarkType: EmphasisMarkType = EmphasisMarkType.DOT) {
public constructor(emphasisMarkType: (typeof EmphasisMarkType)[keyof typeof EmphasisMarkType] = EmphasisMarkType.DOT) {
super(emphasisMarkType);
}
}

View File

@ -1,12 +1,15 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
enum FieldCharacterType {
BEGIN = "begin",
END = "end",
SEPARATE = "separate",
}
const FieldCharacterType = {
BEGIN: "begin",
END: "end",
SEPARATE: "separate",
} as const;
class FidCharAttrs extends XmlAttributeComponent<{ readonly type: FieldCharacterType; readonly dirty?: boolean }> {
class FidCharAttrs extends XmlAttributeComponent<{
readonly type: (typeof FieldCharacterType)[keyof typeof FieldCharacterType];
readonly dirty?: boolean;
}> {
protected readonly xmlKeys = { type: "w:fldCharType", dirty: "w:dirty" };
}

View File

@ -7,11 +7,11 @@ import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
// <xsd:enumeration value="right" />
// </xsd:restriction>
// </xsd:simpleType>
export enum PositionalTabAlignment {
LEFT = "left",
CENTER = "center",
RIGHT = "right",
}
export const PositionalTabAlignment = {
LEFT: "left",
CENTER: "center",
RIGHT: "right",
} as const;
// <xsd:simpleType name="ST_PTabRelativeTo">
// <xsd:restriction base="xsd:string">
@ -19,10 +19,10 @@ export enum PositionalTabAlignment {
// <xsd:enumeration value="indent" />
// </xsd:restriction>
// </xsd:simpleType>
export enum PositionalTabRelativeTo {
MARGIN = "margin",
INDENT = "indent",
}
export const PositionalTabRelativeTo = {
MARGIN: "margin",
INDENT: "indent",
} as const;
// <xsd:simpleType name="ST_PTabLeader">
// <xsd:restriction base="xsd:string">
@ -33,18 +33,19 @@ export enum PositionalTabRelativeTo {
// <xsd:enumeration value="middleDot" />
// </xsd:restriction>
// </xsd:simpleType>
export enum PositionalTabLeader {
NONE = "none",
DOT = "dot",
HYPHEN = "hyphen",
UNDERSCORE = "underscore",
MIDDLE_DOT = "middleDot",
}
export const PositionalTabLeader = {
NONE: "none",
DOT: "dot",
HYPHEN: "hyphen",
UNDERSCORE: "underscore",
// eslint-disable-next-line @typescript-eslint/naming-convention
MIDDLE_DOT: "middleDot",
} as const;
export interface PositionalTabOptions {
readonly alignment: PositionalTabAlignment;
readonly relativeTo: PositionalTabRelativeTo;
readonly leader: PositionalTabLeader;
readonly alignment: (typeof PositionalTabAlignment)[keyof typeof PositionalTabAlignment];
readonly relativeTo: (typeof PositionalTabRelativeTo)[keyof typeof PositionalTabRelativeTo];
readonly leader: (typeof PositionalTabLeader)[keyof typeof PositionalTabLeader];
}
// <xsd:complexType name="CT_PTab">
@ -58,9 +59,9 @@ export class PositionalTab extends XmlComponent {
this.root.push(
new NextAttributeComponent<{
readonly alignment: PositionalTabAlignment;
readonly relativeTo: PositionalTabRelativeTo;
readonly leader: PositionalTabLeader;
readonly alignment: (typeof PositionalTabAlignment)[keyof typeof PositionalTabAlignment];
readonly relativeTo: (typeof PositionalTabRelativeTo)[keyof typeof PositionalTabRelativeTo];
readonly leader: (typeof PositionalTabLeader)[keyof typeof PositionalTabLeader];
}>({
alignment: {
key: "w:alignment",

View File

@ -25,15 +25,18 @@ interface IFontOptions {
readonly hint?: string;
}
export enum TextEffect {
BLINK_BACKGROUND = "blinkBackground",
LIGHTS = "lights",
ANTS_BLACK = "antsBlack",
ANTS_RED = "antsRed",
SHIMMER = "shimmer",
SPARKLE = "sparkle",
NONE = "none",
}
/* eslint-disable @typescript-eslint/naming-convention */
export const TextEffect = {
BLINK_BACKGROUND: "blinkBackground",
LIGHTS: "lights",
ANTS_BLACK: "antsBlack",
ANTS_RED: "antsRed",
SHIMMER: "shimmer",
SPARKLE: "sparkle",
NONE: "none",
} as const;
/* eslint-enable */
export interface IRunStylePropertiesOptions {
readonly noProof?: boolean;
@ -43,11 +46,11 @@ export interface IRunStylePropertiesOptions {
readonly italicsComplexScript?: boolean;
readonly underline?: {
readonly color?: string;
readonly type?: UnderlineType;
readonly type?: (typeof UnderlineType)[keyof typeof UnderlineType];
};
readonly effect?: TextEffect;
readonly effect?: (typeof TextEffect)[keyof typeof TextEffect];
readonly emphasisMark?: {
readonly type?: EmphasisMarkType;
readonly type?: (typeof EmphasisMarkType)[keyof typeof EmphasisMarkType];
};
readonly color?: string;
readonly kern?: number | PositiveUniversalMeasure;
@ -127,6 +130,8 @@ export interface IRunPropertiesChangeOptions extends IRunPropertiesOptions, ICha
// <xsd:element name="oMath" type="CT_OnOff"/>
// </xsd:choice>
// </xsd:group>
/* eslint-disable functional/immutable-data */
export class RunProperties extends IgnoreIfEmptyXmlComponent {
public constructor(options?: IRunPropertiesOptions) {
super("w:rPr");
@ -294,6 +299,8 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
}
}
/* eslint-enable */
export class RunPropertiesChange extends XmlComponent {
public constructor(options: IRunPropertiesChangeOptions) {
super("w:rPrChange");

View File

@ -12,7 +12,7 @@ import { TextAttributes } from "../text-attributes";
// </xsd:complexType>
interface ITextOptions {
readonly space?: SpaceType;
readonly space?: (typeof SpaceType)[keyof typeof SpaceType];
readonly text?: string;
}

View File

@ -71,7 +71,7 @@ export interface IRunOptions extends IRunPropertiesOptions {
| FieldInstruction
| Separate
| End
| PageNumber
| (typeof PageNumber)[keyof typeof PageNumber]
| FootnoteReferenceRun
| Break
| AnnotationReference
@ -98,11 +98,14 @@ export interface IRunOptions extends IRunPropertiesOptions {
readonly text?: string;
}
export enum PageNumber {
CURRENT = "CURRENT",
TOTAL_PAGES = "TOTAL_PAGES",
TOTAL_PAGES_IN_SECTION = "TOTAL_PAGES_IN_SECTION",
}
/* eslint-disable @typescript-eslint/naming-convention */
export const PageNumber = {
CURRENT: "CURRENT",
TOTAL_PAGES: "TOTAL_PAGES",
TOTAL_PAGES_IN_SECTION: "TOTAL_PAGES_IN_SECTION",
} as const;
/* eslint-enable */
export class Run extends XmlComponent {
protected readonly properties: RunProperties;

View File

@ -1,6 +1,8 @@
import { SpaceType } from "@file/shared";
import { XmlAttributeComponent } from "@file/xml-components";
export class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
export class TextAttributes extends XmlAttributeComponent<{
readonly space: (typeof SpaceType)[keyof typeof SpaceType];
}> {
protected readonly xmlKeys = { space: "xml:space" };
}

View File

@ -1,29 +1,29 @@
import { Attributes, XmlComponent } from "@file/xml-components";
import { hexColorValue } from "@util/values";
export enum UnderlineType {
SINGLE = "single",
WORDS = "words",
DOUBLE = "double",
THICK = "thick",
DOTTED = "dotted",
DOTTEDHEAVY = "dottedHeavy",
DASH = "dash",
DASHEDHEAVY = "dashedHeavy",
DASHLONG = "dashLong",
DASHLONGHEAVY = "dashLongHeavy",
DOTDASH = "dotDash",
DASHDOTHEAVY = "dashDotHeavy",
DOTDOTDASH = "dotDotDash",
DASHDOTDOTHEAVY = "dashDotDotHeavy",
WAVE = "wave",
WAVYHEAVY = "wavyHeavy",
WAVYDOUBLE = "wavyDouble",
NONE = "none",
}
export const UnderlineType = {
SINGLE: "single",
WORDS: "words",
DOUBLE: "double",
THICK: "thick",
DOTTED: "dotted",
DOTTEDHEAVY: "dottedHeavy",
DASH: "dash",
DASHEDHEAVY: "dashedHeavy",
DASHLONG: "dashLong",
DASHLONGHEAVY: "dashLongHeavy",
DOTDASH: "dotDash",
DASHDOTHEAVY: "dashDotHeavy",
DOTDOTDASH: "dotDotDash",
DASHDOTDOTHEAVY: "dashDotDotHeavy",
WAVE: "wave",
WAVYHEAVY: "wavyHeavy",
WAVYDOUBLE: "wavyDouble",
NONE: "none",
} as const;
export class Underline extends XmlComponent {
public constructor(underlineType: UnderlineType = UnderlineType.SINGLE, color?: string) {
public constructor(underlineType: (typeof UnderlineType)[keyof typeof UnderlineType] = UnderlineType.SINGLE, color?: string) {
super("w:u");
this.root.push(
new Attributes({