@@ -27,11 +27,11 @@ function tryDecode(str: string, decode: (str: string) => string) {
27
27
*/
28
28
export function parse (
29
29
str : string ,
30
- options ? : {
30
+ options : {
31
31
decode ?: ( ( str : string ) => string ) | undefined
32
- } ,
32
+ } = { } ,
33
33
) : Record < string , string > {
34
- const decode = options ? .decode ?? decodeURIComponent
34
+ options . decode ??= decodeURIComponent
35
35
36
36
const obj : Record < string , string > = { }
37
37
const pairs = str . split ( pairSplitRegExp )
@@ -49,7 +49,7 @@ export function parse(
49
49
if ( '"' === val [ 0 ] ) val = val . slice ( 1 , - 1 )
50
50
51
51
// only assign once
52
- if ( obj [ key ] == null ) obj [ key ] = tryDecode ( val , decode )
52
+ if ( obj [ key ] == null ) obj [ key ] = tryDecode ( val , options . decode )
53
53
}
54
54
55
55
return obj
@@ -66,45 +66,45 @@ export type SerializeOptions = Partial<{
66
66
expires : Date
67
67
} >
68
68
69
- export function serialize ( name : string , val : string , opt : SerializeOptions = { } ) : string {
70
- if ( ! opt . encode ) opt . encode = encodeURIComponent
69
+ export function serialize ( name : string , val : string , options : SerializeOptions = { } ) : string {
70
+ options . encode ?? = encodeURIComponent
71
71
72
72
if ( ! fieldContentRegExp . test ( name ) ) throw new TypeError ( "argument name is invalid" )
73
73
74
- const value = opt . encode ( val )
74
+ const value = options . encode ( val )
75
75
76
76
if ( value && ! fieldContentRegExp . test ( value ) ) throw new TypeError ( "argument val is invalid" )
77
77
78
78
let str = `${ name } =${ value } `
79
79
80
- if ( null != opt . maxAge ) {
81
- const maxAge = opt . maxAge - 0
80
+ if ( null != options . maxAge ) {
81
+ const maxAge = options . maxAge - 0
82
82
83
83
if ( Number . isNaN ( maxAge ) || ! Number . isFinite ( maxAge ) ) throw new TypeError ( "option maxAge is invalid" )
84
84
85
85
str += `; Max-Age=${ Math . floor ( maxAge ) } `
86
86
}
87
87
88
- if ( opt . domain ) {
89
- if ( ! fieldContentRegExp . test ( opt . domain ) ) throw new TypeError ( "option domain is invalid" )
88
+ if ( options . domain ) {
89
+ if ( ! fieldContentRegExp . test ( options . domain ) ) throw new TypeError ( "option domain is invalid" )
90
90
91
- str += `; Domain=${ opt . domain } `
91
+ str += `; Domain=${ options . domain } `
92
92
}
93
93
94
- if ( opt . path ) {
95
- if ( ! fieldContentRegExp . test ( opt . path ) ) throw new TypeError ( "option path is invalid" )
94
+ if ( options . path ) {
95
+ if ( ! fieldContentRegExp . test ( options . path ) ) throw new TypeError ( "option path is invalid" )
96
96
97
- str += `; Path=${ opt . path } `
97
+ str += `; Path=${ options . path } `
98
98
}
99
99
100
- if ( opt . expires ) str += `; Expires=${ opt . expires . toUTCString ( ) } `
100
+ if ( options . expires ) str += `; Expires=${ options . expires . toUTCString ( ) } `
101
101
102
- if ( opt . httpOnly ) str += "; HttpOnly"
102
+ if ( options . httpOnly ) str += "; HttpOnly"
103
103
104
- if ( opt . secure ) str += "; Secure"
104
+ if ( options . secure ) str += "; Secure"
105
105
106
- if ( opt . sameSite ) {
107
- const sameSite = typeof opt . sameSite === "string" ? opt . sameSite . toLowerCase ( ) : opt . sameSite
106
+ if ( options . sameSite ) {
107
+ const sameSite = typeof options . sameSite === "string" ? options . sameSite . toLowerCase ( ) : options . sameSite
108
108
109
109
switch ( sameSite ) {
110
110
case true :
0 commit comments