-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(util function): 건물 정보와 관련된 유틸 함수 구현
- 건물 검색 결과, 건물 정보 확인, 길찾기 url, 층별 정보 문자열
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { PKNU_BUILDINGS } from '@constants/pknu-map'; | ||
import { Floor } from '@type/building-info'; | ||
import { BuildingType, Location } from '@type/map'; | ||
|
||
export const getBuildingSearchResult = ( | ||
keyword: string, | ||
): [BuildingType, number] | undefined => { | ||
const formattedKeyword = keyword.replaceAll(' ', '').toUpperCase(); | ||
|
||
for (const buildingType of Object.keys(PKNU_BUILDINGS)) { | ||
const index = PKNU_BUILDINGS[ | ||
buildingType as BuildingType | ||
].buildings.findIndex( | ||
(PKNU_BUILDING) => | ||
PKNU_BUILDING.buildingName === formattedKeyword || | ||
PKNU_BUILDING.buildingNumber === formattedKeyword, | ||
); | ||
|
||
if (index !== -1) return [buildingType as BuildingType, index]; | ||
} | ||
|
||
return; | ||
}; | ||
|
||
export const getBuildingInfo = (buildingNumber: string) => { | ||
const buildingTypes = Object.keys(PKNU_BUILDINGS) as BuildingType[]; | ||
|
||
for (const type of buildingTypes) { | ||
for (const building of PKNU_BUILDINGS[type].buildings) { | ||
if (building.buildingNumber !== buildingNumber) continue; | ||
|
||
return { | ||
buildingCode: building.buildingCode, | ||
buildingName: building.buildingName, | ||
color: PKNU_BUILDINGS[type].activeColor, | ||
latlng: building.latlng, | ||
}; | ||
} | ||
} | ||
}; | ||
|
||
export const forrmatRoutingUrl = ( | ||
userLocation: Location | null, | ||
latlng: [number, number], | ||
buildingName: string, | ||
): string => { | ||
if (!userLocation) return ''; | ||
|
||
const { LAT, LNG } = userLocation; | ||
const [lat, lng] = latlng; | ||
|
||
const kakaoMapAppURL = `kakaomap://route?sp=${LAT},${LNG}&ep=${lat},${lng}`; | ||
const kakaoMapWebURL = `https://map.kakao.com/link/from/현위치,${LAT},${LNG}/to/${buildingName},${lat},${lng}`; | ||
const isKakaoMapInstalled = /KAKAOMAP/i.test(navigator.userAgent); | ||
const openUrl = isKakaoMapInstalled ? kakaoMapAppURL : kakaoMapWebURL; | ||
|
||
return openUrl; | ||
}; | ||
|
||
export const formatFloorTitle = (type: Floor, floor: string): string => { | ||
if (type === 'basement') return `B${floor}F`; | ||
if (type === 'ground') return `${floor}F`; | ||
if (type === 'rooftop') return `R${floor}F`; | ||
return ''; | ||
}; |