Files
ismis-helper/parsers.js
T

51 lines
1.2 KiB
JavaScript

(function (root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.ISMIStools = Object.assign(root.ISMIStools || {}, factory());
}
})(typeof self !== 'undefined' ? self : this, function () {
'use strict';
function cleanText(s) {
return String(s == null ? '' : s)
.replace(/<[^>]+>/g, ' ')
.replace(/&nbsp;/gi, ' ')
.replace(/\s+/g, ' ')
.trim();
}
function extractRows(html) {
const rows = [];
const trRe = /<tr[^>]*>([\s\S]*?)<\/tr>/gi;
let trMatch;
while ((trMatch = trRe.exec(html)) !== null) {
const rowHtml = trMatch[1];
if (/<th/i.test(rowHtml)) continue;
const tds = [];
const tdRe = /<td[^>]*>([\s\S]*?)<\/td>/gi;
let tdMatch;
while ((tdMatch = tdRe.exec(rowHtml)) !== null) {
tds.push(cleanText(tdMatch[1]));
}
rows.push(tds);
}
return rows;
}
function parseStudyLoadTable(html) {
return extractRows(html)
.filter((tds) => tds.length >= 6)
.map((tds) => ({
group: tds[0],
courseCode: tds[1],
schedule: tds[2],
faculty: tds[3],
units: tds[4],
status: tds[5]
}));
}
return { parseStudyLoadTable };
});