-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
37 lines (28 loc) · 693 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import {XMLParser, XMLValidator} from 'fast-xml-parser';
export default function isSvg(string) {
if (typeof string !== 'string') {
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
}
string = string.trim();
if (string.length === 0) {
return false;
}
// Has to be `!==` as it can also return an object with error info.
if (XMLValidator.validate(string) !== true) {
return false;
}
let jsonObject;
const parser = new XMLParser();
try {
jsonObject = parser.parse(string);
} catch {
return false;
}
if (!jsonObject) {
return false;
}
if (!Object.keys(jsonObject).some(x => x.toLowerCase() === 'svg')) {
return false;
}
return true;
}