diff --git a/src/file/drawing/anchor/anchor.spec.ts b/src/file/drawing/anchor/anchor.spec.ts index d2da912761..b6a368b4e6 100644 --- a/src/file/drawing/anchor/anchor.spec.ts +++ b/src/file/drawing/anchor/anchor.spec.ts @@ -41,11 +41,11 @@ const createAnchor = (drawingOptions: IDrawingOptions): Anchor => describe("Anchor", () => { before(() => { - stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0); + stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0); }); after(() => { - (convenienceFunctions.uniqueNumericId as SinonStub).restore(); + (convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore(); }); let anchor: Anchor; diff --git a/src/file/drawing/doc-properties/doc-properties.ts b/src/file/drawing/doc-properties/doc-properties.ts index 423317d6c8..8b0093c7f1 100644 --- a/src/file/drawing/doc-properties/doc-properties.ts +++ b/src/file/drawing/doc-properties/doc-properties.ts @@ -2,7 +2,7 @@ import { IContext, IXmlableObject, NextAttributeComponent, XmlComponent } from "@file/xml-components"; import { ConcreteHyperlink } from "@file/paragraph"; -import { uniqueNumericIdCreator } from "@util/convenience-functions"; +import { docPropertiesUniqueNumericId } from "@util/convenience-functions"; import { createHyperlinkClick } from "./doc-properties-children"; @@ -24,8 +24,6 @@ export interface DocPropertiesOptions { readonly title: string; } -const uniqueNumericId = uniqueNumericIdCreator(); - export class DocProperties extends XmlComponent { public constructor({ name, description, title }: DocPropertiesOptions = { name: "", description: "", title: "" }) { super("wp:docPr"); @@ -34,7 +32,7 @@ export class DocProperties extends XmlComponent { new NextAttributeComponent({ id: { key: "id", - value: uniqueNumericId(), + value: docPropertiesUniqueNumericId(), }, name: { key: "name", diff --git a/src/file/drawing/drawing.spec.ts b/src/file/drawing/drawing.spec.ts index d445a01055..8850881159 100644 --- a/src/file/drawing/drawing.spec.ts +++ b/src/file/drawing/drawing.spec.ts @@ -31,11 +31,11 @@ const createDrawing = (drawingOptions?: IDrawingOptions): Drawing => describe("Drawing", () => { before(() => { - stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0); + stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0); }); after(() => { - (convenienceFunctions.uniqueNumericId as SinonStub).restore(); + (convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore(); }); let currentBreak: Drawing; diff --git a/src/file/numbering/numbering.spec.ts b/src/file/numbering/numbering.spec.ts index 71540f8691..ff4c781ebe 100644 --- a/src/file/numbering/numbering.spec.ts +++ b/src/file/numbering/numbering.spec.ts @@ -8,11 +8,13 @@ import { Numbering } from "./numbering"; describe("Numbering", () => { before(() => { - stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0); + stub(convenienceFunctions, "abstractNumUniqueNumericId").callsFake(() => 0); + stub(convenienceFunctions, "concreteNumUniqueNumericId").callsFake(() => 0); }); after(() => { - (convenienceFunctions.uniqueNumericId as SinonStub).restore(); + (convenienceFunctions.abstractNumUniqueNumericId as SinonStub).restore(); + (convenienceFunctions.concreteNumUniqueNumericId as SinonStub).restore(); }); describe("#constructor", () => { diff --git a/src/file/numbering/numbering.ts b/src/file/numbering/numbering.ts index b8eeef2feb..4ec5206327 100644 --- a/src/file/numbering/numbering.ts +++ b/src/file/numbering/numbering.ts @@ -2,7 +2,7 @@ // https://stackoverflow.com/questions/58622437/purpose-of-abstractnum-and-numberinginstance import { AlignmentType } from "@file/paragraph"; import { IContext, IXmlableObject, XmlComponent } from "@file/xml-components"; -import { convertInchesToTwip, uniqueNumericIdCreator } from "@util/convenience-functions"; +import { abstractNumUniqueNumericId, concreteNumUniqueNumericId, convertInchesToTwip } from "@util/convenience-functions"; import { DocumentAttributes } from "../document/document-attributes"; import { AbstractNumbering } from "./abstract-numbering"; @@ -16,9 +16,6 @@ export interface INumberingOptions { }[]; } -const abstractNumUniqueNumericId = uniqueNumericIdCreator(); -const concreteNumUniqueNumericId = uniqueNumericIdCreator(1); // Setting initial to 1 as we have numId = 1 for "default-bullet-numbering" - // // // diff --git a/src/file/paragraph/links/bookmark.ts b/src/file/paragraph/links/bookmark.ts index d88bfd7f46..8bb7c9b5ed 100644 --- a/src/file/paragraph/links/bookmark.ts +++ b/src/file/paragraph/links/bookmark.ts @@ -1,19 +1,17 @@ // http://officeopenxml.com/WPbookmark.php import { XmlComponent } from "@file/xml-components"; -import { uniqueNumericIdCreator } from "@util/convenience-functions"; +import { bookmarkUniqueNumericId } from "@util/convenience-functions"; import { ParagraphChild } from "../paragraph"; import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes"; -const uniqueNumericId = uniqueNumericIdCreator(); - export class Bookmark { public readonly start: BookmarkStart; public readonly children: readonly ParagraphChild[]; public readonly end: BookmarkEnd; public constructor(options: { readonly id: string; readonly children: readonly ParagraphChild[] }) { - const linkId = uniqueNumericId(); + const linkId = bookmarkUniqueNumericId(); this.start = new BookmarkStart(options.id, linkId); this.children = options.children; diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts index b74359de86..7d21b5d171 100644 --- a/src/file/paragraph/paragraph.spec.ts +++ b/src/file/paragraph/paragraph.spec.ts @@ -20,12 +20,12 @@ import { TextRun } from "./run"; describe("Paragraph", () => { before(() => { stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id"); - stub(convenienceFunctions, "uniqueNumericId").callsFake(() => -101); + stub(convenienceFunctions, "bookmarkUniqueNumericId").callsFake(() => -101); }); after(() => { (convenienceFunctions.uniqueId as SinonStub).restore(); - (convenienceFunctions.uniqueNumericId as SinonStub).restore(); + (convenienceFunctions.bookmarkUniqueNumericId as SinonStub).restore(); }); describe("#constructor()", () => { diff --git a/src/file/paragraph/run/image-run.spec.ts b/src/file/paragraph/run/image-run.spec.ts index edc4aa5910..9e9ad89505 100644 --- a/src/file/paragraph/run/image-run.spec.ts +++ b/src/file/paragraph/run/image-run.spec.ts @@ -11,12 +11,12 @@ import { ImageRun } from "./image-run"; describe("ImageRun", () => { before(() => { stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id"); - stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0); + stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0); }); after(() => { (convenienceFunctions.uniqueId as SinonStub).restore(); - (convenienceFunctions.uniqueNumericId as SinonStub).restore(); + (convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore(); }); describe("#constructor()", () => { diff --git a/src/util/convenience-functions.spec.ts b/src/util/convenience-functions.spec.ts index 8fa26f61a3..4bca649b6b 100644 --- a/src/util/convenience-functions.spec.ts +++ b/src/util/convenience-functions.spec.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; -import { convertInchesToTwip, convertMillimetersToTwip, uniqueId, uniqueNumericId } from "./convenience-functions"; +import { convertInchesToTwip, convertMillimetersToTwip, uniqueId, uniqueNumericIdCreator } from "./convenience-functions"; describe("Utility", () => { describe("#convertMillimetersToTwip", () => { @@ -17,8 +17,9 @@ describe("Utility", () => { }); }); - describe("#uniqueNumericId", () => { + describe("#uniqueNumericIdCreator", () => { it("should generate a unique incrementing ID", () => { + const uniqueNumericId = uniqueNumericIdCreator(); expect(uniqueNumericId()).to.not.be.undefined; }); }); diff --git a/src/util/convenience-functions.ts b/src/util/convenience-functions.ts index 8dac4a929b..543b6e9c7c 100644 --- a/src/util/convenience-functions.ts +++ b/src/util/convenience-functions.ts @@ -11,4 +11,9 @@ export const uniqueNumericIdCreator = (initial = 0): (() => number) => { return () => ++currentCount; }; +export const abstractNumUniqueNumericId = uniqueNumericIdCreator(); +export const concreteNumUniqueNumericId = uniqueNumericIdCreator(1); // Setting initial to 1 as we have numId = 1 for "default-bullet-numbering" +export const docPropertiesUniqueNumericId = uniqueNumericIdCreator(); +export const bookmarkUniqueNumericId = uniqueNumericIdCreator(); + export const uniqueId = (): string => nanoid().toLowerCase();