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

ngrx-enums

v0.0.11

Published

A small library that provides the base classes for for implementing @ngrx actions and reducers with ts-enums.

Downloads

3

Readme

ngrx-enums

A small library that provides the base classes for for implementing @ngrx actions and reducers with ts-enums

Motivation

@ngrx/store is a very powerful utility for managing the state of Angular apps, but some developers have criticized the example app for containing too much boilerplate (particularly in the action classes) and for having large switch statements in the reducers. ngrx-example-app-enums is a fork of the example app that uses ts-enums to encapsulate the actions and reducers, thereby reducing boilerplate and hiding the switch statement from view.

This library builds on ts-enums to provide just the base files that ngrx-example-app-enums uses so that they can be used separately in your apps.

The basics

Install:

npm install ngrx-enums

Example

These examples are included in a test.

Actions Defined via Enums

import {ActionEnum, ActionEnumValue} from 'ngrx-enums';

class LayoutAction<T> extends ActionEnumValue<T> {
  constructor(name: string) {
    super(name);
  }
}

class LayoutActionEnumType extends ActionEnum<LayoutAction<any>> {
  OPEN_SIDENAV = new LayoutAction<void>('[Layout] Open Sidenav');
  OPEN_SIDENAV_ALSO = new LayoutAction<void>('[Layout] Open Sidenav Also');
  SET_SIDENAV = new LayoutAction<boolean>('[Layout] Set Sidenav');

  constructor() {
    super();
    this.initEnum('layoutActions');
  }
}

export const LayoutActionEnum: LayoutActionEnumType = new LayoutActionEnumType();

Reducer Defined via matches() Methods

import {simplePropertyReducer, TypedAction} from 'ngrx-enums';

interface State {
  showSidenav: boolean;
}

const initialState: State = {
  showSidenav: false
};

function layoutReducer(state = initialState, action: TypedAction<any>): State {
  if (LayoutActionEnum.matches(action,
      LayoutActionEnum.OPEN_SIDENAV, LayoutActionEnum.OPEN_SIDENAV_ALSO)) {
    // a simple reducer that always sets the same value
    // this shows how to respond to multiple actions
    // (fall-through in switch)
    return {...state, showSidenav: true};
  } else if (LayoutActionEnum.SET_SIDENAV.matches(action)) {
    // a reducer that accepts a value and copies it as a property into state
    return simplePropertyReducer<State, boolean>('showSidenav')(state, action);
  } else {
    return state;
  }
}

Reducer Defined via Enums

import {
  ActionEnumValue,
  ReducerEnum,
  ReducerEnumValue,
  ReducerFunction,
  simplePropertyReducer
} from 'ngrx-enums';

interface State {
  showSidenav: boolean;
}

const initialState: State = {
  showSidenav: false
};

class LayoutReducer<T> extends ReducerEnumValue<State, T> {
  constructor(action: ActionEnumValue<T> | ActionEnumValue<T>[],
              reduce: ReducerFunction<State, T>) {
    super(action, reduce);
  }
}

class LayoutReducerEnumType extends ReducerEnum<LayoutReducer<any>, State> {
  // a simple reducer that always sets the same value
  // this shows how to respond to multiple actions
  // (fall-through in switch)
  OPEN_SIDENAV = new LayoutReducer<void>(
    [LayoutActionEnum.OPEN_SIDENAV, LayoutActionEnum.OPEN_SIDENAV_ALSO],
      (state: State) => ({showSidenav: true}));
  // a reducer that accepts a value and copies it as a property into state
  SET_SIDENAV = new LayoutReducer<boolean>(
    LayoutActionEnum.SET_SIDENAV,
    simplePropertyReducer<State, boolean>('showSidenav')
  );

  constructor() {
    super(initialState);
    this.initEnum('layoutReducers');
  }
}

export const LayoutReducerEnum: LayoutReducerEnumType = new LayoutReducerEnumType();

More information

  • Some users get an error that looks something like this when compiling:

    ERROR in Error encountered resolving symbol values statically. Calling function 'ɵmakeDecorator', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol Injectable...

    If you get this error, you might be able to fix the problem by adding a path of "@angular/*": ["../node_modules/@angular/*"] to your tsconfig.app.json file (more information).