@@ -17,11 +17,26 @@ type VisitorPoiSource = Pick<MuseumPoi, 'id' | 'name' | 'floorId' | 'floorLabel'
|
||||
primaryCategory?: { label?: string }
|
||||
}
|
||||
|
||||
const locationHintFor = (poi: VisitorPoiSource) => [
|
||||
poi.floorLabel?.trim(),
|
||||
poi.hallName?.trim(),
|
||||
poi.primaryCategory?.label?.trim()
|
||||
].filter((value, index, values) => Boolean(value) && values.indexOf(value) === index).join(' · ')
|
||||
const normalizedComparisonText = (value?: string) => normalizeDisplayName(value || '')
|
||||
.replace(/[\s·、,,。.!!??]/g, '')
|
||||
.toLowerCase()
|
||||
|
||||
const locationHintFor = (poi: VisitorPoiSource, baseName: string) => {
|
||||
const baseKey = normalizedComparisonText(baseName)
|
||||
const values = [
|
||||
poi.floorLabel?.trim(),
|
||||
poi.hallName?.trim(),
|
||||
poi.primaryCategory?.label?.trim()
|
||||
].filter((value): value is string => Boolean(value))
|
||||
|
||||
const seen = new Set<string>([baseKey])
|
||||
return values.filter((value) => {
|
||||
const key = normalizedComparisonText(value)
|
||||
if (!key || seen.has(key)) return false
|
||||
seen.add(key)
|
||||
return true
|
||||
}).join(' · ')
|
||||
}
|
||||
|
||||
export const createVisitorPoiPresentations = <TPoi extends VisitorPoiSource>(pois: TPoi[]) => {
|
||||
const baseNames = new Map<string, number>()
|
||||
@@ -31,13 +46,20 @@ export const createVisitorPoiPresentations = <TPoi extends VisitorPoiSource>(poi
|
||||
})
|
||||
|
||||
const duplicateIndexes = new Map<string, number>()
|
||||
const duplicateLocationCounts = new Map<string, number>()
|
||||
return new Map(pois.map((poi) => {
|
||||
const baseName = normalizeDisplayName(poi.name || '') || '未命名点位'
|
||||
const duplicateCount = baseNames.get(baseName) || 0
|
||||
const nextIndex = (duplicateIndexes.get(baseName) || 0) + 1
|
||||
duplicateIndexes.set(baseName, nextIndex)
|
||||
const locationHint = locationHintFor(poi)
|
||||
const disambiguation = duplicateCount > 1 ? (locationHint || `设施 ${nextIndex}`) : locationHint
|
||||
const locationHint = locationHintFor(poi, baseName)
|
||||
const duplicateLocationKey = `${baseName}|${locationHint}`
|
||||
const locationIndex = (duplicateLocationCounts.get(duplicateLocationKey) || 0) + 1
|
||||
duplicateLocationCounts.set(duplicateLocationKey, locationIndex)
|
||||
const sameLocation = duplicateCount > 1 && locationIndex > 1
|
||||
const disambiguation = duplicateCount > 1
|
||||
? `${locationHint || '设施'}${sameLocation ? ` ${locationIndex}` : ''}`
|
||||
: locationHint
|
||||
|
||||
return [poi.id, {
|
||||
displayName: duplicateCount > 1 ? `${baseName} · ${disambiguation}` : baseName,
|
||||
|
||||
Reference in New Issue
Block a user