storage
The storage object provides an interface to the browser's storage storage object. You can use it to set, get, and remove key/value pairs stored in the storage storage object.
Usage
To use this module, you first need to import the storage:
import { storage } from '@ponsetya/browser'Methods
storage.key()
The key() method returns name of the key with the specified index.
storage.key(index: number): string | undefinedParameters
- 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
storage.set('name', 'John')
storage.set('surname', 'Doe')
console.log(storage.key(0)) // 'name'
console.log(storage.key(1)) // 'surname'storage.size()
The size() method returns the number of items stored in the browsers Storage Object, for this particular domain.
storage.get(name: string): stringParameters
- Returns:
number - A Integer, representing the number of stored items.
Examples
console.log(storage.size()) // numberstorage.get()
The get() method returns value of the specified Storage Storage object item.
storage.get<T = string>(key: string): T | undefinedParameters
- 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
storage.set('name', 'John')
storage.set('surname', 'Doe')
console.log(storage.get<string>('name')) // 'John'
console.log(storage.get<string>('surname')) // 'Doe'
console.log(storage.get<number>('age')) // undefinedstorage.remove()
The remove() method removes the specified Storage Storage object item.
storage.remove(name: string): voidParameters
- key:
string - A String specifying the name of the item you want to remove
Examples
storage.set('name', 'John')
console.log(storage.get('name')) // 'John'
storage.remove('name')
console.log(storage.get('name')) // ''storage.set()
The set() method sets the value of the specified Storage Storage object item.
storage.set(key: string, value: StorageValue): voidParameters
- key:
string - A String specifying the name of the key you want to set the value of.
- value:
StorageValue A String specifying the value of the key you want to set the value of.
Examples
storage.set('name', 'John')
storage.set('surname', 'Doe')
console.log(storage.get<string>('name')) // 'John'
console.log(storage.get<string>('surname')) // 'Doe'Type Aliases
Storage Value
The StorageValue type represents the possible values that can be stored in a web storage (e.g. localStorage).
type StorageValue = string | number | boolean | object | null