-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
731b8c4
commit b097782
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {getItem, setItem, removeItem} from './' | ||
|
||
class SessionStorage implements SessionStorage { | ||
map: {[key: string]: any} = {} | ||
getItem(key: string): void { | ||
return this.map[key] | ||
} | ||
setItem(key: string, value: any): void { | ||
this.map[key] = value | ||
} | ||
removeItem(key: string): void { | ||
delete this.map[key] | ||
} | ||
} | ||
|
||
Object.defineProperty(window, 'sessionStorage', { | ||
value: new SessionStorage() | ||
}) | ||
|
||
const TEST_VALUES: Record<string, any> = { | ||
string: 'bar', | ||
boolean: true, | ||
number: 1, | ||
object: {foo: true}, | ||
array: [1, 2, 'asd'] | ||
} | ||
|
||
Object.entries(TEST_VALUES).forEach(([key, value]) => { | ||
test(`sessionStorage ${key}`, () => { | ||
expect(getItem(key)).toBeNull() | ||
setItem(key, value) | ||
expect(getItem(key)).toEqual(value) | ||
removeItem(key) | ||
expect(getItem(key)).toBeNull() | ||
}) | ||
|
||
test(`sessionStorage raw ${key}`, () => { | ||
expect(getItem(`raw${key}`, {raw: true})).toBeNull() | ||
setItem(`raw${key}`, value, {raw: true}) | ||
expect(getItem(`raw${key}`, {raw: true})).toEqual(String(value)) | ||
removeItem(`raw${key}`) | ||
expect(getItem(`raw${key}`, {raw: true})).toBeNull() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** Session storage item options */ | ||
export interface StorageItemOptions { | ||
/** Use the raw value, by default the value is converted to JSON */ | ||
raw?: boolean | ||
} | ||
|
||
/** Get an item from session storage */ | ||
export const getItem = <T>( | ||
key: string, | ||
options: StorageItemOptions = {} | ||
): T => { | ||
const {raw} = options | ||
let item | ||
|
||
try { | ||
const value = window.sessionStorage.getItem(key) | ||
|
||
item = raw ? value : JSON.parse(value ?? '') | ||
} catch {} | ||
|
||
return item ?? null | ||
} | ||
|
||
/** Put an item to session storage */ | ||
export const setItem = <T>( | ||
key: string, | ||
value: T, | ||
options: StorageItemOptions = {} | ||
): void => { | ||
const {raw} = options | ||
|
||
try { | ||
const resultValue = raw ? String(value) : JSON.stringify(value) | ||
|
||
window.sessionStorage.setItem(key, resultValue) | ||
} catch {} | ||
} | ||
|
||
/** Remove an item from session storage */ | ||
export const removeItem = (key: string): void => { | ||
try { | ||
window.sessionStorage.removeItem(key) | ||
} catch {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@rambler-tech/session-storage", | ||
"version": "0.0.0", | ||
"main": "dist", | ||
"module": "dist", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../tsconfig.package.json |