Skip to content

Commit 6cb604d

Browse files
committed
Extract parsing JSON from localStorage into method
1 parent 15fe029 commit 6cb604d

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

src/dropbox.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const isFolder = util.isFolder;
4646
const cleanPath = util.cleanPath;
4747
const shouldBeTreatedAsBinary = util.shouldBeTreatedAsBinary;
4848
const readBinaryData = util.readBinaryData;
49+
const getJSONFromLocalStorage = util.getJSONFromLocalStorage;
4950

5051
/**
5152
* Map a local path to a path in Dropbox.
@@ -180,16 +181,11 @@ var Dropbox = function (rs) {
180181
hasLocalStorage = util.localStorageAvailable();
181182

182183
if (hasLocalStorage){
183-
var settings;
184-
try {
185-
settings = JSON.parse(localStorage.getItem(SETTINGS_KEY));
186-
} catch(e) { /* ok to ignore, probably no data in localStorage */ }
184+
const settings = getJSONFromLocalStorage(SETTINGS_KEY);
187185
if (settings) {
188186
this.configure(settings);
189187
}
190-
try {
191-
this._itemRefs = JSON.parse(localStorage.getItem(SETTINGS_KEY+':shares')) || {};
192-
} catch(e) { /* ok to ignore, no shares in localStorage */ }
188+
this._itemRefs = getJSONFromLocalStorage(`${SETTINGS_KEY}:shares`) || {};
193189
}
194190
if (this.connected) {
195191
setTimeout(this._emit.bind(this), 0, 'connected');

src/googledrive.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const isFolder = util.isFolder;
3333
const cleanPath = util.cleanPath;
3434
const shouldBeTreatedAsBinary = util.shouldBeTreatedAsBinary;
3535
const readBinaryData = util.readBinaryData;
36+
const getJSONFromLocalStorage = util.getJSONFromLocalStorage;
3637

3738
let hasLocalStorage;
3839

@@ -142,12 +143,7 @@ const GoogleDrive = function (remoteStorage, clientId) {
142143
hasLocalStorage = util.localStorageAvailable();
143144

144145
if (hasLocalStorage){
145-
let settings;
146-
try {
147-
settings = JSON.parse(localStorage.getItem(SETTINGS_KEY));
148-
} catch(e) {
149-
// no settings stored
150-
}
146+
const settings = getJSONFromLocalStorage(SETTINGS_KEY);
151147
if (settings) {
152148
this.configure(settings);
153149
}

src/remotestorage.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const log = require('./log');
1212
const Features = require('./features');
1313
const globalContext = util.getGlobalContext();
1414
const eventHandling = require('./eventhandling');
15+
const getJSONFromLocalStorage = util.getJSONFromLocalStorage;
1516

1617
var hasLocalStorage;
1718

@@ -90,11 +91,7 @@ var RemoteStorage = function (cfg) {
9091
hasLocalStorage = util.localStorageAvailable();
9192

9293
if (hasLocalStorage) {
93-
try {
94-
this.apiKeys = JSON.parse(localStorage.getItem('remotestorage:api-keys')) || {};
95-
} catch(exc) {
96-
// ignored
97-
}
94+
this.apiKeys = getJSONFromLocalStorage('remotestorage:api-keys') || {};
9895
this.setBackend(localStorage.getItem('remotestorage:backend') || 'remotestorage');
9996
}
10097

src/util.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,23 @@ var util = {
242242
}
243243
},
244244

245+
/**
246+
* Extract and parse JSON data from localStorage.
247+
*
248+
* @param {string} key - localStorage key
249+
*
250+
* @returns {object} parsed object or undefined
251+
*/
252+
getJSONFromLocalStorage (key) {
253+
const context = util.getGlobalContext();
254+
255+
try {
256+
return JSON.parse(context.localStorage.getItem(key));
257+
} catch(e) {
258+
// no JSON stored
259+
}
260+
},
261+
245262
/**
246263
* Decide if data should be treated as binary based on the content
247264
* and content-type.

src/wireclient.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ if (typeof(ArrayBufferView) === 'function') {
7575
const isFolder = util.isFolder;
7676
const cleanPath = util.cleanPath;
7777
const shouldBeTreatedAsBinary = util.shouldBeTreatedAsBinary;
78+
const getJSONFromLocalStorage = util.getJSONFromLocalStorage;
7879

7980
function addQuotes(str) {
8081
if (typeof(str) !== 'string') {
@@ -167,12 +168,7 @@ var WireClient = function WireClient(rs) {
167168
eventHandling(this, 'connected', 'not-connected');
168169

169170
if (hasLocalStorage) {
170-
var settings;
171-
try {
172-
settings = JSON.parse(localStorage[SETTINGS_KEY]);
173-
} catch(e) {
174-
// no settings stored
175-
}
171+
const settings = getJSONFromLocalStorage(SETTINGS_KEY);
176172
if (settings) {
177173
setTimeout(function () {
178174
this.configure(settings);

test/unit/util-suite.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,34 @@ define(['./src/util'], function (util) {
151151

152152
test.assert(util.localStorageAvailable(), false);
153153
}
154+
},
155+
156+
{
157+
desc: "getJSONFromLocalStorage returns the object for the given key",
158+
run: function(env, test) {
159+
160+
global.localStorage = {
161+
getItem: function() {
162+
return '{ "foo": "bar" }';
163+
}
164+
};
165+
166+
test.assert(util.getJSONFromLocalStorage('somekey'), { foo: 'bar' });
167+
}
168+
},
169+
170+
{
171+
desc: "getJSONFromLocalStorage returns null when there is no object for the given key",
172+
run: function(env, test) {
173+
174+
global.localStorage = {
175+
getItem: function() {
176+
return null;
177+
}
178+
};
179+
180+
test.assert(util.getJSONFromLocalStorage('somekey'), null);
181+
}
154182
}
155183

156184
]

0 commit comments

Comments
 (0)