Skip to content

Commit 87b69db

Browse files
committed
fix: 日時のカンマに対する挙動を修正
1 parent 38b814b commit 87b69db

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twinte-parser",
3-
"version": "2.1.3",
3+
"version": "2.2.0",
44
"description": "Twinte内部で使用するために開発されたKdBパーサ",
55
"private": false,
66
"main": "dist/index.js",

src/parser.ts

+46-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {Course, Day, Module} from './types'
2-
import {read as readXLSX, utils} from 'xlsx'
1+
import { Course, Day, Module } from './types'
2+
import { read as readXLSX, utils } from 'xlsx'
33
import * as assert from 'assert'
44

55
/**
@@ -9,36 +9,52 @@ import * as assert from 'assert'
99
*/
1010
const analyzeDayAndPeriod = (str: string): { day: Day; period: number }[] => {
1111
const result: { day: Day; period: number }[] = []
12-
//全ての曜日に対して
13-
Object.entries(Day).forEach((k) => {
14-
const day = k[1] as Day //月, 火 , .... , 日 のどれか
15-
// 1から8限について
16-
for (let i = 1; i <= 8; i++) {
17-
/*
18-
strが{曜日}(任意の文字列){時限} にマッチするか調べる
19-
任意の文字列を間に挟むことで検出が一度で済む
20-
例1: 月1,2 は、月.*1と月.*2のテストに合格する
21-
例2: 月・水3は、月.*3と水.*3のテストに合格する
22-
*/
23-
if (new RegExp(`${day}.*${i}`).test(str)) {
24-
result.push({
25-
day: day,
26-
period: i,
27-
})
28-
}
29-
}
30-
// 月1-4 のようなハイフン表記のテスト
31-
const longTermTest = new RegExp(`([${day}]).*(\\d)-(\\d)`).exec(str)
32-
if (longTermTest) {
33-
for (let i = Number(longTermTest[2]); i <= Number(longTermTest[3]); i++) {
34-
if (!result.find((el) => el.day === day && el.period === i))
12+
/* 本来時限区切り(木1,2)で使われているカンマが
13+
* ```
14+
* 水2
15+
* 月・金3
16+
* ```
17+
* のように本来改行で区切られている書式の省略版として
18+
* 「水2,月・金3」のように使われていることを発見したので
19+
* 「,曜日」でsplitして解析を行う
20+
*/
21+
str.split(/,(?=[])/).forEach((
22+
str //全ての曜日に対して
23+
) =>
24+
Object.entries(Day).forEach((k) => {
25+
const day = k[1] as Day //月, 火 , .... , 日 のどれか
26+
// 1から8限について
27+
for (let i = 1; i <= 8; i++) {
28+
/*
29+
strが{曜日}(任意の文字列){時限} にマッチするか調べる
30+
任意の文字列を間に挟むことで検出が一度で済む
31+
例1: 月1,2 は、月.*1と月.*2のテストに合格する
32+
例2: 月・水3は、月.*3と水.*3のテストに合格する
33+
*/
34+
if (new RegExp(`${day}.*${i}`).test(str)) {
3535
result.push({
3636
day: day,
3737
period: i,
3838
})
39+
}
3940
}
40-
}
41-
})
41+
// 月1-4 のようなハイフン表記のテスト
42+
const longTermTest = new RegExp(`([${day}]).*(\\d)-(\\d)`).exec(str)
43+
if (longTermTest) {
44+
for (
45+
let i = Number(longTermTest[2]);
46+
i <= Number(longTermTest[3]);
47+
i++
48+
) {
49+
if (!result.find((el) => el.day === day && el.period === i))
50+
result.push({
51+
day: day,
52+
period: i,
53+
})
54+
}
55+
}
56+
})
57+
)
4258

4359
// 集中
4460
if (str.includes(Day.Intensive))
@@ -63,7 +79,7 @@ const analyzeDayAndPeriod = (str: string): { day: Day; period: number }[] => {
6379

6480
//どのテストにも合格しなかったが空文字でなければ仮にunknownとする
6581
if (str !== '' && result.length === 0)
66-
result.push({day: Day.Unknown, period: 0})
82+
result.push({ day: Day.Unknown, period: 0 })
6783

6884
return result
6985
}
@@ -209,11 +225,11 @@ export default (data: Buffer): Course[] => {
209225

210226
for (let r = 5; ; r++) {
211227
// sheetの終端で終了
212-
if (typeof sheet[utils.encode_cell({r, c: 0})] === 'undefined') break
228+
if (typeof sheet[utils.encode_cell({ r, c: 0 })] === 'undefined') break
213229

214230
const columns: string[] = []
215231
for (let c = 0; c <= 16; c++)
216-
columns.push(sheet[utils.encode_cell({r, c})].v)
232+
columns.push(sheet[utils.encode_cell({ r, c })].v)
217233

218234
// 科目番号が空の行はスキップ
219235
if (columns[0] === '') continue

0 commit comments

Comments
 (0)