Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
Fixes #79
Closes #80
Fixes #81
  • Loading branch information
sindresorhus committed Jan 15, 2022
1 parent 141d76c commit 0908e9c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 79 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
42 changes: 19 additions & 23 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import {Options as NormalizeUrlOptions} from 'normalize-url';

declare namespace getUrls {
interface Options extends NormalizeUrlOptions {
/**
Extract URLs that appear as query parameters in the found URLs.
export interface Options extends NormalizeUrlOptions {
/**
Extract URLs that appear as query parameters in the found URLs.
@default false
*/
readonly extractFromQueryString?: boolean;
@default false
*/
readonly extractFromQueryString?: boolean;

/**
Exclude URLs that match URLs in the given array.
/**
Exclude URLs that match URLs in the given array.
@default []
*/
readonly exclude?: string[];
@default []
*/
readonly exclude?: string[];

/**
Require URLs to have a scheme or leading `www.` to be considered an URL. When `false`, matches against a list of valid TLDs, so it will match URLs like `unicorn.education`.
/**
Require URLs to have a scheme or leading `www.` to be considered an URL. When `false`, matches against a list of valid TLDs, so it will match URLs like `unicorn.education`.
Does not affect URLs in query parameters if using the `extractFromQueryString` option.
Does not affect URLs in query parameters if using the `extractFromQueryString` option.
@default false
*/
readonly requireSchemeOrWww?: boolean;
}
@default false
*/
readonly requireSchemeOrWww?: boolean;
}

/**
Expand All @@ -36,14 +34,12 @@ The URLs will be [normalized](https://github.com/sindresorhus/normalize-url).
@example
```
import getUrls = require('get-urls');
import getUrls from 'get-urls';
const text = 'Lorem ipsum dolor sit amet, //sindresorhus.com consectetuer adipiscing http://yeoman.io elit.';
getUrls(text);
//=> Set {'http://sindresorhus.com', 'http://yeoman.io'}
```
*/
declare function getUrls(text: string, options?: getUrls.Options): Set<string>;

export = getUrls;
export default function getUrls(text: string, options?: Options): Set<string>;
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const urlRegex = require('url-regex-safe');
const normalizeUrl = require('normalize-url');
import urlRegex from 'url-regex-safe';
import normalizeUrl from 'normalize-url';

const getUrlsFromQueryParameters = url => {
const returnValue = new Set();
Expand All @@ -15,7 +14,7 @@ const getUrlsFromQueryParameters = url => {
return returnValue;
};

module.exports = (text, options = {}) => {
export default function getUrls(text, options = {}) {
if (typeof text !== 'string') {
throw new TypeError(`The \`text\` argument should be a string, got ${typeof text}`);
}
Expand All @@ -34,16 +33,18 @@ module.exports = (text, options = {}) => {

const urls = text.match(
urlRegex(options.requireSchemeOrWww === undefined ? undefined : {
strict: options.requireSchemeOrWww
})
strict: options.requireSchemeOrWww,
parens: true,
}),
) || [];

for (const url of urls) {
add(url);

if (options.extractFromQueryString) {
const qsUrls = getUrlsFromQueryParameters(url);
for (const qsUrl of qsUrls) {
add(qsUrl);
const queryStringUrls = getUrlsFromQueryParameters(url);
for (const queryStringUrl of queryStringUrls) {
add(queryStringUrl);
}
}
}
Expand All @@ -58,4 +59,4 @@ module.exports = (text, options = {}) => {
}

return returnValue;
};
}
6 changes: 3 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {expectType} from 'tsd';
import getUrls = require('.');
import getUrls from './index.js';

const text =
'Lorem ipsum dolor sit amet, //sindresorhus.com consectetuer adipiscing http://yeoman.io elit.';
const text
= 'Lorem ipsum dolor sit amet, //sindresorhus.com consectetuer adipiscing http://yeoman.io elit.';

expectType<Set<string>>(getUrls(text));
expectType<Set<string>>(getUrls(text, {extractFromQueryString: true}));
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10.12.0"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -31,12 +33,13 @@
"string"
],
"dependencies": {
"normalize-url": "^5.1.0",
"url-regex-safe": "^2.0.2"
"normalize-url": "^7.0.2",
"re2": "^1.17.2",
"url-regex-safe": "^3.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.13.1",
"xo": "^0.33.0"
"ava": "^4.0.1",
"tsd": "^0.19.1",
"xo": "^0.47.0"
}
}
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
The URLs will be [normalized](https://github.com/sindresorhus/normalize-url).

*Don't use this for any kind of security-related validation.*
*Do not use this for any kind of security-related validation.*

Please note the [known limitation](https://github.com/niftylettuce/url-regex-safe#limitations). You can work around this by setting `requireSchemeOrWww` to `true`.

## Install

```
$ npm install get-urls
```sh
npm install get-urls
```

## Usage

```js
const getUrls = require('get-urls');
import getUrls from 'get-urls';

const text = 'Lorem ipsum dolor sit amet, //sindresorhus.com consectetuer adipiscing http://yeoman.io elit.';

Expand Down
60 changes: 32 additions & 28 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import fs from 'node:fs';
import test from 'ava';
import getUrls from '.';
import getUrls from './index.js';

test('get unique cleaned-up urls from a string', t => {
t.deepEqual(
Expand All @@ -12,8 +12,8 @@ test('get unique cleaned-up urls from a string', t => {
'http://twitter.com/sindresorhus',
'https://tastejs.com',
'http://example.com',
'http://github.com'
])
'http://github.com',
]),
);
});

Expand All @@ -23,8 +23,8 @@ test('do not get nested urls from query strings', t => {
t.deepEqual(
getUrls(text),
new Set([
'http://awin1.com/cread.php?a=b&p=https%3A%2F%2Fuk.hotels.com%2Fhotel%2Fdetails.html%3Ftab%3Ddescription%26hotelId%3D287452%26q-localised-check-in%3D15%2F12%2F2017%26q-localised-check-out%3D19%2F12%2F2017%26q-room-0-adults%3D2%26q-room-0-children%3D0%26locale%3Den_GB%26pos%3DHCOM_UK'
])
'http://awin1.com/cread.php?a=b&p=https%3A%2F%2Fuk.hotels.com%2Fhotel%2Fdetails.html%3Ftab%3Ddescription%26hotelId%3D287452%26q-localised-check-in%3D15%2F12%2F2017%26q-localised-check-out%3D19%2F12%2F2017%26q-room-0-adults%3D2%26q-room-0-children%3D0%26locale%3Den_GB%26pos%3DHCOM_UK',
]),
);
});

Expand All @@ -35,8 +35,8 @@ test('get nested urls from query strings', t => {
getUrls(text, {extractFromQueryString: true}),
new Set([
'http://awin1.com/cread.php?a=b&p=https%3A%2F%2Fuk.hotels.com%2Fhotel%2Fdetails.html%3Ftab%3Ddescription%26hotelId%3D287452%26q-localised-check-in%3D15%2F12%2F2017%26q-localised-check-out%3D19%2F12%2F2017%26q-room-0-adults%3D2%26q-room-0-children%3D0%26locale%3Den_GB%26pos%3DHCOM_UK',
'https://uk.hotels.com/hotel/details.html?hotelId=287452&locale=en_GB&pos=HCOM_UK&q-localised-check-in=15%2F12%2F2017&q-localised-check-out=19%2F12%2F2017&q-room-0-adults=2&q-room-0-children=0&tab=description'
])
'https://uk.hotels.com/hotel/details.html?hotelId=287452&locale=en_GB&pos=HCOM_UK&q-localised-check-in=15%2F12%2F2017&q-localised-check-out=19%2F12%2F2017&q-room-0-adults=2&q-room-0-children=0&tab=description',
]),
);
});

Expand All @@ -45,7 +45,7 @@ test('don\'t strip hash when stripHash is set to false', t => {

t.deepEqual(
getUrls(text, {stripHash: false}),
new Set(['http://foobar.com/document.html#about'])
new Set(['http://foobar.com/document.html#about']),
);
});

Expand Down Expand Up @@ -91,29 +91,31 @@ test('exclude matching urls', t => {
'http://twitter.com/sindresorhus',
'https://tastejs.com',
'http://example.com',
'http://github.com'
])
'http://github.com',
]),
);
});

test('throw TypeError for non-array `exclude` option', t => {
t.throws(() => {
getUrls('http://w3.org/2000/svg', {exclude: ''});
}, 'The `exclude` option must be an array');
}, {
message: 'The `exclude` option must be an array',
});
});

test('get urls without scheme', t => {
const text = 'Lorem ipsum dolor sit amet, //sindresorhus.com consectetuer adipiscing http://yeoman.io elit. www.github.com';

t.deepEqual(
getUrls(text, {
extractFromQueryString: true
extractFromQueryString: true,
}),
new Set([
'http://sindresorhus.com',
'http://yeoman.io',
'http://github.com'
])
'http://github.com',
]),
);
});

Expand All @@ -122,12 +124,12 @@ test('get schemeless url from query string', t => {

t.deepEqual(
getUrls(text, {
extractFromQueryString: true
extractFromQueryString: true,
}),
new Set([
'http://awin1.com/cread.php?a=b&p=%2F%2Fuk.hotels.com%2Fhotel%2Fdetails.html%3Ftab%3Ddescription%26hotelId%3D287452%26q-localised-check-in%3D15%2F12%2F2017%26q-localised-check-out%3D19%2F12%2F2017%26q-room-0-adults%3D2%26q-room-0-children%3D0%26locale%3Den_GB%26pos%3DHCOM_UK',
'http://uk.hotels.com/hotel/details.html?hotelId=287452&locale=en_GB&pos=HCOM_UK&q-localised-check-in=15%2F12%2F2017&q-localised-check-out=19%2F12%2F2017&q-room-0-adults=2&q-room-0-children=0&tab=description'
])
'http://uk.hotels.com/hotel/details.html?hotelId=287452&locale=en_GB&pos=HCOM_UK&q-localised-check-in=15%2F12%2F2017&q-localised-check-out=19%2F12%2F2017&q-room-0-adults=2&q-room-0-children=0&tab=description',
]),
);
});

Expand All @@ -136,12 +138,12 @@ test('requireSchemeOrWww turned off', t => {

t.deepEqual(
getUrls(text, {
requireSchemeOrWww: false
requireSchemeOrWww: false,
}),
new Set([
'http://sindresorhus.com',
'http://unicorn.education'
])
'http://unicorn.education',
]),
);
});

Expand All @@ -168,15 +170,17 @@ test('filter all items from options.exclude', t => {
getUrls(text, {exclude}),
new Set([
'http://domain.com/81820190301/818201903010604/index.m3u8',
'http://domain.com/81820190301/818201903010606/index.m3u8'
])
'http://domain.com/81820190301/818201903010606/index.m3u8',
]),
);
});

test('throw an error when the text argument is not a string', t => {
t.throws(() => {
getUrls();
}, TypeError);
}, {
instanceOf: TypeError,
});
});

test('handles parens', t => {
Expand All @@ -185,8 +189,8 @@ test('handles parens', t => {
t.deepEqual(
getUrls(text),
new Set([
'https://sindresorhus.com/some/example'
])
'https://sindresorhus.com/some/example',
]),
);
});

Expand All @@ -197,7 +201,7 @@ test('handles Markdown', t => {
getUrls(text),
new Set([
'https://sindresorhus.com/unicorn.png',
'https://sindresorhus.com/?foo=bar'
])
'https://sindresorhus.com/?foo=bar',
]),
);
});

0 comments on commit 0908e9c

Please sign in to comment.