npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-lss

v2.0.2

Published

Reactive adapter for browser's `localStoarge` API

Downloads

15

Readme

ngx-lss is an angular library that provides a reactive adapter for localStorage API

Features

  • 🎆 Provides Observable API
  • 👀 Allows watching changes made in other tabs
  • 🐣 Easily adaptable and replaceable (3 simple methods: save, get, remove)
  • 🔬 Fully tested with jasmine

Installation

npm i ngx-lss

Setting up

  1. Import NgxLocalStorageModule to root module of your app
import { NgxLocalStorageModule } from 'ngx-lss';

@NgModule({
  // ...
  imports: [
    // ...
    NgxLocalStorageModule.forRoot(),
  ]
})
export class AppModule { }
  1. Inject the service in your injectable class
import { Component } from '@angular/core';
import { NgxLocalStorageService } from 'ngx-lss';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  public constructor(
    private readonly NgxLocalStorageService: NgxLocalStorageService,
 ) {
    // ...
  }
}

Usage examples

Abstracting the service in a domain specific storage

💡 This is a recommended way to use the ngx-lss in your applications. It allows you to clearly separate the logic of working with the local storage from the business logic of your application. Also, this way, you can easily replace the library with another storage or your own implementation.

import { Injectable } from '@angular/core';
import { NgxLocalStorageService } from 'ngx-lss';
import { map, Observable } from 'rxjs';

import { UserSecret } from '../models/user-secret';


const USER_SECRET_STORAGE_KEY = 'user';

@Injectable({ providedIn: 'root' })
export class UserSecretStorageService {
  /** Credential secret of the current user. */
  public readonly currentSecret$: Observable<UserSecret | null>;

  public constructor(
    private readonly storageService: NgxLocalStorageService,
  ) {
    this.currentSecret$ = this.storageService.get<UserSecret>(USER_SECRET_STORAGE_KEY);
  }

  /**
   * Saves secret to the current storage.
   * @param secret Secret to save.
   */
  public saveSecret(
    secret: UserSecret,
  ): Observable<UserSecret> {
    return this.storageService.save(USER_SECRET_STORAGE_KEY, secret).pipe(map(() => secret));
  }

  /** Removes current secret. */
  public removeSecret(): Observable<void> {
    return this.storageService.remove(USER_SECRET_STORAGE_KEY);
  }
}

Accessing the storage API directly from a component

import { Component } from '@angular/core';
import { NgxLocalStorageService } from 'ngx-lss';

const COUNTER_KEY = 'counter';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  /** Clicks counter. */
  public readonly counter$: Observable<number | null>;

  public constructor(
    private readonly storage: NgxLocalStorageService,
  ) {
    this.counter$ = this.storage.get<number>(COUNTER_KEY);
  }

  public onUserClick(): void {
    this.counter$.pipe(
      first(),
      map(counter => counter ?? 0),
      switchMap(counter => this.storage.save(COUNTER_KEY, counter + 1)),
    ).subscribe();
  }
}