viki-web-utils
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -11,3 +11,2 @@ 'use strict'; | ||
var ResizeCounter = require('./resize_counter/ResizeCounter'); | ||
var BlockedResourceChecker = require('./blocked_checker/BlockedResourceChecker'); | ||
@@ -18,4 +17,3 @@ module.exports = { | ||
LocalStorageTabCounter: LocalStorageTabCounter, | ||
ResizeCounter: ResizeCounter, | ||
BlockedResourceChecker: BlockedResourceChecker | ||
ResizeCounter: ResizeCounter | ||
}; |
@@ -22,5 +22,5 @@ 'use strict'; | ||
/** | ||
/** | ||
@constructor | ||
@param {String} lsKey - Key for localStorage entry. Defaults to `tabCounter` | ||
@param {String} lsKey - Key for localStorage entry. Defaults to `tabCounter` | ||
**/ | ||
@@ -36,3 +36,3 @@ function LocalStorageTabCounter(lsKey) { | ||
/** | ||
@returns {number} - Number of tabs open in session | ||
@returns {boolean} - Status for localStorage usage. This is inspired by lscache https://github.com/pamelafox/lscache/blob/master/lscache.js#L54 | ||
**/ | ||
@@ -42,5 +42,32 @@ | ||
_createClass(LocalStorageTabCounter, [{ | ||
key: 'isLocalStorageEnabled', | ||
value: function isLocalStorageEnabled() { | ||
if (this.localStorageEnabled !== undefined) { | ||
return this.localStorageEnabled; | ||
} | ||
var key = 'viki-web-utils-random-storage-key'; | ||
try { | ||
localStorage.setItem(key, 1); | ||
localStorage.getItem(key); | ||
localStorage.removeItem(key); | ||
this.localStorageEnabled = true; | ||
} catch (e) { | ||
this.localStorageEnabled = false; | ||
} | ||
return this.localStorageEnabled; | ||
} | ||
/** | ||
@returns {number|null} - Number of tabs open in session or null if localStorage is not working in current browser | ||
**/ | ||
}, { | ||
key: 'getCount', | ||
value: function getCount() { | ||
return parseInt(localStorage.getItem(this.options.LS_KEY), 10) || 0; | ||
if (this.isLocalStorageEnabled()) { | ||
return parseInt(localStorage.getItem(this.options.LS_KEY), 10) || 0; | ||
} else { | ||
return null; | ||
} | ||
} | ||
@@ -51,3 +78,7 @@ }, { | ||
this.count = count; | ||
localStorage.setItem(this.options.LS_KEY, count); | ||
if (this.isLocalStorageEnabled()) { | ||
// Fix for safari issue - sometimes throws QUOTA_EXCEEDED_ERR on setItem. | ||
localStorage.removeItem(this.options.LS_KEY); | ||
localStorage.setItem(this.options.LS_KEY, count); | ||
} | ||
} | ||
@@ -54,0 +85,0 @@ }]); |
{ | ||
"name": "viki-web-utils", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"description": "Utils used by the Viki web app", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -71,3 +71,3 @@ # viki-web-utils | ||
``` | ||
npm run tests | ||
npm run test | ||
``` | ||
@@ -74,0 +74,0 @@ |
@@ -29,2 +29,47 @@ /* eslint-env jasmine */ | ||
describe('isLocalStorageEnabled', function () { | ||
describe('return true', function () { | ||
it('as new result', function () { | ||
this.tabCounter = new LocalStorageTabCounter(); | ||
this.tabCounter.localStorageEnabled = undefined; | ||
spyOn(localStorage, 'setItem').and.callFake(function(){}); | ||
spyOn(localStorage, 'getItem').and.callFake(function(){}); | ||
spyOn(localStorage, 'removeItem').and.callFake(function(){}); | ||
expect(this.tabCounter.isLocalStorageEnabled()).toBe(true); | ||
expect(localStorage.setItem).toHaveBeenCalled(); | ||
expect(localStorage.getItem).toHaveBeenCalled(); | ||
expect(localStorage.removeItem).toHaveBeenCalled(); | ||
}); | ||
it('from previous result', function () { | ||
this.tabCounter = new LocalStorageTabCounter(); | ||
this.tabCounter.localStorageEnabled = true; | ||
spyOn(localStorage, 'setItem'); | ||
spyOn(localStorage, 'getItem'); | ||
spyOn(localStorage, 'removeItem'); | ||
expect(this.tabCounter.isLocalStorageEnabled()).toBe(true); | ||
expect(localStorage.setItem).not.toHaveBeenCalled(); | ||
expect(localStorage.getItem).not.toHaveBeenCalled(); | ||
expect(localStorage.removeItem).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
describe('return false', function () { | ||
it('from previous result', function () { | ||
this.tabCounter = new LocalStorageTabCounter(); | ||
this.tabCounter.localStorageEnabled = false; | ||
spyOn(localStorage, 'setItem'); | ||
spyOn(localStorage, 'getItem'); | ||
spyOn(localStorage, 'removeItem'); | ||
expect(this.tabCounter.isLocalStorageEnabled()).toBe(false); | ||
expect(localStorage.setItem).not.toHaveBeenCalled(); | ||
expect(localStorage.getItem).not.toHaveBeenCalled(); | ||
expect(localStorage.removeItem).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
describe('getCount', function () { | ||
@@ -36,2 +81,11 @@ it('should return current count', function () { | ||
}); | ||
it('should not return current count if localStorage is not enabled', function () { | ||
this.tabCounter = new LocalStorageTabCounter(); | ||
spyOn(this.tabCounter, 'isLocalStorageEnabled').and.returnValue(false); | ||
spyOn(localStorage, 'getItem'); | ||
expect(this.tabCounter.getCount()).toBe(null); | ||
expect(localStorage.getItem).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
@@ -64,3 +118,13 @@ | ||
}); | ||
it('should not save current count if localStorage is not enabled', function () { | ||
this.tabCounter = new LocalStorageTabCounter(); | ||
spyOn(this.tabCounter, 'isLocalStorageEnabled').and.returnValue(false); | ||
spyOn(localStorage, 'setItem'); | ||
this.tabCounter.saveCount(4); | ||
expect(localStorage.setItem).not.toHaveBeenCalled(); | ||
expect(this.tabCounter.getCount()).toBe(null); | ||
}); | ||
}); | ||
}); |
@@ -22,6 +22,2 @@ /* eslint-env jasmine */ | ||
}); | ||
it('should have BlockedResourceChecker', function () { | ||
expect(Viki.BlockedResourceChecker).toEqual(jasmine.any(Function)); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
11
1185339
72
2341