From 1d65b060d70c5db029e7c0f0801a7d530ce50ce5 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Thu, 9 Jun 2022 14:03:47 +0300 Subject: [PATCH] Simplify parse function in utils, check for bad aliases --- packages/utils/src/icon-set/parse.ts | 52 +++++------------------- packages/utils/src/index.ts | 2 +- packages/utils/tests/parse-icons-test.ts | 4 ++ 3 files changed, 15 insertions(+), 43 deletions(-) diff --git a/packages/utils/src/icon-set/parse.ts b/packages/utils/src/icon-set/parse.ts index 10c77ab..fe3db73 100644 --- a/packages/utils/src/icon-set/parse.ts +++ b/packages/utils/src/icon-set/parse.ts @@ -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; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 1c0284c..9b6e6a6 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -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'; diff --git a/packages/utils/tests/parse-icons-test.ts b/packages/utils/tests/parse-icons-test.ts index d73a0e2..0995a38 100644 --- a/packages/utils/tests/parse-icons-test.ts +++ b/packages/utils/tests/parse-icons-test.ts @@ -139,6 +139,10 @@ describe('Testing parsing icon set', () => { hFlip: true, width: 20, }, + // invalid alias + icon2: { + parent: 'icon1', + }, }, height: 24, },