server/src/IconService.ts

51 lines
No EOL
1.3 KiB
TypeScript

import * as faSolid from '@fortawesome/free-solid-svg-icons'
import * as faBrands from '@fortawesome/free-brands-svg-icons'
import * as faRegular from '@fortawesome/free-regular-svg-icons'
import { IconDefinition } from '@fortawesome/fontawesome-common-types'
export interface IconInterface {
id: string,
width: number,
height: number,
path: string
}
export class IconService {
static snakeToCamel(str: string): string {
return str.replace(
/([-_][a-z])/g,
group => group.toUpperCase().replace('-', '').replace('_', '')
)
}
static getIcon(id: string): IconInterface|null {
let components: string[] = id.split(id.indexOf('_') !== -1 ? '_' : ' ')
if (components.length < 2) {
return null
}
let iconSet: any = {}
switch (components[0]) {
case 'fas':
iconSet = faSolid
break
case 'fab':
iconSet = faBrands
break
case 'far':
iconSet = faRegular
break
}
let iconName: string = this.snakeToCamel(components[1])
let iconVal: IconDefinition|undefined = iconSet[iconName]
if (iconVal === undefined) {
return null
}
return {
id: components[0] + ' ' + components[1],
width: iconVal.icon[0],
height: iconVal.icon[1],
path: iconVal.icon[4] as string
}
}
}