1
2
3
4export const ATS_LIST = ['greenhouse', 'lever', 'ashby', 'workable', 'recruitee'];
5
6const SLUG_RE = /^[a-z0-9][a-z0-9._-]{0,99}$/i;
7
8
9export function parseCompany(raw) {
10 const s = String(raw ?? '').trim();
11 if (!s) throw new Error('Empty company entry');
12 const eu = s.match(/^lever[-_ ]?eu\s*[:/]\s*([^/?#\s]+)$/i);
13 if (eu) return { ats: 'lever', slug: eu[1], region: 'eu' };
14 const pref = s.match(/^(greenhouse|lever|ashby|workable|recruitee)\s*[:/]\s*([^/?#\s]+)$/i);
15 if (pref) return { ats: pref[1].toLowerCase(), slug: pref[2], region: null };
16 if (/^https?:\/\//i.test(s) || /^[a-z0-9.-]+\.(io|co|com)\//i.test(s) || /\.recruitee\.com/i.test(s)) {
17 let u;
18 try { u = new URL(/^https?:\/\//i.test(s) ? s : `https://${s}`); } catch { throw new Error(`"${s}" is not a valid URL`); }
19 const h = u.hostname.toLowerCase();
20 const parts = u.pathname.split('/').filter(Boolean);
21 if (h.endsWith('greenhouse.io')) {
22 const forParam = u.searchParams.get('for');
23 if (forParam) return { ats: 'greenhouse', slug: forParam, region: null };
24 if (h.startsWith('boards-api')) { const i = parts.indexOf('boards'); if (i >= 0 && parts[i + 1]) return { ats: 'greenhouse', slug: parts[i + 1], region: null }; }
25 if (parts[0] && parts[0] !== 'embed') return { ats: 'greenhouse', slug: parts[0], region: null };
26 }
27 if (h.endsWith('lever.co')) {
28 const region = h.includes('.eu.') ? 'eu' : null;
29 if (h.startsWith('api.')) { const i = parts.indexOf('postings'); if (i >= 0 && parts[i + 1]) return { ats: 'lever', slug: parts[i + 1], region }; }
30 if (parts[0]) return { ats: 'lever', slug: parts[0], region };
31 }
32 if (h.endsWith('ashbyhq.com')) {
33 if (h.startsWith('api.')) { const i = parts.indexOf('job-board'); if (i >= 0 && parts[i + 1]) return { ats: 'ashby', slug: parts[i + 1], region: null }; }
34 if (parts[0]) return { ats: 'ashby', slug: parts[0], region: null };
35 }
36 if (h.endsWith('workable.com')) {
37 if (parts[0] === 'api') { const i = parts.indexOf('accounts'); if (i >= 0 && parts[i + 1]) return { ats: 'workable', slug: parts[i + 1], region: null }; }
38 if (parts[0] && parts[0] !== 'j') return { ats: 'workable', slug: parts[0], region: null };
39 }
40 if (h.endsWith('.recruitee.com')) return { ats: 'recruitee', slug: h.split('.')[0], region: null };
41 throw new Error(`"${s}" is not a supported career-site URL. Supported: Greenhouse, Lever, Ashby, Workable and Recruitee job boards (or just type the company's board name, e.g. "stripe").`);
42 }
43 if (!SLUG_RE.test(s)) throw new Error(`"${s}" is not a valid company board name (use e.g. "stripe" or a job-board URL)`);
44 return { ats: null, slug: s, region: null };
45}
46
47export function endpoint(ats, slug, region) {
48 const e = encodeURIComponent(slug);
49 switch (ats) {
50 case 'greenhouse': return `https://boards-api.greenhouse.io/v1/boards/${e}/jobs?content=true`;
51 case 'lever': return `https://api.${region === 'eu' ? 'eu.' : ''}lever.co/v0/postings/${e}?mode=json`;
52 case 'ashby': return `https://api.ashbyhq.com/posting-api/job-board/${e}?includeCompensation=true`;
53 case 'workable': return `https://apply.workable.com/api/v1/widget/accounts/${e}?details=true`;
54 case 'recruitee': return `https://${e}.recruitee.com/api/offers/`;
55 default: throw new Error(`Unknown ATS ${ats}`);
56 }
57}
58export function boardUrl(ats, slug, region) {
59 const e = encodeURIComponent(slug);
60 return {
61 greenhouse: `https://job-boards.greenhouse.io/${e}`,
62 lever: `https://jobs.${region === 'eu' ? 'eu.' : ''}lever.co/${e}`,
63 ashby: `https://jobs.ashbyhq.com/${e}`,
64 workable: `https://apply.workable.com/${e}`,
65 recruitee: `https://${e}.recruitee.com`,
66 }[ats];
67}
68
69const ENT = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', '#39': "'", ndash: '–', mdash: '—', rsquo: '’', lsquo: '‘', rdquo: '”', ldquo: '“', hellip: '…', bull: '•', middot: '·', euro: '€', pound: '£', copy: '©', reg: '®', trade: '™' };
70export function decodeEntities(s) {
71 return String(s ?? '').replace(/&(#x[0-9a-f]+|#\d+|[a-z0-9]+);/gi, (m, g) => {
72 if (g[0] === '#') { const n = g[1].toLowerCase() === 'x' ? parseInt(g.slice(2), 16) : parseInt(g.slice(1), 10); return Number.isFinite(n) && n > 0 && n < 0x110000 ? String.fromCodePoint(n) : m; }
73 return ENT[g.toLowerCase()] ?? m;
74 });
75}
76export function htmlToText(html) {
77 if (!html) return null;
78 let s = String(html);
79 if (/<\/?[a-z]/i.test(s)) s = decodeEntities(s);
80 s = s.replace(/<(script|style)[\s\S]*?<\/\1>/gi, ' ')
81 .replace(/<br\s*\/?>/gi, '\n').replace(/<li[^>]*>/gi, '\n• ').replace(/<\/(p|div|h[1-6]|li|ul|ol|tr|section)>/gi, '\n')
82 .replace(/<[^>]+>/g, ' ');
83 s = decodeEntities(s).replace(/[ \t\u00a0]+/g, ' ').replace(/ *\n */g, '\n').replace(/\n{3,}/g, '\n\n').trim();
84 return s || null;
85}
86export function decodedHtml(html) {
87 if (!html) return null;
88 const s = String(html);
89 return /<\/?[a-z]/i.test(s) ? decodeEntities(s) : s;
90}
91
92const CUR = { $: 'USD', '£': 'GBP', '€': 'EUR', USD: 'USD', GBP: 'GBP', EUR: 'EUR', CAD: 'CAD', AUD: 'AUD', CHF: 'CHF' };
93function num(x, k) { let n = parseFloat(String(x).replace(/,/g, '')); if (/k/i.test(k || '')) n *= 1000; return n; }
94
95export function extractSalary(text) {
96 if (!text) return null;
97 const re = /(USD|CAD|AUD|GBP|EUR|CHF|[$£€])\s?(\d{1,3}(?:[,.]\d{3})+|\d+(?:\.\d+)?)\s?([kK])?\s*(?:-|–|—|to)\s*(?:USD|CAD|AUD|GBP|EUR|CHF|[$£€])?\s?(\d{1,3}(?:[,.]\d{3})+|\d+(?:\.\d+)?)\s?([kK])?/;
98 const m = String(text).match(re);
99 if (!m) return null;
100 const min = num(m[2], m[3] || m[5]); const max = num(m[4], m[5]);
101 if (!(min > 0 && max >= min)) return null;
102 if (max < 1000 && !/k/i.test((m[3] || '') + (m[5] || ''))) {
103 return { salaryText: m[0].trim(), salaryMin: min, salaryMax: max, salaryCurrency: CUR[m[1]] ?? null, salaryInterval: max < 500 ? 'hour' : null };
104 }
105 return { salaryText: m[0].trim(), salaryMin: min, salaryMax: max, salaryCurrency: CUR[m[1]] ?? null, salaryInterval: 'year' };
106}
107
108const iso = (v) => {
109 if (v === null || v === undefined || v === '') return null;
110 const d = typeof v === 'number' ? new Date(v) : new Date(String(v).replace(' UTC', 'Z').replace(/^(\d{4}-\d\d-\d\d) (\d)/, '$1T$2'));
111 return Number.isNaN(d.getTime()) ? null : d.toISOString();
112};
113const isRemoteText = (s) => /\bremote\b|anywhere|work from home|telecommut/i.test(s || '');
114
115function base(ats, slug, companyName) {
116 return { ats, companySlug: slug, companyName: companyName || null };
117}
118function finish(o, includeHtml) {
119 const sal = o.salaryMin == null ? extractSalary(o.descriptionText) : null;
120 if (sal) Object.assign(o, sal);
121 if (!includeHtml) delete o.descriptionHtml;
122 for (const k of ['salaryText', 'salaryMin', 'salaryMax', 'salaryCurrency', 'salaryInterval']) if (o[k] === undefined) o[k] = null;
123 return o;
124}
125
126
127export class FormatError extends Error {}
128
129
130
131
132
133
134
135export function pickBoard(candidates) {
136 const found = candidates.filter(Boolean);
137 if (!found.length) return null;
138 let best = found[0];
139 for (const c of found) if (c.jobs.length > best.jobs.length) best = c;
140 return { best, others: found.filter((c) => c !== best && c.jobs.length > 0) };
141}
142
143
144
145
146
147
148
149
150
151async function fetchBoard(ats, slug, region, fetchJson) {
152 const r = await fetchJson(endpoint(ats, slug, region));
153 if (r.notFound && ats === 'lever' && !region) {
154 const eu = await fetchJson(endpoint(ats, slug, 'eu'));
155 return { r: eu, region: eu.notFound ? null : 'eu' };
156 }
157 return { r, region: region ?? null };
158}
159
160export async function resolveBoard(t, fetchJson, atsList = ATS_LIST) {
161 if (t.ats) {
162 const { r, region } = await fetchBoard(t.ats, t.slug, t.region, fetchJson);
163 if (r.notFound) return null;
164 return { ats: t.ats, region, ...extractRaw(t.ats, r.body), alsoFoundOn: [] };
165 }
166 const errors = [];
167 const candidates = await Promise.all(atsList.map(async (ats) => {
168 try {
169 const { r, region } = await fetchBoard(ats, t.slug, t.region, fetchJson);
170 return r.notFound ? null : { ats, region, ...extractRaw(ats, r.body) };
171 } catch (e) { errors.push(e); return null; }
172 }));
173 const pick = pickBoard(candidates);
174
175 if ((!pick || !pick.best.jobs.length) && errors.length) throw errors.find((e) => e instanceof FormatError) ?? errors[0];
176 if (!pick) return null;
177 return { ...pick.best, alsoFoundOn: pick.others.map((c) => ({ ats: c.ats, jobs: c.jobs.length })) };
178}
179
180export function extractRaw(ats, body) {
181 switch (ats) {
182 case 'greenhouse': if (!Array.isArray(body?.jobs)) throw new FormatError('greenhouse: no jobs array'); return { companyName: body.jobs[0]?.company_name ?? null, jobs: body.jobs };
183 case 'lever': if (!Array.isArray(body)) throw new FormatError('lever: response is not an array'); return { companyName: null, jobs: body };
184 case 'ashby': if (!Array.isArray(body?.jobs)) throw new FormatError('ashby: no jobs array'); return { companyName: null, jobs: body.jobs.filter((j) => j.isListed !== false) };
185 case 'workable': if (!Array.isArray(body?.jobs)) throw new FormatError('workable: no jobs array'); return { companyName: body.name ?? null, jobs: body.jobs };
186 case 'recruitee': if (!Array.isArray(body?.offers)) throw new FormatError('recruitee: no offers array'); return { companyName: body.offers[0]?.company_name ?? null, jobs: body.offers };
187 default: throw new FormatError(`unknown ats ${ats}`);
188 }
189}
190
191export function mapJob(ats, slug, companyName, j, { includeHtml = false, region = null } = {}) {
192 const b = base(ats, slug, companyName);
193 if (ats === 'greenhouse') {
194 const html = decodedHtml(j.content); const text = htmlToText(j.content);
195 const loc = j.location?.name ?? null;
196 return finish({ ...b, jobId: String(j.id), title: j.title?.trim() ?? null,
197 department: j.departments?.map((d) => d.name).filter(Boolean).join(', ') || null, team: null,
198 location: loc, locations: loc ? [loc] : [], country: null,
199 remote: isRemoteText(loc) || null, workplaceType: isRemoteText(loc) ? 'remote' : null, employmentType: null,
200 postedAt: iso(j.first_published), updatedAt: iso(j.updated_at),
201 jobUrl: j.absolute_url ?? null, applyUrl: j.absolute_url ?? null, boardUrl: boardUrl(ats, slug),
202 descriptionText: text, descriptionHtml: html }, includeHtml);
203 }
204 if (ats === 'lever') {
205 const c = j.categories || {};
206 const html = [j.opening, j.descriptionBody || j.description, ...(j.lists || []).map((l) => `<h3>${l.text}</h3><ul>${l.content}</ul>`), j.additional].filter(Boolean).join('\n');
207 const text = [j.openingPlain || j.descriptionPlain, ...(j.lists || []).map((l) => `${l.text}\n${htmlToText(l.content) || ''}`), j.additionalPlain].filter(Boolean).join('\n\n').trim() || htmlToText(html);
208 const sr = j.salaryRange;
209 const locs = c.allLocations?.length ? c.allLocations : (c.location ? [c.location] : []);
210 return finish({ ...b, jobId: j.id, title: j.text?.trim() ?? null, department: c.department ?? null, team: c.team ?? null,
211 location: c.location ?? null, locations: locs, country: j.country ?? null,
212 remote: j.workplaceType ? j.workplaceType === 'remote' : (isRemoteText(c.location) || null), workplaceType: j.workplaceType && j.workplaceType !== 'unspecified' ? j.workplaceType : null,
213 employmentType: c.commitment ?? null, postedAt: iso(j.createdAt), updatedAt: null,
214 jobUrl: j.hostedUrl ?? null, applyUrl: j.applyUrl ?? null, boardUrl: boardUrl(ats, slug, region),
215 ...(sr ? { salaryText: null, salaryMin: sr.min ?? null, salaryMax: sr.max ?? null, salaryCurrency: sr.currency ?? null, salaryInterval: sr.interval ?? null } : {}),
216 descriptionText: text, descriptionHtml: html || null }, includeHtml);
217 }
218 if (ats === 'ashby') {
219 const comp = j.compensation || {};
220 const tier = comp.summaryComponents?.find((x) => x.compensationType === 'Salary') ?? comp.compensationTiers?.[0]?.components?.find((x) => x.compensationType === 'Salary');
221 const locs = [j.location, ...(j.secondaryLocations || []).map((l) => l.location)].filter(Boolean);
222 return finish({ ...b, jobId: j.id, title: j.title?.trim() ?? null, department: j.department ?? null, team: j.team ?? null,
223 location: j.location ?? null, locations: locs, country: j.address?.postalAddress?.addressCountry ?? null,
224 remote: typeof j.isRemote === 'boolean' ? j.isRemote : null, workplaceType: j.workplaceType ? String(j.workplaceType).toLowerCase() : null,
225 employmentType: j.employmentType ?? null, postedAt: iso(j.publishedAt), updatedAt: null,
226 jobUrl: j.jobUrl ?? null, applyUrl: j.applyUrl ?? null, boardUrl: boardUrl(ats, slug),
227 ...(tier ? { salaryText: comp.scrapeableCompensationSalarySummary ?? comp.compensationTierSummary ?? null, salaryMin: tier.minValue ?? null, salaryMax: tier.maxValue ?? null, salaryCurrency: tier.currencyCode ?? null, salaryInterval: tier.interval ? String(tier.interval).replace(/^1 /, '').toLowerCase() : null } : {}),
228 descriptionText: j.descriptionPlain?.trim() || htmlToText(j.descriptionHtml), descriptionHtml: j.descriptionHtml ?? null }, includeHtml);
229 }
230 if (ats === 'workable') {
231 const locs = (j.locations || []).filter((l) => !l.hidden).map((l) => [l.city, l.region, l.country].filter(Boolean).join(', ')).filter(Boolean);
232 const loc = [j.city, j.state, j.country].filter(Boolean).join(', ') || locs[0] || null;
233 return finish({ ...b, jobId: j.shortcode ?? null, title: j.title?.trim() ?? null, department: j.department || null, team: j.function || null,
234 location: loc, locations: locs.length ? locs : (loc ? [loc] : []), country: j.locations?.[0]?.countryCode ?? null,
235 remote: typeof j.telecommuting === 'boolean' ? j.telecommuting : null, workplaceType: j.telecommuting ? 'remote' : null,
236 employmentType: j.employment_type || null, postedAt: iso(j.published_on || j.created_at), updatedAt: null,
237 jobUrl: j.url ?? j.shortlink ?? null, applyUrl: j.application_url ?? null, boardUrl: boardUrl(ats, slug),
238 descriptionText: htmlToText(j.description), descriptionHtml: j.description ?? null }, includeHtml);
239 }
240 if (ats === 'recruitee') {
241 const locs = (j.locations || []).map((l) => [l.city, l.state, l.country].filter(Boolean).join(', ')).filter(Boolean);
242 const html = [j.description, j.requirements].filter(Boolean).join('\n');
243 const s = j.salary || {};
244 return finish({ ...b, jobId: String(j.id), title: j.title?.trim() ?? null, department: j.department ?? null, team: null,
245 location: j.location || locs[0] || null, locations: locs, country: j.country_code ?? null,
246 remote: typeof j.remote === 'boolean' ? j.remote : null, workplaceType: j.remote ? 'remote' : (j.hybrid ? 'hybrid' : (j.on_site ? 'onsite' : null)),
247 employmentType: j.employment_type_code ?? null, postedAt: iso(j.published_at || j.created_at), updatedAt: iso(j.updated_at),
248 jobUrl: j.careers_url ?? null, applyUrl: j.careers_apply_url ?? null, boardUrl: boardUrl(ats, slug),
249 ...(s.min || s.max ? { salaryText: null, salaryMin: s.min ? Number(s.min) : null, salaryMax: s.max ? Number(s.max) : null, salaryCurrency: s.currency ?? null, salaryInterval: s.period ?? null } : {}),
250 descriptionText: htmlToText(html), descriptionHtml: html || null }, includeHtml);
251 }
252 throw new FormatError(`unknown ats ${ats}`);
253}
254
255export function parseDateInput(v, now = Date.now()) {
256 if (v === null || v === undefined || String(v).trim() === '') return null;
257 const s = String(v).trim();
258 const rel = s.match(/^(\d+)\s*(hour|day|week|month)s?(\s+ago)?$/i);
259 if (rel) { const n = Number(rel[1]); const ms = { hour: 3600e3, day: 86400e3, week: 7 * 86400e3, month: 30 * 86400e3 }[rel[2].toLowerCase()]; return new Date(now - n * ms); }
260 const d = new Date(s);
261 if (Number.isNaN(d.getTime())) throw new Error(`"${s}" is not a valid date. Use e.g. 2026-10-01 or "7 days".`);
262 return d;
263}
264
265
266export function matches(job, f) {
267 const lc = (x) => String(x ?? '').toLowerCase();
268 if (f.titleKeywords?.length && !f.titleKeywords.some((k) => lc(job.title).includes(lc(k)))) return false;
269 if (f.excludeTitleKeywords?.length && f.excludeTitleKeywords.some((k) => lc(job.title).includes(lc(k)))) return false;
270 if (f.locationKeywords?.length) {
271 const hay = lc([job.location, ...(job.locations || []), job.country].join(' | '));
272 if (!f.locationKeywords.some((k) => hay.includes(lc(k)))) return false;
273 }
274 if (f.departmentKeywords?.length && !f.departmentKeywords.some((k) => lc(`${job.department} ${job.team}`).includes(lc(k)))) return false;
275 if (f.remoteOnly && job.remote !== true) return false;
276 if (f.since && job.postedAt && new Date(job.postedAt) < f.since) return false;
277 if (f.since && !job.postedAt && f.strictDates) return false;
278 return true;
279}