Skip to content

Commit 22ba30a

Browse files
authored
[FIX] Fix all checkJs errors in uri.js (playcanvas#2887)
1 parent 87eea5f commit 22ba30a

File tree

1 file changed

+37
-65
lines changed

1 file changed

+37
-65
lines changed

src/core/uri.js

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @returns {string} A URI string.
1515
*/
1616
function createURI(options) {
17-
var s = "";
17+
let s = "";
1818
if ((options.authority || options.scheme) && (options.host || options.hostpath)) {
1919
throw new Error("Can't have 'scheme' or 'authority' and 'host' or 'hostpath' option");
2020
}
@@ -56,58 +56,32 @@ function createURI(options) {
5656
return s;
5757
}
5858

59-
/**
60-
* @private
61-
* @class
62-
* @name URI
63-
* @description Create a new URI object.
64-
* @classdesc A URI object.
65-
* @param {string} uri - URI string.
66-
*/
67-
function URI(uri) {
68-
// See http://tools.ietf.org/html/rfc2396#appendix-B for details of RegExp
69-
var re = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/,
70-
result = uri.match(re);
71-
72-
/**
73-
* @private
74-
* @name URI#scheme
75-
* @type {string}
76-
* @description The scheme. (e.g. Http).
77-
*/
78-
this.scheme = result[2];
79-
80-
/**
81-
* @private
82-
* @name URI#authority
83-
* @type {string}
84-
* @description The authority. (e.g. Www.example.com).
85-
*/
86-
this.authority = result[4];
87-
88-
/**
89-
* @private
90-
* @name URI#path
91-
* @type {string}
92-
* @description The path. (e.g. /users/example).
93-
*/
94-
this.path = result[5];
95-
96-
/**
97-
* @private
98-
* @name URI#query
99-
* @type {string}
100-
* @description The query, the section after a ?. (e.g. Search=value).
101-
*/
102-
this.query = result[7];
59+
// See http://tools.ietf.org/html/rfc2396#appendix-B for details of RegExp
60+
const re = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
10361

62+
class URI {
10463
/**
10564
* @private
106-
* @name URI#fragment
107-
* @type {string}
108-
* @description The fragment, the section after a #.
65+
* @class
66+
* @name URI
67+
* @description Create a new URI object.
68+
* @classdesc A URI object.
69+
* @param {string} uri - URI string.
70+
* @property {string} scheme The scheme. (e.g. http).
71+
* @property {string} authority The authority. (e.g. www.example.com).
72+
* @property {string} path The path. (e.g. /users/example).
73+
* @property {string} query The query, the section after a ?. (e.g. search=value).
74+
* @property {string} fragment The fragment, the section after a #.
10975
*/
110-
this.fragment = result[9];
76+
constructor(uri) {
77+
let result = uri.match(re);
78+
79+
this.scheme = result[2];
80+
this.authority = result[4];
81+
this.path = result[5];
82+
this.query = result[7];
83+
this.fragment = result[9];
84+
}
11185

11286
/**
11387
* @private
@@ -116,8 +90,8 @@ function URI(uri) {
11690
* @description Convert URI back to string.
11791
* @returns {string} The URI as a string.
11892
*/
119-
this.toString = function () {
120-
var s = "";
93+
toString() {
94+
let s = "";
12195

12296
if (this.scheme) {
12397
s += this.scheme + ":";
@@ -138,7 +112,7 @@ function URI(uri) {
138112
}
139113

140114
return s;
141-
};
115+
}
142116

143117
/**
144118
* @private
@@ -154,21 +128,19 @@ function URI(uri) {
154128
* console.log(q.b); // logs "2"
155129
* console.log(q.c); // logs "3"
156130
*/
157-
this.getQuery = function () {
158-
var vars;
159-
var pair;
160-
var result = {};
131+
getQuery() {
132+
const result = {};
161133

162134
if (this.query) {
163-
vars = decodeURIComponent(this.query).split("&");
164-
vars.forEach(function (item, index, arr) {
165-
pair = item.split("=");
135+
const queryParams = decodeURIComponent(this.query).split("&");
136+
for (const queryParam of queryParams) {
137+
let pair = queryParam.split("=");
166138
result[pair[0]] = pair[1];
167-
}, this);
139+
}
168140
}
169141

170142
return result;
171-
};
143+
}
172144

173145
/**
174146
* @private
@@ -185,9 +157,9 @@ function URI(uri) {
185157
* });
186158
* console.log(uri.toString()); // logs "http://example.com?a=1&b=2
187159
*/
188-
this.setQuery = function (params) {
189-
var q = "";
190-
for (var key in params) {
160+
setQuery(params) {
161+
let q = "";
162+
for (const key in params) {
191163
if (params.hasOwnProperty(key)) {
192164
if (q !== "") {
193165
q += "&";
@@ -197,7 +169,7 @@ function URI(uri) {
197169
}
198170

199171
this.query = q;
200-
};
172+
}
201173
}
202174

203175
export { createURI, URI };

0 commit comments

Comments
 (0)