Skip to content

Commit

Permalink
feat(session-storage): init
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Sep 21, 2023
1 parent 731b8c4 commit b097782
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/session-storage/index.test.ts
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()
})
})
44 changes: 44 additions & 0 deletions packages/session-storage/index.ts
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 {}
}
12 changes: 12 additions & 0 deletions packages/session-storage/package.json
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"
}
}
1 change: 1 addition & 0 deletions packages/session-storage/tsconfig.json

0 comments on commit b097782

Please sign in to comment.