Skip to content

Commit e164d6a

Browse files
Add validate option (#43)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 61a51ac commit e164d6a

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

index.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
export type Options = {
2+
/**
3+
Whether to validate the SVG as proper XML.
4+
5+
Turning this off can improve performance significantly.
6+
7+
@default true
8+
*/
9+
validate?: boolean;
10+
};
11+
112
/**
213
Check if a string is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics).
314
@@ -9,4 +20,4 @@ isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
920
//=> true
1021
```
1122
*/
12-
export default function isSvg(string: string): boolean;
23+
export default function isSvg(string: string, options?: Options): boolean;

index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {XmlTextDetector} from '@file-type/xml';
22

3-
export default function isSvg(string) {
3+
export default function isSvg(string, {validate = true} = {}) {
44
if (typeof string !== 'string') {
55
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
66
}
@@ -11,7 +11,23 @@ export default function isSvg(string) {
1111
return false;
1212
}
1313

14-
const xmlTextDetector = new XmlTextDetector();
15-
xmlTextDetector.write(string);
16-
return xmlTextDetector.isValid() && xmlTextDetector.fileType?.ext === 'svg';
14+
const xmlTextDetector = new XmlTextDetector({fullScan: validate});
15+
16+
if (validate) {
17+
xmlTextDetector.write(string);
18+
19+
if (!xmlTextDetector.isValid()) {
20+
return false;
21+
}
22+
} else {
23+
const chunkSize = 128;
24+
25+
let offset = 0;
26+
while (string.length > offset && !xmlTextDetector.onEnd) {
27+
xmlTextDetector.write(string.slice(offset, Math.min(offset + chunkSize, string.length)));
28+
offset += chunkSize;
29+
}
30+
}
31+
32+
return xmlTextDetector.fileType?.ext === 'svg';
1733
}

index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import {expectType} from 'tsd';
22
import isSvg from './index.js';
33

44
expectType<boolean>(isSvg('<svg></svg>'));
5+
expectType<boolean>(isSvg('<svg></svg>', {validate: false}));

readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ import isSvg from 'is-svg';
1616
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
1717
//=> true
1818
```
19+
20+
## API
21+
22+
### isSvg(string, options?)
23+
24+
#### options
25+
26+
Type: `object`
27+
28+
##### validate
29+
30+
Type: `boolean`\
31+
Default: `true`
32+
33+
Whether to validate the SVG as proper XML.
34+
35+
Turning this off can improve performance significantly.

0 commit comments

Comments
 (0)