From c0911ac2836a864b1d691633e570eecd9002b2f4 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Fri, 1 Jul 2022 23:09:53 +0300 Subject: [PATCH] Return only partial IconifyIcon in getIconData in Utils --- packages/utils/src/icon-set/get-icon.ts | 49 +++--------------- packages/utils/src/icon-set/parse.ts | 7 ++- packages/utils/tests/get-icon-test.ts | 67 +++---------------------- 3 files changed, 17 insertions(+), 106 deletions(-) diff --git a/packages/utils/src/icon-set/get-icon.ts b/packages/utils/src/icon-set/get-icon.ts index 6708897..81b8728 100644 --- a/packages/utils/src/icon-set/get-icon.ts +++ b/packages/utils/src/icon-set/get-icon.ts @@ -1,5 +1,4 @@ import type { ExtendedIconifyIcon, IconifyJSON } from '@iconify/types'; -import { defaultIconProps, FullExtendedIconifyIcon } from '../icon/defaults'; import { mergeIconData } from '../icon/merge'; import { getIconsTree } from './tree'; @@ -9,21 +8,8 @@ import { getIconsTree } from './tree'; export function internalGetIconData( data: IconifyJSON, name: string, - tree: string[], - full: true -): FullExtendedIconifyIcon; -export function internalGetIconData( - data: IconifyJSON, - name: string, - tree: string[], - full: false -): ExtendedIconifyIcon; -export function internalGetIconData( - data: IconifyJSON, - name: string, - tree: string[], - full: boolean -): FullExtendedIconifyIcon | ExtendedIconifyIcon { + tree: string[] +): ExtendedIconifyIcon { const icons = data.icons; const aliases = data.aliases || {}; @@ -41,45 +27,22 @@ export function internalGetIconData( tree.forEach(parse); // Add default values - currentProps = mergeIconData( - data, - currentProps - ) as unknown as ExtendedIconifyIcon; - - // Return icon - return full - ? Object.assign({}, defaultIconProps, currentProps) - : currentProps; + return mergeIconData(data, currentProps) as unknown as ExtendedIconifyIcon; } /** * Get data for icon */ -export function getIconData( - data: IconifyJSON, - name: string, - full: true -): FullExtendedIconifyIcon | null; -export function getIconData( - data: IconifyJSON, - name: string, - full: false -): ExtendedIconifyIcon | null; export function getIconData( data: IconifyJSON, name: string -): ExtendedIconifyIcon | null; -export function getIconData( - data: IconifyJSON, - name: string, - full = false -): FullExtendedIconifyIcon | ExtendedIconifyIcon | null { +): ExtendedIconifyIcon | null { if (data.icons[name]) { // Parse only icon - return internalGetIconData(data, name, [], full as true); + return internalGetIconData(data, name, []); } // Resolve tree const tree = getIconsTree(data, [name])[name]; - return tree ? internalGetIconData(data, name, tree, full as true) : null; + return tree ? internalGetIconData(data, name, tree) : null; } diff --git a/packages/utils/src/icon-set/parse.ts b/packages/utils/src/icon-set/parse.ts index 9ffa954..6027ffc 100644 --- a/packages/utils/src/icon-set/parse.ts +++ b/packages/utils/src/icon-set/parse.ts @@ -1,5 +1,5 @@ import type { IconifyJSON } from '@iconify/types'; -import type { FullExtendedIconifyIcon } from '../icon/defaults'; +import { defaultIconProps, FullExtendedIconifyIcon } from '../icon/defaults'; import { internalGetIconData } from './get-icon'; import { getIconsTree } from './tree'; @@ -43,7 +43,10 @@ export function parseIconSet( for (const name in tree) { const item = tree[name]; if (item) { - callback(name, internalGetIconData(data, name, item, true)); + callback(name, { + ...defaultIconProps, + ...internalGetIconData(data, name, item), + }); names.push(name); } } diff --git a/packages/utils/tests/get-icon-test.ts b/packages/utils/tests/get-icon-test.ts index 6c85351..7951b6d 100644 --- a/packages/utils/tests/get-icon-test.ts +++ b/packages/utils/tests/get-icon-test.ts @@ -2,8 +2,7 @@ import { getIconData } from '../lib/icon-set/get-icon'; describe('Testing getting icon data', () => { test('Simple icon', () => { - // Short icon - const result1 = getIconData( + const result = getIconData( { prefix: 'foo', icons: { @@ -13,43 +12,16 @@ describe('Testing getting icon data', () => { }, }, }, - 'bar', - false + 'bar' ); - expect(result1).toEqual({ + expect(result).toEqual({ body: '', width: 24, }); - - // Full icon - const result2 = getIconData( - { - prefix: 'foo', - icons: { - bar: { - body: '', - width: 24, - }, - }, - }, - 'bar', - true - ); - expect(result2).toEqual({ - body: '', - left: 0, - top: 0, - width: 24, - height: 16, - rotate: 0, - vFlip: false, - hFlip: false, - }); }); test('Minified icon set', () => { - // Short icon - const result1 = getIconData( + const result = getIconData( { prefix: 'foo', icons: { @@ -60,39 +32,12 @@ describe('Testing getting icon data', () => { width: 24, height: 24, }, - 'bar', - false + 'bar' ); - expect(result1).toEqual({ + expect(result).toEqual({ body: '', width: 24, height: 24, }); - - // Full icon - const result2 = getIconData( - { - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - width: 24, - height: 24, - }, - 'bar', - true - ); - expect(result2).toEqual({ - body: '', - left: 0, - top: 0, - width: 24, - height: 24, - rotate: 0, - vFlip: false, - hFlip: false, - }); }); });