mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
refactor: replace uses of for in (#1382)
Replace the uses of `for in` with `for of` and `Object.entries`/`Object.values`/`Object.keys`. One case was further simplified by using a spread.
This commit is contained in:
@ -305,8 +305,8 @@ JSAPI.prototype.renameElem = function(name) {
|
|||||||
|
|
||||||
if (!this.hasAttr()) return false;
|
if (!this.hasAttr()) return false;
|
||||||
|
|
||||||
for (var name in this.attrs) {
|
for (const attr of Object.values(this.attrs)) {
|
||||||
callback.call(context, this.attrs[name]);
|
callback.call(context, attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -324,8 +324,8 @@ JSAPI.prototype.renameElem = function(name) {
|
|||||||
|
|
||||||
if (!this.hasAttr()) return false;
|
if (!this.hasAttr()) return false;
|
||||||
|
|
||||||
for (var name in this.attrs) {
|
for (const attr of Object.values(this.attrs)) {
|
||||||
if (callback.call(context, this.attrs[name])) return true;
|
if (callback.call(context, attr)) return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -93,7 +93,7 @@ module.exports = function(data) {
|
|||||||
elem.style = new CSSStyleDeclaration(elem);
|
elem.style = new CSSStyleDeclaration(elem);
|
||||||
|
|
||||||
if (Object.keys(data.attributes).length) {
|
if (Object.keys(data.attributes).length) {
|
||||||
for (var name in data.attributes) {
|
for (const [name, attr] of Object.entries(data.attributes)) {
|
||||||
|
|
||||||
if (name === 'class') { // has class attribute
|
if (name === 'class') { // has class attribute
|
||||||
elem.class.hasClass();
|
elem.class.hasClass();
|
||||||
@ -105,9 +105,9 @@ module.exports = function(data) {
|
|||||||
|
|
||||||
elem.attrs[name] = {
|
elem.attrs[name] = {
|
||||||
name: name,
|
name: name,
|
||||||
value: data.attributes[name].value,
|
value: attr.value,
|
||||||
prefix: data.attributes[name].prefix,
|
prefix: attr.prefix,
|
||||||
local: data.attributes[name].local
|
local: attr.local
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,13 +54,7 @@ exports.fn = function(ast, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function cloneObject(obj) {
|
function cloneObject(obj) {
|
||||||
var result = {};
|
return {...obj};
|
||||||
|
|
||||||
for (var key in obj) {
|
|
||||||
result[key] = obj[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function findStyleElems(ast) {
|
function findStyleElems(ast) {
|
||||||
@ -149,9 +143,9 @@ function collectUsageData(ast, options) {
|
|||||||
safe = true;
|
safe = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var key in rawData) {
|
for (const [key, data] of Object.entries(rawData)) {
|
||||||
if (shouldFilter(options, key)) {
|
if (shouldFilter(options, key)) {
|
||||||
usageData[key] = Object.keys(rawData[key]);
|
usageData[key] = Object.keys(data);
|
||||||
hasData = true;
|
hasData = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ exports.fn = function(item) {
|
|||||||
|
|
||||||
item.content.forEach(function(g) {
|
item.content.forEach(function(g) {
|
||||||
|
|
||||||
for (var name in intersection) {
|
for (const [name, attr] of Object.entries(intersection)) {
|
||||||
|
|
||||||
if (!allPath && !hasClip || name !== 'transform') {
|
if (!allPath && !hasClip || name !== 'transform') {
|
||||||
|
|
||||||
@ -72,15 +72,15 @@ exports.fn = function(item) {
|
|||||||
if (name === 'transform') {
|
if (name === 'transform') {
|
||||||
if (!hasTransform) {
|
if (!hasTransform) {
|
||||||
if (item.hasAttr('transform')) {
|
if (item.hasAttr('transform')) {
|
||||||
item.attr('transform').value += ' ' + intersection[name].value;
|
item.attr('transform').value += ' ' + attr.value;
|
||||||
} else {
|
} else {
|
||||||
item.addAttr(intersection[name]);
|
item.addAttr(attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
hasTransform = true;
|
hasTransform = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
item.addAttr(intersection[name]);
|
item.addAttr(attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -106,17 +106,17 @@ function intersectInheritableAttrs(a, b) {
|
|||||||
|
|
||||||
var c = {};
|
var c = {};
|
||||||
|
|
||||||
for (var n in a) {
|
for (const [n, attr] of Object.entries(a)) {
|
||||||
if (
|
if (
|
||||||
// eslint-disable-next-line no-prototype-builtins
|
// eslint-disable-next-line no-prototype-builtins
|
||||||
b.hasOwnProperty(n) &&
|
b.hasOwnProperty(n) &&
|
||||||
inheritableAttrs.indexOf(n) > -1 &&
|
inheritableAttrs.indexOf(n) > -1 &&
|
||||||
a[n].name === b[n].name &&
|
attr.name === b[n].name &&
|
||||||
a[n].value === b[n].value &&
|
attr.value === b[n].value &&
|
||||||
a[n].prefix === b[n].prefix &&
|
attr.prefix === b[n].prefix &&
|
||||||
a[n].local === b[n].local
|
attr.local === b[n].local
|
||||||
) {
|
) {
|
||||||
c[n] = a[n];
|
c[n] = attr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,9 +25,7 @@ var collections = require('./_collections'),
|
|||||||
applyGroups = collections.presentationNonInheritableGroupAttrs;
|
applyGroups = collections.presentationNonInheritableGroupAttrs;
|
||||||
|
|
||||||
// collect and extend all references
|
// collect and extend all references
|
||||||
for (var elem in elems) {
|
for (const elem of Object.values(elems)) {
|
||||||
elem = elems[elem];
|
|
||||||
|
|
||||||
if (elem.attrsGroups) {
|
if (elem.attrsGroups) {
|
||||||
elem.attrs = elem.attrs || [];
|
elem.attrs = elem.attrs || [];
|
||||||
|
|
||||||
@ -39,8 +37,8 @@ for (var elem in elems) {
|
|||||||
if (groupDefaults) {
|
if (groupDefaults) {
|
||||||
elem.defaults = elem.defaults || {};
|
elem.defaults = elem.defaults || {};
|
||||||
|
|
||||||
for (var attrName in groupDefaults) {
|
for (const [attrName, attr] of Object.entries(groupDefaults)) {
|
||||||
elem.defaults[attrName] = groupDefaults[attrName];
|
elem.defaults[attrName] = attr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user