UNPKG

656 BJavaScriptView Raw
1'use strict';
2const htmlCommentRegex = require('html-comment-regex');
3
4const isBinary = buffer => {
5 const isBuffer = Buffer.isBuffer(buffer);
6
7 for (let i = 0; i < 24; i++) {
8 const characterCode = isBuffer ? buffer[i] : buffer.charCodeAt(i);
9
10 if (characterCode === 65533 || characterCode <= 8) {
11 return true;
12 }
13 }
14
15 return false;
16};
17
18const regex = /^\s*(?:<\?xml[^>]*>\s*)?(?:<!doctype svg[^>]*\s*(?:\[?(?:\s*<![^>]*>\s*)*\]?)*[^>]*>\s*)?<svg[^>]*>[^]*<\/svg>\s*$/i;
19
20const isSvg = input => Boolean(input) && !isBinary(input) && regex.test(input.toString().replace(htmlCommentRegex, ''));
21
22module.exports = isSvg;
23module.exports.default = isSvg;