Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change all project enums to objects with as const #2445

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dolan",
"execa",
"falsey",
"horz",
"iife",
"Initializable",
"iroha",
Expand Down
4 changes: 2 additions & 2 deletions src/export/packer/next-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Compiler {
this.numberingReplacer = new NumberingReplacer();
}

public compile(file: File, prettifyXml?: PrettifyType): JSZip {
public compile(file: File, prettifyXml?: (typeof PrettifyType)[keyof typeof PrettifyType]): JSZip {
const zip = new JSZip();
const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml);
const map = new Map<string, IXmlifyedFile | readonly IXmlifyedFile[]>(Object.entries(xmlifiedFileMapping));
Expand All @@ -66,7 +66,7 @@ export class Compiler {
return zip;
}

private xmlifyFile(file: File, prettify?: PrettifyType): IXmlifyedFileMapping {
private xmlifyFile(file: File, prettify?: (typeof PrettifyType)[keyof typeof PrettifyType]): IXmlifyedFileMapping {
const documentRelationshipCount = file.Document.Relationships.RelationshipCount + 1;

const documentXmlData = xml(
Expand Down
27 changes: 15 additions & 12 deletions src/export/packer/packer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import { Compiler } from "./next-compiler";
/**
* Use blanks to prettify
*/
export enum PrettifyType {
NONE = "",
WITH_2_BLANKS = " ",
WITH_4_BLANKS = " ",
WITH_TAB = "\t",
}
export const PrettifyType = {
NONE: "",
WITH_2_BLANKS: " ",
WITH_4_BLANKS: " ",
// eslint-disable-next-line @typescript-eslint/naming-convention
WITH_TAB: "\t",
} as const;

const convertPrettifyType = (prettify?: boolean | PrettifyType): PrettifyType | undefined =>
const convertPrettifyType = (
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
): (typeof PrettifyType)[keyof typeof PrettifyType] | undefined =>
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify;

export class Packer {
public static async toString(file: File, prettify?: boolean | PrettifyType): Promise<string> {
public static async toString(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<string> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
const zipData = await zip.generateAsync({
type: "string",
Expand All @@ -28,7 +31,7 @@ export class Packer {
return zipData;
}

public static async toBuffer(file: File, prettify?: boolean | PrettifyType): Promise<Buffer> {
public static async toBuffer(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<Buffer> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
const zipData = await zip.generateAsync({
type: "nodebuffer",
Expand All @@ -39,7 +42,7 @@ export class Packer {
return zipData;
}

public static async toBase64String(file: File, prettify?: boolean | PrettifyType): Promise<string> {
public static async toBase64String(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<string> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
const zipData = await zip.generateAsync({
type: "base64",
Expand All @@ -50,7 +53,7 @@ export class Packer {
return zipData;
}

public static async toBlob(file: File, prettify?: boolean | PrettifyType): Promise<Blob> {
public static async toBlob(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<Blob> {
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
const zipData = await zip.generateAsync({
type: "blob",
Expand All @@ -61,7 +64,7 @@ export class Packer {
return zipData;
}

public static toStream(file: File, prettify?: boolean | PrettifyType): Stream {
public static toStream(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Stream {
const stream = new Stream();
const zip = this.compiler.compile(file, convertPrettifyType(prettify));

Expand Down
62 changes: 32 additions & 30 deletions src/file/border/border.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { eighthPointMeasureValue, hexColorValue, pointMeasureValue } from "@util/values";

export interface IBorderOptions {
readonly style: BorderStyle;
readonly style: (typeof BorderStyle)[keyof typeof BorderStyle];
/** Border color, in hex (eg 'FF00AA') */
readonly color?: string;
/** Size of the border in 1/8 pt */
Expand Down Expand Up @@ -55,32 +55,34 @@ class BordersAttributes extends XmlAttributeComponent<IBorderOptions> {
};
}

export enum BorderStyle {
SINGLE = "single",
DASH_DOT_STROKED = "dashDotStroked",
DASHED = "dashed",
DASH_SMALL_GAP = "dashSmallGap",
DOT_DASH = "dotDash",
DOT_DOT_DASH = "dotDotDash",
DOTTED = "dotted",
DOUBLE = "double",
DOUBLE_WAVE = "doubleWave",
INSET = "inset",
NIL = "nil",
NONE = "none",
OUTSET = "outset",
THICK = "thick",
THICK_THIN_LARGE_GAP = "thickThinLargeGap",
THICK_THIN_MEDIUM_GAP = "thickThinMediumGap",
THICK_THIN_SMALL_GAP = "thickThinSmallGap",
THIN_THICK_LARGE_GAP = "thinThickLargeGap",
THIN_THICK_MEDIUM_GAP = "thinThickMediumGap",
THIN_THICK_SMALL_GAP = "thinThickSmallGap",
THIN_THICK_THIN_LARGE_GAP = "thinThickThinLargeGap",
THIN_THICK_THIN_MEDIUM_GAP = "thinThickThinMediumGap",
THIN_THICK_THIN_SMALL_GAP = "thinThickThinSmallGap",
THREE_D_EMBOSS = "threeDEmboss",
THREE_D_ENGRAVE = "threeDEngrave",
TRIPLE = "triple",
WAVE = "wave",
}
/* eslint-disable @typescript-eslint/naming-convention */
export const BorderStyle = {
SINGLE: "single",
DASH_DOT_STROKED: "dashDotStroked",
DASHED: "dashed",
DASH_SMALL_GAP: "dashSmallGap",
DOT_DASH: "dotDash",
DOT_DOT_DASH: "dotDotDash",
DOTTED: "dotted",
DOUBLE: "double",
DOUBLE_WAVE: "doubleWave",
INSET: "inset",
NIL: "nil",
NONE: "none",
OUTSET: "outset",
THICK: "thick",
THICK_THIN_LARGE_GAP: "thickThinLargeGap",
THICK_THIN_MEDIUM_GAP: "thickThinMediumGap",
THICK_THIN_SMALL_GAP: "thickThinSmallGap",
THIN_THICK_LARGE_GAP: "thinThickLargeGap",
THIN_THICK_MEDIUM_GAP: "thinThickMediumGap",
THIN_THICK_SMALL_GAP: "thinThickSmallGap",
THIN_THICK_THIN_LARGE_GAP: "thinThickThinLargeGap",
THIN_THICK_THIN_MEDIUM_GAP: "thinThickThinMediumGap",
THIN_THICK_THIN_SMALL_GAP: "thinThickThinSmallGap",
THREE_D_EMBOSS: "threeDEmboss",
THREE_D_ENGRAVE: "threeDEngrave",
TRIPLE: "triple",
WAVE: "wave",
} as const;
/* eslint-enable */
19 changes: 11 additions & 8 deletions src/file/document/body/section-properties/properties/doc-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ import { decimalNumber } from "@util/values";
// <xsd:attribute name="charSpace" type="ST_DecimalNumber"/>
// </xsd:complexType>

export enum DocumentGridType {
DEFAULT = "default",
LINES = "lines",
LINES_AND_CHARS = "linesAndChars",
SNAP_TO_CHARS = "snapToChars",
}
/* eslint-disable @typescript-eslint/naming-convention */
export const DocumentGridType = {
DEFAULT: "default",
LINES: "lines",
LINES_AND_CHARS: "linesAndChars",
SNAP_TO_CHARS: "snapToChars",
} as const;
Comment on lines +21 to +26
Copy link
Contributor

@hshoja hshoja Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having this is beneficial, but it may be challenging to use types in the code base and not having clean types, Additionally using PascalCase for variables is not considered a good practice I would recommend reconsidering the implementation of the types like this:

Suggested change
export const DocumentGridType = {
DEFAULT: "default",
LINES: "lines",
LINES_AND_CHARS: "linesAndChars",
SNAP_TO_CHARS: "snapToChars",
} as const;
const documentGridType = {
DEFAULT: "default",
LINES: "lines",
LINES_AND_CHARS: "linesAndChars",
SNAP_TO_CHARS: "snapToChars",
} as const;
export type DocumentGridType = (typeof documentGridType)[keyof typeof documentGridType]

or refer to the TypeScript documentation

Suggested change
export const DocumentGridType = {
DEFAULT: "default",
LINES: "lines",
LINES_AND_CHARS: "linesAndChars",
SNAP_TO_CHARS: "snapToChars",
} as const;
const ODocumentGridType = {
DEFAULT: "default",
LINES: "lines",
LINES_AND_CHARS: "linesAndChars",
SNAP_TO_CHARS: "snapToChars",
} as const;
export type DocumentGridType = (typeof documentGridType)[keyof typeof documentGridType]

I can make a pr to fix it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to keep naming of enums, otherwise projects after update package version will fall to errors

If talk about pascal, camel, etc. cases, I'd like to use upper case to show their constant state

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make a breaking change, then it has to be a v9 update

I'd like to use upper case to show their constant state

Yes UPPER_CASE to show constants sounds nice to me

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just forked remark-docx which uses this project and was on version 8.2.0 and this was a breaking change. Thanks to @hshoja for providing a solution. I am no TS expert and never saw that spending hours trying to figure it out.


/* eslint-enable */
export interface IDocGridAttributesProperties {
readonly type?: DocumentGridType;
readonly type?: (typeof DocumentGridType)[keyof typeof DocumentGridType];
readonly linePitch?: number;
readonly charSpace?: number;
}
Expand All @@ -38,7 +41,7 @@ export class DocGridAttributes extends XmlAttributeComponent<IDocGridAttributesP
}

export class DocumentGrid extends XmlComponent {
public constructor(linePitch: number, charSpace?: number, type?: DocumentGridType) {
public constructor(linePitch: number, charSpace?: number, type?: (typeof DocumentGridType)[keyof typeof DocumentGridType]) {
super("w:docGrid");

this.root.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
// <xsd:enumeration value="first"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum HeaderFooterReferenceType {
DEFAULT = "default",
FIRST = "first",
EVEN = "even",
}
export const HeaderFooterReferenceType = {
DEFAULT: "default",
FIRST: "first",
EVEN: "even",
} as const;

// </xsd:complexType>
// <xsd:group name="EG_HdrFtrReferences">
Expand All @@ -33,12 +33,12 @@ export enum HeaderFooterReferenceType {
// </xsd:complexType>

export interface IHeaderFooterOptions {
readonly type?: HeaderFooterReferenceType;
readonly type?: (typeof HeaderFooterReferenceType)[keyof typeof HeaderFooterReferenceType];
readonly id?: number;
}

class FooterReferenceAttributes extends XmlAttributeComponent<{
readonly type: HeaderFooterReferenceType;
readonly type: (typeof HeaderFooterReferenceType)[keyof typeof HeaderFooterReferenceType];
readonly id: string;
}> {
protected readonly xmlKeys = {
Expand All @@ -47,12 +47,13 @@ class FooterReferenceAttributes extends XmlAttributeComponent<{
};
}

export enum HeaderFooterType {
HEADER = "w:headerReference",
FOOTER = "w:footerReference",
}
export const HeaderFooterType = {
HEADER: "w:headerReference",
FOOTER: "w:footerReference",
} as const;

export class HeaderFooterReference extends XmlComponent {
public constructor(type: HeaderFooterType, options: IHeaderFooterOptions) {
public constructor(type: (typeof HeaderFooterType)[keyof typeof HeaderFooterType], options: IHeaderFooterOptions) {
super(type);

this.root.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import { decimalNumber, PositiveUniversalMeasure, twipsMeasureValue } from "@uti
// <xsd:enumeration value="continuous"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum LineNumberRestartFormat {
NEW_PAGE = "newPage",
NEW_SECTION = "newSection",
CONTINUOUS = "continuous",
}

/* eslint-disable @typescript-eslint/naming-convention */
export const LineNumberRestartFormat = {
NEW_PAGE: "newPage",
NEW_SECTION: "newSection",
CONTINUOUS: "continuous",
} as const;
/* eslint-enable */

// <xsd:complexType name="CT_LineNumber">
// <xsd:attribute name="countBy" type="ST_DecimalNumber" use="optional"/>
Expand All @@ -25,7 +28,7 @@ export enum LineNumberRestartFormat {
export interface ILineNumberAttributes {
readonly countBy?: number;
readonly start?: number;
readonly restart?: LineNumberRestartFormat;
readonly restart?: (typeof LineNumberRestartFormat)[keyof typeof LineNumberRestartFormat];
readonly distance?: number | PositiveUniversalMeasure;
}

Expand All @@ -36,13 +39,16 @@ export class LineNumberType extends XmlComponent {
new NextAttributeComponent<{
readonly countBy?: number;
readonly start?: number;
readonly restart?: LineNumberRestartFormat;
readonly restart?: (typeof LineNumberRestartFormat)[keyof typeof LineNumberRestartFormat];
readonly distance?: number | PositiveUniversalMeasure;
}>({
countBy: { key: "w:countBy", value: countBy === undefined ? undefined : decimalNumber(countBy) },
start: { key: "w:start", value: start === undefined ? undefined : decimalNumber(start) },
restart: { key: "w:restart", value: restart },
distance: { key: "w:distance", value: distance === undefined ? undefined : twipsMeasureValue(distance) },
distance: {
key: "w:distance",
value: distance === undefined ? undefined : twipsMeasureValue(distance),
},
}),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,41 @@ import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent } from "@file/xml-comp
// <xsd:enumeration value="notFirstPage"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum PageBorderDisplay {
ALL_PAGES = "allPages",
FIRST_PAGE = "firstPage",
NOT_FIRST_PAGE = "notFirstPage",
}

/* eslint-disable @typescript-eslint/naming-convention */
export const PageBorderDisplay = {
ALL_PAGES: "allPages",
FIRST_PAGE: "firstPage",
NOT_FIRST_PAGE: "notFirstPage",
} as const;
/* eslint-enable */

// <xsd:simpleType name="ST_PageBorderOffset">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="page"/>
// <xsd:enumeration value="text"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum PageBorderOffsetFrom {
PAGE = "page",
TEXT = "text",
}
export const PageBorderOffsetFrom = {
PAGE: "page",
TEXT: "text",
} as const;

// <xsd:simpleType name="ST_PageBorderZOrder">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="front"/>
// <xsd:enumeration value="back"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum PageBorderZOrder {
BACK = "back",
FRONT = "front",
}
export const PageBorderZOrder = {
BACK: "back",
FRONT: "front",
} as const;

export interface IPageBorderAttributes {
readonly display?: PageBorderDisplay;
readonly offsetFrom?: PageBorderOffsetFrom;
readonly zOrder?: PageBorderZOrder;
readonly display?: (typeof PageBorderDisplay)[keyof typeof PageBorderDisplay];
readonly offsetFrom?: (typeof PageBorderOffsetFrom)[keyof typeof PageBorderOffsetFrom];
readonly zOrder?: (typeof PageBorderZOrder)[keyof typeof PageBorderZOrder];
}

export interface IPageBordersOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ import { decimalNumber } from "@util/values";
// <xsd:enumeration value="enDash"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum PageNumberSeparator {
HYPHEN = "hyphen",
PERIOD = "period",
COLON = "colon",
EM_DASH = "emDash",
EN_DASH = "endash",
}

/* eslint-disable @typescript-eslint/naming-convention */
export const PageNumberSeparator = {
HYPHEN: "hyphen",
PERIOD: "period",
COLON: "colon",
EM_DASH: "emDash",
EN_DASH: "endash",
} as const;

/* eslint-enable */

export interface IPageNumberTypeAttributes {
readonly start?: number;
readonly formatType?: NumberFormat;
readonly separator?: PageNumberSeparator;
readonly formatType?: (typeof NumberFormat)[keyof typeof NumberFormat];
readonly separator?: (typeof PageNumberSeparator)[keyof typeof PageNumberSeparator];
}

// <xsd:complexType name="CT_PageNumber">
Expand All @@ -40,6 +44,7 @@ export class PageNumberTypeAttributes extends XmlAttributeComponent<IPageNumberT
separator: "w:chapSep",
};
}

export class PageNumberType extends XmlComponent {
public constructor({ start, formatType, separator }: IPageNumberTypeAttributes) {
super("w:pgNumType");
Expand Down
Loading