stringToIcon()

This function is part of Iconify Utils package.

Function stringToIcon() converts icon name string to IconifyIconName object.

Usage

Function has the following parameters:

  • value, string. Icon name.
  • validate, boolean. Optional. If true, icon name will be validated using validateIcon().
  • allowSimpleName, boolean. Optional. If true, simple names without prefix are considered valid.
  • provider, string. Optional. API provider to add to icon name if provider is missing.

Function returns icon name with type IconifyIconName on success, null on failure.

Example

examples.ts
import { stringToIcon } from '@iconify/utils/lib/icon/name';

// { provider: '', prefix: 'mdi', name: 'home' }
console.log(stringToIcon('mdi:home'));

/* {
 provider: 'custom-api',
 prefix: 'icon-set-prefix',
 name: 'some-icon'
} */

console.log(stringToIcon('@custom-api:icon-set-prefix:some-icon'));

// { provider: '', prefix: 'mdi', name: 'home' }
console.log(stringToIcon('mdi-home'));

// null
console.log(stringToIcon('alert'));

// null
console.log(stringToIcon('alert', true));

// { provider: '', prefix: '', name: 'alert' }
console.log(stringToIcon('alert', true, true));

// { provider: '', prefix: '', name: 'alert' } - provider is ignored because there is no prefix
console.log(stringToIcon('alert', true, true, 'test'));

// { provider: 'test', prefix: 'some-icon-set', name: 'alert' }
console.log(stringToIcon('some-icon-set:alert', true, true, 'test'));