Skip to content

Commit

Permalink
fixed constructor default options bug!
Browse files Browse the repository at this point in the history
  • Loading branch information
wuchangming committed Oct 20, 2016
1 parent 8275c2d commit 6cd1242
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
*.log
4 changes: 2 additions & 2 deletions dist/web-storage-cache.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"url": "https://github.com/WQTeam/web-storage-cache/issues"
},
"scripts": {
"test": "grunt test"
"test": "grunt test",
"build": "grunt build"
},
"devDependencies": {
"chai": "~2.1.0",
Expand Down
11 changes: 10 additions & 1 deletion src/web-storage-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"use strict";

var _maxExpireDate = new Date('Fri, 31 Dec 9999 23:59:59 UTC');
var _defaultExpire = _maxExpireDate;

// https://github.com/jeromegn/Backbone.localStorage/blob/master/backbone.localStorage.js#L63
var defaultSerializer = {
Expand Down Expand Up @@ -115,7 +116,7 @@
function CacheItemConstructor (value, exp) {
// createTime
this.c = (new Date()).getTime();
exp = exp || _maxExpireDate;
exp = exp || _defaultExpire;
var expires = _getExpiresDate(exp);
// expiresTime
this.e = expires.getTime();
Expand Down Expand Up @@ -317,6 +318,14 @@ function CacheConstructor (options) {

var opt = _extend(defaults, options);

var expires = opt.exp;

if (expires && typeof expires !== 'number' && !_isValidDate(expires)) {
throw new Error('Constructor `exp` parameter cannot be converted to a valid Date instance');
} else {
_defaultExpire = expires;
}

var storage = _getStorageInstance(opt.storage);

var isSupported = _isStorageSupported(storage);
Expand Down
30 changes: 29 additions & 1 deletion test/unit/test.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ describe('WebStorageCache', function() {
'use strict';
before(function() {
clearStorage();
this.wsCache = new WebStorageCache(storage);
this.wsCache = new WebStorageCache({
storage: storage
});
});
after(function() {
clearStorage();
Expand All @@ -28,6 +30,32 @@ describe('WebStorageCache', function() {
expect(wsCache.add).to.be.a('function');
expect(wsCache.replace).to.be.a('function');
});
it('should set default expires success with number', function(done){
this.timeout(5000);
var cache = new WebStorageCache({
exp: 3
});
cache.set('testDefaultExpires', '1');
expect(cache.get('testDefaultExpires')).to.equal('1');
setTimeout(function() {
expect(cache.get('testDefaultExpires')).to.be.a('null');
done();
}, 3000);
});
it('should set default expires success with an outdate date', function(){
var cache = new WebStorageCache({
exp: new Date('1990 1 12')
});
cache.set('testDefaultExpires', '3');
expect(cache.get('testDefaultExpires')).to.be.a('null');
});
it('should set default expires success with an future date', function(){
var cache = new WebStorageCache({
exp: new Date('9999 1 12')
});
cache.set('testDefaultExpires', 1111);
expect(cache.get('testDefaultExpires')).to.equal(1111);
});
});
describe('#isSupported', function() {
it('should be true', function() {
Expand Down

0 comments on commit 6cd1242

Please sign in to comment.