104 lines
2.8 KiB
TypeScript
104 lines
2.8 KiB
TypeScript
/**
|
|
* 포맷 유틸리티 함수
|
|
* @description 포맷 처리에 필요한 유틸리티 함수를 제공합니다.
|
|
*/
|
|
|
|
/**
|
|
* JWT 디코딩
|
|
* @param base64EncodeVal JWT 인코딩 값
|
|
* @returns JWT 디코딩 값
|
|
*/
|
|
export const csrFormatJWT = (base64EncodeVal: string) => {
|
|
const decodeVal = JSON.parse(
|
|
decodeURIComponent(
|
|
window
|
|
.atob(base64EncodeVal)
|
|
.split('')
|
|
.map(function (c) {
|
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
|
|
})
|
|
.join('')
|
|
)
|
|
)
|
|
|
|
return decodeVal
|
|
}
|
|
|
|
/**
|
|
* 배열 또는 객체를 배열로 변환합니다.
|
|
* @param value 변환할 값 (배열, 객체, 또는 undefined/null)
|
|
* @returns 배열
|
|
*/
|
|
export const formatToArray = <T>(
|
|
value: T[] | Record<string, T> | undefined | null
|
|
): T[] => {
|
|
if (!value) return []
|
|
return Array.isArray(value) ? value : Object.values(value)
|
|
}
|
|
|
|
/**
|
|
* URL 경로에서 로케일 접두사를 제거합니다.
|
|
* @param path 경로 문자열
|
|
* @returns 로케일 접두사가 제거된 경로
|
|
*/
|
|
export const formatPathWithoutLocale = (path: string): string => {
|
|
return path.replace(/^\/[a-z]{2}(?=\/|$)/, '') || '/'
|
|
}
|
|
|
|
/**
|
|
* 리소스 경로를 완전한 호스트 URL로 변환합니다.
|
|
* @param path 리소스 경로
|
|
* @returns 완전한 리소스 URL
|
|
*/
|
|
export const formatPathHost = (
|
|
path: string,
|
|
options: { imageType?: 'public' | 'cdn'; isSkipHost?: boolean } = {}
|
|
): string => {
|
|
if (!path) return ''
|
|
|
|
if (/^(https?:\/\/|www\.)/.test(path)) return path
|
|
|
|
const runtimeConfig = useRuntimeConfig()
|
|
const { staticUrl, assetsUrl } = runtimeConfig.public
|
|
const { imageType = 'cdn', isSkipHost = false } = options
|
|
|
|
const isDevelopment = import.meta.dev
|
|
const isTypeCdn = imageType === 'cdn'
|
|
|
|
if (isSkipHost) return path
|
|
|
|
// 개발 환경일 때는 루트 경로 생략
|
|
if (!isTypeCdn && isDevelopment) return path
|
|
|
|
// 게임/공통 여부에 따른 경로 결정
|
|
const basePath = isTypeCdn ? staticUrl : assetsUrl
|
|
|
|
return `${basePath}${path}`
|
|
}
|
|
|
|
/**
|
|
* snake_case 문자열을 Title Case로 변환합니다.
|
|
* 짧은 단어(pc, stove 등)는 전체 대문자로 표시됩니다.
|
|
* @param str 변환할 문자열 (예: "google_play", "pc", "stove")
|
|
* @returns Title Case로 변환된 문자열 (예: "Google Play", "PC", "STOVE")
|
|
*/
|
|
export const formatSnakeToTitle = (str: string): string => {
|
|
if (!str) return ''
|
|
|
|
// 전체 대문자로 표시할 단어 목록
|
|
const uppercaseWords = ['pc', 'stove']
|
|
|
|
return str
|
|
.split('_')
|
|
.map(word => {
|
|
const lowerWord = word.toLowerCase()
|
|
// 특정 단어는 전체 대문자로 표시
|
|
if (uppercaseWords.includes(lowerWord)) {
|
|
return word.toUpperCase()
|
|
}
|
|
// 일반 단어는 첫 글자만 대문자
|
|
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
|
|
})
|
|
.join(' ')
|
|
}
|