diff --git a/src/OhMyCache.js b/src/OhMyCache.js index c92846b..fecb4d0 100644 --- a/src/OhMyCache.js +++ b/src/OhMyCache.js @@ -148,14 +148,15 @@ export default class OhMyCache { /** * Remove all items + * @param {string} onlyExpired: if true remove only expired datas else remove all items * @return {boolean} */ - clear () { - try { - this.engine.clear() + clear (onlyExpired = false) { + if (onlyExpired) { + this.getAllItems() return true - } catch (e) { - return false + } else { + return _clear(this.engine) } } @@ -264,3 +265,16 @@ function _remove (engine, key) { return false } } + +/** + * Remove all items + * @return {boolean} + */ +function _clear (engine) { + try { + engine.clear() + return true + } catch (e) { + return false + } +} diff --git a/test/specs/OhMyLocalCache.spec.js b/test/specs/OhMyLocalCache.spec.js index 660dfd3..a6ab334 100644 --- a/test/specs/OhMyLocalCache.spec.js +++ b/test/specs/OhMyLocalCache.spec.js @@ -446,7 +446,7 @@ describe('OhMyLocalCache', () => { describe("clear()", function() { - it('clear', function() { + it('clear all', function() { _class.set('k1', 'v1', {readonly: true}) _class.set('k2', 'v2') sessionStorage.setItem('s1', 's2') @@ -458,6 +458,22 @@ describe('OhMyLocalCache', () => { expect(null).toBe(_class.get('k1')) expect(null).toBe(_class.get('k2')) expect('s2').toEqual(sessionStorage.getItem('s1')) - }); - }); + }) + + it('clear only expired', function() { + _class.set('k1', 'v1', {readonly: true}) + _class.set('k2', 'v2') + let created = 1451602800 // 2016-01-01 + let expired = created + 3600 + localStorage.setItem('k3', JSON.stringify({0:'v3', 1:created, 2:expired})) + sessionStorage.setItem('s1', 's2') + + _class.clear(true); + + expect('v1').toBe(_class.get('k1')) + expect('v2').toBe(_class.get('k2')) + expect(null).toBe(_class.get('k3')) + expect('s2').toEqual(sessionStorage.getItem('s1')) + }) + }) }) \ No newline at end of file diff --git a/test/specs/OhMySessionCache.spec.js b/test/specs/OhMySessionCache.spec.js index 765aff9..2e17f0f 100644 --- a/test/specs/OhMySessionCache.spec.js +++ b/test/specs/OhMySessionCache.spec.js @@ -303,7 +303,7 @@ describe('OhMySessionCache', () => { let created = 1451602800 // 2016-01-01 let expired = created + 3600 - localStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired})) + sessionStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired})) let expected = {key2: 'val2', key1: 'val1'} let result = _class.getAll() @@ -331,7 +331,7 @@ describe('OhMySessionCache', () => { let created = 1451602800 // 2016-01-01 let expired = created + 3600 - localStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired})) + sessionStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired})) let expected = {key2: {0: 'val2', 1: time()}, key1: {0: 'val1', 1: time()}} let result = _class.getAllItems() @@ -458,6 +458,22 @@ describe('OhMySessionCache', () => { expect(null).toBe(_class.get('k1')) expect(null).toBe(_class.get('k2')) expect('s2').toEqual(localStorage.getItem('s1')) - }); - }); + }) + + it('clear only expired', function() { + _class.set('k1', 'v1', {readonly: true}) + _class.set('k2', 'v2') + let created = 1451602800 // 2016-01-01 + let expired = created + 3600 + sessionStorage.setItem('k3', JSON.stringify({0:'v3', 1:created, 2:expired})) + localStorage.setItem('s1', 's2') + + _class.clear(true); + + expect('v1').toBe(_class.get('k1')) + expect('v2').toBe(_class.get('k2')) + expect(null).toBe(_class.get('k3')) + expect('s2').toEqual(localStorage.getItem('s1')) + }) + }) }) \ No newline at end of file