2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 23:00:56 +00:00

Fix bug in icon set minification in utils

This commit is contained in:
Vjacheslav Trushkin 2021-10-13 23:38:00 +03:00
parent 2160e2a2f5
commit ee240278fa
3 changed files with 72 additions and 16 deletions

View File

@ -2,7 +2,7 @@
"name": "@iconify/utils",
"description": "Common functions for working with Iconify icon sets used by various packages.",
"author": "Vjacheslav Trushkin",
"version": "1.0.17",
"version": "1.0.18",
"license": "MIT",
"bugs": "https://github.com/iconify/iconify/issues",
"homepage": "https://iconify.design/",

View File

@ -97,34 +97,45 @@ export function minifyIconSet(data: IconifyJSON): void {
}
}
// Found value to minify
const canMinify = maxValue !== null && maxCount > 1;
// Get default value
const oldDefault = hasMinifiedDefault ? data[prop] : null;
const newDefault = canMinify ? maxValue : oldDefault;
// console.log(
// `Prop: ${prop}, oldDefault: ${oldDefault}, canMinify: ${canMinify}, maxValue: ${maxValue}`
// );
// Change global value
if (!canMinify || maxValue === defaultValue) {
if (newDefault === defaultValue) {
delete data[prop];
} else {
data[prop as 'height'] = maxValue as number;
} else if (canMinify) {
data[prop as 'height'] = newDefault as number;
}
// Minify stuff
// Update all icons
icons.forEach((key) => {
const item = data.icons[key];
if (canMinify && item[prop] === maxValue) {
// New value matches minified value
const value =
item[prop] === void 0
? hasMinifiedDefault
? oldDefault
: defaultValue
: item[prop];
if (
value === newDefault ||
(newDefault === null && value === defaultValue)
) {
// Property matches minified value
// Property matches default value and there is no minified value
delete item[prop];
return;
}
if (hasMinifiedDefault && item[prop] === void 0) {
// Old value matches old minified value
item[prop as 'height'] = oldDefault as number;
}
if (!canMinify && item[prop] === iconDefaults[prop]) {
// Current value (after changes above) matches default and there is no minified value
delete item[prop];
if (canMinify && item[prop] === void 0) {
// Value matches old minified value
item[prop as 'height'] = value as number;
}
});
}

View File

@ -97,6 +97,51 @@ describe('Testing minifying icon set', () => {
expect(item).toEqual(expected);
});
test('Default values in icon', () => {
const item: IconifyJSON = {
prefix: 'bar',
icons: {
'chrome-maximize': {
body: '<g fill="currentColor"><path d="M3 3v10h10V3H3zm9 9H4V4h8v8z"/></g>',
width: 24,
height: 24,
hidden: true,
},
'chrome-minimize': {
body: '<g fill="currentColor"><path d="M14 8v1H3V8h11z"/></g>',
width: 24,
height: 24,
hidden: true,
},
'remove': {
body: '<g fill="currentColor"><path d="M15 8H1V7h14v1z"/></g>',
},
},
};
const expected: IconifyJSON = {
prefix: 'bar',
icons: {
'chrome-maximize': {
body: '<g fill="currentColor"><path d="M3 3v10h10V3H3zm9 9H4V4h8v8z"/></g>',
hidden: true,
},
'chrome-minimize': {
body: '<g fill="currentColor"><path d="M14 8v1H3V8h11z"/></g>',
hidden: true,
},
'remove': {
body: '<g fill="currentColor"><path d="M15 8H1V7h14v1z"/></g>',
width: 16,
height: 16,
},
},
width: 24,
height: 24,
};
minifyIconSet(item);
expect(item).toEqual(expected);
});
test('Common value', () => {
const item: IconifyJSON = {
prefix: 'foo',