-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (31 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const PLUGINS = require('./plugins/');
const {stringify, parse} = require('./util');
// doctype and XML processing instruction are ignored since one svg element is processed and returned
// comments and any non element are also ignored, see utils#stringify
/*
Main function
args:
- svgStr (svg string or svg Element)
- optional configs in this format:
{
decimals: 2, // or precision: 1e2, it's converted in this pow10 format to be used in utils#round
iterations: 3 // max number of iterations, until svg size decreases
indent: '' // by default, for minifying, if you put any non-empty string, it will add line returns and indent with it
plugins: {
pluginName: {param1: param1Value, param2: param2Value},
otherPluginName: false // (if false, this plugin will be disabled)
}
}
returns optimized svgString
*/
module.exports = function(svgStr, {decimals, precision=Math.pow(10, decimals||3), indent='', iterations=5, plugins={}}={}) {
const svg = typeof svgStr==='string' || svgStr instanceof Buffer ? parse(svgStr.toString()) : svgStr;
for (const name in PLUGINS) {
const plugin = PLUGINS[name];
const pluginOpts = plugins[name];
if (plugin.active && pluginOpts!==false && pluginOpts!==null) {
plugin(svg, Object.assign({precision}, pluginOpts||{}));
}
}
return stringify(svg, indent);
}