The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
normalizeString when importing csv
Steven Tey19f6d204b98b
diff --git a/apps/web/app/api/cron/import/csv/route.ts b/apps/web/app/api/cron/import/csv/route.tsindex 4049a9b53c..69c13c96f2 100644--- a/apps/web/app/api/cron/import/csv/route.ts+++ b/apps/web/app/api/cron/import/csv/route.ts@@ -15,6 +15,7 @@ import {DUB_DOMAINS_ARRAY,getPrettyUrl,log,+ normalizeString,parseDateTime,} from "@dub/utils";import { NextResponse } from "next/server";@@ -34,11 +35,18 @@ export async function POST(req: Request) {if (!id || !url) throw new Error("Missing ID or URL for the import file");const mapper = (row: Record<string, string>) => {- const linkUrl = getPrettyUrl(row[mapping.link]);+ const actualKey = normalizeString(mapping.link);+ const linkUrl = actualKey ? getPrettyUrl(row[actualKey]) : "";return {...Object.fromEntries(- Object.entries(mapping).map(([key, value]) => [key, row[value]]),+ Object.entries(mapping).map(([key, value]) => {+ // Find the actual key for each mapped field+ const csvKey = Object.keys(row).find(+ (k) => normalizeString(k) === normalizeString(value),+ );+ return [key, csvKey ? row[csvKey] : undefined];+ }),),domain: linkUrl.split("/")[0],// domain.com/path/to/page => path/to/pagediff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.tsindex c72f3ccc55..ad6c9439c2 100644--- a/packages/utils/src/functions/index.ts+++ b/packages/utils/src/functions/index.ts@@ -18,6 +18,7 @@ export * from "./link-constructor";export * from "./log";export * from "./nanoid";export * from "./nformatter";+export * from "./normalize-string";export * from "./pluralize";export * from "./punycode";export * from "./random-value";diff --git a/packages/utils/src/functions/normalize-string.ts b/packages/utils/src/functions/normalize-string.tsnew file mode 100644index 0000000000..a88c803017--- /dev/null+++ b/packages/utils/src/functions/normalize-string.ts@@ -0,0 +1,26 @@+export const normalizeString = (key: string) => {+ const original = key;+ const normalized = key+ .replace(/^\uFEFF/, "")+ .replace(/^\uFFFE/, "")+ .replace(/^\uEFBBBF/, "")+ .replace(/^\u0000\uFEFF/, "")+ .replace(/^\uFFFE0000/, "")+ .replace(/^\u2028/, "")+ .replace(/^\u2029/, "")+ .trim();++ if (original !== normalized) {+ console.log(`Normalized key: "${original}" -> "${normalized}"`);+ console.log(+ "Original char codes:",+ Array.from(original).map((c) => c.charCodeAt(0)),+ );+ console.log(+ "Normalized char codes:",+ Array.from(normalized).map((c) => c.charCodeAt(0)),+ );+ }++ return normalized;+};