Skip to content

Commit 1c511fe

Browse files
authored
Merge pull request #44 from Tch1b0/Tch1b0/issue42
Tch1b0/issue42
2 parents 1b94a95 + dc588af commit 1c511fe

File tree

7 files changed

+88
-16
lines changed

7 files changed

+88
-16
lines changed

README.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
![badge](https://img.shields.io/github/workflow/status/Tch1b0/dsbmobile.js/ci?label=ci)
55
![badge](https://img.shields.io/github/issues/Tch1b0/dsbmobile.js)
66

7-
A Javascript wrapper for the [dsbmobile](https://dsbmobile.de) API
7+
A Javascript package for the [dsbmobile](https://dsbmobile.de) API
88

99
## Installation
1010

11-
```
11+
```bash
1212
$ npm install dsbmobile
1313
```
1414

@@ -17,13 +17,14 @@ $ npm install dsbmobile
1717
```js
1818
import Dsbmobile from "dsbmobile";
1919

20-
let dm = new Dsbmobile("<id>", "<password>");
20+
const dsb = new Dsbmobile("<id>", "<password>");
2121

22-
let t = await dm.getTimetable();
23-
console.log(t.entries[0].newSubject);
22+
dsb.getTimetable().then((timetable) => {
23+
console.log(timetable.entries[0].newSubject);
24+
});
2425
```
2526

26-
```bash
27+
```
2728
English
2829
```
2930

@@ -57,3 +58,9 @@ new Dsbmobile({
5758
After that, all requests will not be sent to the official Server, but to the mydsb.johannespour.de one, which will make a request for you to the resource and add a CORS header.
5859

5960
If you are concerned about privacy and other things, you can [check out the code](https://github.com/Tch1b0/dsb-middleware), because it's open source.
61+
62+
## Incompatible plan
63+
64+
As I only know the layout of my local DSBmobile substitution plan, it is possible that your DSBmobile substitution plan may be incompatible with this package.
65+
66+
If this is the case, please leave a comment below [issue #43](https://github.com/Tch1b0/dsbmobile.js/issues/43), in which you describe the layout of your plan, so a solution can be worked out.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dsbmobile",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"description": "A Javascript wrapper for the dsbmobile api",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/timetable/entry.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { isText } from "../utility";
22
import { UnknownSubject } from "../";
3+
import { SubjectContainer } from "./subjectcontainer";
34

4-
export class Entry {
5+
export class Entry implements SubjectContainer {
56
public readonly date: Date;
67
public readonly subjectShorts = new Map<string, string>([
78
["D", "Deutsch"],
@@ -123,7 +124,7 @@ export class Entry {
123124

124125
/**
125126
* Update the subject shorts with a map
126-
* @param subjectShorts
127+
* @param subjectShorts the subject shorts mapped to their long name
127128
*
128129
* @example
129130
* ```js

src/timetable/subjectcontainer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface SubjectContainer {
2+
subjectShorts: Map<string, string>;
3+
registerSubjectShorts(subjectShorts: Map<string, string>): void;
4+
}

src/timetable/timetable.ts

+56-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,63 @@
11
import { Entry } from "./entry";
2-
import cheerio from "cheerio";
2+
import { load } from "cheerio";
3+
import { SubjectContainer } from "./subjectcontainer";
34

45
/**
56
* The time table resource
67
*/
7-
export class TimeTable {
8-
constructor(public readonly entries: Entry[]) {}
8+
export class TimeTable implements SubjectContainer {
9+
public subjectShorts = new Map<string, string>([
10+
["D", "Deutsch"],
11+
["E", "Englisch"],
12+
["WI", "Wirtschaft"],
13+
["GGK", "Geschichte und Gemeinschaftskunde"],
14+
["CH", "Chemie"],
15+
["S", "Sport"],
16+
["M", "Mathe"],
17+
["BK", "Bildende Kunst"],
18+
["BK1", "Bildende Kunst"],
19+
["BK2", "Bildende Kunst"],
20+
["GS", "Global Studies"],
21+
["PH", "Physik"],
22+
["IT", "Informatik"],
23+
["INF", "Informationstechnik"],
24+
["ITÜS", "IT Softwareentwicklung"],
25+
["ITÜH", "IT Hardware"],
26+
["EVR", "Religion"],
27+
["ETH", "Ethik"],
28+
["SP", "Zweitsprache"],
29+
["IFÖM", "Mathe Förderunterricht"],
30+
["IFÖE", "Englisch Förderunterricht"],
31+
["IFÖD", "Deutsch Förderunterricht"],
32+
]);
33+
34+
constructor(public readonly entries: Entry[]) {
35+
entries.forEach((e) => e.registerSubjectShorts(this.subjectShorts));
36+
}
37+
38+
/**
39+
* Update the subject shorts for all entries in this timetable instance
40+
* @param subjectShorts The subject shorts mapped to their long name
41+
*
42+
* @example
43+
* ```js
44+
* timetable.registerSubjectShorts(new Map([["D", "Deutsch"]]))
45+
* ```
46+
*/
47+
public registerSubjectShorts(subjectShorts: Map<string, string>): void {
48+
for (const [k, v] of subjectShorts) {
49+
this.subjectShorts.set(k, v);
50+
}
51+
52+
for (const entry of this.entries) {
53+
entry.registerSubjectShorts(subjectShorts);
54+
}
55+
}
56+
57+
public addEntry(entry: Entry) {
58+
entry.registerSubjectShorts(this.subjectShorts);
59+
this.entries.push(entry);
60+
}
961

1062
/**
1163
* Find a certain entry by the class name
@@ -74,7 +126,7 @@ export class TimeTable {
74126
* @returns A new `TimeTable` resource
75127
*/
76128
static fromHtml(rawHtml: string) {
77-
const $ = cheerio.load(rawHtml);
129+
const $ = load(rawHtml);
78130

79131
const centers = $("center");
80132
const entries: Entry[] = [];

src/utility.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable */
2-
31
/**
42
* Parse a date from string to a Date object.
53
* Use this instead of the `Date.parse` function,
@@ -26,6 +24,6 @@ export function parseDate(rawDate: string): Date {
2624
* @param data the data to validate
2725
* @returns whether the data is a string
2826
*/
29-
export function isText(data: any): data is string {
27+
export function isText(data: string | unknown): data is string {
3028
return typeof data === "string";
3129
}

test/main.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ describe("Test DSBmobile Wrapper", () => {
4242
expect(otherEntry.oldSubject).to.eq("CH");
4343
expect(otherEntry.longOldSubject).to.eq("Chemie");
4444

45+
otherEntry.registerSubjectShorts(
46+
new Map([["CH", "LONG SUBJECT NAME"]]),
47+
);
48+
expect(otherEntry.oldSubject).to.eq("CH");
49+
expect(otherEntry.longOldSubject).to.eq("LONG SUBJECT NAME");
50+
51+
t.registerSubjectShorts(new Map([["CH", "OTHER SUBJECT NAME"]]));
52+
expect(otherEntry.oldSubject).to.eq("CH");
53+
expect(otherEntry.longOldSubject).to.eq("OTHER SUBJECT NAME");
54+
4555
const someCertainEntry = t.findByClassName("TGM11/1");
4656
expect(someCertainEntry).to.be.instanceOf(Array);
4757

0 commit comments

Comments
 (0)