import type { TIconsStorageKeys } from '../components/icon/iconsStorage' import { IconsStorage } from '../components/icon/iconsStorage' export interface IIconObj { content: string viewBox: number[] } export async function parseSvg(iconName: TIconsStorageKeys): Promise { if (!IconsStorage.has(iconName)) throw new Error('Invalid icon name!') const inlineIcon = await IconsStorage.get(iconName)() const content = parseContent(inlineIcon.default) const viewBox = parseViewBox(inlineIcon.default) return { content, viewBox } } export function parseContent(inlineIcon: string): string { const indexStart = inlineIcon.indexOf('>') + 1 const indexEnd = inlineIcon.lastIndexOf('<') if (indexStart < 0 || indexEnd < 0) return '' return inlineIcon.slice(indexStart, indexEnd) } export function parseViewBox(inlineIcon: string): number[] { const re = /viewBox=["|'](.*?)["|']/gmi const matched = re.exec(inlineIcon) if (matched?.length && matched.length > 1) { const viewboxString = matched[1] const values = viewboxString.split(/[ ,]/).filter(Boolean) return values.map(Number) } return [] }