From 3c409ea0489e7c928907050ca9c42a20f14922a9 Mon Sep 17 00:00:00 2001 From: Adrian Bonpin Date: Tue, 18 Aug 2026 15:28:12 +0800 Subject: [PATCH] feat: parse profile page header --- parsers.js | 26 +++++++++++++++++++++++++- test/parsers.test.js | 11 +++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/parsers.js b/parsers.js index 756b14d..4f34ac9 100644 --- a/parsers.js +++ b/parsers.js @@ -46,5 +46,29 @@ })); } - return { parseStudyLoadTable }; + function parseProfilePage(html) { + const result = {}; + const bMatch = /([\s\S]*?)<\/b>/i.exec(html); + if (bMatch) result.program = cleanText(bMatch[1]); + + const smallMatch = /([\s\S]*?)<\/small>/i.exec(html); + if (smallMatch) { + const lines = smallMatch[1].split(//i).map(cleanText).filter(Boolean); + result.semester = lines[lines.length - 1] || ''; + } + + const kvRe = /]*>([\s\S]*?)<\/td>\s*]*class="bold"[^>]*>([\s\S]*?)<\/td>/gi; + let m; + while ((m = kvRe.exec(html)) !== null) { + const key = cleanText(m[1]); + const val = cleanText(m[2]); + if (/full name/i.test(key)) result.fullName = val; + else if (/student id/i.test(key)) result.studentId = val; + else if (/year level/i.test(key)) result.yearLevel = val; + else if (/student type/i.test(key)) result.studentType = val; + } + return result; + } + + return { parseStudyLoadTable, parseProfilePage }; }); diff --git a/test/parsers.test.js b/test/parsers.test.js index fbb9f21..7573bc2 100644 --- a/test/parsers.test.js +++ b/test/parsers.test.js @@ -3,8 +3,10 @@ const assert = require('node:assert'); const fs = require('node:fs'); const path = require('node:path'); const { parseStudyLoadTable } = require('../parsers.js'); +const { parseProfilePage } = require('../parsers.js'); const fixture = fs.readFileSync(path.join(__dirname, 'fixtures', 'study-load-table.html'), 'utf8'); +const profileFixture = fs.readFileSync(path.join(__dirname, 'fixtures', 'profile.html'), 'utf8'); test('parseStudyLoadTable extracts all course rows', () => { const rows = parseStudyLoadTable(fixture); @@ -26,3 +28,12 @@ test('parseStudyLoadTable handles empty faculty', () => { const it4102 = rows.find(r => r.courseCode === 'IT 4102N'); assert.strictEqual(it4102.faculty, ''); }); + +test('parseProfilePage extracts header fields', () => { + const p = parseProfilePage(profileFixture); + assert.strictEqual(p.fullName, 'ADRIAN ALFRED CHIONG BONPIN'); + assert.strictEqual(p.studentId, '22103625'); + assert.strictEqual(p.yearLevel, '4'); + assert.strictEqual(p.program, 'BACHELOR OF SCIENCE IN INFORMATION TECHNOLOGY'); + assert.strictEqual(p.semester, '1ST Semester 2026'); +});