import { Jsonified as JsonifiedMap } from "../map.js" ; import { Jsonified as JsonifiedItem } from "../item.js" ; export default abstract class Format { extension ? : string ; mime ? : string ; constructor ( readonly id , readonly label ) { repo . set ( id , this ) ; } abstract to ( data ) ; abstract from ( data ) ; get option ( ) { return new Option ( this . label , this . id ) ; } } export let repo = new Map < string , Format > ( ) ; function getByProperty ( property , value ) { type FormatProp = keyof Format ; let filtered = [ ... repo . values ( ) ] . filter ( format => format [ property as FormatProp ] == value ) ; return ( filtered [ 0 ] || null ) ; } export function getByName ( name ) { let index = name . lastIndexOf ( "." ) ; if ( index == - 1 ) { return null ; } let extension = name . substring ( index + 1 ) . toLowerCase ( ) ; return getByProperty ( "extension" , extension ) ; } export function getByMime ( mime ) { return getByProperty ( "mime" , mime ) ; } export function nl2br ( str ) { return str . replace ( /\n/g , "<br/>" ) ; } export function br2nl ( str ) { return str . replace ( /<br\s*\/?>/g , "\n" ) ; }