-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.chrome.storage.js
More file actions
63 lines (63 loc) · 1.47 KB
/
mock.chrome.storage.js
File metadata and controls
63 lines (63 loc) · 1.47 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
/*!
* mock.chrome.storage.js
*
* Basic mocking for Chrome's storage API - sync & local.
* https://developer.chrome.com/extensions/storage
*
* Created by Pete Saia <pete@lev-interactive.com>
*
* Copyright 2013, 2014 Lev Interacitve, LLC
* Released under the MIT license
* http://lev-interactive.com/
*/
(function(global) {
global.chromeStorageMock = function() {
return Object.create({
cache: {},
get: function(key, fn) {
var obj = {};
if (this.cache[key]) {
obj[key] = this.cache[key];
}
if (fn) {
fn(obj);
}
},
set: function(data, fn) {
var key = Object.keys(data)[0];
var val = data[key];
this.cache[key] = JSON.parse(JSON.stringify(val));
if (fn) {
fn(data);
}
},
remove: function(keys, fn) {
if (Array.isArray(keys)) {
keys.forEach(function(key) {
if (this.cache[key]) {
delete this.cache[key];
}
}.bind(this));
} else if ("string" === typeof keys) {
if (this.cache[keys]) {
delete this.cache[keys];
}
}
if (fn) {
fn();
}
},
clear: function(fn) {
this.cache = {};
if (fn) {
fn();
}
},
getBytesInUse: function(keys, fn) {
if (fn) {
fn(100); // No reason for accuracy at this time.
}
}
});
};
})(window);