Browser
References
session

session

The session object provides an interface to the browser's session storage object. You can use it to set, get, and remove key/value pairs stored in the session storage object.

Usage

To use this module, you first need to import the session:

import { session } from '@ponsetya/browser'

Methods

session.key()

The key() method returns name of the key with the specified index.

session.key(index: number): string | undefined

Parameters

index: number

A Number representing the index of the key you want to get the name of.

Returns: string | undefined
A String representing the name of the specified key.

Examples

session.set('name', 'John')
session.set('surname', 'Doe')
 
console.log(session.key(0)) // 'name'
console.log(session.key(1)) // 'surname'

session.size()

The size() method returns the number of items stored in the browsers Storage Object, for this particular domain.

session.get(name: string): string

Parameters

Returns: number
A Integer, representing the number of stored items.

Examples

console.log(session.size()) // number

session.get()

The get() method returns value of the specified Session Storage object item.

session.get<T = string>(key: string): T | undefined

Parameters

key: string
A String specifying the name of the key you want to get the value of.
Returns: T | undefined
A String, representing the value of the specified key.

Examples

session.set('name', 'John')
session.set('surname', 'Doe')
 
console.log(session.get<string>('name')) // 'John'
console.log(session.get<string>('surname')) // 'Doe'
console.log(session.get<number>('age')) // undefined

session.remove()

The remove() method removes the specified Session Storage object item.

session.remove(name: string): void

Parameters

key: string
A String specifying the name of the item you want to remove

Examples

session.set('name', 'John')
 
console.log(session.get('name')) // 'John'
 
session.remove('name')
 
console.log(session.get('name')) // ''

session.set()

The set() method sets the value of the specified Session Storage object item.

session.set(key: string, value: SessionValue): void

Parameters

key: string
A String specifying the name of the key you want to set the value of.
value: SessionValue

A String specifying the value of the key you want to set the value of.

Examples

session.set('name', 'John')
session.set('surname', 'Doe')
 
console.log(session.get<string>('name')) // 'John'
console.log(session.get<string>('surname')) // 'Doe'

Type Aliases

Session Value

The SessionValue type represents the possible values that can be stored in a web storage (e.g. sessionStorage).

type SessionValue = string | number | boolean | object | null