forked from nbubna/store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore2.js
230 lines (221 loc) · 8.61 KB
/
store2.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
/*! store2 - v2.3.2 - 2015-10-27
* Copyright (c) 2015 Nathan Bubna; Licensed MIT, GPL */
;(function(window, define) {
var _ = {
version: "2.3.2",
areas: {},
apis: {},
// utilities
inherit: function(api, o) {
for (var p in api) {
if (!o.hasOwnProperty(p)) {
o[p] = api[p];
}
}
return o;
},
stringify: function(d) {
return d === undefined || typeof d === "function" ? d+'' : JSON.stringify(d);
},
parse: function(s) {
// if it doesn't parse, return as is
try{ return JSON.parse(s); }catch(e){ return s; }
},
// extension hooks
fn: function(name, fn) {
_.storeAPI[name] = fn;
for (var api in _.apis) {
_.apis[api][name] = fn;
}
},
get: function(area, key){ return area.getItem(key); },
set: function(area, key, string){ area.setItem(key, string); },
remove: function(area, key){ area.removeItem(key); },
key: function(area, i){ return area.key(i); },
length: function(area){ return area.length; },
clear: function(area){ area.clear(); },
// core functions
Store: function(id, area, namespace) {
var store = _.inherit(_.storeAPI, function(key, data, overwrite) {
if (arguments.length === 0){ return store.getAll(); }
if (data !== undefined){ return store.set(key, data, overwrite); }
if (typeof key === "string" || typeof key === "number"){ return store.get(key); }
if (!key){ return store.clear(); }
return store.setAll(key, data);// overwrite=data, data=key
});
store._id = id;
try {
var testKey = '_safariPrivate_';
area.setItem(testKey, 'sucks');
store._area = area;
area.removeItem(testKey);
} catch (e) {}
if (!store._area) {
store._area = _.inherit(_.storageAPI, { items: {}, name: 'fake' });
}
store._ns = namespace || '';
if (!_.areas[id]) {
_.areas[id] = store._area;
}
if (!_.apis[store._ns+store._id]) {
_.apis[store._ns+store._id] = store;
}
return store;
},
storeAPI: {
// admin functions
area: function(id, area) {
var store = this[id];
if (!store || !store.area) {
store = _.Store(id, area, this._ns);//new area-specific api in this namespace
if (!this[id]){ this[id] = store; }
}
return store;
},
namespace: function(namespace, noSession) {
if (!namespace){
return this._ns ? this._ns.substring(0,this._ns.length-1) : '';
}
var ns = namespace, store = this[ns];
if (!store || !store.namespace) {
store = _.Store(this._id, this._area, this._ns+ns+'.');//new namespaced api
if (!this[ns]){ this[ns] = store; }
if (!noSession){ store.area('session', _.areas.session); }
}
return store;
},
isFake: function(){ return this._area.name === 'fake'; },
toString: function() {
return 'store'+(this._ns?'.'+this.namespace():'')+'['+this._id+']';
},
// storage functions
has: function(key) {
if (this._area.has) {
return this._area.has(this._in(key));//extension hook
}
return !!(this._in(key) in this._area);
},
size: function(){ return this.keys().length; },
each: function(fn, and) {
for (var i=0, m=_.length(this._area); i<m; i++) {
var key = this._out(_.key(this._area, i));
if (key !== undefined) {
if (fn.call(this, key, and || this.get(key)) === false) {
break;
}
}
if (m > _.length(this._area)) { m--; i--; }// in case of removeItem
}
return and || this;
},
keys: function() {
return this.each(function(k, list){ list.push(k); }, []);
},
get: function(key, alt) {
var s = _.get(this._area, this._in(key));
return s !== null ? _.parse(s) : alt || s;// support alt for easy default mgmt
},
getAll: function() {
return this.each(function(k, all){ all[k] = this.get(k); }, {});
},
set: function(key, data, overwrite) {
var d = this.get(key);
if (d != null && overwrite === false) {
return data;
}
return _.set(this._area, this._in(key), _.stringify(data), overwrite) || d;
},
setAll: function(data, overwrite) {
var changed, val;
for (var key in data) {
val = data[key];
if (this.set(key, val, overwrite) !== val) {
changed = true;
}
}
return changed;
},
remove: function(key) {
var d = this.get(key);
_.remove(this._area, this._in(key));
return d;
},
clear: function() {
if (!this._ns) {
_.clear(this._area);
} else {
this.each(function(k){ _.remove(this._area, this._in(k)); }, 1);
}
return this;
},
clearAll: function() {
var area = this._area;
for (var id in _.areas) {
if (_.areas.hasOwnProperty(id)) {
this._area = _.areas[id];
this.clear();
}
}
this._area = area;
return this;
},
// internal use functions
_in: function(k) {
if (typeof k !== "string"){ k = _.stringify(k); }
return this._ns ? this._ns + k : k;
},
_out: function(k) {
return this._ns ?
k && k.indexOf(this._ns) === 0 ?
k.substring(this._ns.length) :
undefined : // so each() knows to skip it
k;
}
},// end _.storeAPI
storageAPI: {
length: 0,
has: function(k){ return this.items.hasOwnProperty(k); },
key: function(i) {
var c = 0;
for (var k in this.items){
if (this.has(k) && i === c++) {
return k;
}
}
},
setItem: function(k, v) {
if (!this.has(k)) {
this.length++;
}
this.items[k] = v;
},
removeItem: function(k) {
if (this.has(k)) {
delete this.items[k];
this.length--;
}
},
getItem: function(k){ return this.has(k) ? this.items[k] : null; },
clear: function(){ for (var k in this.list){ this.removeItem(k); } },
toString: function(){ return this.length+' items in '+this.name+'Storage'; }
}// end _.storageAPI
};
// setup the primary store fn
if (window.store){ _.conflict = window.store; }
var store =
// safely set this up (throws error in IE10/32bit mode for local files)
_.Store("local", (function(){try{ return localStorage; }catch(e){}})());
store.local = store;// for completeness
store._ = _;// for extenders and debuggers...
// safely setup store.session (throws exception in FF for file:/// urls)
store.area("session", (function(){try{ return sessionStorage; }catch(e){}})());
//Expose store to the global object
window.store = store;
if (typeof define === 'function' && define.amd !== undefined) {
define(function () {
return store;
});
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = store;
}
})(this, this.define);