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

@aegis-techno/ngx-local-storage

v17.0.0

Published

An Angular wrapper for local storage access.

Downloads

86

Readme

ngx-local-storage

An Angular wrapper for local storage access.

Installation

Install via npm:

npm install @aegis-techno/ngx-local-storage --save

Install manually:

  • Add @aegis-techno/ngx-local-storage into package.json.
  • Run npm install.
  • Import NgxLocalStorageModule.forRoot() into the root module of your default application (or defining a project by using the --project <PROJECT_NAME> and/or --module <MODULE_PATH> CLI parameters).

Usage

1. Import NgxLocalStorageModule

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {NgxLocalStorageModule} from '@aegis-techno/ngx-local-storage';

@NgModule({
    imports: [BrowserModule, NgxLocalStorageModule.withConfig()],
    bootstrap: [AppComponent],
})
export class AppModule {}

Configuration (NgxLocalStorageModule.withConfig(moduleConfig))

  • prefix
    • Type: string
    • Determines the key prefix.
    • Default: null
  • allowNull
    • Type: boolean
    • Determines if null | 'null' values should be stored.
    • Default: true
  • saveEventName
    • Type: string
    • Determines which key listen from EventBusService.
    • Default: undefined

Serialization

Default serialization

The library utilizes the JSON.stringify()/JSON.parse() mechanics to pass values (of any type) to and from localstorage per default. If you wish you can override that behaviour by injecting your own custom serializer (app wide) or pass one per stoage call.

App wide serializer

Inject your custom serializer implentation using the provided injection token:

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {NgxLocalStorageModule, NGX_LOCAL_STORAGE_SERIALIZER} from 'ngx-localstorage';

@NgModule({
    imports: [
        BrowserModule,
        NgxLocalStorageModule.forRoot()
    ],
    bootstrap: [AppComponent],
    providers: [
        {
            provide: NGX_LOCAL_STORAGE_SERIALIZER,
            useClass: <custom serializer implementing StorageSerializer >
        }
    ]
})
export class AppModule {
}

Per call serializer

Every set()/get call on NgxLocalStorageService supports to pass an optional (de)seriaizer. If none is provided the app wide (or default) one is used.

API

LocalStorageService

Methods

  • set(key: string, value: string, options? : {prefix?: string, serilizer?: StorageSerializer}): void: Adds the value with the given key or updates an existing entry using either the provided or default serializer.
  • get(key: string, options? : {prefix?: string, serilizer?: StorageSerializer}): string | null | undefined: Gets the entry specified by the given key or null using either the provided or default serializer.
  • remove(key: string, prefix?: string): void: Removes the entry specified by the given key.
  • clear(): void: Clears all entries of the applications local storage.

Example

import {LocalStorageService} from '@aegis-techno/ngx-local-storage';

@Component({
    selector: 'app-storage-access',
    template: './storage-access.component.html',
})
export class StorageAccessComponent implements OnInit {
    constructor(private _storageService: NgxLocalStorageService) {}

    ngOnInit(): void {
        this._storageService.set('key', 'value');
        console.log('Value : ', this._storageService.get('key')); // expected "Value : value"
    }
}

EventBusService support

Example

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {LocalStorageService, NgxLocalStorageModule, NGX_LOCAL_STORAGE_SERIALIZER} from '@aegis-techno/ngx-local-storage';

@NgModule({
    imports: [
        BrowserModule,
        NgxLocalStorageModule.forRoot({
            saveEventName: 'saveEvent',
        }),
    ],
    bootstrap: [StorageAccessComponent],
})
export class AppModule {}

@Component({
    selector: 'app-storage-access',
    template: './storage-access.component.html',
})
export class StorageAccessComponent implements OnInit {
    constructor(private _storageService: NgxLocalStorageService) {}

    ngOnInit(): void {
        EventBusService.getInstance<EventBusService>(EventBusService).emit({
            name: saveEventName,
            value: {
                key: key,
                value: 'value',
            },
        });
        console.log('Value : ', this._storageService.get('key')); // expected "Value : value"
    }
}