Skip to content

Require Node.js 8, add TypeScript definition #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
sudo: false
language: node_js
node_js:
- '6'
- '4'
- '10'
- '8'
50 changes: 50 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
declare namespace urlRegex {
interface Options {
/**
Only match an exact string. Useful with `RegExp#test` to check if a string is a URL.

@default false
*/
readonly exact?: boolean;

/**
Force URLs to start with a valid protocol or `www`. If set to `false` it'll match the TLD against a list of valid [TLDs](https://github.com/stephenmathieson/node-tlds).

@default true
*/
readonly strict?: boolean;
}
}

/**
Regular expression for matching URLs.

@example
```
import urlRegex = require('url-regex');

urlRegex().test('http://github.com foo bar');
//=> true

urlRegex().test('www.github.com foo bar');
//=> true

urlRegex({exact: true}).test('http://github.com foo bar');
//=> false

urlRegex({exact: true}).test('http://github.com');
//=> true

urlRegex({strict: false}).test('github.com foo bar');
//=> true

urlRegex({exact: true, strict: false}).test('github.com');
//=> true

'foo http://github.com bar //google.com'.match(urlRegex());
//=> ['http://github.com', '//google.com']
```
*/
declare function urlRegex(options?: urlRegex.Options): RegExp;

export = urlRegex;
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
const ipRegex = require('ip-regex');
const tlds = require('tlds');

module.exports = opts => {
opts = Object.assign({strict: true}, opts);
module.exports = options => {
options = {
strict: true,
...options
};

const protocol = `(?:(?:[a-z]+:)?//)${opts.strict ? '' : '?'}`;
const protocol = `(?:(?:[a-z]+:)?//)${options.strict ? '' : '?'}`;
const auth = '(?:\\S+(?::\\S*)?@)?';
const ip = ipRegex.v4().source;
const host = '(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)';
const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
const tld = `(?:\\.${opts.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`;
const tld = `(?:\\.${options.strict ? '(?:[a-z\\u00a1-\\uffff]{2,})' : `(?:${tlds.sort((a, b) => b.length - a.length).join('|')})`})\\.?`;
const port = '(?::\\d{2,5})?';
const path = '(?:[/?#][^\\s"]*)?';
const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ip}|${host}${domain}${tld})${port}${path}`;

return opts.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig');
return options.exact ? new RegExp(`(?:^${regex}$)`, 'i') : new RegExp(regex, 'ig');
};
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import urlRegex = require('.');

expectType<RegExp>(urlRegex());
expectType<RegExp>(urlRegex({exact: true}));
expectType<RegExp>(urlRegex({strict: false}));
69 changes: 34 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
{
"name": "url-regex",
"version": "4.1.1",
"description": "Regular expression for matching URLs",
"license": "MIT",
"repository": "kevva/url-regex",
"author": {
"name": "Kevin Mårtensson",
"email": "[email protected]",
"url": "https://github.com/kevva"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"regex",
"string",
"url"
],
"dependencies": {
"ip-regex": "^1.0.1",
"tlds": "^1.187.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
"name": "url-regex",
"version": "4.1.1",
"description": "Regular expression for matching URLs",
"license": "MIT",
"repository": "kevva/url-regex",
"author": {
"name": "Kevin Mårtensson",
"email": "[email protected]",
"url": "https://github.com/kevva"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"regex",
"string",
"url"
],
"dependencies": {
"ip-regex": "^4.1.0",
"tlds": "^1.203.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Based on this [gist](https://gist.github.com/dperini/729294) by Diego Perini.
## Install

```
$ npm install --save url-regex
$ npm install url-regex
```


Expand Down Expand Up @@ -42,9 +42,9 @@ urlRegex({exact: true, strict: false}).test('github.com');

## API

### urlRegex(options)
### urlRegex([options])

Returns a regex for matching URLs.
Returns a `RegExp` for matching URLs.

#### options

Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import m from './';
import urlRegex from '.';

test('match exact URLs', t => {
const fixtures = [
Expand Down Expand Up @@ -63,7 +63,7 @@ test('match exact URLs', t => {
];

for (const x of fixtures) {
t.true(m({exact: true}).test(x));
t.true(urlRegex({exact: true}).test(x));
}
});

Expand All @@ -82,7 +82,7 @@ test('match URLs in text', t => {
'http://example.com/with-path',
'https://another.example.com',
'//bar.net/?q=Query'
], fixture.match(m()));
], fixture.match(urlRegex()));
});

test('do not match URLs', t => {
Expand Down Expand Up @@ -129,7 +129,7 @@ test('do not match URLs', t => {
];

for (const x of fixtures) {
t.false(m({exact: true}).test(x));
t.false(urlRegex({exact: true}).test(x));
}
});

Expand Down Expand Up @@ -192,6 +192,6 @@ test('match using list of TLDs', t => {
];

for (const x of fixtures) {
t.true(m({exact: true, strict: false}).test(x));
t.true(urlRegex({exact: true, strict: false}).test(x));
}
});