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

fix: added unique numeric id creator to avoid numbering render errors #2087

Merged
merged 2 commits into from
Apr 19, 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
4 changes: 2 additions & 2 deletions src/file/drawing/anchor/anchor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/file/drawing/doc-properties/doc-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { IContext, IXmlableObject, NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { ConcreteHyperlink } from "@file/paragraph";

import { uniqueNumericId } from "@util/convenience-functions";
import { docPropertiesUniqueNumericId } from "@util/convenience-functions";

import { createHyperlinkClick } from "./doc-properties-children";

Expand Down Expand Up @@ -32,7 +32,7 @@ export class DocProperties extends XmlComponent {
new NextAttributeComponent({
id: {
key: "id",
value: uniqueNumericId(),
value: docPropertiesUniqueNumericId(),
},
name: {
key: "name",
Expand Down
4 changes: 2 additions & 2 deletions src/file/drawing/drawing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/file/numbering/numbering.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
8 changes: 4 additions & 4 deletions src/file/numbering/numbering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, uniqueNumericId } from "@util/convenience-functions";
import { abstractNumUniqueNumericId, concreteNumUniqueNumericId, convertInchesToTwip } from "@util/convenience-functions";

import { DocumentAttributes } from "../document/document-attributes";
import { AbstractNumbering } from "./abstract-numbering";
Expand Down Expand Up @@ -55,7 +55,7 @@ export class Numbering extends XmlComponent {
}),
);

const abstractNumbering = new AbstractNumbering(uniqueNumericId(), [
const abstractNumbering = new AbstractNumbering(abstractNumUniqueNumericId(), [
{
level: 0,
format: LevelFormat.BULLET,
Expand Down Expand Up @@ -176,7 +176,7 @@ export class Numbering extends XmlComponent {
this.abstractNumberingMap.set("default-bullet-numbering", abstractNumbering);

for (const con of options.config) {
this.abstractNumberingMap.set(con.reference, new AbstractNumbering(uniqueNumericId(), con.levels));
this.abstractNumberingMap.set(con.reference, new AbstractNumbering(abstractNumUniqueNumericId(), con.levels));
this.referenceConfigMap.set(con.reference, con.levels);
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ export class Numbering extends XmlComponent {
const firstLevelStartNumber = referenceConfigLevels && referenceConfigLevels[0].start;

const concreteNumberingSettings = {
numId: uniqueNumericId(),
numId: concreteNumUniqueNumericId(),
abstractNumId: abstractNumbering.id,
reference,
instance,
Expand Down
4 changes: 2 additions & 2 deletions src/file/paragraph/links/bookmark.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// http://officeopenxml.com/WPbookmark.php
import { XmlComponent } from "@file/xml-components";
import { uniqueNumericId } from "@util/convenience-functions";
import { bookmarkUniqueNumericId } from "@util/convenience-functions";

import { ParagraphChild } from "../paragraph";
import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes";
Expand All @@ -11,7 +11,7 @@ export class Bookmark {
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;
Expand Down
4 changes: 2 additions & 2 deletions src/file/paragraph/paragraph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/file/paragraph/run/image-run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()", () => {
Expand Down
5 changes: 3 additions & 2 deletions src/util/convenience-functions.spec.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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;
});
});
Expand Down
13 changes: 10 additions & 3 deletions src/util/convenience-functions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { nanoid } from "nanoid/non-secure";

let currentCount = 0;

// Twip - twentieths of a point
export const convertMillimetersToTwip = (millimeters: number): number => Math.floor((millimeters / 25.4) * 72 * 20);

export const convertInchesToTwip = (inches: number): number => Math.floor(inches * 72 * 20);

export const uniqueNumericId = (): number => ++currentCount;
export const uniqueNumericIdCreator = (initial = 0): (() => number) => {
let currentCount = initial;

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();