From e3fbd023569a1808f988154019d1a96e0da9d633 Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Fri, 25 Oct 2019 10:25:59 -0700 Subject: [PATCH] Rename to localCookie.js, add force cookies option. - Align class + filename to repository name - Allow options to specificy whether we should force cookies (useful for the tests). - Update test to test localStorage and Cookies all in 1 go. --- dist/localCookie.js | 1 + dist/storage.js | 1 - module/localCookie.js | 1 + module/storage.js | 1 - readme.md | 4 +- rollup.config.js | 8 ++-- src/{storage.js => localCookie.js} | 11 +++-- test/test.js | 73 ++++++++++++++++++++++++++---- 8 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 dist/localCookie.js delete mode 100644 dist/storage.js create mode 100644 module/localCookie.js delete mode 100644 module/storage.js rename src/{storage.js => localCookie.js} (91%) diff --git a/dist/localCookie.js b/dist/localCookie.js new file mode 100644 index 0000000..2a7521d --- /dev/null +++ b/dist/localCookie.js @@ -0,0 +1 @@ +var Storage=function(){"use strict";class e{constructor(e){return e=e||{},this.forceCookies=e.forceCookies||!1,!0===this._checkIfLocalStorageWorks()&&!0!==e.forceCookies?{getItem:this._getItemLocalStorage,setItem:this._setItemLocalStorage,removeItem:this._removeItemLocalStorage,clear:this._clearLocalStorage}:{getItem:this._getItemCookie,setItem:this._setItemCookie,removeItem:this._removeItemCookie,clear:this._clearCookies}}_checkIfLocalStorageWorks(){if("undefined"==typeof localStorage)return!1;try{return localStorage.setItem("feature_test","yes"),"yes"===localStorage.getItem("feature_test")&&(localStorage.removeItem("feature_test"),!0)}catch(e){return!1}}_getItemLocalStorage(e){return window.localStorage.getItem(e)}_setItemLocalStorage(e,t){return window.localStorage.setItem(e,t)}_removeItemLocalStorage(e){return window.localStorage.removeItem(e)}_clearLocalStorage(){return window.localStorage.clear()}_getItemCookie(e){var t=document.cookie.match(RegExp("(?:^|;\\s*)"+function(e){return e.replace(/([.*+?\^${}()|\[\]\/\\])/g,"\\$1")}(e)+"=([^;]*)"));return t&&""===t[1]&&(t[1]=null),t?t[1]:null}_setItemCookie(e,t){document.cookie=`${e}=${t}`}_removeItemCookie(e){document.cookie=`${e}=;Max-Age=-99999999;`}_clearCookies(){document.cookie.split(";").forEach(e=>{document.cookie=e.replace(/^ +/,"").replace(/=.*/,"=;expires=Max-Age=-99999999")})}}return e}(); diff --git a/dist/storage.js b/dist/storage.js deleted file mode 100644 index 5b1b061..0000000 --- a/dist/storage.js +++ /dev/null @@ -1 +0,0 @@ -var Storage=function(){"use strict";class e{constructor(){return!0===this._checkIfLocalStorageWorks()?{getItem:this._getItemLocalStorage,setItem:this._setItemLocalStorage,removeItem:this._removeItemLocalStorage,clear:this._clearLocalStorage}:{getItem:this._getItemCookie,setItem:this._setItemCookie,removeItem:this._removeItemCookie,clear:this._clearCookies}}_checkIfLocalStorageWorks(){if("undefined"==typeof localStorage)return!1;try{return localStorage.setItem("feature_test","yes"),"yes"===localStorage.getItem("feature_test")&&(localStorage.removeItem("feature_test"),!0)}catch(e){return!1}}_getItemLocalStorage(e){return window.localStorage.getItem(e)}_setItemLocalStorage(e,t){return window.localStorage.setItem(e,t)}_removeItemLocalStorage(e){return window.localStorage.removeItem(e)}_clearLocalStorage(){return window.localStorage.clear()}_getItemCookie(e){var t=document.cookie.match(RegExp("(?:^|;\\s*)"+function(e){return e.replace(/([.*+?\^${}()|\[\]\/\\])/g,"\\$1")}(e)+"=([^;]*)"));return t&&""===t[1]&&(t[1]=null),t?t[1]:null}_setItemCookie(e,t){document.cookie=`${e}=${t}`}_removeItemCookie(e){document.cookie=`${e}=;Max-Age=-99999999;`}_clearCookies(){document.cookie.split(";").forEach(e=>{document.cookie=e.replace(/^ +/,"").replace(/=.*/,"=;expires=Max-Age=-99999999")})}}return e}(); diff --git a/module/localCookie.js b/module/localCookie.js new file mode 100644 index 0000000..6dc0a5f --- /dev/null +++ b/module/localCookie.js @@ -0,0 +1 @@ +class localCookie{constructor(e){return e=e||{},this.forceCookies=e.forceCookies||!1,!0===this._checkIfLocalStorageWorks()&&!0!==e.forceCookies?{getItem:this._getItemLocalStorage,setItem:this._setItemLocalStorage,removeItem:this._removeItemLocalStorage,clear:this._clearLocalStorage}:{getItem:this._getItemCookie,setItem:this._setItemCookie,removeItem:this._removeItemCookie,clear:this._clearCookies}}_checkIfLocalStorageWorks(){if("undefined"==typeof localStorage)return!1;try{return localStorage.setItem("feature_test","yes"),"yes"===localStorage.getItem("feature_test")&&(localStorage.removeItem("feature_test"),!0)}catch(e){return!1}}_getItemLocalStorage(e){return window.localStorage.getItem(e)}_setItemLocalStorage(e,t){return window.localStorage.setItem(e,t)}_removeItemLocalStorage(e){return window.localStorage.removeItem(e)}_clearLocalStorage(){return window.localStorage.clear()}_getItemCookie(e){var t=document.cookie.match(RegExp("(?:^|;\\s*)"+function(e){return e.replace(/([.*+?\^${}()|\[\]\/\\])/g,"\\$1")}(e)+"=([^;]*)"));return t&&""===t[1]&&(t[1]=null),t?t[1]:null}_setItemCookie(e,t){document.cookie=`${e}=${t}`}_removeItemCookie(e){document.cookie=`${e}=;Max-Age=-99999999;`}_clearCookies(){document.cookie.split(";").forEach(e=>{document.cookie=e.replace(/^ +/,"").replace(/=.*/,"=;expires=Max-Age=-99999999")})}}export default localCookie; diff --git a/module/storage.js b/module/storage.js deleted file mode 100644 index 4f01699..0000000 --- a/module/storage.js +++ /dev/null @@ -1 +0,0 @@ -class Storage{constructor(){return!0===this._checkIfLocalStorageWorks()?{getItem:this._getItemLocalStorage,setItem:this._setItemLocalStorage,removeItem:this._removeItemLocalStorage,clear:this._clearLocalStorage}:{getItem:this._getItemCookie,setItem:this._setItemCookie,removeItem:this._removeItemCookie,clear:this._clearCookies}}_checkIfLocalStorageWorks(){if("undefined"==typeof localStorage)return!1;try{return localStorage.setItem("feature_test","yes"),"yes"===localStorage.getItem("feature_test")&&(localStorage.removeItem("feature_test"),!0)}catch(e){return!1}}_getItemLocalStorage(e){return window.localStorage.getItem(e)}_setItemLocalStorage(e,t){return window.localStorage.setItem(e,t)}_removeItemLocalStorage(e){return window.localStorage.removeItem(e)}_clearLocalStorage(){return window.localStorage.clear()}_getItemCookie(e){var t=document.cookie.match(RegExp("(?:^|;\\s*)"+function(e){return e.replace(/([.*+?\^${}()|\[\]\/\\])/g,"\\$1")}(e)+"=([^;]*)"));return t&&""===t[1]&&(t[1]=null),t?t[1]:null}_setItemCookie(e,t){document.cookie=`${e}=${t}`}_removeItemCookie(e){document.cookie=`${e}=;Max-Age=-99999999;`}_clearCookies(){document.cookie.split(";").forEach(e=>{document.cookie=e.replace(/^ +/,"").replace(/=.*/,"=;expires=Max-Age=-99999999")})}}export default Storage; diff --git a/readme.md b/readme.md index 069c9e4..d458051 100644 --- a/readme.md +++ b/readme.md @@ -21,9 +21,9 @@ yarn add github:WebPlatformForEmbedded/localCookies Next you can `import` the localCookies dependency into your own script and start implementing it from there. ```js -import Storage from './module/Storage.js', +import Storage from './module/localCookie.js', // or -const Storage = require('./module/Storage.js') +const Storage = require('./module/localCookie.js') ``` ## Build diff --git a/rollup.config.js b/rollup.config.js index f01e91a..3b4d246 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -7,18 +7,18 @@ import peerDepsExternal from 'rollup-plugin-peer-deps-external' export default [ { - input: './src/storage.js', + input: './src/localCookie.js', output: { - file: './dist/storage.js', + file: './dist/localCookie.js', format: 'iife', name: 'Storage', }, plugins: [resolve({ browser: true }), commonjs(), babel(), uglify()], }, { - input: './src/storage.js', + input: './src/localCookie.js', output: { - file: './module/storage.js', + file: './module/localCookie.js', format: 'esm', name: 'Storage', }, diff --git a/src/storage.js b/src/localCookie.js similarity index 91% rename from src/storage.js rename to src/localCookie.js index 9abe88e..9d042df 100644 --- a/src/storage.js +++ b/src/localCookie.js @@ -1,7 +1,10 @@ // LocalStorage and Cookie storage library -class Storage { - constructor() { - if (this._checkIfLocalStorageWorks() === true) +class localCookie { + constructor(options) { + options = options || {} + this.forceCookies = options.forceCookies || false + + if (this._checkIfLocalStorageWorks() === true && options.forceCookies !== true) return { getItem : this._getItemLocalStorage, setItem : this._setItemLocalStorage, @@ -96,4 +99,4 @@ class Storage { } } -export default Storage \ No newline at end of file +export default localCookie \ No newline at end of file diff --git a/test/test.js b/test/test.js index 5bf0fad..0659a08 100644 --- a/test/test.js +++ b/test/test.js @@ -1,8 +1,9 @@ -import Storage from '../src/storage.js' -let ls = new Storage() +import Storage from '../src/localCookie.js' + let expect = chai.expect -describe('Storage', () => { +describe('local Storage', () => { + let ls = new Storage() it('should return an object', () => { expect(ls).to.be.a('object') }) @@ -18,9 +19,7 @@ describe('Storage', () => { it('should have a clear function', () => { expect(ls.clear).to.be.a('function') }) -}) -describe('Store and get data', () => { it('should accept a key/value "a" with value "b"', () => { let _r = ls.setItem('a','b') expect( _r ).to.be.a('undefined') @@ -34,9 +33,7 @@ describe('Store and get data', () => { ls.setItem('anumber', 1) expect( ls.getItem('anumber') ).to.be.a('string') }) -}) -describe('Remove data', () => { it('should removeItem key "a"', () => { let _r = ls.removeItem('a') expect( _r ).to.be.a('undefined') @@ -46,9 +43,7 @@ describe('Remove data', () => { let _r = ls.getItem('a') expect( _r ).to.be.a('null') }) -}) -describe('Clear data', () => { it('set key "a" and key "b"', () => { ls.setItem('a', 'a') ls.setItem('b', 'b') @@ -67,4 +62,64 @@ describe('Clear data', () => { }) }) +describe('Cookies ', () => { + let cs = new Storage({ forceCookies : true }) + it('should return an object', () => { + expect(cs).to.be.a('object') + }) + + it('should have a setItem function', () => { + expect(cs.setItem).to.be.a('function') + }) + + it('should have a removeItem function', () => { + expect(cs.removeItem).to.be.a('function') + }) + + it('should have a clear function', () => { + expect(cs.clear).to.be.a('function') + }) + + it('should accept a key/value "a" with value "b"', () => { + let _r = cs.setItem('a','b') + expect( _r ).to.be.a('undefined') + }) + it('should return the same value "b" for key "a" ', () => { + let _r = cs.getItem('a') + expect( _r ).to.be.equal('b') + }) + + it('should store a number as a string', () => { + cs.setItem('anumber', 1) + expect( cs.getItem('anumber') ).to.be.a('string') + }) + + it('should removeItem key "a"', () => { + let _r = cs.removeItem('a') + expect( _r ).to.be.a('undefined') + }) + + it('should return a null value for key "a"', () => { + let _r = cs.getItem('a') + expect( _r ).to.be.a('null') + }) + + it('set key "a" and key "b"', () => { + cs.setItem('a', 'a') + cs.setItem('b', 'b') + }) + + it('call clear()', () => { + expect( cs.clear() ).to.be.a('undefined') + }) + + it('should not have a value for key "a"', () => { + expect( cs.getItem('a') ).to.be.a('null') + }) + + it('should not have a value for key "b"', () => { + expect( cs.getItem('b') ).to.be.a('null') + }) +}) + mocha.run() \ No newline at end of file