UNPKG

1.4 kBJavaScriptView Raw
1/*
2Copyright 2015, 2016 OpenMarket Ltd
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17/**
18 * A mock implementation of the webstorage api
19 * @constructor
20 */
21function MockStorageApi() {
22 this.data = {};
23 this.keys = [];
24 this.length = 0;
25}
26
27MockStorageApi.prototype = {
28 setItem: function(k, v) {
29 this.data[k] = v;
30 this._recalc();
31 },
32 getItem: function(k) {
33 return this.data[k] || null;
34 },
35 removeItem: function(k) {
36 delete this.data[k];
37 this._recalc();
38 },
39 key: function(index) {
40 return this.keys[index];
41 },
42 _recalc: function() {
43 const keys = [];
44 for (const k in this.data) {
45 if (!this.data.hasOwnProperty(k)) {
46 continue;
47 }
48 keys.push(k);
49 }
50 this.keys = keys;
51 this.length = keys.length;
52 },
53};
54
55/** */
56module.exports = MockStorageApi;