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:
parent
093aed6c0e
commit
bd935e4955
@ -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": [
|
||||
|
@ -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';
|
||||
|
17
packages/utils/src/svg/trim.ts
Normal file
17
packages/utils/src/svg/trim.ts
Normal 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()
|
||||
);
|
||||
}
|
269
packages/utils/tests/trim-svg-test.ts
Normal file
269
packages/utils/tests/trim-svg-test.ts
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user