Skip to content

Commit a2a007c

Browse files
committed
converted localstorage into storage wrapper for unit testing
1 parent 3ef9ab0 commit a2a007c

File tree

7 files changed

+315
-136
lines changed

7 files changed

+315
-136
lines changed

amplitude.js

Lines changed: 147 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ var Cookie = require('./cookie');
108108
var JSON = require('json'); // jshint ignore:line
109109
var language = require('./language');
110110
var localStorage = require('./localstorage'); // jshint ignore:line
111+
var storage = require('./storage');
111112
var md5 = require('JavaScript-MD5');
112113
var object = require('object');
113114
var Request = require('./xhr');
@@ -165,6 +166,7 @@ var Amplitude = function() {
165166
this._unsentIdentifys = [];
166167
this._ua = new UAParser(navigator.userAgent).getResult();
167168
this.options = object.merge({}, DEFAULT_OPTIONS);
169+
this.storage = new storage().getStorage();
168170
this._q = []; // queue for proxied functions before script load
169171
};
170172

@@ -260,7 +262,8 @@ Amplitude.prototype.runQueuedFunctions = function () {
260262
*/
261263
Amplitude.prototype.setLocalStorage = function(item, value) {
262264
var key = item + '_' + this.options.apiKey.slice(0, 6);
263-
localStorage.setItem(key, value);
265+
this.storage.setItem(key, value);
266+
//localStorage.setItem(key, value);
264267
};
265268

266269
/**
@@ -272,7 +275,8 @@ Amplitude.prototype.setLocalStorage = function(item, value) {
272275
*/
273276
Amplitude.prototype.getLocalStorage = function(item, defaultValue) {
274277
var key = item + '_' + this.options.apiKey.slice(0, 6);
275-
return localStorage.getItem(key) || defaultValue;
278+
return this.storage.getItem(key) || defaultValue;
279+
// return localStorage.getItem(key) || defaultValue;
276280
};
277281

278282
Amplitude.prototype._upgradeStoredData = function() {
@@ -869,7 +873,7 @@ Amplitude.prototype.__VERSION__ = version;
869873

870874
module.exports = Amplitude;
871875

872-
}, {"./cookie":3,"json":4,"./language":5,"./localstorage":6,"JavaScript-MD5":7,"object":8,"./xhr":9,"ua-parser-js":10,"./uuid":11,"./version":12,"./identify":13,"./type":14}],
876+
}, {"./cookie":3,"json":4,"./language":5,"./localstorage":6,"./storage":7,"JavaScript-MD5":8,"object":9,"./xhr":10,"ua-parser-js":11,"./uuid":12,"./version":13,"./identify":14,"./type":15}],
873877
3: [function(require, module, exports) {
874878
/*
875879
* Cookie data
@@ -996,8 +1000,8 @@ module.exports = {
9961000

9971001
};
9981002

999-
}, {"./base64":15,"json":4,"top-domain":16}],
1000-
15: [function(require, module, exports) {
1003+
}, {"./base64":16,"json":4,"top-domain":17}],
1004+
16: [function(require, module, exports) {
10011005
/* jshint bitwise: false */
10021006
/* global escape, unescape */
10031007

@@ -1096,8 +1100,8 @@ var Base64 = {
10961100

10971101
module.exports = Base64;
10981102

1099-
}, {"./utf8":17}],
1100-
17: [function(require, module, exports) {
1103+
}, {"./utf8":18}],
1104+
18: [function(require, module, exports) {
11011105
/* jshint bitwise: false */
11021106

11031107
/*
@@ -1167,8 +1171,8 @@ module.exports = parse && stringify
11671171
? JSON
11681172
: require('json-fallback');
11691173

1170-
}, {"json-fallback":18}],
1171-
18: [function(require, module, exports) {
1174+
}, {"json-fallback":19}],
1175+
19: [function(require, module, exports) {
11721176
/*
11731177
json2.js
11741178
2014-02-04
@@ -1658,7 +1662,7 @@ module.exports = parse && stringify
16581662
}());
16591663

16601664
}, {}],
1661-
16: [function(require, module, exports) {
1665+
17: [function(require, module, exports) {
16621666

16631667
/**
16641668
* Module dependencies.
@@ -1706,8 +1710,8 @@ function domain(url){
17061710
return match ? match[0] : '';
17071711
};
17081712

1709-
}, {"url":19}],
1710-
19: [function(require, module, exports) {
1713+
}, {"url":20}],
1714+
20: [function(require, module, exports) {
17111715

17121716
/**
17131717
* Parse the given `url`.
@@ -1906,8 +1910,8 @@ if (!localStorage) {
19061910

19071911
module.exports = localStorage;
19081912

1909-
}, {"./localstorage-cookie.js":20}],
1910-
20: [function(require, module, exports) {
1913+
}, {"./localstorage-cookie.js":21}],
1914+
21: [function(require, module, exports) {
19111915
/* jshint -W020, unused: false, noempty: false, boss: true */
19121916
/* global escape, unescape */
19131917

@@ -1962,6 +1966,122 @@ module.exports = cookieStorage;
19621966

19631967
}, {"./cookie.js":3}],
19641968
7: [function(require, module, exports) {
1969+
/* jshint -W020, unused: false, noempty: false, boss: true */
1970+
1971+
/*
1972+
* Wrapper to determine best storage to use. In most cases
1973+
* localStorage is good, although if it is unavailable, then
1974+
* fall back to using global storage, html div, or cookies.
1975+
* Implement localStorage to support Firefox 2-3 and IE 5-7.
1976+
*/
1977+
var storage = function() {
1978+
this.storage = null;
1979+
};
1980+
1981+
// test that Window.localStorage is available and works
1982+
storage.prototype._windowLocalStorageAvailable = function() {
1983+
var uid = new Date();
1984+
var result;
1985+
try {
1986+
window.localStorage.setItem(uid, uid);
1987+
result = window.localStorage.getItem(uid) === String(uid);
1988+
window.localStorage.removeItem(uid);
1989+
return result;
1990+
} catch (e) {
1991+
// localStorage not available
1992+
}
1993+
return false;
1994+
};
1995+
1996+
storage.prototype.getStorage = function() {
1997+
if (this.storage !== null) {
1998+
return this.storage;
1999+
}
2000+
2001+
if (this._windowLocalStorageAvailable()) {
2002+
this.storage = window.localStorage;
2003+
} else if (window.globalStorage) {
2004+
// Firefox 2-3 use globalStorage
2005+
// See https://developer.mozilla.org/en/dom/storage#globalStorage
2006+
try {
2007+
this.storage = window.globalStorage[window.location.hostname];
2008+
} catch (e) {
2009+
// Something bad happened...
2010+
}
2011+
} else {
2012+
// IE 5-7 use userData
2013+
// See http://msdn.microsoft.com/en-us/library/ms531424(v=vs.85).aspx
2014+
var div = document.createElement('div'),
2015+
attrKey = 'localStorage';
2016+
div.style.display = 'none';
2017+
document.getElementsByTagName('head')[0].appendChild(div);
2018+
if (div.addBehavior) {
2019+
div.addBehavior('#default#userdata');
2020+
this.storage = {
2021+
length: 0,
2022+
setItem: function(k, v) {
2023+
div.load(attrKey);
2024+
if (!div.getAttribute(k)) {
2025+
this.length++;
2026+
}
2027+
div.setAttribute(k, v);
2028+
div.save(attrKey);
2029+
},
2030+
getItem: function(k) {
2031+
div.load(attrKey);
2032+
return div.getAttribute(k);
2033+
},
2034+
removeItem: function(k) {
2035+
div.load(attrKey);
2036+
if (div.getAttribute(k)) {
2037+
this.length--;
2038+
}
2039+
div.removeAttribute(k);
2040+
div.save(attrKey);
2041+
},
2042+
clear: function() {
2043+
div.load(attrKey);
2044+
var i = 0;
2045+
var attr;
2046+
while (attr = div.XMLDocument.documentElement.attributes[i++]) {
2047+
div.removeAttribute(attr.name);
2048+
}
2049+
div.save(attrKey);
2050+
this.length = 0;
2051+
},
2052+
key: function(k) {
2053+
div.load(attrKey);
2054+
return div.XMLDocument.documentElement.attributes[k];
2055+
}
2056+
};
2057+
div.load(attrKey);
2058+
this.storage.length = div.XMLDocument.documentElement.attributes.length;
2059+
} else {
2060+
this.storage = require('./localstorage-cookie.js');
2061+
}
2062+
}
2063+
if (!this.storage) {
2064+
this.storage = {
2065+
length: 0,
2066+
setItem: function(k, v) {
2067+
},
2068+
getItem: function(k) {
2069+
},
2070+
removeItem: function(k) {
2071+
},
2072+
clear: function() {
2073+
},
2074+
key: function(k) {
2075+
}
2076+
};
2077+
}
2078+
return this.storage;
2079+
};
2080+
2081+
module.exports = storage;
2082+
2083+
}, {"./localstorage-cookie.js":21}],
2084+
8: [function(require, module, exports) {
19652085
/*
19662086
* JavaScript MD5 1.0.1
19672087
* https://github.com/blueimp/JavaScript-MD5
@@ -2249,7 +2369,7 @@ module.exports = cookieStorage;
22492369
}(this));
22502370

22512371
}, {}],
2252-
8: [function(require, module, exports) {
2372+
9: [function(require, module, exports) {
22532373

22542374
/**
22552375
* HOP ref.
@@ -2335,7 +2455,7 @@ exports.isEmpty = function(obj){
23352455
return 0 == exports.length(obj);
23362456
};
23372457
}, {}],
2338-
9: [function(require, module, exports) {
2458+
10: [function(require, module, exports) {
23392459
var querystring = require('querystring');
23402460

23412461
/*
@@ -2371,8 +2491,8 @@ Request.prototype.send = function(callback) {
23712491

23722492
module.exports = Request;
23732493

2374-
}, {"querystring":21}],
2375-
21: [function(require, module, exports) {
2494+
}, {"querystring":22}],
2495+
22: [function(require, module, exports) {
23762496

23772497
/**
23782498
* Module dependencies.
@@ -2447,8 +2567,8 @@ exports.stringify = function(obj){
24472567
return pairs.join('&');
24482568
};
24492569

2450-
}, {"trim":22,"type":23}],
2451-
22: [function(require, module, exports) {
2570+
}, {"trim":23,"type":24}],
2571+
23: [function(require, module, exports) {
24522572

24532573
exports = module.exports = trim;
24542574

@@ -2468,7 +2588,7 @@ exports.right = function(str){
24682588
};
24692589

24702590
}, {}],
2471-
23: [function(require, module, exports) {
2591+
24: [function(require, module, exports) {
24722592
/**
24732593
* toString ref.
24742594
*/
@@ -2507,7 +2627,7 @@ module.exports = function(val){
25072627
};
25082628

25092629
}, {}],
2510-
10: [function(require, module, exports) {
2630+
11: [function(require, module, exports) {
25112631
/* jshint eqeqeq: false, forin: false */
25122632
/* global define */
25132633

@@ -3390,7 +3510,7 @@ module.exports = function(val){
33903510
})(this);
33913511

33923512
}, {}],
3393-
11: [function(require, module, exports) {
3513+
12: [function(require, module, exports) {
33943514
/* jshint bitwise: false, laxbreak: true */
33953515

33963516
/**
@@ -3424,11 +3544,11 @@ var uuid = function(a) {
34243544
module.exports = uuid;
34253545

34263546
}, {}],
3427-
12: [function(require, module, exports) {
3547+
13: [function(require, module, exports) {
34283548
module.exports = '2.5.0';
34293549

34303550
}, {}],
3431-
13: [function(require, module, exports) {
3551+
14: [function(require, module, exports) {
34323552
var type = require('./type');
34333553

34343554
/*
@@ -3491,8 +3611,8 @@ Identify.prototype._addOperation = function(operation, property, value) {
34913611

34923612
module.exports = Identify;
34933613

3494-
}, {"./type":14}],
3495-
14: [function(require, module, exports) {
3614+
}, {"./type":15}],
3615+
15: [function(require, module, exports) {
34963616
/* Taken from: https://github.com/component/type */
34973617

34983618
/**

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Cookie = require('./cookie');
22
var JSON = require('json'); // jshint ignore:line
33
var language = require('./language');
44
var localStorage = require('./localstorage'); // jshint ignore:line
5+
var storage = require('./storage');
56
var md5 = require('JavaScript-MD5');
67
var object = require('object');
78
var Request = require('./xhr');
@@ -59,6 +60,7 @@ var Amplitude = function() {
5960
this._unsentIdentifys = [];
6061
this._ua = new UAParser(navigator.userAgent).getResult();
6162
this.options = object.merge({}, DEFAULT_OPTIONS);
63+
this.storage = new storage().getStorage();
6264
this._q = []; // queue for proxied functions before script load
6365
};
6466

@@ -154,7 +156,8 @@ Amplitude.prototype.runQueuedFunctions = function () {
154156
*/
155157
Amplitude.prototype.setLocalStorage = function(item, value) {
156158
var key = item + '_' + this.options.apiKey.slice(0, 6);
157-
localStorage.setItem(key, value);
159+
this.storage.setItem(key, value);
160+
//localStorage.setItem(key, value);
158161
};
159162

160163
/**
@@ -166,7 +169,8 @@ Amplitude.prototype.setLocalStorage = function(item, value) {
166169
*/
167170
Amplitude.prototype.getLocalStorage = function(item, defaultValue) {
168171
var key = item + '_' + this.options.apiKey.slice(0, 6);
169-
return localStorage.getItem(key) || defaultValue;
172+
return this.storage.getItem(key) || defaultValue;
173+
// return localStorage.getItem(key) || defaultValue;
170174
};
171175

172176
Amplitude.prototype._upgradeStoredData = function() {

0 commit comments

Comments
 (0)