Change docx folder to more appropriate "file" folder

This commit is contained in:
Dolan
2017-12-20 01:03:20 +00:00
parent 43ebfe7a2f
commit 2358139a6b
117 changed files with 22 additions and 23 deletions

View 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");
});
});
});

View 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",
};
}

View 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;
}

View 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};
}
}

View File

@ -0,0 +1,3 @@
export * from "./xml-component";
export * from "./attributes";
export * from "./default-attributes";

View 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");
});
});
});

View 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,
};
}
}

View File

@ -0,0 +1,3 @@
export interface IXmlableObject extends Object {
_attr?: { [key: string]: (string | number | boolean) };
}