Skip to content

Commit 6cbae3b

Browse files
feature/local-storage (#17)
* feat: add `LocalStorage` Class * tests: add `LocalStorage` unit tests
1 parent b5b755e commit 6cbae3b

File tree

5 files changed

+186
-2
lines changed

5 files changed

+186
-2
lines changed

__tests__/storage/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
14
import { Storage } from '@/storage/Storage'
25

36
describe( 'Storage', () => {

__tests__/storage/local.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
import { LocalStorage } from '@/storage/LocalStorage'
5+
6+
describe( 'LocalStorage', () => {
7+
8+
beforeEach( () => {
9+
localStorage.clear()
10+
} )
11+
12+
13+
describe( 'LocalStorage.set()', () => {
14+
15+
it( 'sets an item', () => {
16+
LocalStorage.set( 'key', 'value' )
17+
expect( LocalStorage.get( 'key' ) ).toBe( 'value' )
18+
} )
19+
20+
} )
21+
22+
23+
describe( 'LocalStorage.get()', () => {
24+
25+
it( 'gets an item', () => {
26+
LocalStorage.set( 'key', 'value' )
27+
expect( LocalStorage.get( 'key' ) ).toBe( 'value' )
28+
} )
29+
30+
it( 'returns `undefined` for non-existing key', () => {
31+
expect( LocalStorage.get( 'nonExistingKey' ) ).toBeUndefined()
32+
} )
33+
34+
} )
35+
36+
37+
describe( 'LocalStorage.delete()', () => {
38+
39+
it( 'removes an item', () => {
40+
LocalStorage.set( 'key', 'value' )
41+
LocalStorage.delete( 'key' )
42+
expect( LocalStorage.get( 'key' ) ).toBeUndefined()
43+
} )
44+
45+
} )
46+
47+
48+
describe( 'LocalStorage.clear()', () => {
49+
50+
it( 'clears all items', () => {
51+
LocalStorage.set( 'key1', 'value1' )
52+
LocalStorage.set( 'key2', 'value2' )
53+
LocalStorage.clear()
54+
expect( LocalStorage.get( 'key1' ) ).toBeUndefined()
55+
expect( LocalStorage.get( 'key2' ) ).toBeUndefined()
56+
} )
57+
58+
} )
59+
60+
describe( 'LocalStorage.key()', () => {
61+
62+
it( 'returns the correct key for a given index', () => {
63+
LocalStorage.set( 'key1', 'value1' )
64+
LocalStorage.set( 'key2', 'value2' )
65+
expect( LocalStorage.key( 0 ) ).toBe( 'key1' )
66+
expect( LocalStorage.key( 1 ) ).toBe( 'key2' )
67+
} )
68+
69+
} )
70+
71+
describe( 'LocalStorage.getLength()', () => {
72+
73+
it( 'returns the correct length', () => {
74+
expect( LocalStorage.getLength() ).toBe( 0 )
75+
LocalStorage.set( 'key1', 'value1' )
76+
expect( LocalStorage.getLength() ).toBe( 1 )
77+
LocalStorage.set( 'key2', 'value2' )
78+
expect( LocalStorage.getLength() ).toBe( 2 )
79+
LocalStorage.clear()
80+
expect( LocalStorage.getLength() ).toBe( 0 )
81+
} )
82+
83+
} )
84+
85+
} )

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292
"test:validation": "pnpm test:watch validation.test.ts",
9393
"test:storage": "pnpm test:watch storage/index.test.ts",
9494
"test:cookie": "pnpm test:watch storage/cookie.test.ts",
95-
"test:cookie:jsdom": "pnpm test:watch storage/cookie-jsdom.test.ts"
95+
"test:cookie:jsdom": "pnpm test:watch storage/cookie-jsdom.test.ts",
96+
"test:local": "pnpm test:watch storage/local.test.ts"
9697
},
9798
"devDependencies": {
9899
"@alessiofrittoli/node-scripts": "^2.2.1",

src/storage/LocalStorage.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Storage } from './Storage'
2+
3+
4+
/**
5+
* A browser-compatible implementation of [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage). Data is stored
6+
* unencrypted in the file specified by the `--localstorage-file` CLI flag.
7+
* The maximum amount of data that can be stored is 10 MB.
8+
* Any modification of this data outside of the Web Storage API is not supported.
9+
* Enable this API with the `--experimental-webstorage` CLI flag.
10+
* `localStorage` data is not stored per user or per request when used in the context
11+
* of a server, it is shared across all users and requests.
12+
*/
13+
export class LocalStorage
14+
{
15+
/**
16+
* Removes all key/value pairs, if there are any.
17+
*
18+
* Dispatches a storage event on Window objects holding an equivalent Storage object.
19+
*
20+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/clear)
21+
*/
22+
static clear()
23+
{
24+
return localStorage.clear()
25+
}
26+
27+
28+
/**
29+
* Returns the current value associated with the given key, or undefined if the given key does not exist.
30+
*
31+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/getItem)
32+
*/
33+
static get<T>( name: string )
34+
{
35+
return (
36+
Storage.parseValue<T>( localStorage.getItem( name ) || undefined )
37+
)
38+
}
39+
40+
41+
/**
42+
* Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
43+
*
44+
* Throws a "QuotaExceededError" DOMException exception if the new value couldn't be set. (Setting could fail if, e.g., the user has disabled storage for the site, or if the quota has been exceeded.)
45+
*
46+
* Dispatches a storage event on Window objects holding an equivalent Storage object.
47+
*
48+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/setItem)
49+
*/
50+
static set<T>( name: string, value: T )
51+
{
52+
return localStorage.setItem( name, Storage.stringifyValue( value ) )
53+
}
54+
55+
56+
/**
57+
* Removes the key/value pair with the given key, if a key/value pair with the given key exists.
58+
*
59+
* Dispatches a storage event on Window objects holding an equivalent Storage object.
60+
*
61+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/removeItem)
62+
*/
63+
static delete( name: string )
64+
{
65+
return localStorage.removeItem( name )
66+
}
67+
68+
69+
/**
70+
* Returns the name of the nth key, or null if n is greater than or equal to the number of key/value pairs.
71+
*
72+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/key)
73+
*/
74+
static key( index: number )
75+
{
76+
return localStorage.key( index )
77+
}
78+
79+
80+
/**
81+
* Returns the number of key/value pairs.
82+
*
83+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/length)
84+
*/
85+
static getLength()
86+
{
87+
return localStorage.length
88+
}
89+
}

src/storage/Storage.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isPromise, isString } from '@/types'
55
import { isEmpty } from '@/validation'
66

77
import { Cookie } from './Cookie'
8+
import { LocalStorage } from './LocalStorage'
89

910

1011
/**
@@ -78,9 +79,14 @@ export class Storage
7879
}
7980
}
8081

81-
8282
/**
8383
* Reference to the {@link Cookie} utility.
8484
*/
8585
static cookie = Cookie
86+
87+
88+
/**
89+
* Reference to the {@link LocalStorage} storage utility.
90+
*/
91+
static local = LocalStorage
8692
}

0 commit comments

Comments
 (0)