UNPKG

rsshub

Version:
43 lines (42 loc) 1.84 kB
//#region lib/routes/tokyometro/utils.ts /** Collapse whitespace (incl. NBSP) from cheerio `.text()`. */ const cellText = (text) => (text ?? "").replaceAll(/\s+/g, " ").trim(); /** '529,947' → 529947; '-' / '' / '(151,951)' (a parenthesised note) → null. */ const parseCount = (text) => { const s = text.replaceAll(/[,,\s]/g, "").normalize("NFKC"); return /^\d+$/.test(s) ? Number(s) : null; }; /** '2.3' → 2.3; '▲ 11.9' / '-0.1' / '△0.8' → negative; '' / '-' → null. */ const parsePct = (text) => { const m = text.replaceAll(/[\s,]/g, "").normalize("NFKC").match(/^([▲△-])?(\d+(?:\.\d+)?)$/); if (!m) return null; const n = Number(m[2]); return m[1] ? -n : n; }; /** '2025年度' anywhere in the text → 2025; nothing → null. */ const parseFiscalYear = (text) => { const m = text.match(/(\d{4})年度/); return m ? Number(m[1]) : null; }; /** Item title / guid / description are derived from `_extra` only, so all four operators read the same way. */ const ridershipItem = (extra, link) => { const figure = extra.daily_average ?? extra.annual_total ?? null; const people = figure === null ? "不明" : `${figure.toLocaleString("ja-JP")}${extra.unit}`; const yoy = extra.yoy_pct === null ? null : `前年比 ${extra.yoy_pct > 0 ? "+" : ""}${extra.yoy_pct}%`; return { title: `${extra.station}${extra.line ?? extra.operator}${extra.fiscal_year}年度 ${people}`, link, guid: `${extra.source}/ridership:${extra.fiscal_year}:${extra.line ?? "-"}:${extra.station}`, description: [ extra.operator, extra.line, `${extra.fiscal_year}年度`, people, yoy, extra.rank === null ? null : `${extra.rank}位` ].filter((p) => p !== null).join(" / "), _extra: extra }; }; //#endregion export { ridershipItem as a, parsePct as i, parseCount as n, parseFiscalYear as r, cellText as t };