This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookiejs.es.js
159 lines (135 loc) · 3.65 KB
/
cookiejs.es.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*!
* cookiejs.js | v1.0.1 | cookiejs object for setting/getting/removing cookies
* Copyright (c) 2017 Eric Zieger (MIT license)
* https://github.com/theZieger/cookiejs.js/blob/master/LICENSE
*/
/**
* throwTypeError
*
* @param {String} sName
* @param {String} sType
*
* @throws {TypeError}
*/
var throwTypeError = function(sName, sType) {
throw new TypeError(sName + ' is not of type ' + sType);
};
/**
* check variable (specifically sCookieName) for a non-falsy value and it's type to be string
*
* @param {String} sCookieName
*
* @throws {TypeError}
*/
var checkCookieName = function(sCookieName) {
isString(sCookieName, 'sCookieName');
if (!sCookieName) {
throwTypeError('sCookieName', 'string');
}
};
/**
* check variable type to be a string
*
* @param {String} variableToTest
* @param {String} name
*
* @throws {TypeError}
*/
var isString = function(variableToTest, name) {
if (typeof variableToTest !== 'string') {
throwTypeError(name, 'string');
}
};
/**
* check variable type to be an object
*
* @param {String} variableToTest
*
* @returns {bool}
*/
var isObject = function(variableToTest) {
return typeof variableToTest === 'object' && !Array.isArray(variableToTest);
};
var cookiejs = {
global: typeof window !== 'undefined' ? document : { cookie: '' },
/**
* sets or overwrites a cookie
*
* @param {String} sCookieName - the name of the cookie you want to set
* @param {String} sValue - the value you want to set
* @param {String} oAttributes - options e.g. domain, path, expires
*
* @throws {TypeError} if argument sCookieName is empty or not a string
*/
set: function(sCookieName, sValue, oAttributes) {
var sAttributes = '';
sValue = sValue || '';
checkCookieName(sCookieName);
isString(sValue, 'sValue');
if (oAttributes === undefined) {
sAttributes += '; path=/';
} else {
if (!isObject(oAttributes)) {
throwTypeError('oAttributes', 'object');
}
Object.keys(oAttributes).forEach(function(sAttr) {
var cookiePropValue = oAttributes[sAttr];
if (sAttr === 'secure') {
if (cookiePropValue !== true && cookiePropValue !== 'true') {
throwTypeError(sAttr, 'boolean');
}
sAttributes += ';' + sAttr;
} else {
isString(cookiePropValue, sAttr);
sAttributes += ';' + sAttr + '=' + cookiePropValue;
}
});
}
cookiejs.global.cookie =
encodeURIComponent(sCookieName) +
'=' +
encodeURIComponent(sValue) +
sAttributes;
},
/**
* returns the value of a cookie
*
* @param {String} sCookieName
*
* @throws {TypeError}
*
* @returns {String|Boolean}
*/
get: function(sCookieName) {
var gCookieValue = false;
checkCookieName(sCookieName);
cookiejs.global.cookie.split('; ').some(function(sCookie) {
var aCookie = sCookie.split('=');
if (decodeURIComponent(aCookie[0]) === sCookieName) {
gCookieValue = decodeURIComponent(aCookie[1]);
return true;
}
return false;
});
return gCookieValue;
},
/**
* removes a specific cookie
*
* oAttributes must contain the correct path and domain it won't
* remove the cookie
*
* @param {String} sCookieName
* @param {Object} oAttributes - options e.g. domain, path, expires
*
* @throws {TypeError}
*/
remove: function(sCookieName, oAttributes) {
var oRemoveAttributes = oAttributes || {};
if (isObject(oRemoveAttributes)) {
oRemoveAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
}
cookiejs.set(sCookieName, '', oRemoveAttributes);
}
};
export default cookiejs;