forked from forbesmyester/SyncIt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FakeLocalStorage.js
63 lines (51 loc) · 1.11 KB
/
FakeLocalStorage.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
module.exports = (function () {
"use strict";
// Author: Matthew Forrester <matt_at_keyboardwritescode.com>
// Copyright: Matthew Forrester
// License: MIT/BSD-style
/**
* LocalStorage Mock
*/
var FakeLocalStorage = function() {
this.data = {};
this.keys = [];
};
FakeLocalStorage.prototype.setItem = function(k,v) {
this.data[k] = v;
this._regen();
};
FakeLocalStorage.prototype._regen = function() {
this.keys = [];
this.length = 0;
for (var k in this.data) {
if (this.data.hasOwnProperty(k)) {
this.keys.push(k);
}
}
this.keys = this.keys.sort();
this.length = this.keys.length;
};
FakeLocalStorage.prototype.clear = function() {
this.data = {};
this._regen();
};
FakeLocalStorage.prototype.key = function(i) {
if (i < this.length) {
return this.keys[i];
}
return null;
};
FakeLocalStorage.prototype.getItem = function(k) {
if (!this.data.hasOwnProperty(k)) {
return null;
}
return this.data[k];
};
FakeLocalStorage.prototype.removeItem = function(key) {
if (this.data.hasOwnProperty(key)) {
delete this.data[key];
this._regen();
}
};
return FakeLocalStorage;
}());