From 00ec0f71fe29f963e80d8bbf232a97ce6a867651 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 9 Mar 2021 19:58:00 +0300 Subject: [PATCH] Format all plugins with prettier --- plugins/addAttributesToSVGElement.js | 65 ++- plugins/addClassesToSVGElement.js | 29 +- plugins/cleanupAttrs.js | 66 ++- plugins/cleanupEnableBackground.js | 100 ++--- plugins/cleanupIDs.js | 363 ++++++++++------- plugins/cleanupListOfValues.js | 205 +++++----- plugins/cleanupNumericValues.js | 124 +++--- plugins/collapseGroups.js | 101 ++--- plugins/convertColors.js | 146 ++++--- plugins/convertEllipseToCircle.js | 36 +- plugins/convertStyleToAttrs.js | 179 ++++---- plugins/convertTransform.js | 472 +++++++++++----------- plugins/minifyStyles.js | 233 ++++++----- plugins/moveElemsAttrsToGroup.js | 135 +++---- plugins/moveGroupAttrsToElems.js | 61 ++- plugins/prefixIds.js | 367 +++++++++-------- plugins/removeAttributesBySelector.js | 24 +- plugins/removeAttrs.js | 132 +++--- plugins/removeComments.js | 10 +- plugins/removeDesc.js | 16 +- plugins/removeDimensions.js | 53 ++- plugins/removeDoctype.js | 10 +- plugins/removeEditorsNSData.js | 63 ++- plugins/removeElementsByAttr.js | 13 +- plugins/removeEmptyText.js | 35 +- plugins/removeMetadata.js | 6 +- plugins/removeNonInheritableGroupAttrs.js | 35 +- plugins/removeOffCanvasPaths.js | 190 ++++----- plugins/removeRasterImages.js | 16 +- plugins/removeScriptElement.js | 6 +- plugins/removeStyleElement.js | 6 +- plugins/removeTitle.js | 6 +- plugins/removeUnknownsAndDefaults.js | 197 ++++----- plugins/removeUnusedNS.js | 150 ++++--- plugins/removeUselessDefs.js | 45 +-- plugins/removeUselessStrokeAndFill.js | 155 ++++--- plugins/removeXMLNS.js | 15 +- plugins/removeXMLProcInst.js | 8 +- plugins/reusePaths.js | 41 +- plugins/sortAttrs.js | 132 +++--- plugins/sortDefsChildren.js | 49 ++- 41 files changed, 2078 insertions(+), 2017 deletions(-) diff --git a/plugins/addAttributesToSVGElement.js b/plugins/addAttributesToSVGElement.js index fed86573e..db846dc05 100644 --- a/plugins/addAttributesToSVGElement.js +++ b/plugins/addAttributesToSVGElement.js @@ -43,47 +43,46 @@ plugins: [ } } ] -` +`; /** * Add attributes to an outer element. Example config: * * @author April Arcus */ -exports.fn = function(data, params) { - if (!params || !(Array.isArray(params.attributes) || params.attribute)) { - console.error(ENOCLS); - return data; - } +exports.fn = function (data, params) { + if (!params || !(Array.isArray(params.attributes) || params.attribute)) { + console.error(ENOCLS); + return data; + } - var attributes = params.attributes || [ params.attribute ], - svg = data.content[0]; + var attributes = params.attributes || [params.attribute], + svg = data.content[0]; - if (svg.isElem('svg')) { - attributes.forEach(function (attribute) { - if (typeof attribute === 'string') { - if (!svg.hasAttr(attribute)) { - svg.addAttr({ - name: attribute, - prefix: '', - local: attribute - }); - } - } else if (typeof attribute === 'object') { - Object.keys(attribute).forEach(function (key) { - if (!svg.hasAttr(key)) { - svg.addAttr({ - name: key, - value: attribute[key], - prefix: '', - local: key - }); - } - }); - } + if (svg.isElem('svg')) { + attributes.forEach(function (attribute) { + if (typeof attribute === 'string') { + if (!svg.hasAttr(attribute)) { + svg.addAttr({ + name: attribute, + prefix: '', + local: attribute, + }); + } + } else if (typeof attribute === 'object') { + Object.keys(attribute).forEach(function (key) { + if (!svg.hasAttr(key)) { + svg.addAttr({ + name: key, + value: attribute[key], + prefix: '', + local: key, + }); + } }); - } - - return data; + } + }); + } + return data; }; diff --git a/plugins/addClassesToSVGElement.js b/plugins/addClassesToSVGElement.js index ef215fc93..c8d195a90 100644 --- a/plugins/addClassesToSVGElement.js +++ b/plugins/addClassesToSVGElement.js @@ -32,19 +32,24 @@ plugins: * * @author April Arcus */ -exports.fn = function(data, params) { - if (!params || !(Array.isArray(params.classNames) && params.classNames.some(String) || params.className)) { - console.error(ENOCLS); - return data; - } - - var classNames = params.classNames || [ params.className ], - svg = data.content[0]; +exports.fn = function (data, params) { + if ( + !params || + !( + (Array.isArray(params.classNames) && params.classNames.some(String)) || + params.className + ) + ) { + console.error(ENOCLS); + return data; + } - if (svg.isElem('svg')) { - svg.class.add.apply(svg.class, classNames); - } + var classNames = params.classNames || [params.className], + svg = data.content[0]; - return data; + if (svg.isElem('svg')) { + svg.class.add.apply(svg.class, classNames); + } + return data; }; diff --git a/plugins/cleanupAttrs.js b/plugins/cleanupAttrs.js index 47e77ac79..6c3257078 100644 --- a/plugins/cleanupAttrs.js +++ b/plugins/cleanupAttrs.js @@ -4,17 +4,18 @@ exports.type = 'perItem'; exports.active = true; -exports.description = 'cleanups attributes from newlines, trailing and repeating spaces'; +exports.description = + 'cleanups attributes from newlines, trailing and repeating spaces'; exports.params = { - newlines: true, - trim: true, - spaces: true + newlines: true, + trim: true, + spaces: true, }; var regNewlinesNeedSpace = /(\S)\r?\n(\S)/g, - regNewlines = /\r?\n/g, - regSpaces = /\s{2,}/g; + regNewlines = /\r?\n/g, + regSpaces = /\s{2,}/g; /** * Cleanup attributes values from newlines, trailing and repeating spaces. @@ -25,32 +26,29 @@ var regNewlinesNeedSpace = /(\S)\r?\n(\S)/g, * * @author Kir Belevich */ -exports.fn = function(item, params) { - - if (item.isElem()) { - - item.eachAttr(function(attr) { - - if (params.newlines) { - // new line which requires a space instead of themselve - attr.value = attr.value.replace(regNewlinesNeedSpace, function(match, p1, p2) { - return p1 + ' ' + p2; - }); - - // simple new line - attr.value = attr.value.replace(regNewlines, ''); - } - - if (params.trim) { - attr.value = attr.value.trim(); - } - - if (params.spaces) { - attr.value = attr.value.replace(regSpaces, ' '); - } - - }); - - } - +exports.fn = function (item, params) { + if (item.isElem()) { + item.eachAttr(function (attr) { + if (params.newlines) { + // new line which requires a space instead of themselve + attr.value = attr.value.replace( + regNewlinesNeedSpace, + function (match, p1, p2) { + return p1 + ' ' + p2; + } + ); + + // simple new line + attr.value = attr.value.replace(regNewlines, ''); + } + + if (params.trim) { + attr.value = attr.value.trim(); + } + + if (params.spaces) { + attr.value = attr.value.replace(regSpaces, ' '); + } + }); + } }; diff --git a/plugins/cleanupEnableBackground.js b/plugins/cleanupEnableBackground.js index 579a1ab21..399c153dc 100644 --- a/plugins/cleanupEnableBackground.js +++ b/plugins/cleanupEnableBackground.js @@ -4,7 +4,8 @@ exports.type = 'full'; exports.active = true; -exports.description = 'remove or cleanup enable-background attribute when possible'; +exports.description = + 'remove or cleanup enable-background attribute when possible'; /** * Remove or cleanup enable-background attr which coincides with a width/height box. @@ -21,64 +22,65 @@ exports.description = 'remove or cleanup enable-background attribute when possib * * @author Kir Belevich */ -exports.fn = function(data) { +exports.fn = function (data) { + var regEnableBackground = /^new\s0\s0\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)$/, + hasFilter = false, + elems = ['svg', 'mask', 'pattern']; - var regEnableBackground = /^new\s0\s0\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)$/, - hasFilter = false, - elems = ['svg', 'mask', 'pattern']; + function checkEnableBackground(item) { + if ( + item.isElem(elems) && + item.hasAttr('enable-background') && + item.hasAttr('width') && + item.hasAttr('height') + ) { + var match = item + .attr('enable-background') + .value.match(regEnableBackground); - function checkEnableBackground(item) { + if (match) { if ( - item.isElem(elems) && - item.hasAttr('enable-background') && - item.hasAttr('width') && - item.hasAttr('height') + item.attr('width').value === match[1] && + item.attr('height').value === match[3] ) { - - var match = item.attr('enable-background').value.match(regEnableBackground); - - if (match) { - if ( - item.attr('width').value === match[1] && - item.attr('height').value === match[3] - ) { - if (item.isElem('svg')) { - item.removeAttr('enable-background'); - } else { - item.attr('enable-background').value = 'new'; - } - } - } - + if (item.isElem('svg')) { + item.removeAttr('enable-background'); + } else { + item.attr('enable-background').value = 'new'; + } } + } } + } - function checkForFilter(item) { - if (item.isElem('filter')) { - hasFilter = true; - } + function checkForFilter(item) { + if (item.isElem('filter')) { + hasFilter = true; } + } - function monkeys(items, fn) { - items.content.forEach(function(item) { - fn(item); + function monkeys(items, fn) { + items.content.forEach(function (item) { + fn(item); - if (item.content) { - monkeys(item, fn); - } - }); - return items; - } - - var firstStep = monkeys(data, function(item) { - checkEnableBackground(item); - if (!hasFilter) { - checkForFilter(item); - } + if (item.content) { + monkeys(item, fn); + } }); + return items; + } - return hasFilter ? firstStep : monkeys(firstStep, function(item) { - //we don't need 'enable-background' if we have no filters - item.removeAttr('enable-background'); - }); + var firstStep = monkeys(data, function (item) { + checkEnableBackground(item); + if (!hasFilter) { + checkForFilter(item); + } + }); + + return hasFilter + ? firstStep + : monkeys(firstStep, function (item) { + //we don't need 'enable-background' if we have no filters + item.removeAttr('enable-background'); + }); }; diff --git a/plugins/cleanupIDs.js b/plugins/cleanupIDs.js index bf541d132..0276ebdec 100644 --- a/plugins/cleanupIDs.js +++ b/plugins/cleanupIDs.js @@ -7,24 +7,74 @@ exports.active = true; exports.description = 'removes unused IDs and minifies used'; exports.params = { - remove: true, - minify: true, - prefix: '', - preserve: [], - preservePrefixes: [], - force: false + remove: true, + minify: true, + prefix: '', + preserve: [], + preservePrefixes: [], + force: false, }; var referencesProps = new Set(require('./_collections').referencesProps), - regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/, - regReferencesHref = /^#(.+?)$/, - regReferencesBegin = /(\w+)\./, - styleOrScript = ['style', 'script'], - generateIDchars = [ - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' - ], - maxIDindex = generateIDchars.length - 1; + regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/, + regReferencesHref = /^#(.+?)$/, + regReferencesBegin = /(\w+)\./, + styleOrScript = ['style', 'script'], + generateIDchars = [ + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i', + 'j', + 'k', + 'l', + 'm', + 'n', + 'o', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + ], + maxIDindex = generateIDchars.length - 1; /** * Remove unused and minify used IDs @@ -35,128 +85,155 @@ var referencesProps = new Set(require('./_collections').referencesProps), * * @author Kir Belevich */ -exports.fn = function(data, params) { - var currentID, - currentIDstring, - IDs = new Map(), - referencesIDs = new Map(), - hasStyleOrScript = false, - preserveIDs = new Set(Array.isArray(params.preserve) ? params.preserve : params.preserve ? [params.preserve] : []), - preserveIDPrefixes = new Set(Array.isArray(params.preservePrefixes) ? params.preservePrefixes : (params.preservePrefixes ? [params.preservePrefixes] : [])), - idValuePrefix = '#', - idValuePostfix = '.'; - - /** - * Bananas! - * - * @param {Array} items input items - * @return {Array} output items - */ - function monkeys(items) { - for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) { - var item = items.content[i]; - - // quit if