Skip to content

Instantly share code, notes, and snippets.

@MetaiR
Created February 18, 2018 07:17
Show Gist options
  • Save MetaiR/d7a6d9a1492363afd66b0626a3585fee to your computer and use it in GitHub Desktop.
Save MetaiR/d7a6d9a1492363afd66b0626a3585fee to your computer and use it in GitHub Desktop.
simple-storage-tools examples
import { Storage } from 'simple-storage-tools';
enum StorageKeys {
KEY1 = 'fist-storage',
KEY2 = 'second-storage',
localKey = 'key-for-local-storage',
sessionKey = 'key-for-session-storage'
}
/**
* this is equal to:
* sessionStorage.setItem('key-for-session-storage', 'Hello World');
*/
Storage.set(StorageKeys.sessionKey, 'Hello World');
/**
* this is equal to:
* console.log(sessionStorage.getItem('key-for-session-storage'));
*/
console.log(Storage.get(StorageKeys.sessionKey)); // print Hello World in console
// you can check the localStorage (it will be null with this key)
console.log(Storage.get(StorageKeys.sessionKey, true)); // print undefined
// this example is for using localStorage instead of sessionStorage
/**
* this is equal to:
* localStorage.setItem('key-for-local-storage', 'Hello World');
*/
Storage.set(StorageKeys.localKey, 'Hello World', true);
/**
* this is equal to:
* console.log(localStorage.getItem('key-for-local-storage'));
*/
console.log(Storage.get(StorageKeys.localKey, true)); // print Hello World in console
// you can check the sessionStorage (it will be null with this key)
console.log(Storage.get(StorageKeys.localKey));
// this example is for putting json as an arguman
const json = {
value1: 'Hello',
value2: 'World'
};
// set and get in and from sessionStorage
Storage.set(StorageKeys.KEY1, json);
let stored = <Object>Storage.get(StorageKeys.KEY1);
console.log(stored.value1 + ' ' + stored.value2); // print Hello World in the console
// set and get in and from localStorage
Storage.set(StorageKeys.KEY2, json, true);
stored = <Object>Storage.get(StorageKeys.KEY2, true);
console.log(stored.value1 + ' ' + stored.value2); // print Hello World in the console
// example for remove a key from storages
// remove a key from sessionStorage
Storage.remove(StorageKeys.KEY1);
console.log(Storage.get(StorageKeys.KEY1) == null); // print true
// remove a key from localStorage
Storage.remove(StorageKeys.KEY2, true);
console.log(Storage.get(StorageKeys.KEY2) == null); // print true
// clean all storages in sessionStorage
Storage.clear();
console.log(Storage.get(StorageKeys.sessionKey) == null); // print true
console.log(Storage.get(StorageKeys.localKey) == null); // print false because you cleaned sessionKey not localStorage
// clear all storages in localStorage
Storage.clear(true);
console.log(Storage.get(StorageKeys.localKey) == null); // print true
// you also can clean all storages with single method:
// Storage.clearAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment