0
# is-svg
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: is-svg
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install is-svg`
10
11
## Core Imports
12
13
```typescript
14
import isSvg from 'is-svg';
15
```
16
17
This package requires Node.js 20+ and only provides ES module exports.
18
19
## Basic Usage
20
21
```typescript
22
import isSvg from 'is-svg';
23
24
// Basic SVG detection
25
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
26
//=> true
27
28
isSvg('<div>Not SVG</div>');
29
//=> false
30
31
// Performance mode with reduced validation
32
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><rect/></svg>', {validate: false});
33
//=> true
34
35
// Full validation mode (default)
36
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><rect/></svg>', {validate: true});
37
//=> true
38
```
39
40
## Capabilities
41
42
### SVG Detection
43
44
Checks if a string contains valid SVG content using XML validation.
45
46
```typescript { .api }
47
/**
48
* Check if a string is SVG
49
* @param string - The input string to check for SVG content
50
* @param options - Configuration options
51
* @returns true if the string contains valid SVG, false otherwise
52
* @throws TypeError if first argument is not a string
53
*/
54
function isSvg(string: string, options?: Options): boolean;
55
56
interface Options {
57
/**
58
* Whether to validate the SVG as proper XML.
59
* Turning this off can improve performance significantly.
60
* @default true
61
*/
62
validate?: boolean;
63
}
64
```
65
66
**Behavior:**
67
- Throws `TypeError` if the first argument is not a string
68
- Trims input string and returns `false` for empty strings
69
- Uses XML validation to determine if content is valid SVG
70
- Returns `true` if the string contains valid SVG, `false` otherwise
71
72
**Performance Considerations:**
73
- Setting `validate: false` improves performance significantly by using chunked processing
74
- Setting `validate: true` (default) provides thorough XML validation for accuracy
75
- Package handles large strings efficiently and avoids quadratic regex behavior
76
77
**Usage Examples:**
78
79
```typescript
80
import isSvg from 'is-svg';
81
82
// Valid SVG examples
83
isSvg('<svg xmlns="http://www.w3.org/2000/svg"></svg>');
84
//=> true
85
86
isSvg('<svg><rect width="100" height="100"/></svg>');
87
//=> true
88
89
// Invalid examples
90
isSvg('<div>Not an SVG</div>');
91
//=> false
92
93
isSvg('');
94
//=> false
95
96
isSvg(' ');
97
//=> false
98
99
// Error handling
100
try {
101
isSvg(123); // TypeError: Expected a `string`, got `number`
102
} catch (error) {
103
console.error(error.message);
104
}
105
106
// Performance optimization for large files
107
const largeSvgString = '...'; // Large SVG content
108
const isValidSvg = isSvg(largeSvgString, {validate: false}); // Faster processing
109
```
110
111
## Error Handling
112
113
The function throws a `TypeError` when the first argument is not a string:
114
115
```typescript
116
// This will throw: TypeError: Expected a `string`, got `number`
117
isSvg(123);
118
119
// This will throw: TypeError: Expected a `string`, got `undefined`
120
isSvg();
121
```
122
123
For all other cases (invalid XML, non-SVG content, empty strings), the function returns `false` rather than throwing errors.