2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-06 07:20:40 +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 { 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;
}

View File

@ -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);
}
}

View File

@ -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: '<g />',
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', () => {
// 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: '<g />',
width: 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,
});
});
});