Skip to content

Commit

Permalink
Add missing interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-korsa committed Dec 16, 2020
1 parent f24f0ad commit 3a56cfb
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 71 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
"indent": ["error", 2],
"@typescript-eslint/indent": ["error", 2]
},
"overrides": [
{
"files": [
"test/**"
],
"rules": {
"@typescript-eslint/no-non-null-assertion": ["off"]
}
}
],
"env": {
"mocha": true
}
Expand Down
46 changes: 25 additions & 21 deletions lib/Table.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as cheerio from 'cheerio';
import TableHour from './TableHour';
import { TableHour, TableLesson } from './types';

export default class Table {
public $: CheerioStatic;
Expand Down Expand Up @@ -28,21 +28,25 @@ export default class Table {
rows.forEach((row: CheerioElement): void => {
const number = parseInt(this.$(row).find('.nr').text().trim(), 10);
const timesText = this.$(row).find('.g').text();
let [timeFrom, timeTo] = timesText.split('-');
timeFrom = timeFrom.trim();
timeTo = timeTo.trim();
hours[number] = new TableHour(number, timeFrom, timeTo);
const [timeFrom, timeTo] = timesText
.split('-')
.map((e): string => e.trim());
hours[number] = {
number,
timeFrom,
timeTo,
};
});
return hours;
}

/*
* Return table in original form (without transposing) for easier displaying.
*/
public getRawDays(): Record<string, string>[][][] {
public getRawDays(): TableLesson[][][] {
const rows = this.$('.tabela tr:not(:first-of-type)').toArray();

const days: Record<string, string>[][][] = [];
const days: TableLesson[][][] = [];

rows.forEach((row, index): void => {
const lessons = this.$(row).find('.l').toArray();
Expand All @@ -61,10 +65,10 @@ export default class Table {
}


public getDays(): Record<string, string>[][][] {
public getDays(): TableLesson[][][] {
const rows = this.$('.tabela tr:not(:first-of-type)').toArray();

const days: Record<string, string>[][][] = [
const days: TableLesson[][][] = [
[],
[],
[],
Expand All @@ -87,26 +91,26 @@ export default class Table {
return days;
}

private parseLessons(data: CheerioElement[]): Record<string, string>[] {
let groups: Record<string, string>[] = [{}];
private parseLessons(data: CheerioElement[]): TableLesson[] {
let groups: Partial<TableLesson>[] = [{}];
let groupNumber = 0;
let commaSeperated = false;
let commaSeparated = false;


data.forEach((element): void => {
if (element.tagName === 'br') {
groupNumber += 1;
groups[groupNumber] = {};
} else if (/,/.test(this.$(element).text())) {
const groupNameMatch = this.$(element).text().match(/-\d{1,}\/\d{1,}/);
const groupNameMatch = this.$(element).text().match(/-\d+\/\d+/);

if (groupNameMatch) {
groups[groupNumber].groupName = groupNameMatch[0].substr(1);
}

groupNumber += 1;
groups[groupNumber] = {};
commaSeperated = true;
commaSeparated = true;

if (groups[groupNumber - 1].teacher) {
groups[groupNumber].teacher = groups[groupNumber - 1].teacher;
Expand All @@ -116,27 +120,27 @@ export default class Table {
groups[groupNumber].subject = groups[groupNumber - 1].subject;
}
} else {
const groupNameMatch = this.$(element).text().match(/-\d{1,}\/\d{1,}/);
const groupNameMatch = this.$(element).text().match(/-\d+\/\d+/);

if (groupNameMatch) {
groups[groupNumber].groupName = groupNameMatch[0].substr(1);
}

if (this.$(element).hasClass('p')) {
if (!groups[groupNumber].subject) {
groups[groupNumber].subject = this.$(element).text().replace(/-\d{1,}\/\d{1,}/, '');
groups[groupNumber].subject = this.$(element).text().replace(/-\d+\/\d+/, '');
} else {
groups[groupNumber].subject += ' ';
groups[groupNumber].subject += this.$(element).text().replace(/-\d{1,}\/\d{1,}/, '');
groups[groupNumber].subject += this.$(element).text().replace(/-\d+\/\d+/, '');
}
}

if (this.$(element).find('.p').length !== 0) {
if (!groups[groupNumber].subject) {
groups[groupNumber].subject = this.$(element).find('.p').text().replace(/-\d{1,}\/\d{1,}/, '');
groups[groupNumber].subject = this.$(element).find('.p').text().replace(/-\d+\/\d+/, '');
} else {
groups[groupNumber].subject += ' ';
groups[groupNumber].subject += this.$(element).find('.p').text().replace(/-\d{1,}\/\d{1,}/, '');
groups[groupNumber].subject += this.$(element).find('.p').text().replace(/-\d+\/\d+/, '');
}
}

Expand Down Expand Up @@ -164,7 +168,7 @@ export default class Table {
groups[groupNumber].room = this.$(element).find('.s').text();
}

if (commaSeperated) {
if (commaSeparated) {
groups.slice(0, groups.length - 1).forEach((group, groupIndex): void => {
if (!groups[groupIndex].teacher && groups[groupNumber].teacher) {
groups[groupIndex].teacher = groups[groupNumber].teacher;
Expand All @@ -182,6 +186,6 @@ export default class Table {
(group): boolean => (Object.getOwnPropertyNames(group).length !== 0),
);

return groups;
return groups as TableLesson[];
}
}
13 changes: 0 additions & 13 deletions lib/TableHour.ts

This file was deleted.

24 changes: 14 additions & 10 deletions lib/TimetableList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cheerio from 'cheerio';
import { List, ListItem } from './types';

export default class TimetableList {
public $: CheerioStatic;
Expand All @@ -7,7 +8,7 @@ export default class TimetableList {
this.$ = cheerio.load(html);
}

public getList(): Record<string, Record<string, string>[]> {
public getList(): List {
if (this.getListType() === 'select') {
return this.getSelectList();
} if (this.getListType() === 'unordered') {
Expand All @@ -29,19 +30,19 @@ export default class TimetableList {
return 'unordered';
}

private getSelectList(): Record<string, Record<string, string>[]> {
private getSelectList(): List {
return {
classes: this.getSelectListValues('oddzialy'),
teachers: this.getSelectListValues('nauczyciele'),
rooms: this.getSelectListValues('sale'),
};
}

private getSelectListValues(name: string): Record<string, string>[] {
private getSelectListValues(name: string): ListItem[] {
const nodes = this.$(`[name=${name}] option`).toArray();
nodes.shift();

const values: Record<string, string>[] = [];
const values: ListItem[] = [];
nodes.forEach((node): void => {
values.push({
name: this.$(node).text(),
Expand All @@ -52,15 +53,15 @@ export default class TimetableList {
return values;
}

private getExpandableList(): Record<string, Record<string, string>[]> {
private getExpandableList(): List {
return this.getTimetableUrlSubType(
'#oddzialy a',
'#nauczyciele a',
'#sale a',
);
}

private getUnorderedList(): Record<string, Record<string, string>[]> {
private getUnorderedList(): List {
let teachersQuery = 'ul:nth-of-type(2) a';
let roomsQuery = 'ul:nth-of-type(3) a';
if (this.$('h4').length === 1) {
Expand All @@ -77,17 +78,20 @@ export default class TimetableList {
);
}

private getTimetableUrlSubType(classQuery: string, teachersQuery: string, roomsQuery: string):
Record<string, Record<string, string>[]> {
private getTimetableUrlSubType(
classQuery: string,
teachersQuery: string,
roomsQuery: string,
): List {
return {
classes: this.getSubTypeValue(classQuery, 'o'),
teachers: this.getSubTypeValue(teachersQuery, 'n'),
rooms: this.getSubTypeValue(roomsQuery, 's'),
};
}

private getSubTypeValue(query: string, prefix: string): Record<string, string>[] {
const values: Record<string, string>[] = [];
private getSubTypeValue(query: string, prefix: string): ListItem[] {
const values: ListItem[] = [];

const nodes = this.$(query).toArray();
nodes.forEach((node: CheerioElement): void => {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Table from './Table';
import TableHour from './TableHour';
import TimetableList from './TimetableList';
import Timetable from './Timetable';

export {
Table, TableHour, TimetableList, Timetable,
Table, TimetableList, Timetable,
};
export * from './types';
24 changes: 24 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export interface TableHour {
number: number;
timeFrom: string;
timeTo: string;
}

export interface TableLesson {
subject: string;
room?: string;
groupName?: string;
teacher?: string;
className?: string;
}

export interface List {
classes: ListItem[];
teachers?: ListItem[];
rooms?: ListItem[];
}

export interface ListItem {
name: string;
value: string;
}
70 changes: 45 additions & 25 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect } from 'chai';
import * as fs from 'fs';
import * as path from 'path';
import { Timetable, TimetableList, Table } from '../lib/index';
import {
Timetable, TimetableList, Table,
} from '../lib/index';

describe('Timetable test', (): void => {
const indexFilename = path.join(__dirname, 'fixtures', 'index.html');
Expand Down Expand Up @@ -42,18 +44,24 @@ describe('Timetable list test', (): void => {

it('Return value check', (): void => {
const nodesList = list.getList();
expect(nodesList.classes).not.to.equal(undefined);
expect(nodesList.teachers).not.to.equal(undefined);
expect(nodesList.rooms).not.to.equal(undefined);

expect(nodesList.classes[0].name).to.equal('1Tc');
expect(nodesList.classes[0].value).to.equal('1');
expect(nodesList.classes[1].name).to.equal('1Ti');
expect(nodesList.classes[1].value).to.equal('2');
expect(nodesList.teachers[0].name).to.equal('I.Ochocki (Io)');
expect(nodesList.teachers[0].value).to.equal('1');
expect(nodesList.teachers[1].name).to.equal('M.Oleszkiewicz (Mo)');
expect(nodesList.teachers[1].value).to.equal('3');
expect(nodesList.rooms[0].name).to.equal('16 prac. geograficzna');
expect(nodesList.rooms[0].value).to.equal('1');
expect(nodesList.rooms[1].name).to.equal('17 prac. fizyczna');
expect(nodesList.rooms[1].value).to.equal('2');

expect(nodesList.teachers![0].name).to.equal('I.Ochocki (Io)');
expect(nodesList.teachers![0].value).to.equal('1');
expect(nodesList.teachers![1].name).to.equal('M.Oleszkiewicz (Mo)');
expect(nodesList.teachers![1].value).to.equal('3');

expect(nodesList.rooms![0].name).to.equal('16 prac. geograficzna');
expect(nodesList.rooms![0].value).to.equal('1');
expect(nodesList.rooms![1].name).to.equal('17 prac. fizyczna');
expect(nodesList.rooms![1].value).to.equal('2');
});
});

Expand All @@ -75,18 +83,24 @@ describe('Timetable list test', (): void => {

it('Return value check', (): void => {
const nodesList = list.getList();
expect(nodesList.classes).not.to.equal(undefined);
expect(nodesList.teachers).not.to.equal(undefined);
expect(nodesList.rooms).not.to.equal(undefined);

expect(nodesList.classes[0].name).to.equal('1Tc');
expect(nodesList.classes[0].value).to.equal('1');
expect(nodesList.classes[1].name).to.equal('1Ti');
expect(nodesList.classes[1].value).to.equal('2');
expect(nodesList.teachers[0].name).to.equal('I.Ochocki (Io)');
expect(nodesList.teachers[0].value).to.equal('1');
expect(nodesList.teachers[1].name).to.equal('M.Oleszkiewicz (Mo)');
expect(nodesList.teachers[1].value).to.equal('3');
expect(nodesList.rooms[0].name).to.equal('16 prac. geograficzna');
expect(nodesList.rooms[0].value).to.equal('1');
expect(nodesList.rooms[1].name).to.equal('17 prac. fizyczna');
expect(nodesList.rooms[1].value).to.equal('2');

expect(nodesList.teachers![0].name).to.equal('I.Ochocki (Io)');
expect(nodesList.teachers![0].value).to.equal('1');
expect(nodesList.teachers![1].name).to.equal('M.Oleszkiewicz (Mo)');
expect(nodesList.teachers![1].value).to.equal('3');

expect(nodesList.rooms![0].name).to.equal('16 prac. geograficzna');
expect(nodesList.rooms![0].value).to.equal('1');
expect(nodesList.rooms![1].name).to.equal('17 prac. fizyczna');
expect(nodesList.rooms![1].value).to.equal('2');
});
});

Expand All @@ -108,18 +122,24 @@ describe('Timetable list test', (): void => {

it('Return value check', (): void => {
const nodesList = list.getList();
expect(nodesList.classes).not.to.equal(undefined);
expect(nodesList.teachers).not.to.equal(undefined);
expect(nodesList.rooms).not.to.equal(undefined);

expect(nodesList.classes[0].name).to.equal('1Tc');
expect(nodesList.classes[0].value).to.equal('1');
expect(nodesList.classes[1].name).to.equal('1Ti');
expect(nodesList.classes[1].value).to.equal('2');
expect(nodesList.teachers[0].name).to.equal('I.Ochocki (Io)');
expect(nodesList.teachers[0].value).to.equal('1');
expect(nodesList.teachers[1].name).to.equal('M.Oleszkiewicz (Mo)');
expect(nodesList.teachers[1].value).to.equal('3');
expect(nodesList.rooms[0].name).to.equal('16 prac. geograficzna');
expect(nodesList.rooms[0].value).to.equal('1');
expect(nodesList.rooms[1].name).to.equal('17 prac. fizyczna');
expect(nodesList.rooms[1].value).to.equal('2');

expect(nodesList.teachers![0].name).to.equal('I.Ochocki (Io)');
expect(nodesList.teachers![0].value).to.equal('1');
expect(nodesList.teachers![1].name).to.equal('M.Oleszkiewicz (Mo)');
expect(nodesList.teachers![1].value).to.equal('3');

expect(nodesList.rooms![0].name).to.equal('16 prac. geograficzna');
expect(nodesList.rooms![0].value).to.equal('1');
expect(nodesList.rooms![1].name).to.equal('17 prac. fizyczna');
expect(nodesList.rooms![1].value).to.equal('2');
});
});
});
Expand Down

0 comments on commit 3a56cfb

Please sign in to comment.