Total Complexity | 17 |
Complexity/F | 8.5 |
Lines of Code | 58 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | "use strict"; |
||
2 | const hasToStringTag = require("has-to-string-tag-x"); |
||
3 | const isObject = require("is-object"); |
||
4 | |||
5 | const toString = Object.prototype.toString; |
||
6 | const urlClass = "[object URL]"; |
||
7 | |||
8 | const hash = "hash"; |
||
9 | const host = "host"; |
||
10 | const hostname = "hostname"; |
||
11 | const href = "href"; |
||
12 | const password = "password"; |
||
13 | const pathname = "pathname"; |
||
14 | const port = "port"; |
||
15 | const protocol = "protocol"; |
||
16 | const search = "search"; |
||
17 | const username = "username"; |
||
18 | |||
19 | |||
20 | |||
21 | const isURL = (url, supportIncomplete/*=false*/) => |
||
22 | { |
||
23 | if (!isObject(url)) return false; |
||
|
|||
24 | |||
25 | // Native implementation in older browsers |
||
26 | if (!hasToStringTag && toString.call(url) === urlClass) return true; |
||
27 | |||
28 | if (!(href in url)) return false; |
||
29 | if (!(protocol in url)) return false; |
||
30 | if (!(username in url)) return false; |
||
31 | if (!(password in url)) return false; |
||
32 | if (!(hostname in url)) return false; |
||
33 | if (!(port in url)) return false; |
||
34 | if (!(host in url)) return false; |
||
35 | if (!(pathname in url)) return false; |
||
36 | if (!(search in url)) return false; |
||
37 | if (!(hash in url)) return false; |
||
38 | |||
39 | if (supportIncomplete !== true) |
||
40 | { |
||
41 | if (!isObject(url.searchParams)) return false; |
||
42 | |||
43 | // TODO :: write a separate isURLSearchParams ? |
||
44 | } |
||
45 | |||
46 | return true; |
||
47 | } |
||
48 | |||
49 | |||
50 | |||
51 | isURL.lenient = url => |
||
52 | { |
||
53 | return isURL(url, true); |
||
54 | }; |
||
55 | |||
56 | |||
57 | |||
58 | module.exports = isURL; |
||
59 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.