2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 23:00:56 +00:00

Add function to trim SVG to utils

This commit is contained in:
Vjacheslav Trushkin 2022-03-21 18:54:25 +02:00
parent 093aed6c0e
commit bd935e4955
4 changed files with 291 additions and 0 deletions

View File

@ -190,6 +190,10 @@
"./lib/svg/size": {
"require": "./lib/svg/size.cjs",
"import": "./lib/svg/size.mjs"
},
"./lib/svg/trim": {
"require": "./lib/svg/trim.cjs",
"import": "./lib/svg/trim.mjs"
}
},
"files": [

View File

@ -40,6 +40,7 @@ export { iconToSVG } from './svg/build';
export { replaceIDs } from './svg/id';
export { calculateSize } from './svg/size';
export { encodeSvgForCss } from './svg/encode-svg-for-css';
export { trimSVG } from './svg/trim';
// Colors
export { colorKeywords } from './colors/keywords';

View File

@ -0,0 +1,17 @@
/**
* Remove whitespace
*/
export function trimSVG(str: string): string {
return (
str
// Replace new line only after one of allowed characters that are not part of common attributes
.replace(/(["';{}}><])\s*\n\s*/g, '$1')
// Keep one space in case it is inside attribute
.replace(/\s*\n\s*/g, ' ')
// Trim attribute values
.replace(/\s+"/g, '"')
.replace(/="\s+/g, '="')
// Trim it
.trim()
);
}

File diff suppressed because one or more lines are too long