-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathkizzy.js
211 lines (179 loc) · 4.45 KB
/
kizzy.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
/*!
* Kizzy - a cross-browser LocalStorage API
* Copyright: Dustin Diaz 2012
* https://github.com/ded/kizzy
* License: MIT
*/
!function (name, definition) {
if (typeof module != 'undefined') module.exports = definition()
else if (typeof define == 'function' && typeof define.amd == 'object') define(definition)
else this[name] = definition()
}('kizzy', function () {
function noop() {}
var hasLocalStorage
, doc = document
, store = doc.domain
, html5 = 0
, writeThrough = function () {
return 1
}
try {
// HTML5 local storage
hasLocalStorage = !!localStorage || !!globalStorage
if (!localStorage) {
localStorage = globalStorage[store]
}
html5 = 1
} catch (ex1) {
html5 = 0
// IE local storage
try {
// this try / if is required. trust me
if (doc.documentElement.addBehavior) {
html5 = 0
hasLocalStorage = 1
var dataStore = doc.documentElement
dataStore.addBehavior('#default#userData')
dataStore.load(store)
var xmlDoc = dataStore.xmlDocument
, xmlDocEl = xmlDoc.documentElement
}
} catch (ex2) {
hasLocalStorage = false
}
}
var setLocalStorage = noop
, getLocalStorage = noop
, removeLocalStorage = noop
, clearLocalStorage = noop
if (hasLocalStorage) {
setLocalStorage = html5 ? html5setLocalStorage : setUserData
getLocalStorage = html5 ? html5getLocalStorage : getUserData
removeLocalStorage = html5 ? html5removeLocalStorage : removeUserData
clearLocalStorage = html5 ? html5clearLocalStorage : clearUserData
writeThrough = function (inst) {
try {
var v = JSON.stringify(inst._)
if( v == '{}' ) {
removeLocalStorage(inst.ns)
} else {
setLocalStorage(inst.ns, v)
}
return 1
} catch (x) {
return 0
}
}
}
function time() {
return +new Date()
}
function checkExpiry(inst, k) {
if (inst._[k] && inst._[k].e && inst._[k].e < time()) {
inst.remove(k)
}
}
function isNumber(n) {
return typeof n === 'number' && isFinite(n)
}
function html5getLocalStorage(k) {
return localStorage[k]
}
function html5setLocalStorage(k, v) {
localStorage[k] = v
return v
}
function html5removeLocalStorage(k) {
delete localStorage[k]
}
function html5clearLocalStorage() {
localStorage.clear()
}
function getNodeByName(name) {
var childNodes = xmlDocEl.childNodes
, node
, returnVal = null
for (var i = 0, len = childNodes.length; i < len; i++) {
node = childNodes.item(i)
if (node.getAttribute("key") == name) {
returnVal = node
break
}
}
return returnVal
}
function getUserData(name) {
var node = getNodeByName(name)
var returnVal = null
if (node) {
returnVal = node.getAttribute("value")
}
return returnVal
}
function setUserData(name, value) {
var node = getNodeByName(name)
if (!node) {
node = xmlDoc.createNode(1, "item", "")
node.setAttribute("key", name)
node.setAttribute("value", value)
xmlDocEl.appendChild(node)
}
else {
node.setAttribute("value", value)
}
dataStore.save(store)
return value
}
function removeUserData(name) {
getNodeByName(name) && xmlDocEl.removeChild(node)
dataStore.save(store)
}
function clearUserData() {
while (xmlDocEl.firstChild) {
xmlDocEl.removeChild(xmlDocEl.firstChild)
}
dataStore.save(store)
}
function _Kizzy() {
this._ = {}
}
_Kizzy.prototype = {
set: function (k, v, optTtl) {
this._[k] = {
value: v,
e: isNumber(optTtl) ? time() + optTtl : 0
}
writeThrough(this) || this.remove(k)
return v
},
get: function (k) {
checkExpiry(this, k)
return this._[k] ? this._[k].value : undefined
},
remove: function (k) {
delete this._[k];
writeThrough(this)
},
clear: function () {
this._ = {}
writeThrough(this)
},
clearExpireds: function() {
for (var k in this._) {
checkExpiry(this, k)
}
writeThrough(this)
}
}
function Kizzy(ns) {
this.ns = ns
this._ = JSON.parse(getLocalStorage(ns) || '{}')
}
Kizzy.prototype = _Kizzy.prototype
function kizzy(ns) {
return new Kizzy(ns)
}
kizzy.remove = removeLocalStorage
kizzy.clear = clearLocalStorage
return kizzy
})