Skip to content

Commit

Permalink
Rename cleanupIDs to cleanupIds for consistency with prefixIds
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Oct 23, 2022
1 parent bc07c48 commit 6295c60
Show file tree
Hide file tree
Showing 25 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Default preset includes the following list of plugins:
- mergeStyles
- inlineStyles
- minifyStyles
- cleanupIDs
- cleanupIds
- removeUselessDefs
- cleanupNumericValues
- convertColors
Expand Down Expand Up @@ -209,7 +209,7 @@ const config = await loadConfig(configFile, cwd);
| [removeUselessStrokeAndFill](https://github.com/svg/svgo/blob/main/plugins/removeUselessStrokeAndFill.js) | remove useless `stroke` and `fill` attributes | `enabled` |
| [removeUnusedNS](https://github.com/svg/svgo/blob/main/plugins/removeUnusedNS.js) | remove unused namespaces declaration | `enabled` |
| [prefixIds](https://github.com/svg/svgo/blob/main/plugins/prefixIds.js) | prefix IDs and classes with the SVG filename or an arbitrary string | `disabled` |
| [cleanupIDs](https://github.com/svg/svgo/blob/main/plugins/cleanupIDs.js) | remove unused and minify used IDs | `enabled` |
| [cleanupIds](https://github.com/svg/svgo/blob/main/plugins/cleanupIds.js) | remove unused and minify used IDs | `enabled` |
| [cleanupNumericValues](https://github.com/svg/svgo/blob/main/plugins/cleanupNumericValues.js) | round numeric values to the fixed precision, remove default `px` units | `enabled` |
| [cleanupListOfValues](https://github.com/svg/svgo/blob/main/plugins/cleanupListOfValues.js) | round numeric values in attributes that take a list of numbers (like `viewBox` or `enable-background`) | `disabled` |
| [moveElemsAttrsToGroup](https://github.com/svg/svgo/blob/main/plugins/moveElemsAttrsToGroup.js) | move elements' attributes to their enclosing group | `enabled` |
Expand Down
2 changes: 1 addition & 1 deletion lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports.builtin = [
require('../plugins/addClassesToSVGElement.js'),
require('../plugins/cleanupAttrs.js'),
require('../plugins/cleanupEnableBackground.js'),
require('../plugins/cleanupIDs.js'),
require('../plugins/cleanupIds.js'),
require('../plugins/cleanupListOfValues.js'),
require('../plugins/cleanupNumericValues.js'),
require('../plugins/collapseGroups.js'),
Expand Down
2 changes: 1 addition & 1 deletion lib/svgo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type CustomPlugin = {
type DefaultPlugin =
| Usage<typeof import('../plugins/cleanupAttrs.js')>
| Usage<typeof import('../plugins/cleanupEnableBackground.js')>
| Usage<typeof import('../plugins/cleanupIDs.js')>
| Usage<typeof import('../plugins/cleanupIds.js')>
| Usage<typeof import('../plugins/cleanupNumericValues.js')>
| Usage<typeof import('../plugins/collapseGroups.js')>
| Usage<typeof import('../plugins/convertColors.js')>
Expand Down
58 changes: 29 additions & 29 deletions plugins/cleanupIDs.js → plugins/cleanupIds.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
const { visitSkip } = require('../lib/xast.js');
const { referencesProps } = require('./_collections.js');

exports.name = 'cleanupIDs';
exports.name = 'cleanupIds';
exports.description = 'removes unused IDs and minifies used';

const regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/;
const regReferencesHref = /^#(.+?)$/;
const regReferencesBegin = /(\w+)\./;
const generateIDchars = [
const generateIdChars = [
'a',
'b',
'c',
Expand Down Expand Up @@ -67,7 +67,7 @@ const generateIDchars = [
'Y',
'Z',
];
const maxIDindex = generateIDchars.length - 1;
const maxIdIndex = generateIdChars.length - 1;

/**
* Check if an ID starts with any one of a list of strings.
Expand All @@ -86,35 +86,35 @@ const hasStringPrefix = (string, prefixes) => {
/**
* Generate unique minimal ID.
*
* @type {(currentID: null | Array<number>) => Array<number>}
* @type {(currentId: null | Array<number>) => Array<number>}
*/
const generateID = (currentID) => {
if (currentID == null) {
const generateId = (currentId) => {
if (currentId == null) {
return [0];
}
currentID[currentID.length - 1] += 1;
for (let i = currentID.length - 1; i > 0; i--) {
if (currentID[i] > maxIDindex) {
currentID[i] = 0;
if (currentID[i - 1] !== undefined) {
currentID[i - 1]++;
currentId[currentId.length - 1] += 1;
for (let i = currentId.length - 1; i > 0; i--) {
if (currentId[i] > maxIdIndex) {
currentId[i] = 0;
if (currentId[i - 1] !== undefined) {
currentId[i - 1]++;
}
}
}
if (currentID[0] > maxIDindex) {
currentID[0] = 0;
currentID.unshift(0);
if (currentId[0] > maxIdIndex) {
currentId[0] = 0;
currentId.unshift(0);
}
return currentID;
return currentId;
};

/**
* Get string from generated ID array.
*
* @type {(arr: Array<number>) => string}
*/
const getIDstring = (arr) => {
return arr.map((i) => generateIDchars[i]).join('');
const getIdString = (arr) => {
return arr.map((i) => generateIdChars[i]).join('');
};

/**
Expand All @@ -139,10 +139,10 @@ exports.fn = (_root, params) => {
preservePrefixes = [],
force = false,
} = params;
const preserveIDs = new Set(
const preserveIds = new Set(
Array.isArray(preserve) ? preserve : preserve ? [preserve] : []
);
const preserveIDPrefixes = Array.isArray(preservePrefixes)
const preserveIdPrefixes = Array.isArray(preservePrefixes)
? preservePrefixes
: preservePrefixes
? [preservePrefixes]
Expand Down Expand Up @@ -240,11 +240,11 @@ exports.fn = (_root, params) => {
* @type {(id: string) => boolean}
**/
const isIdPreserved = (id) =>
preserveIDs.has(id) || hasStringPrefix(id, preserveIDPrefixes);
preserveIds.has(id) || hasStringPrefix(id, preserveIdPrefixes);
/**
* @type {null | Array<number>}
*/
let currentID = null;
let currentId = null;
for (const [id, refs] of referencesById) {
const node = nodeById.get(id);
if (node != null) {
Expand All @@ -253,24 +253,24 @@ exports.fn = (_root, params) => {
/**
* @type {null | string}
*/
let currentIDString = null;
let currentIdString = null;
do {
currentID = generateID(currentID);
currentIDString = getIDstring(currentID);
} while (isIdPreserved(currentIDString));
node.attributes.id = currentIDString;
currentId = generateId(currentId);
currentIdString = getIdString(currentId);
} while (isIdPreserved(currentIdString));
node.attributes.id = currentIdString;
for (const { element, name, value } of refs) {
if (value.includes('#')) {
// replace id in href and url()
element.attributes[name] = value.replace(
`#${id}`,
`#${currentIDString}`
`#${currentIdString}`
);
} else {
// replace id in begin attribute
element.attributes[name] = value.replace(
`${id}.`,
`${currentIDString}.`
`${currentIdString}.`
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/preset-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const cleanupAttrs = require('./cleanupAttrs.js');
const mergeStyles = require('./mergeStyles.js');
const inlineStyles = require('./inlineStyles.js');
const minifyStyles = require('./minifyStyles.js');
const cleanupIDs = require('./cleanupIDs.js');
const cleanupIds = require('./cleanupIds.js');
const removeUselessDefs = require('./removeUselessDefs.js');
const cleanupNumericValues = require('./cleanupNumericValues.js');
const convertColors = require('./convertColors.js');
Expand Down Expand Up @@ -50,7 +50,7 @@ const presetDefault = createPreset({
mergeStyles,
inlineStyles,
minifyStyles,
cleanupIDs,
cleanupIds,
removeUselessDefs,
cleanupNumericValues,
convertColors,
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6295c60

Please sign in to comment.