@@ -85,34 +85,29 @@ var pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;
85
85
* The object has the various cookies as keys(names) => values
86
86
*
87
87
* @param {string } str
88
- * @param {object } [options ]
88
+ * @param {object } [opt ]
89
89
* @return {object }
90
90
* @public
91
91
*/
92
92
93
- function parse ( str , options ) {
93
+ function parse ( str , opt ) {
94
94
if ( typeof str !== 'string' ) {
95
95
throw new TypeError ( 'argument str must be a string' ) ;
96
96
}
97
97
98
98
var obj = { } ;
99
99
var len = str . length ;
100
100
// RFC 6265 sec 4.1.1, RFC 2616 2.2 defines a cookie name consists of one char minimum, plus '='.
101
- var max = len - 2 ;
102
- if ( max < 0 ) return obj ;
101
+ if ( len < 2 ) return obj ;
103
102
104
- var dec = ( options && options . decode ) || decode ;
103
+ var dec = ( opt && opt . decode ) || decode ;
105
104
var index = 0 ;
106
105
var eqIdx = 0 ;
107
106
var endIdx = 0 ;
108
107
109
108
do {
110
109
eqIdx = str . indexOf ( '=' , index ) ;
111
-
112
- // no more cookie pairs
113
- if ( eqIdx === - 1 ) {
114
- break ;
115
- }
110
+ if ( eqIdx === - 1 ) break ; // No more cookie pairs.
116
111
117
112
endIdx = str . indexOf ( ';' , index ) ;
118
113
@@ -129,7 +124,7 @@ function parse(str, options) {
129
124
var key = str . slice ( keyStartIdx , keyEndIdx ) ;
130
125
131
126
// only assign once
132
- if ( undefined === obj [ key ] ) {
127
+ if ( ! obj . hasOwnProperty ( key ) ) {
133
128
var valStartIdx = startIndex ( str , eqIdx + 1 , endIdx ) ;
134
129
var valEndIdx = endIndex ( str , endIdx , valStartIdx ) ;
135
130
@@ -143,7 +138,7 @@ function parse(str, options) {
143
138
}
144
139
145
140
index = endIdx + 1
146
- } while ( index < max ) ;
141
+ } while ( index < len ) ;
147
142
148
143
return obj ;
149
144
}
@@ -175,14 +170,13 @@ function endIndex(str, index, min) {
175
170
*
176
171
* @param {string } name
177
172
* @param {string } val
178
- * @param {object } [options ]
173
+ * @param {object } [opt ]
179
174
* @return {string }
180
175
* @public
181
176
*/
182
177
183
- function serialize ( name , val , options ) {
184
- var opt = options || { } ;
185
- var enc = opt . encode || encode ;
178
+ function serialize ( name , val , opt ) {
179
+ var enc = ( opt && opt . encode ) || encodeURIComponent ;
186
180
187
181
if ( typeof enc !== 'function' ) {
188
182
throw new TypeError ( 'option encode is invalid' ) ;
@@ -194,20 +188,21 @@ function serialize(name, val, options) {
194
188
195
189
var value = enc ( val ) ;
196
190
197
- if ( value && ! cookieValueRegExp . test ( value ) ) {
191
+ if ( ! cookieValueRegExp . test ( value ) ) {
198
192
throw new TypeError ( 'argument val is invalid' ) ;
199
193
}
200
194
201
195
var str = name + '=' + value ;
196
+ if ( ! opt ) return str ;
202
197
203
198
if ( null != opt . maxAge ) {
204
- var maxAge = opt . maxAge - 0 ;
199
+ var maxAge = Math . floor ( opt . maxAge ) ;
205
200
206
201
if ( ! isFinite ( maxAge ) ) {
207
202
throw new TypeError ( 'option maxAge is invalid' )
208
203
}
209
204
210
- str += '; Max-Age=' + Math . floor ( maxAge ) ;
205
+ str += '; Max-Age=' + maxAge ;
211
206
}
212
207
213
208
if ( opt . domain ) {
@@ -250,8 +245,7 @@ function serialize(name, val, options) {
250
245
251
246
if ( opt . priority ) {
252
247
var priority = typeof opt . priority === 'string'
253
- ? opt . priority . toLowerCase ( )
254
- : opt . priority
248
+ ? opt . priority . toLowerCase ( ) : opt . priority ;
255
249
256
250
switch ( priority ) {
257
251
case 'low' :
@@ -306,17 +300,6 @@ function decode (str) {
306
300
: str
307
301
}
308
302
309
- /**
310
- * URL-encode value.
311
- *
312
- * @param {string } val
313
- * @returns {string }
314
- */
315
-
316
- function encode ( val ) {
317
- return encodeURIComponent ( val )
318
- }
319
-
320
303
/**
321
304
* Determine if value is a Date.
322
305
*
@@ -325,8 +308,7 @@ function encode (val) {
325
308
*/
326
309
327
310
function isDate ( val ) {
328
- return __toString . call ( val ) === '[object Date]' ||
329
- val instanceof Date
311
+ return __toString . call ( val ) === '[object Date]' ;
330
312
}
331
313
332
314
/**
0 commit comments