1.2.5 • Published 2 years ago

local-storage-helper v1.2.5

Weekly downloads
4
License
MIT
Repository
github
Last release
2 years ago

Local Storage Helper

It helps you to work with LocalStorage

npm version GitHub issues GitHub stars GitHub license

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

MethodTypeDescription
save(new (options: any) => T | any) => voidThis method save data to LocalStorage
restoreAs(Type?: new (options: any) => T) => T | anyThis method get data from LocalStorage, and can automatically convert to Class instance

Static Methods

MethodTypeDescription
setAppPrefix(value: string) => voidThis 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;
  }
}

License

MIT

1.2.5

2 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.1

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago