mirror of
https://github.com/iconify/iconify.git
synced 2024-11-09 23:00:56 +00:00
Parse icons tree only once when parsing icon set
This commit is contained in:
parent
89a2def126
commit
7b4409665a
@ -4,6 +4,57 @@ import type { IconifyIcon, FullIconifyIcon } from '../icon/defaults';
|
|||||||
import { mergeIconData } from '../icon/merge';
|
import { mergeIconData } from '../icon/merge';
|
||||||
import { getIconsTree } from './tree';
|
import { getIconsTree } from './tree';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get icon data, using prepared aliases tree
|
||||||
|
*/
|
||||||
|
export function internalGetIconData(
|
||||||
|
data: IconifyJSON,
|
||||||
|
name: string,
|
||||||
|
tree: string[],
|
||||||
|
full: true
|
||||||
|
): FullIconifyIcon;
|
||||||
|
export function internalGetIconData(
|
||||||
|
data: IconifyJSON,
|
||||||
|
name: string,
|
||||||
|
tree: string[],
|
||||||
|
full: false
|
||||||
|
): IconifyIcon;
|
||||||
|
export function internalGetIconData(
|
||||||
|
data: IconifyJSON,
|
||||||
|
name: string,
|
||||||
|
tree: string[],
|
||||||
|
full: boolean
|
||||||
|
): FullIconifyIcon | IconifyIcon {
|
||||||
|
const icons = data.icons;
|
||||||
|
const aliases = data.aliases || {};
|
||||||
|
|
||||||
|
let currentProps = {} as IconifyIcon;
|
||||||
|
|
||||||
|
// Parse parent item
|
||||||
|
function parse(name: string) {
|
||||||
|
currentProps = mergeIconData(
|
||||||
|
icons[name] || aliases[name],
|
||||||
|
currentProps,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
parse(name);
|
||||||
|
tree.forEach(parse);
|
||||||
|
|
||||||
|
// Add default values
|
||||||
|
currentProps = mergeIconData(
|
||||||
|
data,
|
||||||
|
currentProps,
|
||||||
|
false
|
||||||
|
) as unknown as IconifyIcon;
|
||||||
|
|
||||||
|
// Return icon
|
||||||
|
return full
|
||||||
|
? Object.assign({}, defaultIconProps, currentProps)
|
||||||
|
: currentProps;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get data for icon
|
* Get data for icon
|
||||||
*/
|
*/
|
||||||
@ -22,43 +73,12 @@ export function getIconData(
|
|||||||
name: string,
|
name: string,
|
||||||
full = false
|
full = false
|
||||||
): FullIconifyIcon | IconifyIcon | null {
|
): FullIconifyIcon | IconifyIcon | null {
|
||||||
const icons = data.icons;
|
if (data.icons[name]) {
|
||||||
const aliases = data.aliases || {};
|
// Parse only icon
|
||||||
|
return internalGetIconData(data, name, [], full as true);
|
||||||
let currentProps = {} as IconifyIcon;
|
|
||||||
|
|
||||||
// Parse parent item
|
|
||||||
function parse(name: string) {
|
|
||||||
currentProps = mergeIconData(
|
|
||||||
icons[name] || aliases[name],
|
|
||||||
currentProps,
|
|
||||||
false
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const icon = icons[name];
|
|
||||||
if (icon) {
|
|
||||||
// Parse only icon
|
|
||||||
parse(name);
|
|
||||||
} else {
|
|
||||||
// Resolve tree
|
// Resolve tree
|
||||||
const tree = getIconsTree(data, [name])[name];
|
const tree = getIconsTree(data, [name])[name];
|
||||||
if (!tree) {
|
return tree ? internalGetIconData(data, name, tree, full as true) : null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
parse(name);
|
|
||||||
tree.forEach(parse);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add default values
|
|
||||||
currentProps = mergeIconData(
|
|
||||||
data,
|
|
||||||
currentProps,
|
|
||||||
false
|
|
||||||
) as unknown as IconifyIcon;
|
|
||||||
|
|
||||||
// Return icon
|
|
||||||
return full
|
|
||||||
? Object.assign({}, defaultIconProps, currentProps)
|
|
||||||
: currentProps;
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import type { IconifyJSON } from '@iconify/types';
|
import type { IconifyJSON } from '@iconify/types';
|
||||||
import type { FullIconifyIcon } from '../icon/defaults';
|
import type { FullIconifyIcon } from '../icon/defaults';
|
||||||
import { getIconData } from './get-icon';
|
import { internalGetIconData } from './get-icon';
|
||||||
|
import { getIconsTree } from './tree';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback to call for each icon.
|
* Callback to call for each icon.
|
||||||
@ -37,29 +38,15 @@ export function parseIconSet(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get icons
|
// Get tree
|
||||||
const icons = data.icons;
|
const tree = getIconsTree(data);
|
||||||
for (const name in icons) {
|
for (const name in tree) {
|
||||||
const iconData = getIconData(data, name, true);
|
const item = tree[name];
|
||||||
if (iconData) {
|
if (item) {
|
||||||
// Call callback
|
callback(name, internalGetIconData(data, name, item, true));
|
||||||
callback(name, iconData);
|
|
||||||
names.push(name);
|
names.push(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get aliases
|
|
||||||
const aliases = data.aliases;
|
|
||||||
if (aliases) {
|
|
||||||
for (const name in aliases) {
|
|
||||||
const iconData = icons[name] ? null : getIconData(data, name, true);
|
|
||||||
if (iconData) {
|
|
||||||
// Call callback
|
|
||||||
callback(name, iconData);
|
|
||||||
names.push(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
|
@ -66,8 +66,8 @@ describe('Testing parsing icon set', () => {
|
|||||||
|
|
||||||
test('Aliases', () => {
|
test('Aliases', () => {
|
||||||
// Names list
|
// Names list
|
||||||
const names: string[] = ['icon1', 'icon2', 'alias1', 'alias2'];
|
let names: string[] = ['icon1', 'icon2', 'alias1', 'alias2'];
|
||||||
const namesCopy = names.slice(0);
|
const expectedNames = names.slice(0).sort((a, b) => a.localeCompare(b));
|
||||||
|
|
||||||
// Resolved data
|
// Resolved data
|
||||||
const expected: Record<string, FullIconifyIcon | null> = {
|
const expected: Record<string, FullIconifyIcon | null> = {
|
||||||
@ -114,8 +114,7 @@ describe('Testing parsing icon set', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Do stuff
|
// Do stuff
|
||||||
expect(
|
const parsedNames = parseIconSet(
|
||||||
parseIconSet(
|
|
||||||
{
|
{
|
||||||
prefix: 'foo',
|
prefix: 'foo',
|
||||||
icons: {
|
icons: {
|
||||||
@ -147,23 +146,25 @@ describe('Testing parsing icon set', () => {
|
|||||||
height: 24,
|
height: 24,
|
||||||
},
|
},
|
||||||
(name, data) => {
|
(name, data) => {
|
||||||
// Make sure name matches
|
// Make sure name exists in array of pending names
|
||||||
expect(names.length).toBeGreaterThanOrEqual(1);
|
const index = names.indexOf(name);
|
||||||
expect(name).toBe(names.shift());
|
expect(index).not.toBe(-1);
|
||||||
|
names = names.slice(0, index).concat(names.slice(index + 1));
|
||||||
|
|
||||||
// Check icon data
|
// Check icon data
|
||||||
expect(data).toEqual(expected[name]);
|
expect(data).toEqual(expected[name]);
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
).toEqual(namesCopy);
|
|
||||||
|
|
||||||
// All names should have been parsed
|
// All names should have been parsed, not necessary in expected order
|
||||||
expect(names).toEqual([]);
|
expect(names).toEqual([]);
|
||||||
|
parsedNames.sort((a, b) => a.localeCompare(b));
|
||||||
|
expect(parsedNames).toEqual(expectedNames);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Nested aliases', () => {
|
test('Nested aliases', () => {
|
||||||
// Names list
|
// Names list
|
||||||
const names: string[] = [
|
let names: string[] = [
|
||||||
'icon1',
|
'icon1',
|
||||||
'icon2',
|
'icon2',
|
||||||
'alias2a',
|
'alias2a',
|
||||||
@ -175,7 +176,7 @@ describe('Testing parsing icon set', () => {
|
|||||||
'alias2z6',
|
'alias2z6',
|
||||||
'alias2z7',
|
'alias2z7',
|
||||||
];
|
];
|
||||||
const namesCopy = names.slice(0);
|
const expectedNames = names.slice(0).sort((a, b) => a.localeCompare(b));
|
||||||
|
|
||||||
// Resolved data
|
// Resolved data
|
||||||
const expected: Record<string, FullIconifyIcon | null> = {
|
const expected: Record<string, FullIconifyIcon | null> = {
|
||||||
@ -290,8 +291,7 @@ describe('Testing parsing icon set', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Do stuff
|
// Do stuff
|
||||||
expect(
|
const parsedNames = parseIconSet(
|
||||||
parseIconSet(
|
|
||||||
{
|
{
|
||||||
prefix: 'foo',
|
prefix: 'foo',
|
||||||
icons: {
|
icons: {
|
||||||
@ -355,17 +355,19 @@ describe('Testing parsing icon set', () => {
|
|||||||
height: 24,
|
height: 24,
|
||||||
},
|
},
|
||||||
(name, data) => {
|
(name, data) => {
|
||||||
// Make sure name matches
|
// Make sure name exists in array of pending names
|
||||||
expect(names.length).toBeGreaterThanOrEqual(1);
|
const index = names.indexOf(name);
|
||||||
expect(name).toBe(names.shift());
|
expect(index).not.toBe(-1);
|
||||||
|
names = names.slice(0, index).concat(names.slice(index + 1));
|
||||||
|
|
||||||
// Check icon data
|
// Check icon data
|
||||||
expect(data).toEqual(expected[name]);
|
expect(data).toEqual(expected[name]);
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
).toEqual(namesCopy);
|
|
||||||
|
|
||||||
// All names should have been parsed
|
// All names should have been parsed, not necessary in expected order
|
||||||
expect(names).toEqual([]);
|
expect(names).toEqual([]);
|
||||||
|
parsedNames.sort((a, b) => a.localeCompare(b));
|
||||||
|
expect(parsedNames).toEqual(expectedNames);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user