Skip to content

Commit b4bf0e0

Browse files
authored
Merge pull request #55 from Tch1b0/enhance-customization
Enhance customization
2 parents 6b90eab + 16240f6 commit b4bf0e0

File tree

9 files changed

+77
-19
lines changed

9 files changed

+77
-19
lines changed

.npmignore

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
test/
1+
.github/
22
src/
3+
test/
4+
docs/
35
node_modules/
6+
practical-test/
7+
8+
.prettierrc
49
package-lock.json
510
tsconfig.json
6-
.prettierrc
7-
.github/
8-
practical-test/

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dsbmobile",
3-
"version": "1.1.9",
3+
"version": "1.2.0",
44
"description": "A Javascript wrapper for the dsbmobile api",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -10,6 +10,7 @@
1010
"btest": "npm run build && npm test",
1111
"test": "ts-mocha --config tsconfig.json --paths test/**/*.spec.ts",
1212
"lint": "eslint . --ext .ts",
13+
"format": "eslint . --ext .ts --fix",
1314
"docs": "typedoc"
1415
},
1516
"repository": {

src/documents/documentpost.ts

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ export class DocumentPost {
2424
this.date = isText(date) ? new Date(date) : date;
2525
}
2626

27+
public equals(other: DocumentPost): boolean {
28+
return (
29+
this.id === other.id &&
30+
this.title === other.title &&
31+
this.date.getTime() === other.date.getTime() &&
32+
this.url === other.url &&
33+
this.previewURL === other.previewURL
34+
);
35+
}
36+
2737
/**
2838
* Create a new `DocumentPost` from the json structure of the dsb backend
2939
* @param data The `JSON` data

src/dsbmobile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export default class Dsbmobile {
187187
public async fetchToken() {
188188
const res = await this.requester.get(
189189
"/authid?bundleid=de.heinekingmedia.dsbmobile&appversion=35&osversion=22&pushid" +
190-
`&user=${this.id}&password=${this.password}`,
190+
`&user=${this.id}&password=${this.password}`,
191191
);
192192

193193
this.token = res.data;

src/news/newspost.ts

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ export class NewsPost {
1111
this.date = isText(date) ? new Date(date) : date;
1212
}
1313

14+
public equals(other: NewsPost): boolean {
15+
return (
16+
this.title === other.title &&
17+
this.date.getTime() === other.date.getTime() &&
18+
this.detail === other.detail
19+
);
20+
}
21+
1422
/**
1523
* Create a `NewsPost` object from JSON
1624
* @param json The json object you want to generate a `NewsPost` from

src/timetable/entry.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { SubjectContainer } from "./subjectcontainer";
44

55
export class Entry implements SubjectContainer {
66
public readonly date: Date;
7-
public readonly subjectShorts = new Map<string, string>(defaultSubjectShorts);
7+
public readonly subjectShorts = new Map<string, string>(
8+
defaultSubjectShorts,
9+
);
810

911
constructor(
1012
date: Date | string,
@@ -24,7 +26,9 @@ export class Entry implements SubjectContainer {
2426
/**
2527
* The entry only holds a `period` attribute, which does
2628
* not represent actual time.
27-
* This function gives you the real time of the lesson
29+
* This function gives you the real time of the lesson.
30+
*
31+
* This function assumes that the first period takes place at 07:30 and one period takes 45 minutes.
2832
*
2933
* @returns Real time of the lesson
3034
*/
@@ -139,14 +143,16 @@ export class Entry implements SubjectContainer {
139143
* @returns whether the entries are equal
140144
*/
141145
public equals(other: Entry): boolean {
142-
return this.className === other.className &&
143-
this.date.toString() === other.date.toString() &&
146+
return (
147+
this.className === other.className &&
148+
this.date.getTime() === other.date.getTime() &&
144149
this.day === other.day &&
145150
this.description === other.description &&
146151
this.oldRoom === other.oldRoom &&
147152
this.newRoom === other.newRoom &&
148153
this.oldSubject === other.oldSubject &&
149-
this.newSubject === other.newSubject;
154+
this.newSubject === other.newSubject
155+
);
150156
}
151157

152158
/**

src/timetable/timetable.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Entry } from "./entry";
2-
import { load } from "cheerio";
2+
import { CheerioAPI, load } from "cheerio";
33
import { SubjectContainer } from "./subjectcontainer";
44
import { defaultSubjectShorts } from "../utility";
55

@@ -9,6 +9,31 @@ import { defaultSubjectShorts } from "../utility";
99
export class TimeTable implements SubjectContainer {
1010
public subjectShorts = new Map<string, string>(defaultSubjectShorts);
1111

12+
/**
13+
* Parse the `TimeTable` from HTML in a custom way
14+
*
15+
* @param html The html object from where the data is to be extracted
16+
* @returns a new `TimeTable` instance
17+
*
18+
* @example
19+
* ```js
20+
* // use custom html-handler
21+
* TimeTable.htmlHandler = (html) => {
22+
* var div = html("my-div")
23+
* var data = div.text()
24+
*
25+
* return new TimeTable(...)
26+
* }
27+
* ```
28+
*
29+
* @example
30+
* ```js
31+
* // reset html-handler to default
32+
* TimeTable.htmlHandler = undefined
33+
* ```
34+
*/
35+
public static htmlHandler: (html: CheerioAPI) => TimeTable;
36+
1237
constructor(public readonly entries: Entry[]) {
1338
entries.forEach((e) => e.registerSubjectShorts(this.subjectShorts));
1439
}
@@ -103,8 +128,11 @@ export class TimeTable implements SubjectContainer {
103128
* @param rawHtml The raw html string
104129
* @returns A new `TimeTable` resource
105130
*/
106-
static fromHtml(rawHtml: string) {
131+
public static fromHtml(rawHtml: string) {
107132
const $ = load(rawHtml);
133+
if (TimeTable.htmlHandler !== undefined) {
134+
return TimeTable.htmlHandler($);
135+
}
108136

109137
const centers = $("center");
110138
const entries: Entry[] = [];
@@ -133,9 +161,10 @@ export class TimeTable implements SubjectContainer {
133161
);
134162

135163
for (const row of $(center).find("tr")) {
136-
if ($(row).find("th").length !== 0) continue;
164+
const rowCheer = $(row);
165+
if (rowCheer.find("th").length !== 0) continue;
137166

138-
const columns = $(row).find("td");
167+
const columns = rowCheer.find("td");
139168

140169
let period = $(columns[1]).text();
141170
if (period.includes("-")) {

src/utility.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ export const defaultSubjectShorts: [string, string][] = [
5151
["IFÖM", "Mathe Förderunterricht"],
5252
["IFÖE", "Englisch Förderunterricht"],
5353
["IFÖD", "Deutsch Förderunterricht"],
54-
]
54+
];

test/components.spec.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("Test components(classes)", () => {
2222
expect(entry1.longOldSubject).to.be.a("string").and.equal("Mathe");
2323
expect(entry1.longNewSubject).to.be.a("string").and.equal("Deutsch");
2424

25-
const entry2 = Entry.fromJSON(data)
25+
const entry2 = Entry.fromJSON(data);
2626

2727
data["old-subject"] = "This subject does not exist";
2828
const entry3 = Entry.fromJSON(data);
@@ -38,12 +38,13 @@ describe("Test components(classes)", () => {
3838
detail: "Detial",
3939
};
4040
const post = NewsPost.fromJSON(data);
41+
const post2 = NewsPost.fromJSON(post.toJSON());
4142

4243
expect(JSON.stringify(post.toJSON())).to.equal(JSON.stringify(data));
4344
expect(post.title).to.be.a("string").and.equal(data["title"]);
4445
expect(post.date).to.be.a("date").and.equal(data["date"]);
4546
expect(post.detail).to.be.a("string").and.equal(data["detail"]);
46-
expect(NewsPost.fromJSON(post.toJSON()).date).be.instanceOf(Date);
47+
expect(post2.equals(post)).to.be.true;
4748
});
4849

4950
it("Create and test DocumentPost", () => {
@@ -55,6 +56,7 @@ describe("Test components(classes)", () => {
5556
"preview-url": "previewURL",
5657
};
5758
const post = DocumentPost.fromJSON(data);
59+
const post2 = DocumentPost.fromJSON(post.toJSON());
5860

5961
expect(JSON.stringify(post.toJSON())).to.equal(JSON.stringify(data));
6062
expect(post.id).to.be.a("string").and.equal(data["id"]);
@@ -64,6 +66,6 @@ describe("Test components(classes)", () => {
6466
expect(post.previewURL)
6567
.to.be.a("string")
6668
.and.equal(data["preview-url"]);
67-
expect(DocumentPost.fromJSON(post.toJSON()).date).be.instanceOf(Date);
69+
expect(post2.equals(post)).to.be.true;
6870
});
6971
});

0 commit comments

Comments
 (0)