@@ -85,34 +85,29 @@ var pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;
8585 * The object has the various cookies as keys(names) => values
8686 *
8787 * @param {string } str
88- * @param {object } [options ]
88+ * @param {object } [opt ]
8989 * @return {object }
9090 * @public
9191 */
9292
93- function parse ( str , options ) {
93+ function parse ( str , opt ) {
9494 if ( typeof str !== 'string' ) {
9595 throw new TypeError ( 'argument str must be a string' ) ;
9696 }
9797
9898 var obj = { } ;
9999 var len = str . length ;
100100 // 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 ;
103102
104- var dec = ( options && options . decode ) || decode ;
103+ var dec = ( opt && opt . decode ) || decode ;
105104 var index = 0 ;
106105 var eqIdx = 0 ;
107106 var endIdx = 0 ;
108107
109108 do {
110109 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.
116111
117112 endIdx = str . indexOf ( ';' , index ) ;
118113
@@ -129,7 +124,7 @@ function parse(str, options) {
129124 var key = str . slice ( keyStartIdx , keyEndIdx ) ;
130125
131126 // only assign once
132- if ( undefined === obj [ key ] ) {
127+ if ( ! obj . hasOwnProperty ( key ) ) {
133128 var valStartIdx = startIndex ( str , eqIdx + 1 , endIdx ) ;
134129 var valEndIdx = endIndex ( str , endIdx , valStartIdx ) ;
135130
@@ -143,7 +138,7 @@ function parse(str, options) {
143138 }
144139
145140 index = endIdx + 1
146- } while ( index < max ) ;
141+ } while ( index < len ) ;
147142
148143 return obj ;
149144}
@@ -175,14 +170,13 @@ function endIndex(str, index, min) {
175170 *
176171 * @param {string } name
177172 * @param {string } val
178- * @param {object } [options ]
173+ * @param {object } [opt ]
179174 * @return {string }
180175 * @public
181176 */
182177
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 ;
186180
187181 if ( typeof enc !== 'function' ) {
188182 throw new TypeError ( 'option encode is invalid' ) ;
@@ -194,20 +188,21 @@ function serialize(name, val, options) {
194188
195189 var value = enc ( val ) ;
196190
197- if ( value && ! cookieValueRegExp . test ( value ) ) {
191+ if ( ! cookieValueRegExp . test ( value ) ) {
198192 throw new TypeError ( 'argument val is invalid' ) ;
199193 }
200194
201195 var str = name + '=' + value ;
196+ if ( ! opt ) return str ;
202197
203198 if ( null != opt . maxAge ) {
204- var maxAge = opt . maxAge - 0 ;
199+ var maxAge = Math . floor ( opt . maxAge ) ;
205200
206201 if ( ! isFinite ( maxAge ) ) {
207202 throw new TypeError ( 'option maxAge is invalid' )
208203 }
209204
210- str += '; Max-Age=' + Math . floor ( maxAge ) ;
205+ str += '; Max-Age=' + maxAge ;
211206 }
212207
213208 if ( opt . domain ) {
@@ -250,8 +245,7 @@ function serialize(name, val, options) {
250245
251246 if ( opt . priority ) {
252247 var priority = typeof opt . priority === 'string'
253- ? opt . priority . toLowerCase ( )
254- : opt . priority
248+ ? opt . priority . toLowerCase ( ) : opt . priority ;
255249
256250 switch ( priority ) {
257251 case 'low' :
@@ -306,17 +300,6 @@ function decode (str) {
306300 : str
307301}
308302
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-
320303/**
321304 * Determine if value is a Date.
322305 *
@@ -325,8 +308,7 @@ function encode (val) {
325308 */
326309
327310function isDate ( val ) {
328- return __toString . call ( val ) === '[object Date]' ||
329- val instanceof Date
311+ return __toString . call ( val ) === '[object Date]' ;
330312}
331313
332314/**
0 commit comments