| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
( function () { |
| 14 |
|
| 15 |
const pluses = /\+/g; |
| 16 |
let config = null, cookie; |
| 17 |
|
| 18 |
function raw( s ) { |
| 19 |
return s; |
| 20 |
} |
| 21 |
|
| 22 |
function decoded( s ) { |
| 23 |
try { |
| 24 |
return unRfc2068( decodeURIComponent( s.replace( pluses, ' ' ) ) ); |
| 25 |
} catch ( e ) { |
| 26 |
|
| 27 |
|
| 28 |
return ''; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
function unRfc2068( value ) { |
| 33 |
if ( value.indexOf( '"' ) === 0 ) { |
| 34 |
|
| 35 |
value = value.slice( 1, -1 ).replace( /\\"/g, '"' ).replace( /\\\\/g, '\\' ); |
| 36 |
} |
| 37 |
return value; |
| 38 |
} |
| 39 |
|
| 40 |
function fromJSON( value ) { |
| 41 |
return config.json ? JSON.parse( value ) : value; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
config = cookie = function ( key, value, options ) { |
| 57 |
|
| 58 |
|
| 59 |
if ( value !== undefined ) { |
| 60 |
options = Object.assign( {}, config.defaults, options ); |
| 61 |
|
| 62 |
if ( value === null ) { |
| 63 |
options.expires = -1; |
| 64 |
} |
| 65 |
|
| 66 |
if ( typeof options.expires === 'number' ) { |
| 67 |
const days = options.expires, t = options.expires = new Date(); |
| 68 |
t.setDate( t.getDate() + days ); |
| 69 |
} |
| 70 |
|
| 71 |
value = config.json ? JSON.stringify( value ) : String( value ); |
| 72 |
|
| 73 |
try { |
| 74 |
return ( document.cookie = [ |
| 75 |
encodeURIComponent( key ), '=', config.raw ? value : encodeURIComponent( value ), |
| 76 |
options.expires ? '; expires=' + options.expires.toUTCString() : '', |
| 77 |
options.path ? '; path=' + options.path : '', |
| 78 |
options.domain ? '; domain=' + options.domain : '', |
| 79 |
options.secure ? '; secure' : '', |
| 80 |
|
| 81 |
options.sameSite ? '; samesite=' + options.sameSite : '' |
| 82 |
].join( '' ) ); |
| 83 |
} catch ( e ) { |
| 84 |
|
| 85 |
return ''; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
const decode = config.raw ? raw : decoded; |
| 91 |
let cookies; |
| 92 |
try { |
| 93 |
cookies = document.cookie.split( '; ' ); |
| 94 |
} catch ( e ) { |
| 95 |
|
| 96 |
cookies = []; |
| 97 |
} |
| 98 |
let result = key ? null : {}; |
| 99 |
for ( let i = 0, l = cookies.length; i < l; i++ ) { |
| 100 |
const parts = cookies[ i ].split( '=' ); |
| 101 |
const name = decode( parts.shift() ); |
| 102 |
const s = decode( parts.join( '=' ) ); |
| 103 |
|
| 104 |
if ( key && key === name ) { |
| 105 |
result = fromJSON( s ); |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
if ( !key ) { |
| 110 |
result[ name ] = fromJSON( s ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return result; |
| 115 |
}; |
| 116 |
|
| 117 |
config.defaults = {}; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
function removeCookie( key, options ) { |
| 128 |
if ( cookie( key ) !== null ) { |
| 129 |
cookie( key, null, options ); |
| 130 |
return true; |
| 131 |
} |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
module.exports = { |
| 136 |
cookie, |
| 137 |
removeCookie |
| 138 |
}; |
| 139 |
}() ); |