Skip to content

Commit 22835d6

Browse files
FillimericaThomas Filliman
andauthored
Enhanced buildProgramsXml to check neighboring entries for exact object level duplicate. If a duplicate then the trailing object is not added into the xml stream. (#17)
Needed to add an option to the buildoptions to support the import of the assert module from NodeJS. Co-authored-by: Thomas Filliman <fillimerica@yahoo.com>
1 parent 7b301ae commit 22835d6

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/xmltv.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { GridApiResponse } from "./tvlistings.js";
22
import { Command } from "commander";
33

4+
// TF 10/2025 Add node.js module needed for helper function.
5+
import assert from "assert";
6+
47
export function escapeXml(unsafe: string): string {
58
return unsafe
69
.replace(/&/g, "&amp;")
@@ -91,6 +94,18 @@ function toDdProgid(rawId: string | undefined | null): string | null {
9194
return m ? `${m[1]}.${m[2]}` : null;
9295
}
9396

97+
// TF 10/2025 Impement an internal deepStrictEqual function to compare to objects.
98+
// Helper to compare two objects.
99+
100+
function isIdentical(obj1: any, obj2: any): boolean {
101+
try {
102+
assert.deepStrictEqual(obj1,obj2);
103+
} catch(error) {
104+
return false
105+
}
106+
return true
107+
}
108+
94109
export function buildChannelsXml(data: GridApiResponse): string {
95110
let xml = "";
96111

@@ -155,8 +170,19 @@ export function buildProgramsXml(data: GridApiResponse): string {
155170
const sortedEvents = [...channel.events].sort(
156171
(a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime()
157172
);
173+
// TF 10/2025 Impement duplicate checking and skip duplicate entries.
174+
175+
//for (const event of sortedEvents) {
176+
for (let i=0; i<sortedEvents.length; i++) {
177+
let event = sortedEvents[i]!;
178+
179+
if (i>0) {
180+
let pevent = sortedEvents[(i-1)]!;
181+
if (isIdentical(event, pevent)) {
182+
continue
183+
}
184+
}
158185

159-
for (const event of sortedEvents) {
160186
xml += ` <programme start="${formatDate(
161187
event.startTime
162188
)}" stop="${formatDate(event.endTime)}" channel="${escapeXml(channel.channelId)}">\n`;

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"useDefineForClassFields": true,
3030
"forceConsistentCasingInFileNames": true,
3131
"skipLibCheck": true,
32+
"esModuleInterop": true,
3233
},
3334
"include": [
3435
"src/**/*.ts"

0 commit comments

Comments
 (0)