Spaces:
Running
Running
File size: 3,979 Bytes
bd28470 | 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 | import { InsertCompany } from "../../shared/supabase/schema";
import { ScrapedCompany } from "./web-scraper";
import { LinkedInCompanyData } from "./linkedin-scraper";
import { SerperResult } from "../providers/serper";
/**
* Normalizes raw data from multiple sources into a single canonical Company record.
* Priority: LinkedIn > Website > Serper snippet
*/
export function normalizeCompany(
serperResult: SerperResult,
website: ScrapedCompany,
linkedin: LinkedInCompanyData | null,
region: string,
source: string
): InsertCompany {
const name =
linkedin?.name ??
website.name ??
cleanTitle(serperResult.title);
const description =
linkedin?.description ??
website.description ??
serperResult.snippet;
const employeeCount =
linkedin?.employeeCount ??
website.employeeCount ??
null;
const employeeRange =
linkedin?.employeeRange ??
website.employeeRange ??
estimateRange(employeeCount);
const industry =
linkedin?.industry ??
website.industry ??
null;
const country =
linkedin?.headquarters
? extractCountry(linkedin.headquarters)
: regionToCountry(region);
const linkedinUrl =
linkedin !== null
? extractLinkedInCompanyUrl(serperResult.link) ?? website.linkedinUrl
: website.linkedinUrl;
const growthSignals = buildGrowthSignals(website, linkedin);
return {
domain: website.domain,
name: name ?? "Unknown",
industry,
employee_count: employeeCount,
employee_range: employeeRange,
description: description?.slice(0, 1000) ?? null,
website_url: `https://${website.domain}`,
linkedin_url: linkedinUrl ?? null,
country,
region,
tech_stack: website.techStack,
growth_signals: growthSignals,
raw_data: {
serper_title: serperResult.title,
serper_snippet: serperResult.snippet,
serper_link: serperResult.link,
},
source,
status: "discovered",
};
}
// βββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββ
function cleanTitle(title: string): string {
return title
.split(/[|\-β]/)[0]
.replace(/\b(home|official|website|welcome to)\b/gi, "")
.trim();
}
function estimateRange(count: number | null): string | null {
if (!count) return null;
if (count < 50) return "10-49";
if (count < 100) return "50-99";
if (count < 200) return "100-199";
if (count < 500) return "200-499";
if (count < 1000) return "500-999";
return "1000+";
}
function extractCountry(headquarters: string): string | null {
const parts = headquarters.split(",");
return parts[parts.length - 1]?.trim() ?? null;
}
function regionToCountry(region: string): string {
const map: Record<string, string> = {
US: "United States", UK: "United Kingdom",
AU: "Australia", UAE: "United Arab Emirates",
SA: "Saudi Arabia", SG: "Singapore",
};
return map[region] ?? region;
}
function extractLinkedInCompanyUrl(url: string): string | null {
const match = url.match(/https?:\/\/(www\.)?linkedin\.com\/company\/[^/?#]+/);
return match ? match[0] : null;
}
function buildGrowthSignals(
website: ScrapedCompany,
linkedin: LinkedInCompanyData | null
): object[] {
const signals: object[] = [];
// AI-related job postings
website.jobPostings
.filter((j) => j.hasAiSignal)
.forEach((j) => {
signals.push({
type: "job_posting",
content: j.title,
source_url: j.url,
ai_related: true,
detected_at: new Date().toISOString(),
});
});
// Recent LinkedIn posts
(linkedin?.recentPosts ?? []).forEach((post) => {
signals.push({
type: "social_post",
content: post.slice(0, 200),
ai_related: /automat|ai\b|machine learning|digital/i.test(post),
detected_at: new Date().toISOString(),
});
});
return signals.slice(0, 10); // cap at 10 signals
}
|