14
14
* @returns {string } A URI string.
15
15
*/
16
16
function createURI ( options ) {
17
- var s = "" ;
17
+ let s = "" ;
18
18
if ( ( options . authority || options . scheme ) && ( options . host || options . hostpath ) ) {
19
19
throw new Error ( "Can't have 'scheme' or 'authority' and 'host' or 'hostpath' option" ) ;
20
20
}
@@ -56,58 +56,32 @@ function createURI(options) {
56
56
return s ;
57
57
}
58
58
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 = / ^ ( ( [ ^ : \/ ? # ] + ) : ) ? ( \/ \/ ( [ ^ \/ ? # ] * ) ) ? ( [ ^ ? # ] * ) ( \? ( [ ^ # ] * ) ) ? ( # ( .* ) ) ? / ;
103
61
62
+ class URI {
104
63
/**
105
64
* @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 #.
109
75
*/
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
+ }
111
85
112
86
/**
113
87
* @private
@@ -116,8 +90,8 @@ function URI(uri) {
116
90
* @description Convert URI back to string.
117
91
* @returns {string } The URI as a string.
118
92
*/
119
- this . toString = function ( ) {
120
- var s = "" ;
93
+ toString ( ) {
94
+ let s = "" ;
121
95
122
96
if ( this . scheme ) {
123
97
s += this . scheme + ":" ;
@@ -138,7 +112,7 @@ function URI(uri) {
138
112
}
139
113
140
114
return s ;
141
- } ;
115
+ }
142
116
143
117
/**
144
118
* @private
@@ -154,21 +128,19 @@ function URI(uri) {
154
128
* console.log(q.b); // logs "2"
155
129
* console.log(q.c); // logs "3"
156
130
*/
157
- this . getQuery = function ( ) {
158
- var vars ;
159
- var pair ;
160
- var result = { } ;
131
+ getQuery ( ) {
132
+ const result = { } ;
161
133
162
134
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 ( "=" ) ;
166
138
result [ pair [ 0 ] ] = pair [ 1 ] ;
167
- } , this ) ;
139
+ }
168
140
}
169
141
170
142
return result ;
171
- } ;
143
+ }
172
144
173
145
/**
174
146
* @private
@@ -185,9 +157,9 @@ function URI(uri) {
185
157
* });
186
158
* console.log(uri.toString()); // logs "http://example.com?a=1&b=2
187
159
*/
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 ) {
191
163
if ( params . hasOwnProperty ( key ) ) {
192
164
if ( q !== "" ) {
193
165
q += "&" ;
@@ -197,7 +169,7 @@ function URI(uri) {
197
169
}
198
170
199
171
this . query = q ;
200
- } ;
172
+ }
201
173
}
202
174
203
175
export { createURI , URI } ;
0 commit comments