npm package: @svgd/utils
Description: Utility functions to convert SVG to path d.
Latest version: 0.1.49

# @svgd/utils

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

**@svgd/utils** is a collection of utility functions designed to simplify the processing of SVG files. The library provides functions to:

- Parse SVG content to extract and optimize path data.
- Convert SVG content to a base64 encoded PNG image.
- Generate formatted constant names and file names from file paths.
- Retrieve SVG file names from a directory (including nested directories).

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
  - [Importing Functions](#importing-functions)
- [API Reference](#api-reference)
  - [parseSvg](#parsesvg)
  - [getPng](#getpng)
  - [generateConstantName](#generateconstantname)
  - [generateFileName](#generatefilename)
  - [getSvgFileNames](#getsvgfilenames)
- [Use Cases Examples](#use-cases-examples)
  - [Parse SVG to Path Data](#parse-svg-to-path-data)
  - [Convert SVG to Base64 PNG](#convert-svg-to-base64-png)
  - [Generate Constant Name](#generate-constant-name)
  - [Generate File Name](#generate-file-name)
  - [Find SVG Files in a Directory](#find-svg-files-in-a-directory)
- [License](#license)

## Installation

Install **@svgd/utils** via npm:

```bash
npm install @svgd/utils
```

Or with Yarn:

```bash
yarn add @svgd/utils
```

## Usage

### Importing Functions

You can import any of the available functions like so:

```ts
import {
  parseSvg,
  getPng,
  generateConstantName,
  generateFileName,
  getSvgFileNames
} from "@svgd/utils";
```

## API Reference

### parseSvg

Parses and optimizes SVG content to extract the path data (`d` attribute).  
_Throws an error if the SVG is invalid._

**Usage:**

```ts
const svgInput = `<svg ...>...</svg>`;
const pathD = parseSvg(svgInput);
```

### getPng

Converts SVG content to a PNG image (returned as a base64 encoded string). You can optionally specify the image width and height (default size is 64x64).

**Usage:**

```ts
const svgInput = `<svg ...>...</svg>`;
const pngBase64 = await getPng(svgInput, 64, 64);
```

### generateConstantName

Generates a formatted constant name from a file path. Accepts a base directory, an optional template, and a name format. Supported formats include:
- `camelCase`
- `PascalCase`
- `snake_case`
- `SCREAMING_SNAKE_CASE`
- `material`

**Usage:**

```ts
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "";
const format = "camelCase";
const constantName = generateConstantName(filePath, baseDir, template, format);
```

### generateFileName

Generates an output file name from a file path based on the directory structure. An optional template can be provided to customize the output.

**Usage:**

```ts
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "";
const outputFileName = generateFileName(filePath, baseDir, template);
```

### getSvgFileNames

Searches a directory (recursively) for SVG files and returns an array of matching file names with their full paths.

**Usage:**

```ts
const svgFilesDir = "C:\\work\\svg\\svgd\\packages\\utils\\tests\\stories\\svgFiles";
const svgFileNames = getSvgFileNames(svgFilesDir);
```

## Use Cases Examples

### Parse SVG to Path Data

#### **Use Case 1: Material UI Icon (Size 20)**

**Code:**

```ts
import { parseSvg } from "@svgd/utils";
const svgInput = `<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 20 20" height="20" viewBox="0 0 20 20" width="20">
  <g>
    <rect fill="none" height="20" width="20"/>
    <path d="M10,16c-3.31,0-6-2.69-6-6s2.69-6,6-6s6,2.69,6,6S13.31,16,10,16 M10,17c3.87,0,7-3.13,7-7c0-3.87-3.13-7-7-7 c-3.87,0-7,3.13-7,7C3,13.87,6.13,17,10,17L10,17z M10.5,10V7h-1v3H7l3,3l3-3H10.5z"/>
  </g>
</svg>`;
export const pathD = parseSvg(svgInput);
```

**Result:**

```json
{
  "pathD": "M12 19.2c-3.972 0-7.2-3.228-7.2-7.2S8.028 4.8 12 4.8s7.2 3.228 7.2 7.2-3.228 7.2-7.2 7.2m0 1.2c4.644 0 8.4-3.756 8.4-8.4S16.644 3.6 12 3.6 3.6 7.356 3.6 12s3.756 8.4 8.4 8.4m.6-8.4V8.4h-1.2V12h-3l3.6 3.6 3.6-3.6z"
}
```

#### **Use Case 2: Material UI Icon (Size 24)**

**Code:**

```ts
import { parseSvg } from "@svgd/utils";
const svgInput = `<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24" viewBox="0 0 24 24" width="24">
  <g>
    <rect fill="none" height="24" width="24"/>
    <path d="M12,4c4.41,0,8,3.59,8,8s-3.59,8-8,8s-8-3.59-8-8S7.59,4,12,4 M12,2C6.48,2,2,6.48,2,12c0,5.52,4.48,10,10,10 c5.52,0,10-4.48,10-10C22,6.48,17.52,2,12,2L12,2z M13,12l0-4h-2l0,4H8l4,4l4-4H13z"/>
  </g>
</svg>`;
export const pathD = parseSvg(svgInput);
```

**Result:**

```json
{
  "pathD": "M12 4c4.41 0 8 3.59 8 8s-3.59 8-8 8-8-3.59-8-8 3.59-8 8-8m0-2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2m1 10V8h-2v4H8l4 4 4-4z"
}
```

#### **Use Case 3: Transparent Material UI Icon**

**Code:**

```ts
import { parseSvg } from "@svgd/utils";
const svgInput = `<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24" viewBox="0 0 24 24" width="24">
  <g>
    <rect fill="none" height="24" width="24"/>
    <path d="M12,4c-4.41,0-8,3.59-8,8s3.59,8,8,8s8-3.59,8-8S16.41,4,12,4z M12,16l-4-4h3l0-4h2l0,4h3L12,16z" opacity=".3"/>
    <path d="M12,4c4.41,0,8,3.59,8,8s-3.59,8-8,8s-8-3.59-8-8S7.59,4,12,4 M12,2C6.48,2,2,6.48,2,12c0,5.52,4.48,10,10,10 c5.52,0,10-4.48,10-10C22,6.48,17.52,2,12,2L12,2z M13,12l0-4h-2l0,4H8l4,4l4-4H13z"/>
  </g>
</svg>`;
export const pathD = parseSvg(svgInput);
```

**Result:**

```json
{
  "pathD": "o.3 M12 4c-4.41 0-8 3.59-8 8s3.59 8 8 8 8-3.59 8-8-3.59-8-8-8m0 12-4-4h3V8h2v4h3z o1 M12 4c4.41 0 8 3.59 8 8s-3.59 8-8 8-8-3.59-8-8 3.59-8 8-8m0-2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2m1 10V8h-2v4H8l4 4 4-4z"
}
```

#### **Use Case 4: Additional Attributes**

**Code:**

```ts
import { parseSvg } from "@svgd/utils";
const svgInput = `<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <circle cx="50" cy="50" r="40" fill="#00ff00" opacity="0.8"/>
    <rect x="10" y="10" width="30" height="30" fill="#0000ff" opacity="0.6"/>
    <line x1="10" y1="80" x2="90" y2="80" stroke="#ff00ff" stroke-width="2"/>
    <polygon points="50,10 90,90 10,90" fill="#ffff00"/>
    <ellipse transform="scale(.24)" cx="50" cy="50" rx="30" ry="20" fill="#ff9900"/>
</svg>`;
export const pathD = parseSvg(svgInput);
```

**Result:**

```json
{
  "pathD": "o.8 F0f0 M12 2.4a9.6 9.6 0 1 0 0 19.2 9.6 9.6 0 1 0 0-19.2 o.6 F00f M2.4 2.4h7.2v7.2H2.4z ff0f w.48 M2.4 19.2h19.2 Fff0 m12 2.4 9.6 19.2H2.4z Ff90 M2.88 1.728a1.728 1.152 0 1 0 0 2.304 1.728 1.152 0 1 0 0-2.304"
}
```

#### **Use Case 5: Empty SVG**

**Code:**

```ts
import { parseSvg } from "@svgd/utils";
const svgInput = "";
export const pathD = parseSvg(svgInput);
```

**Result:**

```json
{
  "pathD": ""
}
```

#### **Use Case 6: Invalid SVG (Throws Error)**

**Code:**

```ts
import { parseSvg } from "@svgd/utils";
const svgInput = "ss";
export const pathD = parseSvg(svgInput);
```

**Result:**

```
Throw Error
```

### Convert SVG to Base64 PNG

#### **Use Case 1: Material UI Icon (Size 20)**

**Code:**

```ts
import { getPng } from "@svgd/utils";
const svgInput = `<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 20 20" height="20" viewBox="0 0 20 20" width="20">
  <g>
    <rect fill="none" height="20" width="20"/>
    <path d="M10,16c-3.31,0-6-2.69-6-6s2.69-6,6-6s6,2.69,6,6S13.31,16,10,16 M10,17c3.87,0,7-3.13,7-7c0-3.87-3.13-7-7-7 c-3.87,0-7,3.13-7,7C3,13.87,6.13,17,10,17L10,17z M10.5,10V7h-1v3H7l3,3l3-3H10.5z"/>
  </g>
</svg>`;
export const pngBase64 = await getPng(svgInput);
```

**Result:**

```json
{
  "pngBase64": "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAsTAAALEwEAmpwYAAADjklEQVR4nO2azUtUURTAfyakLaplZatKhDZuso9NHyS2j0ErErRaGkpQ0L9RVBBFtUgwgkTBsL1YQpna2ooyyjQjNxWNGhfOwOUw08y89+59z7g/uDC8eXO+3r3nnXPvQCAQCAQCgUDAGw3AEaAd6JbRLtd28B+yE+gDhoHvwFqZsQQMAb0SrHXLCWAUyFfgdKmRFxltrCMOARNlHPsFzALTMmbl2r9+8wI4QIbZBNwCVooYvwjcAc4Au4ANRX5vru2We8y934rIMbJviK5M0QTMFDH4pSS4jRFk1gEdwKsicqeARjLCfuCrMvADkEtQhwninNIxD7SQAeeXlWH9wGYHurYAA0rXcppBaFJPfhW45EHvZdFlzwTvy2GTWvPGoHMe9Z9XQTA5od6jfm6qqejjyWuuKBuu+1z3K5Zisy7T4qF6RR70oXRCZXsXCa+axPjRsmfcR3m7Zo04r7pO4LYM8zkqHcqmVhzyTBU5cbhtyTKfo1IDTFqynuKwq8tbikxxkoUA6FmQd9VW96naPkp56yoAddJGF+RdxAHDlgLTrJChABjuWfIGSZgaFWHTsWUtAGcteaaTTJQGlWlN25q1ADQqG7eTIMcswT9L9PNpB6BWbaqYPcbEyFmC3yYkM+kAGN5bMk+SIN2WYNMEZTUAbyyZXaQcgL3A5zL7fJUM03I3px2AXMQlYAxfiOH8QhXOO10CR2MkwahBqNZ5nQQPk6HXYHOVQajWeeevQRIohCoNQhTnkY7SLtUTZ8hScDeijHJBiOq84b4l5wkO6E2oGSoVhDjO62aoBwc0qHbYtKAkFIQ4zhtOWbL+uFj/BUYtRa+kSYobhLjOGxteW3aN4JA2NW3jboo0x3TecFrZdBzHPLeUzcnGZFpsBT5Z9oz5UNqitsUfkR79lh0rPo/Pb6hpZ46rfHNV2XDN99HYtKV8VY6rfHFBHY1N+j4aK5Se8yoI5rjKx5O3nf8C7CElWoocjw84Sowm4T1Wun4A+0iZFjUTCm+H9ph1QoEaedXZ2b7w5FN33l4OU0XK20mpGE2pWi11UuG9LiE3tWlfino5oi72J6klaVY6xXDTu2tq5TtzzwNV2xdGXrK994RX7fH5eJmW95fsLM3IeAf8LvObsSxN+UpolYPKuH+UHPFR3rpkh5zVDZb4758ei3Jvj8uuLk22yX5dTnZuu+TzYfkuEAgEAoFAIIAH/gJh6BsMxvEViwAAAABJRU5ErkJggg=="
}
```

#### **Use Case 2: Material UI Icon (Size 24)**

**Code:**

```ts
import { getPng } from "@svgd/utils";
const svgInput = `<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24" viewBox="0 0 24 24" width="24">
  <g>
    <rect fill="none" height="24" width="24"/>
    <path d="M12,4c4.41,0,8,3.59,8,8s-3.59,8-8,8s-8-3.59-8-8S7.59,4,12,4 M12,2C6.48,2,2,6.48,2,12c0,5.52,4.48,10,10,10 c5.52,0,10-4.48,10-10C22,6.48,17.52,2,12,2L12,2z M13,12l0-4h-2l0,4H8l4,4l4-4H13z"/>
  </g>
</svg>`;
export const pngBase64 = await getPng(svgInput);
```

**Result:**

```json
{
  "pngBase64": "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEL0lEQVR4nO2aSWsVQRDHfyQH9WJEEwOiXjVePZicXOKHcD2Kcbm44skNEY16UEiIZxUxCK4nNepncAVRUOMGLpBEcEGMlNSD8OiaeTPdPW8C84eCMEn+VV3dXV1V3VChQoUKFQpBJ9ALbAdOAueBSyry8wlgm/6N/O20RwuwFhgEngGTGUX+Z0AdIlzTBu3AUWA0x6AteQscVu7SYg5wGvgecOD1MgGcAtooGTYDnyIOvF4+ApsoyaxfzWj8D+AxcBe4rnJXv/3MyDXczNWwDHjVgJG/gGvAVmBJS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NrUSfJX21hntZlRhtdo3pAjbY2msMf18uWiypB+69PubeYGZoUKFSpUIDv+AUsrytQsHiz+AAAAAElFTkSuQmCC"
}
```

#### **Use Case 3: Transparent Material UI Icon**

**Code:**

```ts
import { getPng } from "@svgd/utils";
const svgInput = `<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24" viewBox="0 0 24 24" width="24">
  <g>
    <rect fill="none" height="24" width="24"/>
    <path d="M12,4c-4.41,0-8,3.59-8,8s3.59,8,8,8s8-3.59,8-8S16.41,4,12,4z M12,16l-4-4h3l0-4h2l0,4h3L12,16z" opacity=".3"/>
    <path d="M12,4c4.41,0,8,3.59,8,8s-3.59,8-8,8s-8-3.59-8-8S7.59,4,12,4 M12,2C6.48,2,2,6.48,2,12c0,5.52,4.48,10,10,10 c5.52,0,10-4.48,10-10C22,6.48,17.52,2,12,2L12,2z M13,12l0-4h-2l0,4H8l4,4l4-4H13z"/>
  </g>
</svg>`;
export const pngBase64 = await getPng(svgInput);
```

**Result:**

```json
{
  "pngBase64": "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAELUlEQVR4nO1b3UpUURT+8AGcKMtHCHsB7aofvTyDXSRlpd5Emr1D5oiho10YKXadEYlX3mjdZFpPkClEQn+WoAZqkIY4sWENyGGvfc7+OzMHzgcLZJhZe621115/ewtkyJAhQ4ZEUA+gGUAvgGEATwE8JxJ/DwG4S98R3009agC0AJgAsAKgpEniN+NkEMErNagDUADw3UBpjr4BeEC8qxYnAIwC+ONQ8TDtARgBkEOVoQPAhkfFw/QLwC1Uya7PaAr/F8BHAAsA5ogW6LN9TV7TlfSGcwDWYgh5QEoWyFOCCOoEMEC/OYjB/zOAhqSVbwKwHSHYOqW7qzGU5kj8tgjgZ8RaWyRTIjgfEeh2KKe3WigeplYyxJ5iXSFTYxJuv60QQpzlaw4VD1M7gMUIT2jwGfDWmIWPADzxqPhxylNxpYoJtT4M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";
const svgInput = `<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <circle cx="50" cy="50" r="40" fill="#00ff00" opacity="0.8"/>
    <rect x="10" y="10" width="30" height="30" fill="#0000ff" opacity="0.6"/>
    <line x1="10" y1="80" x2="90" y2="80" stroke="#ff00ff" stroke-width="2"/>
    <polygon points="50,10 90,90 10,90" fill="#ffff00"/>
    <ellipse transform="scale(.24)" cx="50" cy="50" rx="30" ry="20" fill="#ff9900"/>
</svg>`;
export const pngBase64 = await getPng(svgInput);
```

**Result:**

```json
{
  "pngBase64": "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAET0lEQVR4nO2aXWgUVxzFz+qaF5FWqTRNV4pYm4JYEC2C+KCxMeqLviiRBqtvRqpoQR9UFJW+SamYTZ21UPwKflFLNVgfElpNjBhj/UCjFJVW/Dbqi18J21PuZEfW7exm5t47szc6fziwbBYy55zf3Jm9s0A00UQTTc5wJwbTwmSm8CW3YQaTGIG3YUjEaGENLTyiBWYpTQu/sA6leJOHFjblGH9Nj75799bwIfcWAazxqYkgSkB8AqISxAIQS8cSG4XE68x7lZnPlBQngG24WSgAoerPG361o/KioV27Mb79d2xc2wTiJIgz2bKIu0K574NoBfEDiEUgPgwvAAvHCpnvro+nP/3g8q6CpmPpFMo7GzGzsRPVDfdR3fAAydrruSYTxPlnRPoFkf6IOO8SgqN2ED+C+ALEgGADqEMpLVxwM/+yflDPsorvjxU0L4zP+fmGbTpbLgGI5klQKA8FbjpoBxFoCPsxkBbmtq8e39Sxetw/zd9M+Ss5v/bUyPeu7chrfNjDPag6eul/xvME4LTvBOCBglxtBZEINAj0Ll59n+ejrx7G3H2385p3CSC7fQkKHB0HUVW8AGLpFCa1tBU07hJAbvsKFDhaDiIWbgADe7ZjatNZT+ZzAnBrX4ECR5tAxMMLYEpzh2fzWQHka18DBULfar5K0D0Ar9i7BFCofQ0U9J4OgQZQ3tno23wmgL7a10SB0MxgAhj2cA/m7b0jG4CX9jVR8IemSyRfD2D6bxelzFc3PCj76au/vbSvkYI6vQGUS6KfUd3FMU+8mtdEgVCFngBi6RRmH7oua760Ntn1tDv+r98ANFBwUPGqwBod7W85WvXMr3mNFExTD2DWkStK7b8o8d2+RgpSagG883jXq6+0IbeviYJ2hf0E1tibGUVqXyMFC2UDmGjv5Ii7OAnJrPwBUZCU9I8St20sL/J61xcSBa1ye4y0NyelUvdz1xcSBR/LBDDdhPY1USBxOSQWmtK+BgoWyATwtSnta6BgiUwAq0xqX5GClYEHkAi4fUUKVgZ+ClghtK9AwZJAF8FESO0rUCC1CFaa2L4kBRWB3QglQm5fkoJRsrfCrSa275OCFvnH7bQfURvXvk8KJL8Miel9Pm9k+z4okFgAnSHKMpsKxrXvkQJx7GVQGto/TjCyfQ8UqGyJ5b8cmtK+BwoqdAQwAMR+U9svQMEBfQ9Laf8+x8j2C1AwVY95Z4gtprbvQsFWaB8iMYJoM7H9bApG9u5jKq78eeYccaDYJvvSn+KRWBBD4n0ST4tt0IOeMwgCSGw2wJxXbVY3DE4juMLWZ+fWsTv+0gBj3tQd7+GE0+tfHT/of1eY4AaCnbbqF3cV3ZRfiWN2jh/c8Dac+wxkLWD/Ovf1rgXsv+3roYD9u301Ctj/21ejgEQlCUuXeojtd4m9V4gjHURzG9FygjglJF6L98TfxGfEZ3X+b+FFioJoookmmmjwZs9/dbjGv+sJMQoAAAAASUVORK5CYII="
}
```

#### **Use Case 5: Empty SVG**

**Code:**

```ts
import { getPng } from "@svgd/utils";
const svgInput = "";
export const pngBase64 = await getPng(svgInput);
```

**Result:**

```json
{
  "pngBase64": ""
}
```

#### **Use Case 6: Invalid SVG (Throws Error)**

**Code:**

```ts
import { getPng } from "@svgd/utils";
const svgInput = "invalid_svg";
export const pngBase64 = await getPng(svgInput);
```

**Result:**

```
Throw Error
```

### Generate Constant Name

#### **Use Case 1: Default File Name for Home Icon**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "";
const format = "camelCase";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "iconsGroup1Subgroup1Size24Home"
}
```

#### **Use Case 2: PascalCase Format**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "";
const format = "PascalCase";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "IconsGroup1Subgroup1Size24Home"
}
```

#### **Use Case 3: snake_case Format**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "";
const format = "snake_case";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "icons_group1_subgroup1_size24_home"
}
```

#### **Use Case 4: SCREAMING_SNAKE_CASE Format**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "";
const format = "SCREAMING_SNAKE_CASE";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "ICONS_GROUP1_SUBGROUP1_SIZE24_HOME"
}
```

#### **Use Case 5: material Format**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/3d_rotation_24px.svg";
const baseDir = "src";
const template = "";
const format = "material";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "ThreeDRotation"
}
```

#### **Use Case 6: Template `{-2}{1,-3}{-1}`**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "{-2}{1,-3}{-1}";
const format = "camelCase";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "size24Group1Subgroup1Home"
}
```

#### **Use Case 7: Template `{1,2}`**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "{1,2}";
const format = "camelCase";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "group1Subgroup1"
}
```

#### **Use Case 8: Template `{0,0}`**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "{0,0}";
const format = "camelCase";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "icons"
}
```

#### **Use Case 9: Template `{-1}` (Negative Index)**

**Code:**

```ts
import { generateConstantName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "{-1}";
const format = "camelCase";
export const constantName = generateConstantName(filePath, baseDir, template, format);
```

**Result:**

```json
{
  "constantName": "home"
}
```

### Generate File Name

#### **Use Case 1: Default File Name for Home Icon**

**Code:**

```ts
import { generateFileName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "";
export const outputFileName = generateFileName(filePath, baseDir, template);
```

**Result:**

```json
{
  "outputFileName": "icons/group1/subgroup1/size24/home"
}
```

#### **Use Case 2: Template `{-2}{1,-3}{-1}`**

**Code:**

```ts
import { generateFileName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "{-2}{1,-3}{-1}";
export const outputFileName = generateFileName(filePath, baseDir, template);
```

**Result:**

```json
{
  "outputFileName": "size24/group1/subgroup1/home"
}
```

#### **Use Case 3: Template `{1,2}`**

**Code:**

```ts
import { generateFileName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "{1,2}";
export const outputFileName = generateFileName(filePath, baseDir, template);
```

**Result:**

```json
{
  "outputFileName": "group1/subgroup1"
}
```

#### **Use Case 4: Template `{0,0}` (Picking First Segment)**

**Code:**

```ts
import { generateFileName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "{0,0}";
export const outputFileName = generateFileName(filePath, baseDir, template);
```

**Result:**

```json
{
  "outputFileName": "icons"
}
```

#### **Use Case 5: Template `{-1}` (Negative Index)**

**Code:**

```ts
import { generateFileName } from "@svgd/utils";
const filePath = "src/icons/group1/subgroup1/size24/home.svg";
const baseDir = "src";
const template = "{-1}";
export const outputFileName = generateFileName(filePath, baseDir, template);
```

**Result:**

```json
{
  "outputFileName": "home"
}
```

### Find SVG Files in a Directory

#### **Use Case: Many Levels SVG Folder**

**Code:**

```ts
import { getSvgFileNames } from "@svgd/utils";
const svgFilesDir = "C:\\work\\svg\\svgd\\packages\\utils\\tests\\stories\\svgFiles";
export const svgFileNames = getSvgFileNames(svgFilesDir);
```

**Result:**

```json
{
  "svgFileNames": [
    "C:\\work\\svg\\svgd\\packages\\utils\\tests\\stories\\svgFiles\\test_icon.svg",
    "C:\\work\\svg\\svgd\\packages\\utils\\tests\\stories\\svgFiles\\subdir1\\icon1_20px.svg",
    "C:\\work\\svg\\svgd\\packages\\utils\\tests\\stories\\svgFiles\\subdir1\\icon1_24px.svg",
    "C:\\work\\svg\\svgd\\packages\\utils\\tests\\stories\\svgFiles\\subdir1\\subdir2\\icon5_24px.svg"
  ]
}
```

---

## License

This project is licensed under the [MIT License](LICENSE).