-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathuseJobsFiltering.ts
More file actions
157 lines (130 loc) · 4.18 KB
/
Copy pathuseJobsFiltering.ts
File metadata and controls
157 lines (130 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type { Job } from "@/types/jobs";
import { useMemo, useState } from "react";
function normalizeComparableText(value?: string | null) {
return String(value || "")
.normalize("NFD")
.replace(/\p{Diacritic}/gu, "")
.toLowerCase()
.replace(/[^\p{L}\p{N}]+/gu, " ")
.trim()
.replace(/\s+/g, " ");
}
function normalizeComparableLink(value?: string | null) {
const rawValue = String(value || "").trim();
if (!rawValue) {
return "";
}
try {
const parsedUrl = new URL(rawValue);
parsedUrl.search = "";
parsedUrl.hash = "";
return `${parsedUrl.origin}${parsedUrl.pathname}`.replace(/\/+$/, "");
} catch {
return rawValue.split(/[?#]/)[0].replace(/\/+$/, "");
}
}
function splitJobKeywords(job: Job) {
return [
...new Set(
[
...(Array.isArray(job.keywords) ? job.keywords : []),
...String(job.palavra || "")
.split(/[,;|]+/)
.map((keyword) => keyword.trim()),
].filter(Boolean),
),
];
}
function pickPreferredValue(...values: Array<string | null | undefined>) {
return (
values
.map((value) => String(value || "").trim())
.filter(Boolean)
.sort((left, right) => right.length - left.length)[0] || ""
);
}
function buildDedupKey(job: Job) {
const title = normalizeComparableText(job.titulo);
const company = normalizeComparableText(job.empresa);
const location = normalizeComparableText(job.local);
if (title && company && location) {
return `identity:${title}|${company}|${location}`;
}
if (title && company) {
return `identity:${title}|${company}|${location || "sem-local"}`;
}
const link = normalizeComparableLink(job.link);
if (link) {
return `url:${link}`;
}
return `fallback:${title}|${company}|${location}|${normalizeComparableText(job.source)}`;
}
function dedupeJobs(jobs: Job[]) {
const unique = new Map<string, Job>();
for (const job of jobs) {
const key = buildDedupKey(job);
const existing = unique.get(key);
if (!existing) {
unique.set(key, {
...job,
palavra: splitJobKeywords(job).join(", "),
});
continue;
}
const mergedKeywords = [...new Set([...splitJobKeywords(existing), ...splitJobKeywords(job)])];
const mergedSources = [
...new Set(
[...(existing.sources ?? []), ...(job.sources ?? []), existing.source, job.source]
.map((source) => String(source || "").trim())
.filter(Boolean),
),
];
unique.set(key, {
...existing,
...job,
titulo: pickPreferredValue(existing.titulo, job.titulo),
empresa: pickPreferredValue(existing.empresa, job.empresa),
local: pickPreferredValue(existing.local, job.local),
link: pickPreferredValue(existing.link, job.link),
source: mergedSources.join(", ") || existing.source || job.source || "",
palavra: mergedKeywords.join(", "),
keywords: mergedKeywords,
sources: mergedSources.length > 0 ? mergedSources : undefined,
});
}
return [...unique.values()];
}
export function useJobsFiltering(jobs: Job[]) {
const [search, setSearch] = useState("");
const [keywordFilter, setKeywordFilter] = useState<string[]>([]);
const dedupedJobs = useMemo(() => dedupeJobs(jobs), [jobs]);
const keywords = useMemo(() => {
const values = Array.from(new Set(dedupedJobs.flatMap((job) => splitJobKeywords(job))));
return values.sort((a, b) => a.localeCompare(b));
}, [dedupedJobs]);
const filteredJobs = useMemo(() => {
const term = normalizeComparableText(search);
return dedupedJobs.filter((job) => {
const currentKeywords = splitJobKeywords(job);
const byKeyword = keywordFilter.length === 0 || keywordFilter.some((keyword) => currentKeywords.includes(keyword));
if (!byKeyword) {
return false;
}
if (!term) {
return true;
}
const text = normalizeComparableText(
[job.titulo, job.empresa, job.local, job.link, job.palavra, ...(job.keywords || [])].join(" "),
);
return text.includes(term);
});
}, [dedupedJobs, search, keywordFilter]);
return {
search,
setSearch,
keywordFilter,
setKeywordFilter,
keywords,
filteredJobs,
};
}