Skip to content

Commit 9c93289

Browse files
Refactor job filtering logic and improve expiry calculation
1 parent 736044a commit 9c93289

1 file changed

Lines changed: 37 additions & 26 deletions

File tree

src/filter-jobs.mjs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
1-
import { isJobExpired } from "./utils.mjs";
1+
const filterJobs = (jobs, filter) => {
22

3-
const filterJobs = (jobs, filter) => {
43
const today = new Date();
4+
const minOSS = filter.ossTimeGt ? parseInt(filter.ossTimeGt, 10) : null;
55

6-
// Filter out expired jobs if requested.
7-
if (!filter.showExpired) {
8-
jobs = jobs.filter((job) => !isJobExpired(job, today));
9-
}
10-
11-
if (filter.fullTime) {
12-
jobs = jobs.filter((job) => job.percentTime === 100);
13-
}
14-
15-
if (filter.ossTimeGt) {
16-
// Ensure we're comparing numbers
17-
const minOSS = parseInt(filter.ossTimeGt, 10);
18-
jobs = jobs.filter((job) => job.percentOSS >= minOSS);
19-
}
20-
21-
if (filter.remote) {
22-
// Matches "remote", "Remote (US)", "London / Remote", etc.
23-
jobs = jobs.filter((job) =>
24-
(job.location || "remote").toLowerCase().includes("remote")
25-
);
26-
}
27-
28-
return jobs;
6+
return jobs.filter((job) => {
7+
if (!filter.showExpired && isJobExpired(job, today)) {
8+
return false;
9+
}
10+
11+
if (filter.fullTime && job.percentTime !== 100) {
12+
return false;
13+
}
14+
15+
if (minOSS !== null && job.percentOSS < minOSS) {
16+
return false;
17+
}
18+
19+
if (filter.remote) {
20+
const location = (job.location || "").toLowerCase();
21+
return location.includes("remote");
22+
}
23+
24+
return true;
25+
});
26+
};
27+
28+
29+
// here we consider a job expired if its posted date is more than 30 days old
30+
const isJobExpired = (job, today) => {
31+
if (!job.postedDate) return false; // If no posted date, assume it's not expired
32+
33+
const postedDate = new Date(job.postedDate);
34+
const diffTime = Math.abs(today - postedDate);
35+
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
36+
37+
return diffDays > 30;
2938
};
3039

31-
export default filterJobs;
40+
export default filterJobs;
41+
// we can use this code for mordern js and it will work in node and browser both.
42+

0 commit comments

Comments
 (0)