-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.fs.macaroon.js
114 lines (101 loc) · 2.7 KB
/
jquery.fs.macaroon.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
/*
* Macaroon v3.1.0 - 2015-04-04
* A jQuery plugin for simple access to browser cookies. Part of the Formstone Library.
* http://classic.formstone.it/macaroon/
*
* Copyright 2015 Ben Plum; MIT Licensed
*/
;(function ($, window) {
"use strict";
/**
* @options
* @param domain [string] "Cookie domain"
* @param expires [int] <604800000> "Time until cookie expires"
* @param path [string] "Cookie path"
*/
var options = {
domain: null,
expires: (7 * 24 * 60 * 60 * 1000), // 604800000 = 7 days
path: null
};
/**
* @method
* @name create
* @description Creates a cookie
* @param key [string] "Cookie key"
* @param value [string] "Cookie value"
* @param opts [object] "Options object"
* @example $.macaroon(key, value, options);
*/
function _create(key, value, opts) {
var date = new Date();
opts = $.extend({}, options, opts);
if (opts.expires || typeof opts.expires === "number") {
date.setTime(date.getTime() + opts.expires);
}
var expires = (opts.expires || typeof opts.expires === "number") ? "; expires=" + date.toGMTString() : "",
path = (opts.path) ? "; path=" + opts.path : "",
domain = (opts.domain) ? "; domain=" + opts.domain : "";
document.cookie = key + "=" + value + expires + domain + path;
}
/**
* @method
* @name read
* @description Returns a cookie's value, or null
* @param key [string] "Cookie key"
* @return [string | null] "Cookie's value, or null"
* @example var value = $.macaroon(key);
*/
function _read(key) {
var keyString = key + "=",
cookies = document.cookie.split(';');
for(var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
while (cookie.charAt(0) === ' ') {
cookie = cookie.substring(1, cookie.length);
}
if (cookie.indexOf(keyString) === 0) {
return cookie.substring(keyString.length, cookie.length);
}
}
return null;
}
/**
* @method
* @name erase
* @description Deletes a cookie
* @param key [string] "Cookie key"
* @example $.macaroon(key, null);
*/
function _erase(key) {
_create(key, "FALSE", { expires: -(7 * 24 * 60 * 60 * 1000) });
}
/**
* @method
* @name defaults
* @description Sets default plugin options
* @param opts [object] <{}> "Options object"
* @example $.macaroon(opts);
*/
$.macaroon = function(key, value, opts) {
// Set defaults
if (typeof key === "object") {
options = $.extend(options, key);
return null;
} else {
opts = $.extend({}, options, opts);
}
// Delegate intent
if (typeof key !== "undefined") {
if (typeof value !== "undefined") {
if (value === null) {
_erase(key);
} else {
_create(key, value, opts);
}
} else {
return _read(key);
}
}
};
})(jQuery, window);