-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* eslint-disable no-useless-escape */ | ||
/* eslint-disable no-useless-backreference */ | ||
/* \ | ||
|*| | ||
|*| :: cookies.js :: | ||
|*| | ||
|*| A complete cookies reader/writer framework with full unicode support. | ||
|*| | ||
|*| https://developer.mozilla.org/en-US/docs/DOM/document.cookie | ||
|*| | ||
|*| This framework is released under the GNU Public License, version 3 or later. | ||
|*| http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
|*| | ||
|*| Syntaxes: | ||
|*| | ||
|*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]]) | ||
|*| * docCookies.getItem(name) | ||
|*| * docCookies.removeItem(name[, path], domain) | ||
|*| * docCookies.hasItem(name) | ||
|*| * docCookies.keys() | ||
|*| | ||
\ */ | ||
|
||
export const docCookies = { | ||
getItem: function (cookie: string, sKey: string) { | ||
return ( | ||
decodeURIComponent( | ||
cookie.replace( | ||
new RegExp( | ||
'(?:(?:^|.*;)\\s*' + encodeURIComponent(sKey).replace(/[-.+*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$' | ||
), | ||
'$1' | ||
) | ||
) || undefined | ||
); | ||
}, | ||
setItem: function (sKey: string, sValue: string, vEnd?: number, sPath?: string, sDomain?: string, bSecure?: boolean) { | ||
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { | ||
return false; | ||
} | ||
let sExpires = ''; | ||
if (vEnd) { | ||
switch (vEnd.constructor) { | ||
case Number: | ||
sExpires = vEnd === Infinity ? '; expires=Fri, 31 Dec 9999 23:59:59 GMT' : '; max-age=' + vEnd; | ||
break; | ||
case String: | ||
sExpires = '; expires=' + vEnd; | ||
break; | ||
// case Date: | ||
// sExpires = "; expires=" + vEnd.toUTCString(); | ||
// break; | ||
} | ||
} | ||
document.cookie = | ||
encodeURIComponent(sKey) + | ||
'=' + | ||
encodeURIComponent(sValue) + | ||
sExpires + | ||
(sDomain ? '; domain=' + sDomain : '') + | ||
(sPath ? '; path=' + sPath : '') + | ||
(bSecure ? '; secure' : ''); | ||
return true; | ||
}, | ||
removeItem: function (cookie: string, sKey: string, sPath?: string, sDomain?: string) { | ||
if (!sKey || !this.hasItem(cookie, sKey)) { | ||
return false; | ||
} | ||
document.cookie = | ||
encodeURIComponent(sKey) + | ||
'=; expires=Thu, 01 Jan 1970 00:00:00 GMT' + | ||
(sDomain ? '; domain=' + sDomain : '') + | ||
(sPath ? '; path=' + sPath : ''); | ||
return true; | ||
}, | ||
hasItem: function (cookie: string, sKey: string) { | ||
return new RegExp('(?:^|;\\s*)' + encodeURIComponent(sKey).replace(/[-.+*]/g, '\\$&') + '\\s*\\=').test(cookie); | ||
}, | ||
keys: /* optional method: you can safely remove it! */ function (cookie: string) { | ||
const aKeys = cookie | ||
.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, '') | ||
.split(/\s*(?:\=[^;]*)?;\s*/); | ||
for (let nIdx = 0; nIdx < aKeys.length; nIdx++) { | ||
aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); | ||
} | ||
return aKeys; | ||
}, | ||
}; |