2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-07 15:44:05 +00:00

Return only partial IconifyIcon in getIconData in Utils

This commit is contained in:
Vjacheslav Trushkin 2022-07-01 23:09:53 +03:00
parent 8eca9c6741
commit c0911ac283
3 changed files with 17 additions and 106 deletions

View File

@ -1,5 +1,4 @@
import type { ExtendedIconifyIcon, IconifyJSON } from '@iconify/types'; import type { ExtendedIconifyIcon, IconifyJSON } from '@iconify/types';
import { defaultIconProps, FullExtendedIconifyIcon } from '../icon/defaults';
import { mergeIconData } from '../icon/merge'; import { mergeIconData } from '../icon/merge';
import { getIconsTree } from './tree'; import { getIconsTree } from './tree';
@ -9,21 +8,8 @@ import { getIconsTree } from './tree';
export function internalGetIconData( export function internalGetIconData(
data: IconifyJSON, data: IconifyJSON,
name: string, name: string,
tree: string[], tree: string[]
full: true ): ExtendedIconifyIcon {
): 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 {
const icons = data.icons; const icons = data.icons;
const aliases = data.aliases || {}; const aliases = data.aliases || {};
@ -41,45 +27,22 @@ export function internalGetIconData(
tree.forEach(parse); tree.forEach(parse);
// Add default values // Add default values
currentProps = mergeIconData( return mergeIconData(data, currentProps) as unknown as ExtendedIconifyIcon;
data,
currentProps
) as unknown as ExtendedIconifyIcon;
// Return icon
return full
? Object.assign({}, defaultIconProps, currentProps)
: currentProps;
} }
/** /**
* Get data for icon * 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( export function getIconData(
data: IconifyJSON, data: IconifyJSON,
name: string name: string
): ExtendedIconifyIcon | null; ): ExtendedIconifyIcon | null {
export function getIconData(
data: IconifyJSON,
name: string,
full = false
): FullExtendedIconifyIcon | ExtendedIconifyIcon | null {
if (data.icons[name]) { if (data.icons[name]) {
// Parse only icon // Parse only icon
return internalGetIconData(data, name, [], full as true); return internalGetIconData(data, name, []);
} }
// Resolve tree // Resolve tree
const tree = getIconsTree(data, [name])[name]; const tree = getIconsTree(data, [name])[name];
return tree ? internalGetIconData(data, name, tree, full as true) : null; return tree ? internalGetIconData(data, name, tree) : null;
} }

View File

@ -1,5 +1,5 @@
import type { IconifyJSON } from '@iconify/types'; import type { IconifyJSON } from '@iconify/types';
import type { FullExtendedIconifyIcon } from '../icon/defaults'; import { defaultIconProps, FullExtendedIconifyIcon } from '../icon/defaults';
import { internalGetIconData } from './get-icon'; import { internalGetIconData } from './get-icon';
import { getIconsTree } from './tree'; import { getIconsTree } from './tree';
@ -43,7 +43,10 @@ export function parseIconSet(
for (const name in tree) { for (const name in tree) {
const item = tree[name]; const item = tree[name];
if (item) { if (item) {
callback(name, internalGetIconData(data, name, item, true)); callback(name, {
...defaultIconProps,
...internalGetIconData(data, name, item),
});
names.push(name); names.push(name);
} }
} }

View File

@ -2,8 +2,7 @@ import { getIconData } from '../lib/icon-set/get-icon';
describe('Testing getting icon data', () => { describe('Testing getting icon data', () => {
test('Simple icon', () => { test('Simple icon', () => {
// Short icon const result = getIconData(
const result1 = getIconData(
{ {
prefix: 'foo', prefix: 'foo',
icons: { icons: {
@ -13,43 +12,16 @@ describe('Testing getting icon data', () => {
}, },
}, },
}, },
'bar', 'bar'
false
); );
expect(result1).toEqual({ expect(result).toEqual({
body: '<g />', body: '<g />',
width: 24, width: 24,
}); });
// Full icon
const result2 = getIconData(
{
prefix: 'foo',
icons: {
bar: {
body: '<g />',
width: 24,
},
},
},
'bar',
true
);
expect(result2).toEqual({
body: '<g />',
left: 0,
top: 0,
width: 24,
height: 16,
rotate: 0,
vFlip: false,
hFlip: false,
});
}); });
test('Minified icon set', () => { test('Minified icon set', () => {
// Short icon const result = getIconData(
const result1 = getIconData(
{ {
prefix: 'foo', prefix: 'foo',
icons: { icons: {
@ -60,39 +32,12 @@ describe('Testing getting icon data', () => {
width: 24, width: 24,
height: 24, height: 24,
}, },
'bar', 'bar'
false
); );
expect(result1).toEqual({ expect(result).toEqual({
body: '<g />', body: '<g />',
width: 24, width: 24,
height: 24, height: 24,
}); });
// Full icon
const result2 = getIconData(
{
prefix: 'foo',
icons: {
bar: {
body: '<g />',
},
},
width: 24,
height: 24,
},
'bar',
true
);
expect(result2).toEqual({
body: '<g />',
left: 0,
top: 0,
width: 24,
height: 24,
rotate: 0,
vFlip: false,
hFlip: false,
});
}); });
}); });