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

@datakitchen/ngx-toolkit

v17.0.0

Published

Goodies for Angular developer, backed at DataKitchen

Downloads

46

Readme

Datakitchen's ngx-toolkit :rocket:

@datakitchen/ngx-toolkit

This library contains a collection of utilities for Angular applications. There are three categories of utilities:

  • General utilities such as a @Memoize decorator and a wrapper around the browser localStorage.
  • An abstract component and a set of decorators and interfaces to ease the implementation of common tasks such as search, pagination, and sorting.
  • Testing utilities to ease mocking and implementing common tasks when writing unit tests.

Install

With your package manager of preference, install

@datakitchen/ngx-toolkit

General Utilities

say something nice

@Memoize

Caches a method's result when it has already been calculated for a given input.

class TestClass {

  @Memoize
  testMemo(value: number): number {
    return value * 2;
  }
}

TypedForms

An easier to use version of Angular's TypedForms

TBD add example and comparison with Angular's TypedForms

StorageService

Service that wraps localStorage.

TBD add example and explain optional localStorage

ParameterService

TBD add example and use case especially for when the ParameterService is handy for fixing issue with angular's Router

CoreComponent

TBD add small description or maybe a TOC for all the CoreComponent things

Automatic unsubscription

CoreComponent implements a subscriptions: Subscription[] array for automatic unsubscriptions of observables.

import { CoreComponent } from '@datakitchen/ngx-toolkit';

@Component({
  selector: 'comp',
  template: '<h1>my component</h1>'
})
class TestClassComponent extends CoreComponent {

  private subject$ = new Subject();

  override ngOnInit() {
    // Extend CoreComponent to leverage automatic unsubscriptions
    this.subscriptions.push(this.subject$.subscribe());
  }
}

subject$ will be automatically unsubscribed on OnDestroy.

Defer a function after a lifecycle hooks

Sometimes you may want to defer a method/function call after one of Angular's lifecycle hooks.

import { CoreComponent } from '@datakitchen/ngx-toolkit';

@Component({
  selector: 'comp',
  template: `
<h1>My Component</h1>
`
})
class TestClassComponent extends CoreComponent {

  callAfterNgAfterContentInit() {
    this.defer(() => {
      // do stuff

      /**
       * The callback function is executed after the given lifecycle and,
       * after that, every subsequent call to `callAfterNgAfterContentInit`
       * will execute the callback directly, i.e.: as if the defer block wans't there.
       */
    }).after('AfterContentInit');
  }
}

At the moment, the lifecycle hooks available are OnInit, AfterContentInit and AfterViewInit.

@PersistOnLocalStorage

Annotate a property with @PersistOnLocalStorage() to have it hydrated from localStorage and to keep its value synced on the local storage.

import { CoreComponent } from '@datakitchen/ngx-toolkit';

@Component({
  // eslint-disable-next-line @angular-eslint/component-selector
  selector: 'comp',
  template: `
<h1>my component</h1>
`,
})
class TestComponent extends CoreComponent {

  @PersistOnLocalStorage()
  heroEmail: string = 'b@tman';
}

When the component runs its ngOnInit lifecycle, it checks for all properties decorated with @PersistOnLocalStorage. If a non-nullish entry for the given variable exists on localStorage, then that value is used instead.

The following interface represents the options that can be passed to the decorator

export interface PersistOnLocalStorageOptions {
  /**
   * A string for the namespace to scope the property value with
   * A `DeferredProp` as in
   *
   * @example
   *   @PersistOnLocalStorage({namespace: Prop('myNamespace')})
   *   heroEmailDeferred: string = 'spiderman';
   *
   *   ngOnInit() {
   *     this.myNamespace = someAsyncValue;
   *   }
   *
   * the `Prop` is resolved after ngOnInit inside CoreComponent
   */
  namespace?: string | DeferredProp;

  /**
   * When the decorated property is an object we can whilelist some properties
   * so that only those are persisted.
   *
   * @example
   *
   *  @PersistOnLocalStorage({whiteList: [ 'age' ]})
   *  fgWithWhiteList = new FormGroup({
   *    age: new FormControl(),
   *    birthDate: new FormControl(),
   *  });
   */
  whiteList?: string[];


  /**
   * When the decorated property is an object we can blackList some properties
   * so that only those are not persisted.
   *
   * @example
   *
   *  @PersistOnLocalStorage({blackList: [ 'age' ]})
   *  fgWithBlackList = new FormGroup({
   *    age: new FormControl(),
   *    birthDate: new FormControl(),
   *  });
   */
  blackList?: string[];

}

@BindToQueryParameters

TBD add docs and examples

hasSearchForm

TBD add docs and examples

hasPaginator

TBD add docs and examples

hasSorting

TBD add docs and examples

Testing Utilities

ActivatedRouteMock

TBD add docs and examples

HttpClientMock

TBD add docs and examples

MockComponent

TBD add docs and examples

MockService

TBD add docs and examples