Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
acagastya committed Oct 1, 2020
1 parent 8a57cce commit a952870
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = url => {
module.exports = (url, {httpOnly = true} = {}) => {
if (typeof url !== 'string') {
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``);
}
Expand All @@ -12,5 +12,5 @@ module.exports = url => {

// Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
// Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
return httpOnly ? /^https?:/.test(url) : /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
};
12 changes: 8 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import isAbsoluteUrl from '.';
test('main', t => {
t.true(isAbsoluteUrl('http://sindresorhus.com'));
t.true(isAbsoluteUrl('https://sindresorhus.com'));
t.true(isAbsoluteUrl('httpS://sindresorhus.com'));
t.true(isAbsoluteUrl('file://sindresorhus.com'));
t.true(isAbsoluteUrl('mailto:[email protected]'));
t.true(isAbsoluteUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'));
t.false(isAbsoluteUrl('httpS://sindresorhus.com'));
t.true(isAbsoluteUrl('httpS://sindresorhus.com', {httpOnly: false}));
t.false(isAbsoluteUrl('file://sindresorhus.com'));
t.true(isAbsoluteUrl('file://sindresorhus.com', {httpOnly: false}));
t.false(isAbsoluteUrl('mailto:[email protected]'));
t.true(isAbsoluteUrl('mailto:[email protected]', {httpOnly: false}));
t.false(isAbsoluteUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'));
t.true(isAbsoluteUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D', {httpOnly: false}));
t.false(isAbsoluteUrl('//sindresorhus.com'));
t.false(isAbsoluteUrl('/foo/bar'));
t.false(isAbsoluteUrl('foo/bar'));
Expand Down

0 comments on commit a952870

Please sign in to comment.