Change docx folder to more appropriate "file" folder
This commit is contained in:
26
src/file/xml-components/attribute.spec.ts
Normal file
26
src/file/xml-components/attribute.spec.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Attributes } from "./";
|
||||
|
||||
describe("Attribute", () => {
|
||||
describe("#constructor()", () => {
|
||||
|
||||
it("should have val as defined with populated constructor", () => {
|
||||
const newAttrs = new Attributes({
|
||||
val: "test",
|
||||
});
|
||||
const stringifiedJson = JSON.stringify(newAttrs);
|
||||
const newJson = JSON.parse(stringifiedJson);
|
||||
assert.equal(newJson.root.val, "test");
|
||||
});
|
||||
|
||||
it("should have space value as defined with populated constructor", () => {
|
||||
const newAttrs = new Attributes({
|
||||
space: "spaceTest",
|
||||
});
|
||||
const stringifiedJson = JSON.stringify(newAttrs);
|
||||
const newJson = JSON.parse(stringifiedJson);
|
||||
assert.equal(newJson.root.space, "spaceTest");
|
||||
});
|
||||
});
|
||||
});
|
47
src/file/xml-components/attributes.ts
Normal file
47
src/file/xml-components/attributes.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { XmlAttributeComponent } from "./default-attributes";
|
||||
|
||||
export interface IAttributesProperties {
|
||||
val?: string | number | boolean;
|
||||
color?: string;
|
||||
space?: string;
|
||||
sz?: string;
|
||||
type?: string;
|
||||
rsidR?: string;
|
||||
rsidRPr?: string;
|
||||
rsidSect?: string;
|
||||
w?: string;
|
||||
h?: string;
|
||||
top?: string;
|
||||
right?: string;
|
||||
bottom?: string;
|
||||
left?: string;
|
||||
header?: string;
|
||||
footer?: string;
|
||||
gutter?: string;
|
||||
linePitch?: string;
|
||||
pos?: string | number; // Little strange. Perhaps it is normal. Need to clarify in the spec.
|
||||
}
|
||||
|
||||
export class Attributes extends XmlAttributeComponent<IAttributesProperties> {
|
||||
protected xmlKeys = {
|
||||
val: "w:val",
|
||||
color: "w:color",
|
||||
space: "w:space",
|
||||
sz: "w:sz",
|
||||
type: "w:type",
|
||||
rsidR: "w:rsidR",
|
||||
rsidRPr: "w:rsidRPr",
|
||||
rsidSect: "w:rsidSect",
|
||||
w: "w:w",
|
||||
h: "w:h",
|
||||
top: "w:top",
|
||||
right: "w:right",
|
||||
bottom: "w:bottom",
|
||||
left: "w:left",
|
||||
header: "w:header",
|
||||
footer: "w:footer",
|
||||
gutter: "w:gutter",
|
||||
linePitch: "w:linePitch",
|
||||
pos: "w:pos",
|
||||
};
|
||||
}
|
11
src/file/xml-components/base.ts
Normal file
11
src/file/xml-components/base.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { IXmlableObject } from "./xmlable-object";
|
||||
|
||||
export abstract class BaseXmlComponent {
|
||||
protected rootKey: string;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
this.rootKey = rootKey;
|
||||
}
|
||||
|
||||
public abstract prepForXml(): IXmlableObject;
|
||||
}
|
26
src/file/xml-components/default-attributes.ts
Normal file
26
src/file/xml-components/default-attributes.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { BaseXmlComponent } from "./base";
|
||||
import { IXmlableObject } from "./xmlable-object";
|
||||
|
||||
export type AttributeMap<T> = {[P in keyof T]: string};
|
||||
|
||||
export abstract class XmlAttributeComponent<T> extends BaseXmlComponent {
|
||||
protected root: T;
|
||||
protected xmlKeys: AttributeMap<T>;
|
||||
|
||||
constructor(properties: T) {
|
||||
super("_attr");
|
||||
this.root = properties;
|
||||
}
|
||||
|
||||
public prepForXml(): IXmlableObject {
|
||||
const attrs = {};
|
||||
Object.keys(this.root).forEach((key) => {
|
||||
const value = this.root[key];
|
||||
if (value !== undefined) {
|
||||
const newKey = this.xmlKeys[key];
|
||||
attrs[newKey] = value;
|
||||
}
|
||||
});
|
||||
return {_attr: attrs};
|
||||
}
|
||||
}
|
3
src/file/xml-components/index.ts
Normal file
3
src/file/xml-components/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from "./xml-component";
|
||||
export * from "./attributes";
|
||||
export * from "./default-attributes";
|
24
src/file/xml-components/xml-component.spec.ts
Normal file
24
src/file/xml-components/xml-component.spec.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../tests/utility";
|
||||
import { XmlComponent } from "./";
|
||||
|
||||
class TestComponent extends XmlComponent {
|
||||
|
||||
}
|
||||
|
||||
describe("XmlComponent", () => {
|
||||
let xmlComponent: TestComponent;
|
||||
|
||||
beforeEach(() => {
|
||||
xmlComponent = new TestComponent("w:test");
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
|
||||
it("should create an Xml Component which has the correct rootKey", () => {
|
||||
const newJson = Utility.jsonify(xmlComponent);
|
||||
assert.equal(newJson.rootKey, "w:test");
|
||||
});
|
||||
});
|
||||
});
|
24
src/file/xml-components/xml-component.ts
Normal file
24
src/file/xml-components/xml-component.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { BaseXmlComponent } from "./base";
|
||||
import { IXmlableObject } from "./xmlable-object";
|
||||
export { BaseXmlComponent };
|
||||
|
||||
export abstract class XmlComponent extends BaseXmlComponent {
|
||||
protected root: Array<BaseXmlComponent | string>;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
super(rootKey);
|
||||
this.root = new Array<BaseXmlComponent>();
|
||||
}
|
||||
|
||||
public prepForXml(): IXmlableObject {
|
||||
const children = this.root.map((comp) => {
|
||||
if (comp instanceof BaseXmlComponent) {
|
||||
return comp.prepForXml();
|
||||
}
|
||||
return comp;
|
||||
}).filter((comp) => comp); // Exclude null, undefined, and empty strings
|
||||
return {
|
||||
[this.rootKey]: children,
|
||||
};
|
||||
}
|
||||
}
|
3
src/file/xml-components/xmlable-object.ts
Normal file
3
src/file/xml-components/xmlable-object.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export interface IXmlableObject extends Object {
|
||||
_attr?: { [key: string]: (string | number | boolean) };
|
||||
}
|
Reference in New Issue
Block a user