or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-is-svg

Check if a string is SVG

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/is-svg@6.1.x

To install, run

npx @tessl/cli install tessl/npm-is-svg@6.1.0

index.mddocs/

is-svg

is-svg provides a lightweight utility function for detecting whether a given string contains valid SVG (Scalable Vector Graphics) content. It performs XML validation and specifically identifies SVG format through proper XML parsing with configurable validation options.

Package Information

  • Package Name: is-svg
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install is-svg

Core Imports

import isSvg from 'is-svg';

This package requires Node.js 20+ and only provides ES module exports.

Basic Usage

import isSvg from 'is-svg';

// Basic SVG detection
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
//=> true

isSvg('<div>Not SVG</div>');
//=> false

// Performance mode with reduced validation
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><rect/></svg>', {validate: false});
//=> true

// Full validation mode (default)
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><rect/></svg>', {validate: true});
//=> true

Capabilities

SVG Detection

Checks if a string contains valid SVG content using XML validation.

/**
 * Check if a string is SVG
 * @param string - The input string to check for SVG content
 * @param options - Configuration options
 * @returns true if the string contains valid SVG, false otherwise
 * @throws TypeError if first argument is not a string
 */
function isSvg(string: string, options?: Options): boolean;

interface Options {
  /**
   * Whether to validate the SVG as proper XML.
   * Turning this off can improve performance significantly.
   * @default true
   */
  validate?: boolean;
}

Behavior:

  • Throws TypeError if the first argument is not a string
  • Trims input string and returns false for empty strings
  • Uses XML validation to determine if content is valid SVG
  • Returns true if the string contains valid SVG, false otherwise

Performance Considerations:

  • Setting validate: false improves performance significantly by using chunked processing
  • Setting validate: true (default) provides thorough XML validation for accuracy
  • Package handles large strings efficiently and avoids quadratic regex behavior

Usage Examples:

import isSvg from 'is-svg';

// Valid SVG examples
isSvg('<svg xmlns="http://www.w3.org/2000/svg"></svg>');
//=> true

isSvg('<svg><rect width="100" height="100"/></svg>');
//=> true

// Invalid examples
isSvg('<div>Not an SVG</div>');
//=> false

isSvg('');
//=> false

isSvg('   ');
//=> false

// Error handling
try {
  isSvg(123); // TypeError: Expected a `string`, got `number`
} catch (error) {
  console.error(error.message);
}

// Performance optimization for large files
const largeSvgString = '...'; // Large SVG content
const isValidSvg = isSvg(largeSvgString, {validate: false}); // Faster processing

Error Handling

The function throws a TypeError when the first argument is not a string:

// This will throw: TypeError: Expected a `string`, got `number`
isSvg(123);

// This will throw: TypeError: Expected a `string`, got `undefined`
isSvg();

For all other cases (invalid XML, non-SVG content, empty strings), the function returns false rather than throwing errors.