| Total Complexity | 15 |
| Complexity/F | 1.88 |
| Lines of Code | 71 |
| Function Count | 8 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | export default class URLString { |
||
| 2 | constructor () { |
||
| 3 | 24 | this.url = null |
|
| 4 | 24 | this.path = {} |
|
| 5 | 24 | this.searchParams = {} |
|
| 6 | } |
||
| 7 | |||
| 8 | setUrl (url) { |
||
| 9 | 24 | if (!url || url.constructor !== String) { |
|
| 10 | 1 | throw new Error('url isnt a valid string') |
|
| 11 | } |
||
| 12 | 23 | this.url = new URL(url).href |
|
|
|
|||
| 13 | } |
||
| 14 | |||
| 15 | setSearchParams (searchParams) { |
||
| 16 | 21 | if (!searchParams) { |
|
| 17 | 12 | return |
|
| 18 | } |
||
| 19 | 9 | if (searchParams.constructor !== Object) { |
|
| 20 | 1 | throw new Error('searchParams isnt a valid object') |
|
| 21 | } |
||
| 22 | 8 | this.searchParams = searchParams |
|
| 23 | } |
||
| 24 | |||
| 25 | setPath (path) { |
||
| 26 | 22 | if (!path) { |
|
| 27 | 1 | return |
|
| 28 | } |
||
| 29 | 21 | if (path.constructor !== Object) { |
|
| 30 | 1 | throw new Error('path isnt a valid object') |
|
| 31 | } |
||
| 32 | 20 | this.path = path |
|
| 33 | } |
||
| 34 | |||
| 35 | urlReducer (accumulator, [key, value]) { |
||
| 36 | 32 | if (!value) { |
|
| 37 | 2 | return accumulator + '/' + key |
|
| 38 | } |
||
| 39 | |||
| 40 | 30 | return accumulator + '/' + key + '/' + value |
|
| 41 | } |
||
| 42 | |||
| 43 | toString () { |
||
| 44 | 20 | const completeUrl = new URL(Object.entries(this.path).reduce(this.urlReducer, this.url)) |
|
| 45 | 20 | Object.entries(this.searchParams).forEach(([searchParam, searchValue]) => { |
|
| 46 | 9 | if (searchValue) { |
|
| 47 | 7 | completeUrl.searchParams.append(searchParam, searchValue) |
|
| 48 | } |
||
| 49 | }) |
||
| 50 | |||
| 51 | 20 | return completeUrl.href |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Generate an url from the url, path and search params |
||
| 56 | * |
||
| 57 | * @param {string} url |
||
| 58 | * @param {object} path |
||
| 59 | * @param {object} searchParams |
||
| 60 | * |
||
| 61 | * @return {string} |
||
| 62 | */ |
||
| 63 | static create ({ url, path, searchParams }) { |
||
| 64 | 24 | const newUrl = new URLString() |
|
| 65 | 24 | newUrl.setUrl(url) |
|
| 66 | 22 | newUrl.setPath(path) |
|
| 67 | 21 | newUrl.setSearchParams(searchParams) |
|
| 68 | |||
| 69 | 20 | return newUrl.toString() |
|
| 70 | } |
||
| 71 | } |
||
| 72 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.