Skip to content

Commit

Permalink
fix(module): allow string type for middle song in mwb
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Feb 9, 2024
1 parent c494d4d commit b1e0cc5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/common/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getWStudyDate,
getWStudyTitle,
} from './html_utils.js';
import { extractLastSong, extractSongNumber, extractSourceEnhanced } from './parsing_rules.js';
import { extractSongNumber, extractSourceEnhanced } from './parsing_rules.js';
import { MWBSchedule, WSchedule } from '../types/index.js';

export const startParse = async (epubInput: string | Blob | { url: string }) => {
Expand Down Expand Up @@ -118,7 +118,7 @@ export const parseMWBSchedule = (htmlItem: HTMLElement, mwbYear: number, mwbLang
let tmpSrc = '';

// First song
weekItem.mwb_song_first = extractSongNumber(splits[1]);
weekItem.mwb_song_first = extractSongNumber(splits[1]) as number;

// 10min TGW Source
tmpSrc = splits[3].trim();
Expand Down Expand Up @@ -248,7 +248,7 @@ export const parseMWBSchedule = (htmlItem: HTMLElement, mwbYear: number, mwbLang
nextIndex++;
nextIndex++;
tmpSrc = splits[nextIndex].trim();
weekItem.mwb_song_conclude = extractLastSong(tmpSrc);
weekItem.mwb_song_conclude = extractSongNumber(tmpSrc);

return weekItem;
};
Expand Down Expand Up @@ -276,7 +276,7 @@ export const parseWSchedule = (article: HTMLElement, content: HTMLElement, wLang
const pubRefs = content.querySelectorAll('.pubRefs');

const openingSongText = pubRefs.at(0)!;
weekItem.w_study_opening_song = extractSongNumber(openingSongText.textContent);
weekItem.w_study_opening_song = extractSongNumber(openingSongText.textContent) as number;

let concludingSongText = <HTMLElement>pubRefs.at(-1);

Expand All @@ -285,7 +285,7 @@ export const parseWSchedule = (article: HTMLElement, content: HTMLElement, wLang
concludingSongText = blockTeach!.nextElementSibling!;
}

weekItem.w_study_concluding_song = extractSongNumber(concludingSongText.textContent);
weekItem.w_study_concluding_song = extractSongNumber(concludingSongText.textContent) as number;

return weekItem;
};
Expand Down
13 changes: 7 additions & 6 deletions src/common/parsing_rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ export const extractMonthName = (src: string, lang: string) => {
};

export const extractSongNumber = (src: string) => {
return +src.match(/(\d+)/)![0];
const parseNum = src.match(/(\d+)/);

if (parseNum && parseNum.length > 0) {
return +parseNum![0];
}

return src;
};

export const extractSourceEnhanced = (src: string, lang: string) => {
Expand Down Expand Up @@ -98,11 +104,6 @@ export const extractSourceEnhanced = (src: string, lang: string) => {
throw new JWEPUBParserError('jw-epub-parser', `Parsing failed. The input was: ${src}`);
};

export const extractLastSong = (src: string) => {
const temp = extractSongNumber(src);
return temp > 151 ? src : temp;
};

export const extractWTStudyDate = (src: string, lang: string) => {
let varDay;
let monthIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type MWBSchedule = {
mwb_ayf_part4?: string;
mwb_ayf_part4_time?: number;
mwb_ayf_part4_type?: string;
mwb_song_middle: number;
mwb_song_middle: number | string;
mwb_lc_count: number;
mwb_lc_part1: string;
mwb_lc_part1_time?: number;
Expand Down

0 comments on commit b1e0cc5

Please sign in to comment.