Skip to content

parseIconSet()

This function is part of Iconify Utils package.

Functions parseIconSet() and parseIconSetAsync() parse icon set, calling custom function for every icon.

Usage

Function has the following parameters:

  • data, IconifyJSON. Icon set data.
  • callback, function. Callback, called for each icon found in icon set.

Function returns names of parsed icons as string[].

Callback

Callback function has 2 parameters:

  • name, string. Icon name.
  • data, IconifyIcon|null. Icon data, null if icon is invalid.

Callback is called for each entry in icon set: all icons, aliases and all entries in not_found property.

If data parameter in callback is null, icon is invalid or missing.

parseIconSetAsync

Function parseIconSetAsync() is identical to parseIconSet(), except that it is asynchronous and callback is also an asynchronous function:

jsawait parseIconSetAsync(iconSet, async (name, data) => {
   // do async stuff
});

Example

Exporting all icons from icon set:

usage.ts
tsimport { promises as fs } from 'fs';
import { icons } from '@iconify-json/codicon';
import { validateIconSet, parseIconSet, iconToSVG } from '@iconify/utils';

// Storage for all icons
const exportedSVG: Record<string, string> = Object.create(null);

// Validate icon set before parsing it. Will throw an exception on error
validateIconSet(icons);

// Parse all icons
parseIconSet(icons, (iconName, iconData) => {
   if (!iconData) {
       // Invalid icon
       console.error(`Error parsing icon ${iconName}`);
       return;
   }

   // Render icon
   const renderData = iconToSVG(iconData, {
       height: 'auto',
   });

   // Generate attributes for SVG element
   const svgAttributes: Record<string, string> = {
       'xmlns': 'http://www.w3.org/2000/svg',
       'xmlns:xlink': 'http://www.w3.org/1999/xlink',
       ...renderData.attributes,
   };
   const svgAttributesStr = Object.keys(svgAttributes)
       .map(
           (attr) =>
               // No need to check attributes for special characters, such as quotes,
               // they cannot contain anything that needs escaping.
               `${attr}="${svgAttributes[attr as keyof typeof svgAttributes]}"`
       )
       .join(' ');

   // Generate SVG
   const svg = `<svg ${svgAttributesStr}>${renderData.body}</svg>`;

   // Save SVG
   exportedSVG[iconName] = svg;
});

// Save all icons
(async () => {
   for (const name in exportedSVG) {
       const svg = exportedSVG[name];
       await fs.writeFile(`svg/${name}.svg`, svg, 'utf8');
   }
   console.log(`Saved ${Object.keys(exportedSVG).length} icons`);
})();

Counting icons in icon set:

count.ts
tsimport type { ExtendedIconifyIcon } from '@iconify/types';
import { icons } from '@iconify-json/codicon';
import { parseIconSet } from '@iconify/utils';

// Count icons by parsing them
let count = 0;
parseIconSet(icons, (iconName, iconData) => {
   if (iconData && !(iconData as ExtendedIconifyIcon).hidden) {
       // Check if it is an alias.
       // Aliases without modifications should not count as they are just another name for same icon.
       const isAlias = !icons.icons[iconName];
       if (isAlias && icons.aliases) {
           const aliasData = icons.aliases[iconName];
           if (Object.keys(aliasData).length > 1) {
               // Alias has more than just 'parent' property: assume it overrides something

               // This is a quick way, proper function that accounts for all possibilities
               // is in @iconify/tools package in IconSet class.
               return;
           }
       }

       // Valid icon, not hidden, not basic alias
       count++;
   }
});

console.log(`Found ${count} icons`);

Code used in example above should be used to count icons in icon set when calculating number of icons for IconifyInfo data. It counts all icons, excluding hidden icons and basic aliases.

Validation

Function does not check icon set for errors. Before using it, validate icon set using validateIconSet() or quicklyValidateIconSet().

JSON Modules

When using ES modules, examples above might require running node with "--experimental-json-modules" flag.

Until JSON modules support in Node.js becomes stable, you can avoid using flag by replacing import with require() because require() supports JSON files:

jsconst { icons } = require('@iconify-json/codicon');

Released under the Apache 2.0 License.