Skip to content

Commit d29071c

Browse files
committed
Require Node.js 14 and move to ESM
1 parent 35aeafd commit d29071c

7 files changed

+49
-81
lines changed

index.d.ts

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
declare const isSvg: {
2-
/**
3-
Check if a string or buffer is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics).
4-
5-
@param input - The data to check.
6-
@returns Whether `input` is SVG or not.
7-
8-
@example
9-
```
10-
import isSvg = require('is-svg');
11-
12-
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
13-
//=> true
14-
```
15-
*/
16-
(input: string | Buffer): boolean;
17-
18-
// TODO: Remove this for the next major release, refactor the whole definition to:
19-
// declare function isSvg(input: string | Buffer): boolean;
20-
// export = isSvg;
21-
default: typeof isSvg;
22-
};
23-
24-
export = isSvg;
1+
/**
2+
Check if a string is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics).
3+
4+
@example
5+
```
6+
import isSvg from 'is-svg';
7+
8+
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
9+
//=> true
10+
```
11+
*/
12+
export default function isSvg(string: string): boolean;

index.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
'use strict';
2-
const {XMLParser, XMLValidator} = require('fast-xml-parser');
1+
import {XMLParser, XMLValidator} from 'fast-xml-parser';
32

4-
const isSvg = input => {
5-
if (input === undefined || input === null) {
6-
return false;
3+
export default function isSvg(string) {
4+
if (typeof string !== 'string') {
5+
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
76
}
87

9-
input = input.toString().trim();
8+
string = string.trim();
109

11-
if (input.length === 0) {
10+
if (string.length === 0) {
1211
return false;
1312
}
1413

1514
// Has to be `!==` as it can also return an object with error info.
16-
if (XMLValidator.validate(input) !== true) {
15+
if (XMLValidator.validate(string) !== true) {
1716
return false;
1817
}
1918

2019
let jsonObject;
2120
const parser = new XMLParser();
2221

2322
try {
24-
jsonObject = parser.parse(input);
25-
} catch (_) {
23+
jsonObject = parser.parse(string);
24+
} catch {
2625
return false;
2726
}
2827

@@ -35,8 +34,4 @@ const isSvg = input => {
3534
}
3635

3736
return true;
38-
};
39-
40-
module.exports = isSvg;
41-
// TODO: Remove this for the next major release
42-
module.exports.default = isSvg;
37+
}

index.test-d.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import {expectType} from 'tsd';
2-
import isSvg = require('.');
2+
import isSvg from './index.js';
33

4-
const data = '<svg></svg>';
5-
6-
expectType<boolean>(isSvg(data));
7-
expectType<boolean>(isSvg(Buffer.from(data)));
4+
expectType<boolean>(isSvg('<svg></svg>'));

license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

package.json

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "is-svg",
33
"version": "4.4.0",
4-
"description": "Check if a string or buffer is SVG",
4+
"description": "Check if a string is SVG",
55
"license": "MIT",
66
"repository": "sindresorhus/is-svg",
77
"funding": "https://github.com/sponsors/sindresorhus",
@@ -10,8 +10,13 @@
1010
"email": "[email protected]",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": {
15+
"types": "./index.d.ts",
16+
"default": "./index.js"
17+
},
1318
"engines": {
14-
"node": ">=6"
19+
"node": ">=14.16"
1520
},
1621
"scripts": {
1722
"test": "xo && ava && tsd"
@@ -25,25 +30,21 @@
2530
"vector",
2631
"graphics",
2732
"image",
28-
"img",
29-
"pic",
3033
"picture",
3134
"type",
3235
"detect",
3336
"check",
3437
"is",
35-
"string",
36-
"str",
37-
"buffer"
38+
"string"
3839
],
3940
"dependencies": {
4041
"fast-xml-parser": "^4.1.3"
4142
},
4243
"devDependencies": {
43-
"@types/node": "^11.13.0",
44-
"ava": "^1.4.1",
45-
"time-span": "^4.0.0",
46-
"tsd": "^0.7.2",
47-
"xo": "^0.24.0"
44+
"@types/node": "^18.14.2",
45+
"ava": "^5.2.0",
46+
"time-span": "^5.1.0",
47+
"tsd": "^0.25.0",
48+
"xo": "^0.53.1"
4849
}
4950
}

readme.md

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
# is-svg
22

3-
> Check if a string or buffer is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics)
3+
> Check if a string is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics)
44
55
## Install
66

7-
```
8-
$ npm install is-svg
7+
```sh
8+
npm install is-svg
99
```
1010

1111
## Usage
1212

1313
```js
14-
const isSvg = require('is-svg');
14+
import isSvg from 'is-svg';
1515

1616
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
1717
//=> true
1818
```
19-
20-
---
21-
22-
<div align="center">
23-
<b>
24-
<a href="https://tidelift.com/subscription/pkg/npm-is-svg?utm_source=npm-is-svg&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
25-
</b>
26-
<br>
27-
<sub>
28-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
29-
</sub>
30-
</div>

test.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import fs from 'fs';
1+
import fs from 'node:fs';
22
import test from 'ava';
33
import timeSpan from 'time-span';
4-
import isSvg from '.';
4+
import isSvg from './index.js';
55

66
test('valid SVGs', t => {
7-
t.true(isSvg(fs.readFileSync('fixtures/fixture.svg')));
7+
t.true(isSvg(fs.readFileSync('fixtures/fixture.svg', 'utf8')));
88
t.true(isSvg('<svg width="100" height="100" viewBox="0 0 30 30" version="1.1"></svg>'));
99
t.true(isSvg('<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>'));
1010
t.true(isSvg('<?xml version="1.0" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<svg></svg>'));
@@ -30,17 +30,16 @@ version="1.1"
3030
});
3131

3232
test('invalid SVGs', t => {
33-
t.false(isSvg(fs.readFileSync('fixtures/fixture.jpg')));
33+
t.false(isSvg(fs.readFileSync('fixtures/fixture.jpg', 'utf8')));
3434
t.false(isSvg('<div><svg></svg>'));
3535
t.false(isSvg('<div><svg></svg></div>'));
36-
t.false(isSvg(fs.readFileSync('index.js')));
37-
t.false(isSvg());
36+
t.false(isSvg(fs.readFileSync('index.js', 'utf8')));
3837
t.false(isSvg('this string contains an svg <svg></svg> in the middle'));
3938
t.false(isSvg('<svg><div></svg>'));
4039
t.false(isSvg('this string ends with an svg <svg></svg>'));
4140
t.false(isSvg('<svg> hello I am an svg oops maybe not'));
4241
t.false(isSvg('this is not svg, but it mentions <svg> tags'));
43-
t.false(isSvg(fs.readFileSync('readme.md')));
42+
t.false(isSvg(fs.readFileSync('readme.md', 'utf8')));
4443

4544
// https://github.com/NaturalIntelligence/fast-xml-parser/issues/327
4645
// t.false(isSvg('<svg></svg> this string starts with an svg'));
@@ -85,7 +84,7 @@ test('support markup inside Entity tags', t => {
8584
test('regex should not be quadratic', t => {
8685
const end = timeSpan();
8786

88-
isSvg(`<!doctype svg ${' '.repeat(34560)}`);
87+
isSvg(`<!doctype svg ${' '.repeat(34_560)}`);
8988

9089
if (end.seconds() < 10) {
9190
t.pass();

0 commit comments

Comments
 (0)