parseIconSet()

This function is part of Iconify Utils package.

Function parseIconSet() parses icon set, calling custom function for every icon.

Usage

Function has the following parameters:

  • data, IconifyJSON. Icon set data.
  • callback, string. Icon name.
  • options. Optional options, see below.

Function returns list of parsed icons as string[].

Options

Options object has the following properties:

  • validate, boolean | IconSetValidationOptions. Validates icon set before parsing it. If value is false, validation will be disabled. When value is true, it is equivalent of {fix: true} of IconSetValidationOptions type. By default, validation is enabled.
  • aliases, ParseIconSetAliases. Defines how to parse icon aliases. Default value is "all". See below.

Parsing icon aliases

options.aliases defines how to parse icon aliases:

  • "none" icon aliases will not be parsed.
  • "variations" parses only icon aliases that have transformation. This is useful to count actual icons in icon set because it excludes simple aliases.
  • "all" parses all aliases.

Example

Exporting all icons from icon set:

usage.ts
import { promises as fs } from 'fs';
import { icons } from '@iconify-json/codicon';
import { parseIconSet } from '@iconify/utils/lib/icon-set/parse';
import { iconToSVG } from '@iconify/utils/lib/svg/build';
import { defaults } from '@iconify/utils/lib/customisations';

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

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

   // Render icon
   const renderData = iconToSVG(iconData, {
       ...defaults,
       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
import type { ExtendedIconifyIcon } from '@iconify/types';
import { icons } from '@iconify-json/codicon';
import { parseIconSet } from '@iconify/utils/lib/icon-set/parse';

// Count icons by parsing them
let count = 0;
parseIconSet(
   icons,
   (iconName, iconData) => {
       if (!iconData || (iconData as ExtendedIconifyIcon).hidden) {
           // Invalid or hidden
           return;
       }
       count++;
   },
   {
       // List only icon variations instead of all aliases
       aliases: 'variations',
   }
);

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.

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:

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