diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index 05d8f1edb7..fbcdcf9f88 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -5,9 +5,9 @@ import { FooterWrapper } from "@file/footer-wrapper"; import { HeaderWrapper } from "@file/header-wrapper"; import { VerticalAlign, VerticalAlignElement } from "@file/vertical-align"; import { OnOffElement, XmlComponent } from "@file/xml-components"; +import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values"; import { HeaderFooterReference, HeaderFooterReferenceType, HeaderFooterType } from "./properties/header-footer-reference"; - import { Columns, IColumnsAttributes } from "./properties/columns"; import { DocumentGrid, IDocGridAttributesProperties } from "./properties/doc-grid"; import { ILineNumberAttributes, LineNumberType } from "./properties/line-number"; @@ -17,7 +17,6 @@ import { IPageNumberTypeAttributes, PageNumberType } from "./properties/page-num import { IPageSizeAttributes, PageOrientation, PageSize } from "./properties/page-size"; import { PageTextDirection, PageTextDirectionType } from "./properties/page-text-direction"; import { SectionType, Type } from "./properties/section-type"; -import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values"; export interface IHeaderFooterGroup { readonly default?: T; diff --git a/src/file/table/table-properties/table-float-properties.ts b/src/file/table/table-properties/table-float-properties.ts index fdcf696d07..e7f32dc572 100644 --- a/src/file/table/table-properties/table-float-properties.ts +++ b/src/file/table/table-properties/table-float-properties.ts @@ -1,5 +1,5 @@ import { StringEnumValueElement, XmlAttributeComponent, XmlComponent } from "@file/xml-components"; -import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values"; +import { PositiveUniversalMeasure, signedTwipsMeasureValue, twipsMeasureValue, UniversalMeasure } from "@util/values"; export enum TableAnchorType { MARGIN = "margin", @@ -55,7 +55,7 @@ export interface ITableFloatOptions { * If relativeHorizontalPosition is also specified, then the absoluteHorizontalPosition attribute is ignored. * If the attribute is omitted, the value is assumed to be zero. */ - readonly absoluteHorizontalPosition?: number | string; + readonly absoluteHorizontalPosition?: number | UniversalMeasure; /** * Specifies a relative horizontal position for the table, relative to the horizontalAnchor attribute. @@ -86,7 +86,7 @@ export interface ITableFloatOptions { * If relativeVerticalPosition is also specified, then the absoluteVerticalPosition attribute is ignored. * If the attribute is omitted, the value is assumed to be zero. */ - readonly absoluteVerticalPosition?: number | string; + readonly absoluteVerticalPosition?: number | UniversalMeasure; /** * Specifies a relative vertical position for the table, relative to the verticalAnchor attribute. @@ -104,25 +104,25 @@ export interface ITableFloatOptions { * Specifies the minimum distance to be maintained between the table and the top of text in the paragraph * below the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero. */ - readonly bottomFromText?: number | string; + readonly bottomFromText?: number | PositiveUniversalMeasure; /** * Specifies the minimum distance to be maintained between the table and the bottom edge of text in the paragraph * above the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero. */ - readonly topFromText?: number | string; + readonly topFromText?: number | PositiveUniversalMeasure; /** * Specifies the minimum distance to be maintained between the table and the edge of text in the paragraph * to the left of the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero. */ - readonly leftFromText?: number | string; + readonly leftFromText?: number | PositiveUniversalMeasure; /** * Specifies the minimum distance to be maintained between the table and the edge of text in the paragraph * to the right of the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero. */ - readonly rightFromText?: number | string; + readonly rightFromText?: number | PositiveUniversalMeasure; readonly overlap?: OverlapType; } diff --git a/src/file/table/table-row/table-row-height.ts b/src/file/table/table-row/table-row-height.ts index 12294b7f4e..4c6043bb47 100644 --- a/src/file/table/table-row/table-row-height.ts +++ b/src/file/table/table-row/table-row-height.ts @@ -1,5 +1,5 @@ import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; -import { twipsMeasureValue } from "@util/values"; +import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values"; // // @@ -30,7 +30,7 @@ export class TableRowHeightAttributes extends XmlAttributeComponent<{ } export class TableRowHeight extends XmlComponent { - public constructor(value: number | string, rule: HeightRule) { + public constructor(value: number | PositiveUniversalMeasure, rule: HeightRule) { super("w:trHeight"); this.root.push( diff --git a/src/file/table/table-row/table-row-properties.ts b/src/file/table/table-row/table-row-properties.ts index c4fa01ea6d..d388e917eb 100644 --- a/src/file/table/table-row/table-row-properties.ts +++ b/src/file/table/table-row/table-row-properties.ts @@ -28,6 +28,7 @@ // // import { IgnoreIfEmptyXmlComponent, OnOffElement } from "@file/xml-components"; +import { PositiveUniversalMeasure } from "@util/values"; import { HeightRule, TableRowHeight } from "./table-row-height"; @@ -35,7 +36,7 @@ export interface ITableRowPropertiesOptions { readonly cantSplit?: boolean; readonly tableHeader?: boolean; readonly height?: { - readonly value: number | string; + readonly value: number | PositiveUniversalMeasure; readonly rule: HeightRule; }; } diff --git a/src/util/values.spec.ts b/src/util/values.spec.ts index 7331a2e1d7..32df1a74b2 100644 --- a/src/util/values.spec.ts +++ b/src/util/values.spec.ts @@ -25,13 +25,6 @@ describe("values", () => { expect(universalMeasureValue("5.22pc")).to.eq("5.22pc"); expect(universalMeasureValue("100 pi")).to.eq("100pi"); }); - it("should throw on invalid values", () => { - expect(() => universalMeasureValue("100pp")).to.throw(); - expect(() => universalMeasureValue("foo")).to.throw(); - expect(() => universalMeasureValue("--in")).to.throw(); - expect(() => universalMeasureValue("NaNpc")).to.throw(); - expect(() => universalMeasureValue("50")).to.throw(); - }); }); describe("positiveUniversalMeasureValue", () => { @@ -46,11 +39,6 @@ describe("values", () => { it("should throw on invalid values", () => { expect(() => positiveUniversalMeasureValue("-9mm")).to.throw(); expect(() => positiveUniversalMeasureValue("-0.5in")).to.throw(); - expect(() => positiveUniversalMeasureValue("100pp")).to.throw(); - expect(() => positiveUniversalMeasureValue("foo")).to.throw(); - expect(() => positiveUniversalMeasureValue("--in")).to.throw(); - expect(() => positiveUniversalMeasureValue("NaNpc")).to.throw(); - expect(() => positiveUniversalMeasureValue("50")).to.throw(); }); }); @@ -128,7 +116,6 @@ describe("values", () => { it("should throw on invalid values", () => { expect(() => twipsMeasureValue(-12)).to.throw(); expect(() => twipsMeasureValue(NaN)).to.throw(); - expect(() => twipsMeasureValue("foo")).to.throw(); expect(() => twipsMeasureValue("-5mm")).to.throw(); }); }); @@ -153,7 +140,6 @@ describe("values", () => { }); it("should throw on invalid values", () => { expect(() => hpsMeasureValue(NaN)).to.throw(); - expect(() => hpsMeasureValue("5FF")).to.throw(); }); }); @@ -164,11 +150,6 @@ describe("values", () => { expect(percentageValue("100%")).to.eq("100%"); expect(percentageValue("1000%")).to.eq("1000%"); }); - it("should throw on invalid values", () => { - expect(() => percentageValue("0%%")).to.throw(); - expect(() => percentageValue("20")).to.throw(); - expect(() => percentageValue("FF%")).to.throw(); - }); }); describe("measurementOrPercentValue", () => { @@ -180,8 +161,6 @@ describe("values", () => { }); it("should throw on invalid values", () => { expect(() => measurementOrPercentValue(NaN)).to.throw(); - expect(() => measurementOrPercentValue("10%%")).to.throw(); - expect(() => measurementOrPercentValue("10F")).to.throw(); }); }); diff --git a/src/util/values.ts b/src/util/values.ts index 2f531ccba8..ab0cd20b58 100644 --- a/src/util/values.ts +++ b/src/util/values.ts @@ -87,7 +87,7 @@ export const uCharHexNumber = (val: string): string => hexBinary(val, 1); // // // -export const universalMeasureValue = (val: UniversalMeasure): string => { +export const universalMeasureValue = (val: UniversalMeasure): UniversalMeasure => { const unit = val.slice(-2); if (!universalMeasureUnits.includes(unit)) { throw new Error(`Invalid unit '${unit}' specified. Valid units are ${universalMeasureUnits.join(", ")}`); @@ -96,7 +96,7 @@ export const universalMeasureValue = (val: UniversalMeasure): string => { if (isNaN(Number(amount))) { throw new Error(`Invalid value '${amount}' specified. Expected a valid number.`); } - return `${Number(amount)}${unit}`; + return `${Number(amount)}${unit}` as UniversalMeasure; }; const universalMeasureUnits = ["mm", "cm", "in", "pt", "pc", "pi"]; @@ -105,12 +105,12 @@ const universalMeasureUnits = ["mm", "cm", "in", "pt", "pc", "pi"]; // // // -export const positiveUniversalMeasureValue = (val: PositiveUniversalMeasure): string => { +export const positiveUniversalMeasureValue = (val: PositiveUniversalMeasure): PositiveUniversalMeasure => { const value = universalMeasureValue(val); if (parseFloat(value) < 0) { throw new Error(`Invalid value '${value}' specified. Expected a positive number.`); } - return value; + return value as PositiveUniversalMeasure; }; // @@ -141,7 +141,7 @@ export const hexColorValue = (val: string): string => { // // export const signedTwipsMeasureValue = (val: UniversalMeasure | number): UniversalMeasure | number => - typeof val === "string" ? val : decimalNumber(val); + typeof val === "string" ? universalMeasureValue(val) : decimalNumber(val); // // @@ -159,7 +159,7 @@ export const signedHpsMeasureValue = (val: UniversalMeasure | number): string | // // export const twipsMeasureValue = (val: PositiveUniversalMeasure | number): PositiveUniversalMeasure | number => - typeof val === "string" ? val : unsignedDecimalNumber(val); + typeof val === "string" ? positiveUniversalMeasureValue(val) : unsignedDecimalNumber(val); // // @@ -196,7 +196,7 @@ export const measurementOrPercentValue = (val: number | Percentage | UniversalMe if (val.slice(-1) === "%") { return percentageValue(val as Percentage); } - return val as UniversalMeasure; + return universalMeasureValue(val as UniversalMeasure); }; //