-
Notifications
You must be signed in to change notification settings - Fork 0
/
icookie.js
269 lines (248 loc) · 8.8 KB
/
icookie.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
(function(factory) {
if (typeof exports === 'object' && typeof module === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define("iCookie", [], factory);
} else if (typeof exports === 'object') {
exports.iCookie = factory();
} else {
window.iCookie = factory();
}
})(function() {
var docUrl = 'https://github.com/oxDesigner/iCookie/blob/master/README.md',
cookieObj = {},
optionObj = {
path: '/'
},
cookieStr = '',
class2type = {}.toString;
function iCookie() {
}
function checkType(obj) {
return class2type.call(obj);
}
function setCookieStr(key, value) {
if (typeof value === 'object') {
value = JSON.stringify(value);
}
return key + "=" + escape(value);
}
function setOptionStr(config) {
var configArr = [];
for (var key in config) {
var value = config[key];
if (key === 'expires') {
value = getExpires(config[key])
}
configArr.push(key + '=' + value);
}
return configArr.join(';');
}
function getCookieObj() {
if (cookieStr !== document.cookie) {
cookieStr = document.cookie;
cookieObj = {};
if (cookieStr) {
var cookieArr = cookieStr.split('; ');
cookieArr.forEach(function(item) {
var _cookieArr = item.split('=');
cookieObj[_cookieArr[0]] = unescape(_cookieArr[1]);
});
}
}
return cookieObj;
}
function getExpires(i) {
var date = new Date(),
re = /^\d+$|^(\d+)([a-z])$/;
if (re.test(i)) {
if (typeof i === 'number') {
date.setDate(date.getDate() + i);
} else {
var $1 = Number(RegExp.$1),
$2 = RegExp.$2;
switch ($2) {
case 'y':
date.setFullYear(date.getFullYear() + $1);
break;
case 'm':
date.setMonth(date.getMonth() + $1);
break;
case 'd':
date.setDate(date.getDate() + $1);
break;
case 'h':
date.setHours(date.getHours() + $1);
break;
default:
break;
}
}
} else {
throw new Error('过期时间必须为数字或字符串:1y:1年、2m:2月、3d&3:3天、4h:4小时, 看文档' + docUrl);
}
return date.toUTCString();
}
function removeFn(iCookie, key, options) {
if (iCookie.get(key) != null) {
if (options) {
options.expires = -1;
} else {
options = -1;
}
iCookie.set(key, '', options);
} else {
throw new Error('iCookie.remove: 删除失败,看文档 ' + docUrl);
}
}
iCookie.extend = function() {
var options, name, copy,
target = {},
i = 0,
length = arguments.length;
if (length === 1) {
target = this;
}
for (; i < length; i++) {
if ((options = arguments[i]) != null) {
for (name in options) {
copy = options[name];
if (target === copy) {
continue;
}
if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
}
iCookie.extend({
set: function() {
var args = arguments,
arg1,
arg2,
arg3,
cookieArr = [],
configObj = iCookie.configObj || {},
configStr,
key;
if (args.length === 0) {
throw new Error('iCookie.set: 你参数没写,看文档 ' + docUrl);
return;
}
if (args.length === 1) {
arg1 = args[0];
if (checkType(arg1) === '[object Object]') {
configStr = setOptionStr(iCookie.extend(optionObj, configObj));
for (key in arg1) {
cookieArr.push(setCookieStr(key, arg1[key]) + ';' + configStr);
}
} else {
throw new Error('iCookie.set: 一个参数必须为json, 看文档 ' + docUrl);
return;
}
}
if (args.length === 2) {
arg1 = args[0];
arg2 = args[1];
if (typeof arg1 === 'string') {
configStr = setOptionStr(iCookie.extend(optionObj, configObj));
cookieArr.push(setCookieStr(arg1, arg2) + ';' + configStr);
} else if (checkType(arg1) === '[object Object]' && checkType(arg2) === '[object Object]') {
configStr = setOptionStr(iCookie.extend(optionObj, configObj, arg2));
for (key in arg1) {
cookieArr.push(setCookieStr(key, arg1[key]) + ';' + configStr);
}
} else {
throw new Error('iCookie.set: 你参数写错了, 看文档 ' + docUrl);
return;
}
}
if (args.length === 3) {
arg1 = args[0];
arg2 = args[1];
arg3 = args[2];
if (typeof arg1 === 'string') {
var arg3Type = checkType(arg3);
if (arg3Type === '[object Number]') {
configStr = setOptionStr(iCookie.extend(optionObj, configObj, {
expires: arg3
}));
cookieArr.push(setCookieStr(arg1, arg2) + ';' + configStr);
} else if (arg3Type === '[object Object]') {
configStr = setOptionStr(iCookie.extend(optionObj, configObj, arg3));
cookieArr.push(setCookieStr(arg1, arg2) + ';' + configStr);
}
} else {
throw new Error('iCookie.set: 你参数写错了, 看文档 ' + docUrl);
return;
}
}
if (args.length > 3) {
throw new Error('iCookie.set: 你参数写错了, 看文档 ' + docUrl);
return;
}
cookieArr.forEach(function(item) {
document.cookie = item;
});
return cookieArr;
},
get: function() {
var args = arguments,
arg;
if (args.length === 0) {
return getCookieObj();
}
if (args.length === 1) {
arg = args[0];
var cookieObj = getCookieObj();
if (typeof arg === 'string') {
return cookieObj[arg];
} else if (checkType(arg) === '[object Array]') {
var obj = {};
arg.forEach(function(item) {
obj[item] = cookieObj[item];
})
return obj;
} else {
throw new Error('iCookie.get: 你参数写错了, 看文档 ' + docUrl);
return;
}
}
if (args.length > 1) {
throw new Error('iCookie.get: 你参数写错了, 看文档 ' + docUrl);
return;
}
},
remove: function() {
var args = arguments,
arg1, arg2;
if (args.length === 0) {
throw new Error('iCookie.remove: 你参数写错了, 看文档 ' + docUrl);
return;
}
if (args.length === 1) {
arg1 = args[0];
if (typeof arg === 'string') {
removeFn(iCookie, arg1);
}
}
if (args.length === 2) {
arg1 = args[0];
arg2 = args[1];
if (typeof arg1 === 'string' && checkType(arg2) === '[object Object]') {
removeFn(iCookie, arg1, arg2);
}
}
if (args.length > 2) {
throw new Error('iCookie.remove: 你参数写错了, 看文档 ' + docUrl);
}
},
config: function(config) {
iCookie.configObj = config;
}
});
return iCookie;
})