Skip to content

Commit e3fbd02

Browse files
committed
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.
1 parent 58ef2df commit e3fbd02

File tree

8 files changed

+79
-21
lines changed

8 files changed

+79
-21
lines changed

dist/localCookie.js

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

dist/storage.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

module/localCookie.js

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

module/storage.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ yarn add github:WebPlatformForEmbedded/localCookies
2121
Next you can `import` the localCookies dependency into your own script and start implementing it from there.
2222

2323
```js
24-
import Storage from './module/Storage.js',
24+
import Storage from './module/localCookie.js',
2525
// or
26-
const Storage = require('./module/Storage.js')
26+
const Storage = require('./module/localCookie.js')
2727
```
2828

2929
## Build

rollup.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import peerDepsExternal from 'rollup-plugin-peer-deps-external'
77

88
export default [
99
{
10-
input: './src/storage.js',
10+
input: './src/localCookie.js',
1111
output: {
12-
file: './dist/storage.js',
12+
file: './dist/localCookie.js',
1313
format: 'iife',
1414
name: 'Storage',
1515
},
1616
plugins: [resolve({ browser: true }), commonjs(), babel(), uglify()],
1717
},
1818
{
19-
input: './src/storage.js',
19+
input: './src/localCookie.js',
2020
output: {
21-
file: './module/storage.js',
21+
file: './module/localCookie.js',
2222
format: 'esm',
2323
name: 'Storage',
2424
},

src/storage.js renamed to src/localCookie.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// LocalStorage and Cookie storage library
2-
class Storage {
3-
constructor() {
4-
if (this._checkIfLocalStorageWorks() === true)
2+
class localCookie {
3+
constructor(options) {
4+
options = options || {}
5+
this.forceCookies = options.forceCookies || false
6+
7+
if (this._checkIfLocalStorageWorks() === true && options.forceCookies !== true)
58
return {
69
getItem : this._getItemLocalStorage,
710
setItem : this._setItemLocalStorage,
@@ -96,4 +99,4 @@ class Storage {
9699
}
97100
}
98101

99-
export default Storage
102+
export default localCookie

test/test.js

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import Storage from '../src/storage.js'
2-
let ls = new Storage()
1+
import Storage from '../src/localCookie.js'
2+
33
let expect = chai.expect
44

5-
describe('Storage', () => {
5+
describe('local Storage', () => {
6+
let ls = new Storage()
67
it('should return an object', () => {
78
expect(ls).to.be.a('object')
89
})
@@ -18,9 +19,7 @@ describe('Storage', () => {
1819
it('should have a clear function', () => {
1920
expect(ls.clear).to.be.a('function')
2021
})
21-
})
2222

23-
describe('Store and get data', () => {
2423
it('should accept a key/value "a" with value "b"', () => {
2524
let _r = ls.setItem('a','b')
2625
expect( _r ).to.be.a('undefined')
@@ -34,9 +33,7 @@ describe('Store and get data', () => {
3433
ls.setItem('anumber', 1)
3534
expect( ls.getItem('anumber') ).to.be.a('string')
3635
})
37-
})
3836

39-
describe('Remove data', () => {
4037
it('should removeItem key "a"', () => {
4138
let _r = ls.removeItem('a')
4239
expect( _r ).to.be.a('undefined')
@@ -46,9 +43,7 @@ describe('Remove data', () => {
4643
let _r = ls.getItem('a')
4744
expect( _r ).to.be.a('null')
4845
})
49-
})
5046

51-
describe('Clear data', () => {
5247
it('set key "a" and key "b"', () => {
5348
ls.setItem('a', 'a')
5449
ls.setItem('b', 'b')
@@ -67,4 +62,64 @@ describe('Clear data', () => {
6762
})
6863
})
6964

65+
describe('Cookies ', () => {
66+
let cs = new Storage({ forceCookies : true })
67+
it('should return an object', () => {
68+
expect(cs).to.be.a('object')
69+
})
70+
71+
it('should have a setItem function', () => {
72+
expect(cs.setItem).to.be.a('function')
73+
})
74+
75+
it('should have a removeItem function', () => {
76+
expect(cs.removeItem).to.be.a('function')
77+
})
78+
79+
it('should have a clear function', () => {
80+
expect(cs.clear).to.be.a('function')
81+
})
82+
83+
it('should accept a key/value "a" with value "b"', () => {
84+
let _r = cs.setItem('a','b')
85+
expect( _r ).to.be.a('undefined')
86+
})
87+
it('should return the same value "b" for key "a" ', () => {
88+
let _r = cs.getItem('a')
89+
expect( _r ).to.be.equal('b')
90+
})
91+
92+
it('should store a number as a string', () => {
93+
cs.setItem('anumber', 1)
94+
expect( cs.getItem('anumber') ).to.be.a('string')
95+
})
96+
97+
it('should removeItem key "a"', () => {
98+
let _r = cs.removeItem('a')
99+
expect( _r ).to.be.a('undefined')
100+
})
101+
102+
it('should return a null value for key "a"', () => {
103+
let _r = cs.getItem('a')
104+
expect( _r ).to.be.a('null')
105+
})
106+
107+
it('set key "a" and key "b"', () => {
108+
cs.setItem('a', 'a')
109+
cs.setItem('b', 'b')
110+
})
111+
112+
it('call clear()', () => {
113+
expect( cs.clear() ).to.be.a('undefined')
114+
})
115+
116+
it('should not have a value for key "a"', () => {
117+
expect( cs.getItem('a') ).to.be.a('null')
118+
})
119+
120+
it('should not have a value for key "b"', () => {
121+
expect( cs.getItem('b') ).to.be.a('null')
122+
})
123+
})
124+
70125
mocha.run()

0 commit comments

Comments
 (0)