Skip to content

Commit

Permalink
Add TypeScript definition (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 15, 2019
1 parent 8d74328 commit cb62f6d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
40 changes: 40 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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.
@default false
*/
readonly extractFromQueryString?: boolean;

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

/**
Get all URLs in a string.
The URLs will be [normalized](https://github.com/sindresorhus/normalize-url).
@returns A `Set` of URLs.
@example
```
import getUrls = require('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;
10 changes: 10 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {expectType} from 'tsd';
import getUrls = require('.');

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}));
expectType<Set<string>>(getUrls(text, {exclude: ['foo']}));
expectType<Set<string>>(getUrls(text, {defaultProtocol: 'ftp'}));
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"get",
Expand All @@ -30,11 +31,12 @@
"string"
],
"dependencies": {
"normalize-url": "^4.1.0",
"url-regex": "^4.0.0"
"normalize-url": "^4.3.0",
"url-regex": "^4.1.1"
},
"devDependencies": {
"ava": "^1.2.1",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ $ npm install get-urls
## Usage

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

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

getUrls(text);
Expand Down Expand Up @@ -49,7 +51,8 @@ Extract URLs that appear as query parameters in the found URLs.

#### exclude

Type: `string[]`
Type: `string[]`<br>
Default: `[]`

Exclude URLs that match URLs in the given array.

Expand Down

0 comments on commit cb62f6d

Please sign in to comment.