UNPKG

xenforo-dl

Version:
50 lines 1.28 kB
import { URL } from 'url'; export default class URLHelper { static getTargetTypeByURL(url) { let urlObj; try { urlObj = new URL(url); } catch (error) { throw Error('Invalid URL'); } const forum = this.parseForumURL(urlObj.toString()); if (forum) { return 'forum'; } const thread = this.parseThreadURL(urlObj.toString()); if (thread) { return 'thread'; } return 'unknown'; } static parseForumURL(url) { if (!url) { return null; } const regex = /\/forums\/(.+)\.(\d+)/g; const matches = regex.exec(url); if (matches && matches[2]) { return { slug: matches[1], id: Number(matches[2]) }; } return null; } static parseThreadURL(url) { if (!url) { return null; } const regex = /\/threads\/(.+)\.(\d+)/g; const matches = regex.exec(url); if (matches && matches[2]) { return { slug: matches[1], id: Number(matches[2]) }; } return null; } } //# sourceMappingURL=URLHelper.js.map