local-storage-helper
v1.2.5
Published
It helps you to work with LocalStorage
Downloads
24
Readme
Local Storage Helper
It helps you to work with LocalStorage
Usage
Step 1: Install local-storage-helper
npm install local-storage-helper --save
Step 2: Set up application prefix
import { LocalStorageHelper } from 'local-storage-helper';
LocalStorageHelper.setAppPrefix('yourPrefix');
Step 3: Start using
const account = new LocalStorageHelper('account');
account.save(someData);
Methods
| Method | Type | Description |----------------|--------|------------ | save | (new (options: any) => T | any) => void | This method save data to LocalStorage | restoreAs | (Type?: new (options: any) => T) => T | any | This method get data from LocalStorage, and can automatically convert to Class instance
Static Methods
| Method | Type | Description |----------------|--------|------------ | setAppPrefix | (value: string) => void | This method will set a prefix to your keys in LocalStorage
Example with RxJs
class Account {
...
}
class AccountService {
private readonly subAccount: BehaviorSubject<Account>;
private readonly accountStorage: LocalStorageHelper<Account> = new LocalStorageHelper<Account>('account');
constructor() {
this.subAccount = new BehaviorSubject(this.accountStorage.restoreAs(Account));
this.subAccount.subscribe((account => this.accountStorage.save(account)));
}
public get account(): Account {
return this.subAccount.getValue();
}
public set account(value: Account) {
this.subAccount.next(value);
}
public clear(): void {
this.account = null;
}
}