Merge pull request #14 from felipeochoa/formatting
Clean up xml formatting (clearVariables) and introduced toXml()
This commit is contained in:
@ -33,9 +33,4 @@ export class Document extends XmlComponent {
|
||||
public addParagraph(paragraph: Paragraph): void {
|
||||
this.body.push(paragraph);
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
this.body.clearVariables();
|
||||
delete this.body;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
import { XmlUnitComponent } from "../xml-components";
|
||||
import { XmlComponent } from "../xml-components";
|
||||
|
||||
export class Text extends XmlUnitComponent {
|
||||
export class Text extends XmlComponent {
|
||||
|
||||
constructor(text: string) {
|
||||
super("w:t");
|
||||
this.root = text;
|
||||
if (text) {
|
||||
this.root.push(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,5 @@ export abstract class BaseXmlComponent {
|
||||
this.rootKey = rootKey;
|
||||
}
|
||||
|
||||
public abstract replaceKey(): void;
|
||||
|
||||
public clearVariables(): void {
|
||||
// Do Nothing
|
||||
}
|
||||
public abstract toXml(): object;
|
||||
}
|
||||
|
@ -2,10 +2,10 @@ import * as _ from "lodash";
|
||||
import { BaseXmlComponent } from "./base";
|
||||
|
||||
export abstract class XmlAttributeComponent extends BaseXmlComponent {
|
||||
protected root: Object;
|
||||
private xmlKeys: Object;
|
||||
protected root: object;
|
||||
private xmlKeys: object;
|
||||
|
||||
constructor(xmlKeys: Object, properties: Object) {
|
||||
constructor(xmlKeys: object, properties: object) {
|
||||
super("_attr");
|
||||
this.xmlKeys = xmlKeys;
|
||||
|
||||
@ -16,15 +16,18 @@ export abstract class XmlAttributeComponent extends BaseXmlComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public replaceKey(): void {
|
||||
if (this.root !== undefined) {
|
||||
public toXml(): object {
|
||||
const attrs = {};
|
||||
if (this.root != undefined) {
|
||||
_.forOwn(this.root, (value, key) => {
|
||||
if (value != undefined) {
|
||||
const newKey = this.xmlKeys[key];
|
||||
this.root[newKey] = value;
|
||||
delete this.root[key];
|
||||
});
|
||||
this[this.rootKey] = this.root;
|
||||
delete this.root;
|
||||
attrs[newKey] = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
const ret = {};
|
||||
ret[this.rootKey] = attrs;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,27 @@
|
||||
import * as _ from "lodash";
|
||||
import { BaseXmlComponent } from "./base";
|
||||
export { BaseXmlComponent };
|
||||
|
||||
export abstract class XmlComponent extends BaseXmlComponent {
|
||||
protected root: BaseXmlComponent[];
|
||||
protected root: Array<BaseXmlComponent | string>;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
super(rootKey);
|
||||
this.root = new Array<BaseXmlComponent>();
|
||||
}
|
||||
|
||||
public replaceKey(): void {
|
||||
// console.log(this.rootKey);
|
||||
// console.log(this.root);
|
||||
if (this.root !== undefined) {
|
||||
this.root.forEach((root) => {
|
||||
if (root && root instanceof BaseXmlComponent) {
|
||||
root.replaceKey();
|
||||
public toXml(): object {
|
||||
const ret = this.root.map((comp) => {
|
||||
if (comp instanceof BaseXmlComponent) {
|
||||
return comp.toXml();
|
||||
}
|
||||
});
|
||||
this[this.rootKey] = this.root;
|
||||
delete this.root;
|
||||
return comp
|
||||
}).filter((comp) => comp); // Exclude null, undefined, and empty strings
|
||||
return {
|
||||
[this.rootKey]: ret,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export * from "./attributes"
|
||||
export * from "./default-attributes";
|
||||
export * from "./unit";
|
||||
export * from "./property";
|
||||
|
@ -1,57 +0,0 @@
|
||||
import { ParagraphProperties } from "../paragraph/properties";
|
||||
import { RunProperties } from "../run/properties";
|
||||
import { XmlComponent } from "./";
|
||||
|
||||
export class ParagraphPropertyXmlComponent extends XmlComponent {
|
||||
private paragraphProperties: ParagraphProperties;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
super(rootKey);
|
||||
this.paragraphProperties = new ParagraphProperties();
|
||||
this.root.push(this.paragraphProperties);
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
this.paragraphProperties.clearVariables();
|
||||
|
||||
delete this.paragraphProperties;
|
||||
}
|
||||
}
|
||||
|
||||
export class RunPropertyXmlComponent extends XmlComponent {
|
||||
private runProperties: RunProperties;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
super(rootKey);
|
||||
this.runProperties = new RunProperties();
|
||||
this.root.push(this.runProperties);
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
this.runProperties.clearVariables();
|
||||
|
||||
delete this.runProperties;
|
||||
}
|
||||
}
|
||||
|
||||
export class MultiPropertyXmlComponent extends XmlComponent {
|
||||
private runProperties: RunProperties;
|
||||
private paragraphProperties: ParagraphProperties;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
super(rootKey);
|
||||
this.runProperties = new RunProperties();
|
||||
this.root.push(this.runProperties);
|
||||
|
||||
this.paragraphProperties = new ParagraphProperties();
|
||||
this.root.push(this.paragraphProperties);
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
this.runProperties.clearVariables();
|
||||
this.paragraphProperties.clearVariables();
|
||||
|
||||
delete this.runProperties;
|
||||
delete this.paragraphProperties;
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
import {BaseXmlComponent} from "./base";
|
||||
|
||||
export abstract class XmlUnitComponent extends BaseXmlComponent {
|
||||
protected root: string;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
super(rootKey);
|
||||
}
|
||||
|
||||
public replaceKey(): void {
|
||||
if (this.root !== undefined) {
|
||||
this[this.rootKey] = this.root;
|
||||
delete this.root;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,52 +1,7 @@
|
||||
import * as _ from "lodash";
|
||||
import {XmlComponent} from "../docx/xml-components";
|
||||
import { BaseXmlComponent } from "../docx/xml-components";
|
||||
|
||||
export class Formatter {
|
||||
|
||||
public format(input: any): Object {
|
||||
input.clearVariables();
|
||||
this.replaceKeys(input);
|
||||
const newJson = this.clense(input);
|
||||
// console.log(JSON.stringify(newJson, null, " "));
|
||||
return newJson;
|
||||
public format(input: BaseXmlComponent): any {
|
||||
return input.toXml();
|
||||
}
|
||||
|
||||
private replaceKeys(input: XmlComponent): Object {
|
||||
input.replaceKey();
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
private clense(input: any): Object {
|
||||
const newJson = this.jsonify(input);
|
||||
|
||||
this.deepTraverseJson(newJson, (parent, value, key) => {
|
||||
if (key === "properties") {
|
||||
delete parent[key];
|
||||
}
|
||||
if (key === "xmlKeys") {
|
||||
delete parent[key];
|
||||
}
|
||||
if (key === "rootKey") {
|
||||
delete parent[key];
|
||||
}
|
||||
});
|
||||
|
||||
return newJson;
|
||||
}
|
||||
|
||||
private jsonify(obj: Object): Object {
|
||||
let stringifiedJson = JSON.stringify(obj);
|
||||
return JSON.parse(stringifiedJson);
|
||||
}
|
||||
|
||||
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void {
|
||||
_.forOwn(json, (value, key) => {
|
||||
if (_.isObject(value) && key !== "xmlKeys" && key !== "rootKey") {
|
||||
this.deepTraverseJson(value, lambda);
|
||||
}
|
||||
lambda(json, value, key);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -40,11 +40,4 @@ export class AbstractNumbering extends XmlComponent {
|
||||
this.addLevel(level);
|
||||
return level;
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
_.forEach(this.root, (element) => {
|
||||
element.clearVariables();
|
||||
});
|
||||
delete this.id;
|
||||
}
|
||||
}
|
||||
|
@ -2,12 +2,12 @@ import * as _ from "lodash";
|
||||
import { DocumentAttributes } from "../docx/document/document-attributes";
|
||||
import { Indent } from "../docx/paragraph/indent";
|
||||
import { RunFonts } from "../docx/run/run-fonts";
|
||||
import { MultiPropertyXmlComponent } from "../docx/xml-components";
|
||||
import { XmlComponent } from "../docx/xml-components";
|
||||
import { AbstractNumbering } from "./abstract-numbering";
|
||||
import { Level } from "./level";
|
||||
import { Num } from "./num";
|
||||
|
||||
export class Numbering extends MultiPropertyXmlComponent {
|
||||
export class Numbering extends XmlComponent {
|
||||
private nextId: number;
|
||||
|
||||
constructor() {
|
||||
@ -86,12 +86,4 @@ export class Numbering extends MultiPropertyXmlComponent {
|
||||
this.root.push(num);
|
||||
return num;
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
super.clearVariables();
|
||||
_.forEach(this.root, (element) => {
|
||||
element.clearVariables();
|
||||
});
|
||||
delete this.nextId;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { ParagraphProperties } from "../docx/paragraph/properties";
|
||||
import { RunProperties } from "../docx/run/properties";
|
||||
import { Attributes, MultiPropertyXmlComponent, XmlAttributeComponent, XmlComponent } from "../docx/xml-components";
|
||||
import { Attributes, XmlAttributeComponent, XmlComponent } from "../docx/xml-components";
|
||||
|
||||
interface ILevelAttributesProperties {
|
||||
ilvl?: number;
|
||||
@ -80,14 +80,6 @@ export class Level extends XmlComponent {
|
||||
this.root.push(this.runProperties);
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
this.paragraphProperties.clearVariables();
|
||||
this.runProperties.clearVariables();
|
||||
|
||||
delete this.paragraphProperties;
|
||||
delete this.runProperties;
|
||||
}
|
||||
|
||||
public addParagraphProperty(property: XmlComponent): Level {
|
||||
this.paragraphProperties.push(property);
|
||||
return this;
|
||||
|
@ -34,9 +34,4 @@ export class Num extends XmlComponent {
|
||||
this.root.push(new AbstractNumId(abstractNumId));
|
||||
this.id = numId;
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
super.clearVariables();
|
||||
delete this.id;
|
||||
}
|
||||
}
|
||||
|
@ -1,66 +1,64 @@
|
||||
import { DocumentAttributes } from "../docx/document/document-attributes";
|
||||
import { XmlUnitComponent } from "../docx/xml-components";
|
||||
import { XmlComponent } from "../docx/xml-components";
|
||||
|
||||
export class Title extends XmlUnitComponent {
|
||||
export class Title extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("dc:title");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Subject extends XmlUnitComponent {
|
||||
export class Subject extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("dc:subject");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Creator extends XmlUnitComponent {
|
||||
export class Creator extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("dc:creator");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Keywords extends XmlUnitComponent {
|
||||
export class Keywords extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("cp:keywords");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Description extends XmlUnitComponent {
|
||||
export class Description extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("dc:description");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class LastModifiedBy extends XmlUnitComponent {
|
||||
export class LastModifiedBy extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("cp:lastModifiedBy");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Revision extends XmlUnitComponent {
|
||||
export class Revision extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("cp:revision");
|
||||
const revision = value;
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class DateComponent extends XmlComponent {
|
||||
protected getCurrentDate(): any {
|
||||
protected getCurrentDate(): string {
|
||||
const date = new Date();
|
||||
const year = date.getFullYear();
|
||||
const month = ("0" + (date.getMonth() + 1)).slice(-2);
|
||||
|
@ -23,13 +23,27 @@ export class Properties extends XmlComponent {
|
||||
dcmitype: "http://purl.org/dc/dcmitype/",
|
||||
xsi: "http://www.w3.org/2001/XMLSchema-instance",
|
||||
}));
|
||||
if (options.title) {
|
||||
this.root.push(new Title(options.title));
|
||||
}
|
||||
if (options.subject) {
|
||||
this.root.push(new Subject(options.subject));
|
||||
}
|
||||
if (options.creator) {
|
||||
this.root.push(new Creator(options.creator));
|
||||
}
|
||||
if (options.keywords) {
|
||||
this.root.push(new Keywords(options.keywords));
|
||||
}
|
||||
if (options.description) {
|
||||
this.root.push(new Description(options.description));
|
||||
}
|
||||
if (options.lastModifiedBy) {
|
||||
this.root.push(new LastModifiedBy(options.lastModifiedBy));
|
||||
}
|
||||
if (options.revision) {
|
||||
this.root.push(new Revision(options.revision));
|
||||
}
|
||||
this.root.push(new Created());
|
||||
this.root.push(new Modified());
|
||||
}
|
||||
|
@ -14,11 +14,4 @@ export class DocumentDefaults extends XmlComponent {
|
||||
this.root.push(this.runPropertiesDefaults);
|
||||
this.root.push(this.paragraphPropertiesDefaults);
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
this.runPropertiesDefaults.clearVariables();
|
||||
this.paragraphPropertiesDefaults.clearVariables();
|
||||
delete this.runPropertiesDefaults;
|
||||
delete this.paragraphPropertiesDefaults;
|
||||
}
|
||||
}
|
||||
|
@ -30,12 +30,6 @@ export class Styles extends XmlComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
this.root.forEach((element) => {
|
||||
element.clearVariables();
|
||||
});
|
||||
}
|
||||
|
||||
public createParagraphStyle(styleId: string, name?: string): ParagraphStyle {
|
||||
const para = new ParagraphStyle(styleId, name);
|
||||
this.push(para);
|
||||
|
@ -55,13 +55,6 @@ export class ParagraphStyle extends Style {
|
||||
this.root.push(this.runProperties);
|
||||
}
|
||||
|
||||
public clearVariables(): void {
|
||||
this.paragraphProperties.clearVariables();
|
||||
this.runProperties.clearVariables();
|
||||
delete this.paragraphProperties;
|
||||
delete this.runProperties;
|
||||
}
|
||||
|
||||
public addParagraphProperty(property: XmlComponent): void {
|
||||
this.paragraphProperties.push(property);
|
||||
}
|
||||
|
@ -24,13 +24,4 @@ describe("XmlComponent", () => {
|
||||
assert.equal(newJson.rootKey, "w:test");
|
||||
});
|
||||
});
|
||||
|
||||
describe("#replaceKey", () => {
|
||||
|
||||
it("should replace the key to the specified root key", () => {
|
||||
xmlComponent.replaceKey();
|
||||
let newJson = jsonify(xmlComponent);
|
||||
assert.isDefined(newJson["w:test"]);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,36 +0,0 @@
|
||||
import { XmlUnitComponent } from "../../../docx/xml-components";
|
||||
import { assert } from "chai";
|
||||
|
||||
function jsonify(obj: Object) {
|
||||
let stringifiedJson = JSON.stringify(obj);
|
||||
return JSON.parse(stringifiedJson);
|
||||
}
|
||||
|
||||
class TestComponent extends XmlUnitComponent {
|
||||
|
||||
}
|
||||
|
||||
describe("XmlUnitComponent", () => {
|
||||
let xmlComponent: TestComponent;
|
||||
|
||||
beforeEach(() => {
|
||||
xmlComponent = new TestComponent("w:test");
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
|
||||
it("should create an Xml Component which has the correct rootKey", () => {
|
||||
let newJson = jsonify(xmlComponent);
|
||||
assert.equal(newJson.rootKey, "w:test");
|
||||
});
|
||||
});
|
||||
|
||||
describe("#replaceKey", () => {
|
||||
|
||||
it("should not replace the key to the specified root key as root is null", () => {
|
||||
xmlComponent.replaceKey();
|
||||
let newJson = jsonify(xmlComponent);
|
||||
assert.isUndefined(newJson["w:test"]);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,11 +1,12 @@
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import * as docx from "../../docx";
|
||||
import { Attributes } from "../../docx/xml-components";
|
||||
import { Properties } from "../../properties";
|
||||
import { assert } from "chai";
|
||||
|
||||
function jsonify(obj: Object) {
|
||||
let stringifiedJson = JSON.stringify(obj);
|
||||
import * as docx from "../../docx";
|
||||
import { Attributes } from "../../docx/xml-components";
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Properties } from "../../properties";
|
||||
|
||||
function jsonify(obj: object): any {
|
||||
const stringifiedJson = JSON.stringify(obj);
|
||||
return JSON.parse(stringifiedJson);
|
||||
}
|
||||
|
||||
@ -18,58 +19,55 @@ describe("Formatter", () => {
|
||||
|
||||
describe("#format()", () => {
|
||||
it("should format simple paragraph", () => {
|
||||
let paragraph = new docx.Paragraph();
|
||||
let newJson = formatter.format(paragraph);
|
||||
newJson = jsonify(newJson);
|
||||
const paragraph = new docx.Paragraph();
|
||||
const newJson = formatter.format(paragraph);
|
||||
assert.isDefined(newJson["w:p"]);
|
||||
});
|
||||
|
||||
it("should remove xmlKeys", () => {
|
||||
let paragraph = new docx.Paragraph();
|
||||
let newJson = formatter.format(paragraph);
|
||||
let stringifiedJson = JSON.stringify(newJson);
|
||||
const paragraph = new docx.Paragraph();
|
||||
const newJson = formatter.format(paragraph);
|
||||
const stringifiedJson = JSON.stringify(newJson);
|
||||
assert(stringifiedJson.indexOf("xmlKeys") < 0);
|
||||
});
|
||||
|
||||
it("should format simple paragraph with bold text", () => {
|
||||
let paragraph = new docx.Paragraph();
|
||||
const paragraph = new docx.Paragraph();
|
||||
paragraph.addText(new docx.TextRun("test").bold());
|
||||
let newJson = formatter.format(paragraph);
|
||||
newJson = jsonify(newJson);
|
||||
assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"][0]["_attr"]["w:val"]);
|
||||
const newJson = formatter.format(paragraph);
|
||||
assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"][0]._attr["w:val"]);
|
||||
});
|
||||
|
||||
it("should format attributes (rsidSect)", () => {
|
||||
let attributes = new Attributes({
|
||||
rsidSect: "test2"
|
||||
const attributes = new Attributes({
|
||||
rsidSect: "test2",
|
||||
});
|
||||
let newJson = formatter.format(attributes);
|
||||
newJson = jsonify(newJson);
|
||||
assert.isDefined(newJson["_attr"]["w:rsidSect"]);
|
||||
assert.isDefined(newJson._attr["w:rsidSect"]);
|
||||
});
|
||||
|
||||
it("should format attributes (val)", () => {
|
||||
let attributes = new Attributes({
|
||||
val: "test"
|
||||
const attributes = new Attributes({
|
||||
val: "test",
|
||||
});
|
||||
let newJson = formatter.format(attributes);
|
||||
newJson = jsonify(newJson);
|
||||
assert.isDefined(newJson["_attr"]["w:val"]);
|
||||
assert.isDefined(newJson._attr["w:val"]);
|
||||
});
|
||||
|
||||
it("should should change 'p' tag into 'w:p' tag", () => {
|
||||
let paragraph = new docx.Paragraph();
|
||||
let newJson = formatter.format(paragraph);
|
||||
const paragraph = new docx.Paragraph();
|
||||
const newJson = formatter.format(paragraph);
|
||||
assert.isDefined(newJson["w:p"]);
|
||||
});
|
||||
|
||||
it("should format Properties object correctly", () => {
|
||||
let properties = new Properties({
|
||||
const properties = new Properties({
|
||||
title: "test document",
|
||||
creator: "Dolan"
|
||||
creator: "Dolan",
|
||||
});
|
||||
let newJson = formatter.format(properties);
|
||||
newJson = jsonify(newJson);
|
||||
const newJson = formatter.format(properties);
|
||||
assert.isDefined(newJson["cp:coreProperties"]);
|
||||
});
|
||||
});
|
||||
|
@ -65,7 +65,10 @@ describe("Numbering", () => {
|
||||
const n = numbering.createConcreteNumbering(a2);
|
||||
expect(n).to.be.instanceof(Num);
|
||||
const tree = new Formatter().format(numbering);
|
||||
expect(n.id).to.equal(a2.id);
|
||||
const serializedN = tree["w:numbering"].find(
|
||||
(obj) => obj["w:num"] && obj["w:num"][0]._attr["w:numId"] === n.id
|
||||
);
|
||||
expect(serializedN["w:num"][1]["w:abstractNumId"][0]._attr["w:val"]).to.equal(a2.id);
|
||||
});
|
||||
|
||||
it("assigns a unique ID to each concrete numbering it creates", () => {
|
||||
|
@ -1,25 +1,74 @@
|
||||
import { Properties } from "../properties";
|
||||
import { assert } from "chai";
|
||||
import { expect } from "chai";
|
||||
|
||||
function jsonify(obj: Object) {
|
||||
let stringifiedJson = JSON.stringify(obj);
|
||||
return JSON.parse(stringifiedJson);
|
||||
}
|
||||
import { Formatter } from "../export/formatter";
|
||||
import { Properties } from "../properties";
|
||||
|
||||
describe("Properties", () => {
|
||||
let properties: Properties;
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create properties with a title", () => {
|
||||
properties = new Properties({
|
||||
title: "test document"
|
||||
it("sets the appropriate attributes on the top-level", () => {
|
||||
const properties = new Properties({});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
|
||||
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
|
||||
expect(tree["cp:coreProperties"][0]).to.deep.equal({
|
||||
_attr: {
|
||||
"xmlns:cp": "http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
|
||||
"xmlns:dc": "http://purl.org/dc/elements/1.1/",
|
||||
"xmlns:dcmitype": "http://purl.org/dc/dcmitype/",
|
||||
"xmlns:dcterms": "http://purl.org/dc/terms/",
|
||||
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
|
||||
},
|
||||
});
|
||||
let newJson = jsonify(properties);
|
||||
assert(newJson.root[1].root === "test document");
|
||||
});
|
||||
|
||||
it("should create properties with a title", () => {
|
||||
const properties = new Properties({title: "test document"});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
|
||||
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
|
||||
expect(Object.keys(tree["cp:coreProperties"][0])).to.deep.equal(["_attr"]);
|
||||
expect(tree["cp:coreProperties"][1]).to.deep.equal(
|
||||
{"dc:title": ["test document"]},
|
||||
);
|
||||
});
|
||||
|
||||
it("should create properties with all the attributes given", () => {
|
||||
const properties = new Properties({
|
||||
title: "test document",
|
||||
subject: "test subject",
|
||||
creator: "me",
|
||||
keywords: "test docx",
|
||||
description: "testing document",
|
||||
lastModifiedBy: "the author",
|
||||
revision: "123",
|
||||
});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
|
||||
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
|
||||
const key = (obj) => Object.keys(obj)[0];
|
||||
const props = tree["cp:coreProperties"].map(key).sort();
|
||||
expect(props).to.deep.equal([
|
||||
"_attr",
|
||||
"cp:keywords",
|
||||
"cp:lastModifiedBy",
|
||||
"cp:revision",
|
||||
"dc:creator",
|
||||
"dc:description",
|
||||
"dc:subject",
|
||||
"dc:title",
|
||||
"dcterms:created",
|
||||
"dcterms:modified",
|
||||
]);
|
||||
expect(tree["cp:coreProperties"].slice(1, -2).sort((a, b) => key(a) < key(b) ? -1 : 1)).to.deep.equal([
|
||||
{"cp:keywords": ["test docx"]},
|
||||
{"cp:lastModifiedBy": ["the author"]},
|
||||
{"cp:revision": ["123"]},
|
||||
{"dc:creator": ["me"]},
|
||||
{"dc:description": ["testing document"]},
|
||||
{"dc:subject": ["test subject"]},
|
||||
{"dc:title": ["test document"]},
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user