Files
web-temp/layers/utils/formatUtil.ts

78 lines
2.0 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?: 'common' | 'game'; isSkipHost?: boolean } = {}
): string => {
if (!path) return ''
if (/^(https?:\/\/|www\.)/.test(path)) return path
const runtimeConfig = useRuntimeConfig()
const { staticUrl, assetsUrl } = runtimeConfig.public
const { imageType = 'game', isSkipHost = false } = options
const isDevelopment = import.meta.dev
const isTypeGame = imageType === 'game'
if (isSkipHost) return path
// 개발 환경일 때는 루트 경로 생략
if (!isTypeGame && isDevelopment) return path
// 게임/공통 여부에 따른 경로 결정
const basePath = isTypeGame ? staticUrl : assetsUrl
return `${basePath}${path}`
}