From 1e3ffd798440e7a5e4c663dd5a9639094a971000 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 21 Dec 2020 19:33:08 +0200 Subject: [PATCH] Remove getIconName function, add optional validation to stringToIcon --- packages/core/src/icon/name.ts | 11 ++++++--- packages/core/src/storage/functions.ts | 15 ++---------- .../core/tests/40-modules/10-storage-test.ts | 23 +------------------ packages/react-with-api/src/icon.ts | 5 ++-- 4 files changed, 13 insertions(+), 41 deletions(-) diff --git a/packages/core/src/icon/name.ts b/packages/core/src/icon/name.ts index 2cb2fc1..cf146f8 100644 --- a/packages/core/src/icon/name.ts +++ b/packages/core/src/icon/name.ts @@ -20,7 +20,10 @@ const match = /^[a-z0-9]+(-[a-z0-9]+)*$/; /** * Convert string to Icon object. */ -export const stringToIcon = (value: string): IconifyIconName | null => { +export const stringToIcon = ( + value: string, + validate?: boolean +): IconifyIconName | null => { let provider = ''; const colonSeparated = value.split(':'); @@ -42,22 +45,24 @@ export const stringToIcon = (value: string): IconifyIconName | null => { // "prefix:name" const name = colonSeparated.pop() as string; const prefix = colonSeparated.pop() as string; - return { + const result: IconifyIconName = { // Allow provider without '@': "provider:prefix:name" provider: colonSeparated.length > 0 ? colonSeparated[0] : provider, prefix, name, }; + return validate && !validateIcon(result) ? null : result; } // Attempt to split by dash: "prefix-name" const dashSeparated = colonSeparated[0].split('-'); if (dashSeparated.length > 1) { - return { + const result: IconifyIconName = { provider: provider, prefix: dashSeparated.shift() as string, name: dashSeparated.join('-'), }; + return validate && !validateIcon(result) ? null : result; } return null; diff --git a/packages/core/src/storage/functions.ts b/packages/core/src/storage/functions.ts index 4749c70..b15db69 100644 --- a/packages/core/src/storage/functions.ts +++ b/packages/core/src/storage/functions.ts @@ -35,24 +35,13 @@ export interface IconifyStorageFunctions { addCollection: (data: IconifyJSON, provider?: string) => boolean; } -/** - * Get icon name - */ -export function getIconName(name: string): IconifyIconName | null { - const icon = stringToIcon(name); - if (!validateIcon(icon)) { - return null; - } - return icon; -} - /** * Get icon data */ export function getIconData( name: string | IconifyIconName ): FullIconifyIcon | null { - const icon = typeof name === 'string' ? getIconName(name) : name; + const icon = typeof name === 'string' ? stringToIcon(name, true) : name; return icon ? getIcon(getStorage(icon.provider, icon.prefix), icon.name) : null; @@ -102,7 +91,7 @@ export const storageFunctions: IconifyStorageFunctions = { // Add icon addIcon: (name, data) => { - const icon = getIconName(name); + const icon = stringToIcon(name, true); if (!icon) { return false; } diff --git a/packages/core/tests/40-modules/10-storage-test.ts b/packages/core/tests/40-modules/10-storage-test.ts index 11ebdb8..babbe85 100644 --- a/packages/core/tests/40-modules/10-storage-test.ts +++ b/packages/core/tests/40-modules/10-storage-test.ts @@ -1,7 +1,6 @@ import 'mocha'; import { expect } from 'chai'; -import { getIconName, storageFunctions } from '../../lib/storage/functions'; -import { IconifyIconName } from '../../lib/icon/name'; +import { storageFunctions } from '../../lib/storage/functions'; describe('Testing IconifyStorageFunctions', () => { let count = 0; @@ -10,26 +9,6 @@ describe('Testing IconifyStorageFunctions', () => { return 'storage-test-' + count++; } - it('Getting icon name', () => { - let expected: IconifyIconName; - - expected = { - provider: '', - prefix: 'mdi', - name: 'home', - }; - expect(getIconName('mdi:home')).to.be.eql(expected); - - expected = { - provider: 'local-test', - prefix: 'mdi', - name: 'home', - }; - expect(getIconName('@local-test:mdi:home')).to.be.eql(expected); - - expect(getIconName('test')).to.be.equal(null); - }); - it('Storage functions', () => { const provider = nextProvider(); const testName = `@${provider}:foo:bar`; diff --git a/packages/react-with-api/src/icon.ts b/packages/react-with-api/src/icon.ts index 27364ff..3e2056c 100644 --- a/packages/react-with-api/src/icon.ts +++ b/packages/react-with-api/src/icon.ts @@ -9,7 +9,7 @@ import { } from '@iconify/react/lib/icon'; // Core -import { IconifyIconName } from '@iconify/core/lib/icon/name'; +import { IconifyIconName, stringToIcon } from '@iconify/core/lib/icon/name'; import { IconifyIconCustomisations, IconifyIconSize, @@ -19,7 +19,6 @@ import { import { storageFunctions, getIconData, - getIconName, } from '@iconify/core/lib/storage/functions'; import { calcSize } from '@iconify/core/lib/builder/calc-size'; import { IconifyIcon } from '@iconify/core/lib/icon'; @@ -302,7 +301,7 @@ const component = (props: IconProps, func: typeof ReactIcon): JSX.Element => { // Get icon data if (typeof props.icon === 'string') { - const iconName = getIconName(props.icon); + const iconName = stringToIcon(props.icon, true); if (!iconName) { return null; }