js/storage/siteapp.storage.cookie.js   A
last analyzed

Complexity

Total Complexity 24
Complexity/F 4.8

Size

Lines of Code 52
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
dl 0
loc 52
rs 10
wmc 24
mnd 3
bc 12
fnc 5
bpm 2.4
cpm 4.8
noi 1

5 Functions

Rating   Name   Duplication   Size   Complexity  
A docCookies.removeItem 0 5 4
A docCookies.getItem 0 4 2
A docCookies.hasItem 0 4 3
C docCookies.setItem 0 28 11
A docCookies.keys 0 5 2
1
/*\
2
|*|
3
|*|	:: cookies.js ::
4
|*|
5
|*|	A complete cookies reader/writer framework with full unicode support.
6
|*|
7
|*|	Revision #3 - July 13th, 2017
8
|*|
9
|*|	https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
10
|*|	https://developer.mozilla.org/User:fusionchess
11
|*|	https://github.com/madmurphy/cookies.js
12
|*|
13
|*|	This framework is released under the GNU Public License, version 3 or later.
14
|*|	http://www.gnu.org/licenses/gpl-3.0-standalone.html
15
|*|
16
|*|	Syntaxes:
17
|*|
18
|*|	* docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
19
|*|	* docCookies.getItem(name)
20
|*|	* docCookies.removeItem(name[, path[, domain]])
21
|*|	* docCookies.hasItem(name)
22
|*|	* docCookies.keys()
23
|*|
24
\*/
25
26
var docCookies = {
27
	getItem: function (sKey) {
28
		if (!sKey) { return null; }
29
		return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
30
	},
31
	setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
32
		if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
33
		var sExpires = "";
34
		if (vEnd) {
35
			switch (vEnd.constructor) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
36
				case Number:
37
					sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
38
					/*
39
					Note: Despite officially defined in RFC 6265, the use of `max-age` is not compatible with any
40
					version of Internet Explorer, Edge and some mobile browsers. Therefore passing a number to
41
					the end parameter might not work as expected. A possible solution might be to convert the the
42
					relative time to an absolute time. For instance, replacing the previous line with:
43
					*/
44
					/*
45
					sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; expires=" + (new Date(vEnd * 1e3 + Date.now())).toUTCString();
46
					*/
47
					break;
48
				case String:
49
					sExpires = "; expires=" + vEnd;
50
					break;
51
				case Date:
52
					sExpires = "; expires=" + vEnd.toUTCString();
53
					break;
54
			}
55
		}
56
		document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
57
		return true;
58
	},
59
	removeItem: function (sKey, sPath, sDomain) {
60
		if (!this.hasItem(sKey)) { return false; }
61
		document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
62
		return true;
63
	},
64
	hasItem: function (sKey) {
65
		if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
66
		return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
67
	},
68
	keys: function () {
69
		var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
70
		for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
71
		return aKeys;
72
	}
73
};
74
75
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
76
	module.exports = docCookies;
77
}