-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add HTTP URLs verification by default #14
Conversation
readme.md
Outdated
``` | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do unrelated changes. Applies everywhere.
index.d.ts
Outdated
@@ -17,6 +17,6 @@ isAbsoluteUrl('foo/bar'); | |||
//=> false | |||
``` | |||
*/ | |||
declare function isAbsoluteUrl(url: string): boolean; | |||
declare function isAbsoluteUrl(url: string, options: object): boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be strongly-typed. See: https://github.com/sindresorhus/typescript-definition-style-guide/tree/57a925bb5b3eb46a4666eab31e1c883a26b7bfb5#documentation Should also have docs.
There are a lot of typos and formatting problems. Try to look over the diff and improve it: https://github.com/sindresorhus/is-absolute-url/pull/14/files |
Why 3 states instead of just a |
It's possible to simply set I've added the 3 states because 1) according to the RFC: URLs should be case-sensitive. And 2) it could be expanded in the future to check other conditions p.e. if is an absolute ftp address, contains the port or to check for secure Or do you think it's better to keep it simple and go in a way #12 tried to archive? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a lot of unrelated changes in the docs.
index.js
Outdated
@@ -1,16 +1,35 @@ | |||
'use strict'; | |||
|
|||
module.exports = url => { | |||
// Options - httpOnly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Options - httpOnly | |
// Options | |
// - httpOnly |
readme.md
Outdated
@@ -26,19 +38,90 @@ isAbsoluteUrl('foo/bar'); | |||
``` | |||
|
|||
|
|||
## Related | |||
## Options Parameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## Options Parameters | |
## Options |
readme.md
Outdated
|
||
See [is-relative-url](https://github.com/sindresorhus/is-relative-url) for the inverse. | ||
### httpOnly | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep the docs simple like how it's done in the Usage section. A simple js block with lines and their result as comments would be better for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood. Can you provide feedback now please?
I think it should be a single |
This is incorrect. The URL scheme is case-insensitive. |
You missed this from the original issue:
|
@@ -17,6 +17,10 @@ isAbsoluteUrl('foo/bar'); | |||
//=> false | |||
``` | |||
*/ | |||
declare function isAbsoluteUrl(url: string): boolean; | |||
interface Options { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be inside a namespace: https://github.com/sindresorhus/typescript-definition-style-guide
index.d.ts
Outdated
@@ -17,6 +17,10 @@ isAbsoluteUrl('foo/bar'); | |||
//=> false | |||
``` | |||
*/ | |||
declare function isAbsoluteUrl(url: string): boolean; | |||
interface Options { | |||
httpOnly?: boolean | string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tab-indentation
index.d.ts
Outdated
@param url - The URL to check. | ||
@param options - Object with different parameters to check. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't add much. I would remove it. People know what "options" means.
index.d.ts
Outdated
@@ -1,7 +1,7 @@ | |||
/** | |||
Check if a URL is absolute. | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do unrelated changes.
readme.md
Outdated
@@ -41,4 +56,4 @@ See [is-relative-url](https://github.com/sindresorhus/is-relative-url) for the i | |||
<sub> | |||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | |||
</sub> | |||
</div> | |||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do unrelated changes.
index.test-d.ts
Outdated
@@ -1,4 +1,4 @@ | |||
import {expectType} from 'tsd'; | |||
import isAbsoluteUrl = require('.'); | |||
|
|||
expectType<boolean>(isAbsoluteUrl('https://sindresorhus.com/foo/bar')); | |||
expectType<boolean>(isAbsoluteUrl('https://sindresorhus.com/foo/bar', { httpOnly: true })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectType<boolean>(isAbsoluteUrl('https://sindresorhus.com/foo/bar', { httpOnly: true })); | |
expectType<boolean>(isAbsoluteUrl('https://sindresorhus.com/foo/bar', {httpOnly: true})); |
readme.md
Outdated
isAbsoluteUrl('https://si', { httpOnly: true }); | ||
//=> true | ||
|
||
is absoluteUrl('mailto:[email protected]', { httpOnly: true }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
readme.md
Outdated
@@ -25,6 +25,21 @@ isAbsoluteUrl('foo/bar'); | |||
//=> false | |||
``` | |||
|
|||
## Options |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the same format for readme docs as used here: https://github.com/sindresorhus/boxen#api
test.js
Outdated
t.false(isAbsoluteUrl('C:\\Dev\\test-broken', {})); | ||
t.false(isAbsoluteUrl('ht,tp://sindresorhus.com', {})); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pull request needs a proper descriptive title. |
Done. I've also changed the title and description of this PR. |
index.js
Outdated
// - true : checks http(s) header and correct formatting or URL. | ||
// - false: checks only accordance with rfc3986 | ||
|
||
module.exports = (url, httpOnly = true) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
httpOnly
should be an options-object, not a parameter.
index.js
Outdated
if (typeof url !== 'string') { | ||
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``); | ||
} | ||
|
||
// Optional Chaining not supported by XO? please confirm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code comments is not the place to discuss PR stuff. You can just comment on your own PR if you have questions.
index.js
Outdated
if (typeof url !== 'string') { | ||
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``); | ||
} | ||
|
||
// Optional Chaining not supported by XO? please confirm | ||
if (httpOnly) { | ||
return /^(((([hH][tT]{2}[pP]([sS])?)?:\/\/)(www\.)?)([-a-zA-Z0-9()@:%_.~#?&//=]*))/g.test(url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex can be simplified.
index.js
Outdated
module.exports = url => { | ||
// Param: httpOnly | ||
// - true : checks http(s) header and correct formatting or URL. | ||
// - false: checks only accordance with rfc3986 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove these comments. They don't add anything.
readme.md
Outdated
Type: `boolean` | ||
Default: `true` | ||
|
||
Check if a URL is a http URL. Defaults to `true`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http
=> HTTP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the sentence could be clearer about what it actually does.
readme.md
Outdated
isAbsoluteUrl('https://si', true); | ||
//=> true | ||
|
||
isabsoluteUrl('mailto:[email protected]', false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
readme.md
Outdated
|
||
isabsoluteUrl('mailto:[email protected]', false); | ||
//=> true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace
test.js
Outdated
@@ -17,3 +17,61 @@ test('main', t => { | |||
t.false(isAbsoluteUrl('C:\\Dev\\test-broken')); | |||
t.false(isAbsoluteUrl('ht,tp://sindresorhus.com')); | |||
}); | |||
|
|||
test('HttpOnly set as true (same as default behavior). Assert TRUE tests', t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too verbose test title. Formatting needs work too.
test.js
Outdated
// eslint-disable-line no-script-url | ||
t.true(isAbsoluteUrl('javascript:throw%20document.cookie', false)); | ||
|
||
t.true(isAbsoluteUrl('https://sindresorhus', false)); // Should this behavior exist? missing TLD?... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of asking questions, try to research and figure it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I felt it should at least check for the TLD, but it would return false to URLs as http://localhost:7700
and they are absolute. So it will not check for it.
@@ -1,10 +1,14 @@ | |||
'use strict'; | |||
|
|||
module.exports = url => { | |||
module.exports = (url, options = {httpOnly: true}) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the correct way to do default values for options objects. If another option is added, it will silently not do the right thing as both options will be overwritten if one is set.
module.exports = (url, options = {httpOnly: true}) => { | |
module.exports = (url, {httpOnly = true} = {}) => { |
if (typeof url !== 'string') { | ||
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``); | ||
} | ||
|
||
if (options.httpOnly) { | ||
return /^((http[s]?)?:\/\/([-a-z0-9()@:%_.~#?&//=]*))/gi.test(url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex could still be simplified.
Co-authored-by: Sindre Sorhus <[email protected]>
Attempt to cover #11. Possible breaking changes.
It now verifies if it's an HTTP URL using a RegEx based on this
You can now use
isAbsolutePath('http://sindresorhus.com')
. It will behave asisAbsolutePath('http://sindresorhus.com', true)
.There is possible to
isAbsolutePath('http://sindresorhus.com', false)
and get the same results as before this PR (it even uses the original RegEx to evaluate the string).I've decided to K.I.S.S. and keep the PR as you mentioned here, in #11 and #12. Since there are no requests for extra features there's no need to "prepare" for expansion right now.
Note: this package returns
true
on situations likehttp://sindr esorhus.com
(white-space in between). I kept its original behavior. If you think it would be helpful for this package to check for whitespaces (and warn/return false) please inform me!This last object, called Options, can have some parameters (so it's easier to add extra features later). Right now I've only done 'httpOnly'.It has 3 levels of 'strictness':'strictest'
,'strict'
and'loose'
/'true
. Basicallystrictest
is the rigidhttp://www.lorem.ipsum.com/path
, thestrict
is something your browser could understand, andloose
only checks for the HTTP protocol header.I have wrote some examples on the /readme.md file.I've also copied the current array of tests to test against the 'levels of strictness'. If you have any doubt about each link I suggest open the test files and see what is the result of each "URL" against each parameter.Fixes #11