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;
|
||||
|
||||
for (var name in this.attrs) {
|
||||
callback.call(context, this.attrs[name]);
|
||||
for (const attr of Object.values(this.attrs)) {
|
||||
callback.call(context, attr);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -324,8 +324,8 @@ JSAPI.prototype.renameElem = function(name) {
|
||||
|
||||
if (!this.hasAttr()) return false;
|
||||
|
||||
for (var name in this.attrs) {
|
||||
if (callback.call(context, this.attrs[name])) return true;
|
||||
for (const attr of Object.values(this.attrs)) {
|
||||
if (callback.call(context, attr)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -93,7 +93,7 @@ module.exports = function(data) {
|
||||
elem.style = new CSSStyleDeclaration(elem);
|
||||
|
||||
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
|
||||
elem.class.hasClass();
|
||||
@ -105,9 +105,9 @@ module.exports = function(data) {
|
||||
|
||||
elem.attrs[name] = {
|
||||
name: name,
|
||||
value: data.attributes[name].value,
|
||||
prefix: data.attributes[name].prefix,
|
||||
local: data.attributes[name].local
|
||||
value: attr.value,
|
||||
prefix: attr.prefix,
|
||||
local: attr.local
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -54,13 +54,7 @@ exports.fn = function(ast, options) {
|
||||
};
|
||||
|
||||
function cloneObject(obj) {
|
||||
var result = {};
|
||||
|
||||
for (var key in obj) {
|
||||
result[key] = obj[key];
|
||||
}
|
||||
|
||||
return result;
|
||||
return {...obj};
|
||||
}
|
||||
|
||||
function findStyleElems(ast) {
|
||||
@ -149,9 +143,9 @@ function collectUsageData(ast, options) {
|
||||
safe = true;
|
||||
}
|
||||
|
||||
for (var key in rawData) {
|
||||
for (const [key, data] of Object.entries(rawData)) {
|
||||
if (shouldFilter(options, key)) {
|
||||
usageData[key] = Object.keys(rawData[key]);
|
||||
usageData[key] = Object.keys(data);
|
||||
hasData = true;
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ exports.fn = function(item) {
|
||||
|
||||
item.content.forEach(function(g) {
|
||||
|
||||
for (var name in intersection) {
|
||||
for (const [name, attr] of Object.entries(intersection)) {
|
||||
|
||||
if (!allPath && !hasClip || name !== 'transform') {
|
||||
|
||||
@ -72,15 +72,15 @@ exports.fn = function(item) {
|
||||
if (name === 'transform') {
|
||||
if (!hasTransform) {
|
||||
if (item.hasAttr('transform')) {
|
||||
item.attr('transform').value += ' ' + intersection[name].value;
|
||||
item.attr('transform').value += ' ' + attr.value;
|
||||
} else {
|
||||
item.addAttr(intersection[name]);
|
||||
item.addAttr(attr);
|
||||
}
|
||||
|
||||
hasTransform = true;
|
||||
}
|
||||
} else {
|
||||
item.addAttr(intersection[name]);
|
||||
item.addAttr(attr);
|
||||
}
|
||||
|
||||
}
|
||||
@ -106,17 +106,17 @@ function intersectInheritableAttrs(a, b) {
|
||||
|
||||
var c = {};
|
||||
|
||||
for (var n in a) {
|
||||
for (const [n, attr] of Object.entries(a)) {
|
||||
if (
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
b.hasOwnProperty(n) &&
|
||||
inheritableAttrs.indexOf(n) > -1 &&
|
||||
a[n].name === b[n].name &&
|
||||
a[n].value === b[n].value &&
|
||||
a[n].prefix === b[n].prefix &&
|
||||
a[n].local === b[n].local
|
||||
attr.name === b[n].name &&
|
||||
attr.value === b[n].value &&
|
||||
attr.prefix === b[n].prefix &&
|
||||
attr.local === b[n].local
|
||||
) {
|
||||
c[n] = a[n];
|
||||
c[n] = attr;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,9 +25,7 @@ var collections = require('./_collections'),
|
||||
applyGroups = collections.presentationNonInheritableGroupAttrs;
|
||||
|
||||
// collect and extend all references
|
||||
for (var elem in elems) {
|
||||
elem = elems[elem];
|
||||
|
||||
for (const elem of Object.values(elems)) {
|
||||
if (elem.attrsGroups) {
|
||||
elem.attrs = elem.attrs || [];
|
||||
|
||||
@ -39,8 +37,8 @@ for (var elem in elems) {
|
||||
if (groupDefaults) {
|
||||
elem.defaults = elem.defaults || {};
|
||||
|
||||
for (var attrName in groupDefaults) {
|
||||
elem.defaults[attrName] = groupDefaults[attrName];
|
||||
for (const [attrName, attr] of Object.entries(groupDefaults)) {
|
||||
elem.defaults[attrName] = attr;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user