Tiny wrapper around localStorage and sessionStorage with reliable data storage.
The tinyLocalStorage
is the wrapper over the localStorage
.
The tinySessionStorage
is the wrapper over the sessionStorage
.
npm install tiny-storage
import { tinyLocalStorage, tinySessionStorage } from 'tiny-storage';
// Or as CommonJS
// const { tinyLocalStorage, tinySessionStorage } = require('tiny-storage');
tinyLocalStorage.set('user', {
name: 'Ali',
surname: 'Gasymov'
});
tinySessionStorage.set('user', {
name: 'Ali',
surname: 'Gasymov'
});
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/tiny-storage.min.js"></script>
<script>
tinyLocalStorage.set('user', {
name: 'Ali',
surname: 'Gasymov'
});
tinySessionStorage.set('user', {
name: 'Ali',
surname: 'Gasymov'
});
</script>
When passed a number n, returns the name of the nth key in the storage. The order of keys is user-agent defined, so you should not rely on it.
tinyLocalStorage.key(0);
tinySessionStorage.key(0);
When passed a key name, will return that key's value or null if the key does not exist.
tinyLocalStorage.get('user');
tinySessionStorage.get('user');
When passed a key name and value, will add that key to the storage, or update that key's value if it already exists.
tinyLocalStorage.set('user', {
name: 'Ali',
surname: 'Gasymov'
});
tinySessionStorage.set('user', {
name: 'Ali',
surname: 'Gasymov'
});
When passed a key name, will remove that key from the storage if it exists. If there is no item associated with the given key, this method will do nothing.
tinyLocalStorage.remove('user');
tinySessionStorage.remove('user');
When invoked, clears all stored keys.
tinyLocalStorage.clear();
tinySessionStorage.clear();
Returns an integer representing the number of data items stored in the storage.