diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-10-14 15:05:55 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-10-14 15:05:55 +0800 |
| commit | c06f7ee0a40255214c477e13d9b956a7e07c2900 (patch) | |
| tree | f5982a568ef8fab865d0142e71c7494fea3e4c1b /node_modules/svgo/plugins/cleanupNumericValues.js | |
| parent | 3fd9400d4dccc25aa29b5702216732e0b7542073 (diff) | |
postcss,(plugins): nanocss, postcss-preset-env
Diffstat (limited to 'node_modules/svgo/plugins/cleanupNumericValues.js')
| -rw-r--r-- | node_modules/svgo/plugins/cleanupNumericValues.js | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/node_modules/svgo/plugins/cleanupNumericValues.js b/node_modules/svgo/plugins/cleanupNumericValues.js new file mode 100644 index 00000000..6a15dddd --- /dev/null +++ b/node_modules/svgo/plugins/cleanupNumericValues.js @@ -0,0 +1,106 @@ +'use strict'; + +const { removeLeadingZero } = require('../lib/svgo/tools'); + +exports.name = 'cleanupNumericValues'; +exports.description = + 'rounds numeric values to the fixed precision, removes default ‘px’ units'; + +const regNumericValues = + /^([-+]?\d*\.?\d+([eE][-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/; + +const absoluteLengths = { + // relative to px + cm: 96 / 2.54, + mm: 96 / 25.4, + in: 96, + pt: 4 / 3, + pc: 16, + px: 1, +}; + +/** + * Round numeric values to the fixed precision, + * remove default 'px' units. + * + * @author Kir Belevich + * + * @type {import('./plugins-types').Plugin<'cleanupNumericValues'>} + */ +exports.fn = (_root, params) => { + const { + floatPrecision = 3, + leadingZero = true, + defaultPx = true, + convertToPx = true, + } = params; + + return { + element: { + enter: (node) => { + if (node.attributes.viewBox != null) { + const nums = node.attributes.viewBox.split(/\s,?\s*|,\s*/g); + node.attributes.viewBox = nums + .map((value) => { + const num = Number(value); + return Number.isNaN(num) + ? value + : Number(num.toFixed(floatPrecision)); + }) + .join(' '); + } + + for (const [name, value] of Object.entries(node.attributes)) { + // The `version` attribute is a text string and cannot be rounded + if (name === 'version') { + continue; + } + + const match = value.match(regNumericValues); + + // if attribute value matches regNumericValues + if (match) { + // round it to the fixed precision + let num = Number(Number(match[1]).toFixed(floatPrecision)); + /** + * @type {any} + */ + let matchedUnit = match[3] || ''; + /** + * @type{'' | keyof typeof absoluteLengths} + */ + let units = matchedUnit; + + // convert absolute values to pixels + if (convertToPx && units !== '' && units in absoluteLengths) { + const pxNum = Number( + (absoluteLengths[units] * Number(match[1])).toFixed( + floatPrecision + ) + ); + if (pxNum.toString().length < match[0].length) { + num = pxNum; + units = 'px'; + } + } + + // and remove leading zero + let str; + if (leadingZero) { + str = removeLeadingZero(num); + } else { + str = num.toString(); + } + + // remove default 'px' units + if (defaultPx && units === 'px') { + units = ''; + } + + node.attributes[name] = str + units; + } + } + }, + }, + }; +}; |
