-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathCore.Storage.js
More file actions
171 lines (158 loc) · 5.32 KB
/
Core.Storage.js
File metadata and controls
171 lines (158 loc) · 5.32 KB
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
/*globals define, Core, window, localStorage*/
(function () {
var coreStorage = function () {
var storage,
fallbackStorage = {},
keysLoaded = false,
allKeysString = "allKeys",
allKeys = [],
isLocalStorageSupported = (function() {
var isSupported;
try {
isSupported = 'localStorage' in window && window['localStorage'] !== null;
}
catch(e) {
isSupported = false;
}
return isSupported;
})(),
rebuildKeyIndex = function () {
var key;
//first set all keys to a new array
allKeys = [];
//iterate all keys
for (key in storage) {
allKeys.push(key);
}
//finally store the keys in local storage again
storage[allKeysString] = JSON.stringify(allKeys);
},
loadKeys = function () {
if (keysLoaded === false) {
var storedKeys = getObject(allKeysString);
if (storedKeys === null) {
allKeys = [];
}
else if (Object.prototype.toString.call(storedKeys) !== '[object Array]') {
//we are in trouble here, the stored keys string is not an array
//we need to rebuild the index
rebuildKeyIndex();
}
else {
allKeys = storedKeys;
}
keysLoaded = true;
}
},
getKeys = function () {
return allKeys.slice(0);
},
findKeys = function (regularExpressionString) {
var i,
arrayLength = allKeys.length,
returnValue = [],
re = new RegExp(regularExpressionString, "g"),
key;
for (i = 0; i < arrayLength; i++) {
key = allKeys[i];
if (re.test(key) === true) {
returnValue.push(key);
}
}
return returnValue;
},
setItem = function(key, value) {
try {
//we store all keys that are being used so we do not have to iterate them
//from local storage (which is slow)
if (allKeys.indexOf(key) === -1) {
allKeys.push(key);
storage[allKeysString] = JSON.stringify(allKeys);
}
storage[key] = value;
}
catch (err) {
//we have run out of room, clear it all
clear();
}
},
getItem = function(key) {
var item = storage[key];
if (item === undefined) {
//localStorage returns null, not undefined if an item does not exist
//so do the same if using the fallback
item = null;
}
return item;
},
setObject = function (key, value) {
setItem(key, JSON.stringify(value));
},
getObject = function(key) {
var item = JSON.parse(getItem(key));
if (item === undefined) {
//localStorage returns null, not undefined if an item does not exist
//so do the same if using the fallback
item = null;
}
return item;
},
removeItem = function (key) {
var indexOfKey = allKeys.indexOf(key);
if (indexOfKey !== -1) {
//remove the item from storage
if (storage === fallbackStorage) {
delete storage[key];
}
else {
storage.removeItem(key);
}
//remove the key from the keys array
allKeys.splice(indexOfKey, 1);
//store the key array minus the removed item
storage[allKeysString] = JSON.stringify(allKeys);
}
},
clear = function() {
if (storage === fallbackStorage) {
fallbackStorage = { };
storage = fallbackStorage;
}
else {
storage.clear();
}
//clear the key array as well
allKeys = [];
};
if (isLocalStorageSupported === true) {
storage = localStorage;
}
else {
//if local storage is not supported, fallback to an in memory storage object
storage = fallbackStorage;
}
//do the initial load of the keys
loadKeys();
return {
storageHasNativeSupport: isLocalStorageSupported,
setItem: setItem,
getItem: getItem,
setObject: setObject,
getObject: getObject,
removeItem: removeItem,
findKeys: findKeys,
getKeys: getKeys,
clear: clear
};
};
if (typeof define === "function" && define.amd) {
define("Core.Storage", ["Core"], function (core) {
core.Storage = coreStorage();
return core.Storage;
});
}
else {
//we are going to attach this to the global Core object
Core.Storage = coreStorage();
}
})();