From 5c430b61b184271e6c6b8f3d972758b8a38a1805 Mon Sep 17 00:00:00 2001 From: Florent Date: Sun, 14 Feb 2021 17:37:03 +0100 Subject: [PATCH] Change signature of customFields and make ListenerRSSInfos properties ro --- src/Models/ListenerRSSInfos.ts | 8 ++--- src/listener-rss.ts | 9 +++-- tests/index-spec.ts | 61 ++++++++++++++++------------------ 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/Models/ListenerRSSInfos.ts b/src/Models/ListenerRSSInfos.ts index c193919..6e8a26f 100644 --- a/src/Models/ListenerRSSInfos.ts +++ b/src/Models/ListenerRSSInfos.ts @@ -1,6 +1,6 @@ export interface ListenerRSSInfos { - name: string; // name of the listener - address: string; // feed's address - timeloop?: number; // update time RSS feed - customfields?: [string, string | [string, string]][]; // rss fields custom + readonly name: string; // name of the listener + readonly address: string; // feed's address + readonly timeloop?: number; // update time RSS feed + readonly customfields?: { [key: string]: string | string[] }; // rss fields custom } diff --git a/src/listener-rss.ts b/src/listener-rss.ts index 69a0b01..1c14f0d 100644 --- a/src/listener-rss.ts +++ b/src/listener-rss.ts @@ -7,7 +7,7 @@ export class ListenerRss { name: string = ""; address: string = ""; timeloop: number = DEFAULT_TIMELOOP; // time in seconds - customfields?: [string, string | [string, string]][]; + customfields?: { [key: string]: string[] | string }; // private fields parser: Parser | undefined = undefined; @@ -24,8 +24,8 @@ export class ListenerRss { this.customfields !== undefined ? { customFields: { - item: this.customfields.map((elt) => { - return Array.isArray(elt[1]) ? elt[1][0] : elt[1]; + item: Object.entries(this.customfields).map(([, value]) => { + return Array.isArray(value[1]) ? value[1][0] : value[1]; }), }, } @@ -39,8 +39,7 @@ export class ListenerRss { this.address = infos.address; this.timeloop = infos.timeloop === undefined ? DEFAULT_TIMELOOP : infos.timeloop; - this.customfields = - infos.customfields === undefined ? undefined : infos.customfields; + this.customfields = infos.customfields; } fetchRSS(): any { diff --git a/tests/index-spec.ts b/tests/index-spec.ts index 93c76b1..cda001f 100644 --- a/tests/index-spec.ts +++ b/tests/index-spec.ts @@ -25,10 +25,10 @@ describe("test class RSS: jsonfile", function () { name: "my-test-service", address: "fake.rss.service", timeloop: 15, - customfields: [ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ], + customfields: { + description: ["media:group", "media:description"], + icon: ["media:group", "media:thumbnail"], + }, }; // parseURL tests @@ -101,10 +101,10 @@ describe("test class RSS: jsonfile", function () { expect(myListener.timeloop).to.eql(15); expect(myListener.name).to.eql("my-test-service"); expect(myListener.address).to.eql("fake.rss.service"); - expect(myListener.customfields).to.eql([ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ]); + expect(myListener.customfields).to.eql({ + description: ["media:group", "media:description"], + icon: ["media:group", "media:thumbnail"], + }); /* // TODO test des champs dans l'objet parser expect(myListener.parser.options.customFields).to.eql({ feed: [], @@ -112,20 +112,18 @@ describe("test class RSS: jsonfile", function () { });*/ }); // it("The build without issues (raw infos : 4 params)", function () { - // myListener = new Listeners("my-test-service", "fake.rss.service", 15, [ - // ["description", ["media:group", "media:description"]], - // ["icon", ["media:group", "media:thumbnail"]], - // ]); + // myListener = new Listeners("my-test-service", "fake.rss.service", 15, { // description: ["media:group", "media:description"], + // icon: ["media:group", "media:thumbnail"], + // }); // // // assertions // // myListener data // expect(myListener.timeloop).to.eql(15); // expect(myListener.name).to.eql("my-test-service"); // expect(myListener.address).to.eql("fake.rss.service"); - // expect(myListener.customfields).to.eql([ - // ["description", ["media:group", "media:description"]], - // ["icon", ["media:group", "media:thumbnail"]], - // ]); + // expect(myListener.customfields).to.eql({ // description: ["media:group", "media:description"], + // icon: ["media:group", "media:thumbnail"], + // }); // /* // expect(myListener.parser.options.customFields).to.eql({ // feed: [], @@ -167,10 +165,9 @@ describe("test class RSS: jsonfile", function () { // "my-test-service", // "fake.rss.service", // undefined, - // [ - // ["description", ["media:group", "media:description"]], - // ["icon", ["media:group", "media:thumbnail"]], - // ] + // { // description: ["media:group", "media:description"], + // icon: ["media:group", "media:thumbnail"], + // } // ); // // // assertions @@ -218,10 +215,10 @@ describe("test class RSS: jsonfile", function () { myListener = new Listeners({ name: "my-test-service", address: "bad.rss.service", - customfields: [ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ], + customfields: { + description: ["media:group", "media:description"], + icon: ["media:group", "media:thumbnail"], + }, }); myListener.parser = stubParser; // replace the parser by my fake parser // fetch @@ -249,10 +246,10 @@ describe("test class RSS: jsonfile", function () { name: "my-test-service", address: "fake.rss.service", timeloop: 2, - customfields: [ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ], + customfields: { + description: ["media:group", "media:description"], + icon: ["media:group", "media:thumbnail"], + }, }); //spy @@ -280,10 +277,10 @@ describe("test class RSS: jsonfile", function () { name: "my-test-service", address: "fake.rss.service", timeloop: 2, - customfields: [ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ], + customfields: { + description: ["media:group", "media:description"], + icon: ["media:group", "media:thumbnail"], + }, }); //spy