diff --git a/packages/utils/package.json b/packages/utils/package.json index 62bdf35..e345360 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -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/", diff --git a/packages/utils/src/icon-set/minify.ts b/packages/utils/src/icon-set/minify.ts index 5d6981e..e5731aa 100644 --- a/packages/utils/src/icon-set/minify.ts +++ b/packages/utils/src/icon-set/minify.ts @@ -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; } }); } diff --git a/packages/utils/tests/minify-test.ts b/packages/utils/tests/minify-test.ts index 26db343..379f1dc 100644 --- a/packages/utils/tests/minify-test.ts +++ b/packages/utils/tests/minify-test.ts @@ -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: '', + width: 24, + height: 24, + hidden: true, + }, + 'chrome-minimize': { + body: '', + width: 24, + height: 24, + hidden: true, + }, + 'remove': { + body: '', + }, + }, + }; + const expected: IconifyJSON = { + prefix: 'bar', + icons: { + 'chrome-maximize': { + body: '', + hidden: true, + }, + 'chrome-minimize': { + body: '', + hidden: true, + }, + 'remove': { + body: '', + width: 16, + height: 16, + }, + }, + width: 24, + height: 24, + }; + minifyIconSet(item); + expect(item).toEqual(expected); + }); + test('Common value', () => { const item: IconifyJSON = { prefix: 'foo',