mirror of
https://github.com/iconify/iconify.git
synced 2025-01-06 07:20:40 +00:00
Simplify parse function in utils, check for bad aliases
This commit is contained in:
parent
465b76e05f
commit
1d65b060d7
@ -1,16 +1,7 @@
|
||||
import type { IconifyAlias, IconifyJSON } from '@iconify/types';
|
||||
import { FullIconifyIcon, iconDefaults } from '../icon';
|
||||
import type { IconifyJSON } from '@iconify/types';
|
||||
import type { FullIconifyIcon } from '../icon';
|
||||
import { getIconData } from './get-icon';
|
||||
|
||||
/**
|
||||
* Which aliases to parse:
|
||||
*
|
||||
* none - do not parse aliases
|
||||
* all - parse all aliases
|
||||
* variations - parse only aliases that have transformations (can be considered to be different icon)
|
||||
*/
|
||||
export type ParseIconSetAliases = 'none' | 'all' | 'variations';
|
||||
|
||||
/**
|
||||
* Callback to call for each icon.
|
||||
*
|
||||
@ -21,22 +12,6 @@ export type SplitIconSetCallback = (
|
||||
data: FullIconifyIcon | null
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* Check if alias is a variation
|
||||
*/
|
||||
export function isVariation(item: IconifyAlias): boolean {
|
||||
for (const key in iconDefaults) {
|
||||
if (item[key as keyof typeof iconDefaults] !== void 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export interface ParseIconSetOptions {
|
||||
aliases?: ParseIconSetAliases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract icons from an icon set
|
||||
*
|
||||
@ -44,11 +19,8 @@ export interface ParseIconSetOptions {
|
||||
*/
|
||||
export function parseIconSet(
|
||||
data: IconifyJSON,
|
||||
callback: SplitIconSetCallback,
|
||||
options?: ParseIconSetOptions
|
||||
callback: SplitIconSetCallback
|
||||
): string[] {
|
||||
options = options || {};
|
||||
|
||||
// List of icon names
|
||||
const names: string[] = [];
|
||||
|
||||
@ -67,30 +39,26 @@ export function parseIconSet(
|
||||
|
||||
// Get icons
|
||||
const icons = data.icons;
|
||||
Object.keys(icons).forEach((name) => {
|
||||
for (const name in icons) {
|
||||
const iconData = getIconData(data, name, true);
|
||||
if (iconData) {
|
||||
// Call callback
|
||||
callback(name, iconData);
|
||||
names.push(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get aliases
|
||||
const parseAliases = options.aliases || 'all';
|
||||
if (parseAliases !== 'none' && typeof data.aliases === 'object') {
|
||||
const aliases = data.aliases;
|
||||
Object.keys(aliases).forEach((name) => {
|
||||
if (parseAliases === 'variations' && isVariation(aliases[name])) {
|
||||
return;
|
||||
}
|
||||
const iconData = getIconData(data, name, true);
|
||||
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;
|
||||
|
@ -22,7 +22,7 @@ export {
|
||||
} from './icon/index';
|
||||
|
||||
// Icon set functions
|
||||
export { parseIconSet, isVariation } from './icon-set/parse';
|
||||
export { parseIconSet } from './icon-set/parse';
|
||||
export { validateIconSet } from './icon-set/validate';
|
||||
export { quicklyValidateIconSet } from './icon-set/validate-basic';
|
||||
export { expandIconSet } from './icon-set/expand';
|
||||
|
@ -139,6 +139,10 @@ describe('Testing parsing icon set', () => {
|
||||
hFlip: true,
|
||||
width: 20,
|
||||
},
|
||||
// invalid alias
|
||||
icon2: {
|
||||
parent: 'icon1',
|
||||
},
|
||||
},
|
||||
height: 24,
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user