Files
Perplexica/src/lib/utils.ts
eligrinfeld 372943801d Refactor business search functionality:
- Add utility functions for business ID generation
- Improve database service with proper types
- Fix type safety issues in search implementation
- Add caching layer for search results
2025-01-04 17:16:22 -07:00

39 lines
1.1 KiB
TypeScript

import crypto from 'crypto';
interface BusinessIdentifier {
title?: string;
name?: string;
phone?: string;
address?: string;
url?: string;
website?: string;
}
export function generateBusinessId(business: BusinessIdentifier): string {
const components = [
business.title || business.name,
business.phone,
business.address,
business.url || business.website
].filter(Boolean);
const hash = crypto.createHash('md5')
.update(components.join('|'))
.digest('hex');
return `hash_${hash}`;
}
export function extractPlaceIdFromUrl(url: string): string | null {
try {
// Match patterns like:
// https://www.google.com/maps/place/.../.../data=!3m1!4b1!4m5!3m4!1s0x876c7ed0cb78d6d3:0x2cd0c4490736f7c!8m2!
// https://maps.google.com/maps?q=...&ftid=0x876c7ed0cb78d6d3:0x2cd0c4490736f7c
const placeIdRegex = /[!\/]([0-9a-f]{16}:[0-9a-f]{16})/i;
const match = url.match(placeIdRegex);
return match ? match[1] : null;
} catch (error) {
console.warn('Error extracting place ID from URL:', error);
return null;
}
}