Skip to content

Commit

Permalink
Require Node.js 14 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 28, 2023
1 parent 35aeafd commit d29071c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 81 deletions.
36 changes: 12 additions & 24 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
declare const isSvg: {
/**
Check if a string or buffer is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics).
@param input - The data to check.
@returns Whether `input` is SVG or not.
@example
```
import isSvg = require('is-svg');
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
//=> true
```
*/
(input: string | Buffer): boolean;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function isSvg(input: string | Buffer): boolean;
// export = isSvg;
default: typeof isSvg;
};

export = isSvg;
/**
Check if a string is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics).
@example
```
import isSvg from 'is-svg';
isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
//=> true
```
*/
export default function isSvg(string: string): boolean;
25 changes: 10 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
'use strict';
const {XMLParser, XMLValidator} = require('fast-xml-parser');
import {XMLParser, XMLValidator} from 'fast-xml-parser';

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

input = input.toString().trim();
string = string.trim();

if (input.length === 0) {
if (string.length === 0) {
return false;
}

// Has to be `!==` as it can also return an object with error info.
if (XMLValidator.validate(input) !== true) {
if (XMLValidator.validate(string) !== true) {
return false;
}

let jsonObject;
const parser = new XMLParser();

try {
jsonObject = parser.parse(input);
} catch (_) {
jsonObject = parser.parse(string);
} catch {
return false;
}

Expand All @@ -35,8 +34,4 @@ const isSvg = input => {
}

return true;
};

module.exports = isSvg;
// TODO: Remove this for the next major release
module.exports.default = isSvg;
}
7 changes: 2 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {expectType} from 'tsd';
import isSvg = require('.');
import isSvg from './index.js';

const data = '<svg></svg>';

expectType<boolean>(isSvg(data));
expectType<boolean>(isSvg(Buffer.from(data)));
expectType<boolean>(isSvg('<svg></svg>'));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

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

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:

Expand Down
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "is-svg",
"version": "4.4.0",
"description": "Check if a string or buffer is SVG",
"description": "Check if a string is SVG",
"license": "MIT",
"repository": "sindresorhus/is-svg",
"funding": "https://github.com/sponsors/sindresorhus",
Expand All @@ -10,8 +10,13 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": ">=6"
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -25,25 +30,21 @@
"vector",
"graphics",
"image",
"img",
"pic",
"picture",
"type",
"detect",
"check",
"is",
"string",
"str",
"buffer"
"string"
],
"dependencies": {
"fast-xml-parser": "^4.1.3"
},
"devDependencies": {
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"time-span": "^4.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"@types/node": "^18.14.2",
"ava": "^5.2.0",
"time-span": "^5.1.0",
"tsd": "^0.25.0",
"xo": "^0.53.1"
}
}
20 changes: 4 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
# is-svg

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

```
$ npm install is-svg
```sh
npm install is-svg
```

## Usage

```js
const isSvg = require('is-svg');
import isSvg from 'is-svg';

isSvg('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
//=> true
```

---

<div align="center">
<b>
<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>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
15 changes: 7 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from 'fs';
import fs from 'node:fs';
import test from 'ava';
import timeSpan from 'time-span';
import isSvg from '.';
import isSvg from './index.js';

test('valid SVGs', t => {
t.true(isSvg(fs.readFileSync('fixtures/fixture.svg')));
t.true(isSvg(fs.readFileSync('fixtures/fixture.svg', 'utf8')));
t.true(isSvg('<svg width="100" height="100" viewBox="0 0 30 30" version="1.1"></svg>'));
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>'));
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>'));
Expand All @@ -30,17 +30,16 @@ version="1.1"
});

test('invalid SVGs', t => {
t.false(isSvg(fs.readFileSync('fixtures/fixture.jpg')));
t.false(isSvg(fs.readFileSync('fixtures/fixture.jpg', 'utf8')));
t.false(isSvg('<div><svg></svg>'));
t.false(isSvg('<div><svg></svg></div>'));
t.false(isSvg(fs.readFileSync('index.js')));
t.false(isSvg());
t.false(isSvg(fs.readFileSync('index.js', 'utf8')));
t.false(isSvg('this string contains an svg <svg></svg> in the middle'));
t.false(isSvg('<svg><div></svg>'));
t.false(isSvg('this string ends with an svg <svg></svg>'));
t.false(isSvg('<svg> hello I am an svg oops maybe not'));
t.false(isSvg('this is not svg, but it mentions <svg> tags'));
t.false(isSvg(fs.readFileSync('readme.md')));
t.false(isSvg(fs.readFileSync('readme.md', 'utf8')));

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

isSvg(`<!doctype svg ${' '.repeat(34560)}`);
isSvg(`<!doctype svg ${' '.repeat(34_560)}`);

if (end.seconds() < 10) {
t.pass();
Expand Down

0 comments on commit d29071c

Please sign in to comment.