mirror of
https://github.com/iconify/iconify.git
synced 2024-11-09 23:00:56 +00:00
Remove getIconName function, add optional validation to stringToIcon
This commit is contained in:
parent
5f285fcf14
commit
1e3ffd7984
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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`;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user