// OPT and STEM OPT Form I-765 filing-window estimator (same rules as filing_window.py). // MIT License. No dependencies. Node 18+ or the browser. // Dates in and out are "YYYY-MM-DD" strings; all math is done in UTC so DST can't shift a day. // USCIS records the receipt date as the actual date of receipt (8 CFR 103.2(a)(7)(i)). const DAY_MS = 86400000; const DISCLAIMER = "Estimate from the dates you entered. Day-boundary counting is not " + "defined by USCIS; file inside the window with margin. Not legal advice."; function toDay(s) { const ms = /^\d{4}-\d{2}-\d{2}$/.test(s) ? Date.parse(s + "T00:00:00Z") : NaN; if (Number.isNaN(ms) || new Date(ms).toISOString().slice(0, 10) !== s) { throw new RangeError(`Bad date "${s}". Use a real YYYY-MM-DD date.`); } return ms / DAY_MS; } const toIso = (day) => new Date(day * DAY_MS).toISOString().slice(0, 10); const addDays = (s, n) => toIso(toDay(s) + n); function window_(opens, closes, recommendation, validDays) { const earliest = Math.max(opens, toDay(recommendation)); const latest = Math.min(closes, toDay(recommendation) + validDays); return earliest <= latest ? [toIso(earliest), toIso(latest)] : null; } // [earliest, latest] receipt dates for a post-completion OPT I-765, or null. function optWindow(programEnd, dsoRecommendation) { const end = toDay(programEnd); return window_(end - 90, end + 60, dsoRecommendation, 30); } // [earliest, latest] receipt dates for a STEM OPT I-765, or null. // latest is capped at the EAD end: the filing must be received while the EAD is unexpired. function stemWindow(optEadEnd, dsoStemRecommendation) { const end = toDay(optEadEnd); return window_(end - 90, end, dsoStemRecommendation, 60); } // True if USCIS's receipt date falls inside the window (both ends inclusive). // toDay() validates the input; ISO strings then compare correctly as text. function isTimely(win, receiptDate) { toDay(receiptDate); return win !== null && receiptDate >= win[0] && receiptDate <= win[1]; } // Why optWindow/stemWindow returned null (kind "opt" or "stem"), else null. function emptyReason(kind, endDate, recommendation) { const end = toDay(endDate), rec = toDay(recommendation); const [validDays, closes] = kind === "opt" ? [30, end + 60] : [60, end]; if (rec + validDays < end - 90) { return "DSO recommendation is too old; it expires before the filing window opens. " + "Ask your DSO for a new recommendation."; } if (rec > closes) { return kind === "opt" ? "DSO recommendation is more than 60 days after the program end date, so the OPT " + "filing deadline has passed. Talk to your DSO." : "DSO recommendation is after your OPT EAD end date; a STEM I-765 must be filed " + "before the EAD expires. Talk to your DSO."; } return null; } const api = { optWindow, stemWindow, isTimely, emptyReason, DISCLAIMER }; if (typeof module !== "undefined") module.exports = api;