import * as path from 'path' ; import { OptimizedSvg , OriginalColor } from './types' ; export default { compile ( content , data ) { return content . replace ( /\${(\w+)}/gi , function ( match , name ) { return data [ name ] ? data [ name ] : '' ; } ) ; } , getViewBox ( svgoResult ) { const viewBoxMatch = svgoResult . data . match ( /viewBox="([-\d.]+\s[-\d.]+\s[-\d.]+\s[-\d.]+)"/ ) ; let viewBox = '0 0 200 200' ; if ( viewBoxMatch && viewBoxMatch . length > 1 ) { viewBox = viewBoxMatch [ 1 ] ; } else if ( svgoResult . info . height && svgoResult . info . width ) { viewBox = `0 0 ${ svgoResult . info . width } ${ svgoResult . info . height }` ; } return viewBox ; } , addPid ( content ) { const shapeReg = /<(path|rect|circle|polygon|line|polyline|ellipse)\s/gi ; let id = 0 ; content = content . replace ( shapeReg , function ( match ) { return match + `pid="${ id ++ }" ` ; } ) ; return content ; } , renameStyle ( content ) { const styleShaeReg = /<(path|rect|circle|polygon|line|polyline|g|ellipse).+>/gi ; const styleReg = /fill="|stroke="/gi ; content = content . replace ( styleShaeReg , function ( shape ) { return shape . replace ( styleReg , function ( styleName ) { return '_' + styleName ; } ) ; } ) ; return content ; } , changeId ( content , filePath , name , idSep = '_' ) { const idReg = /svgiconid(\w+)/g ; content = content . replace ( idReg , function ( match , elId ) { return `svgiconid${ idSep }${ filePath . replace ( /[\\/]/g , idSep ) }${ name }${ idSep }${ elId }` ; } ) ; return content ; } , getFilePath ( svgRootPath , filename ) { let filePath = '' ; const rootPaths = Array . isArray ( svgRootPath ) ? svgRootPath : [ svgRootPath ] ; rootPaths . forEach ( ( rp ) => { filename = filename . replace ( rp , '' ) ; } ) ; filePath = filename . replace ( path . basename ( filename ) , '' ) ; if ( /^[/\\]/ . test ( filePath ) ) { filePath = filePath . substr ( 1 ) ; } return filePath . replace ( /\\/g , '/' ) ; } , /** get original colors */ getOriginalColors ( content ) { const reg = /(fill|stroke)="([\w,#\s\\()-_]+)"/gi ; const colors = [ ] ; content . replace ( reg , ( match , prop , color ) => { colors . push ( { type : prop , color , } ) ; return match ; } ) ; return colors ; } , /** get original colors */ getStopColors ( content ) { const reg = /stop-color="([\w,#\s'()-_]+)"/gi ; const colors = [ ] ; content . replace ( reg , ( match , color ) => { colors . push ( color ) ; return match ; } ) ; return colors ; } , } ;