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

type-safe-state-js

v1.2.8

Published

Functional, type-safe nested state object.

Downloads

21

Readme

type-safe-state-js

License: MIT npm version Build Status Coverage Status

REACT NATIVE USERS, PLEASE READ THIS FIRST:

Since React Native only allows the state to be a normal key-value object, using this State will lead to errors when we call setState on a Component:

One of the sources for assign has an enumerable key on the prototype chain. Are you trying to assign a prototype property? We don't allow it, as this is an edge case that we do not support. This error is a performance optimization and not spec compliant.

The workaround for this issue is to convert the State to a normal KV object with flatten before setting state, and re-convert this.state back with State.fromKeyValue when we want to read data. When I define a Component, I usually do it as such:

import { Component } from 'react';
import { State, StateType } from 'type-safe-state-js';

class App extends Component<Props.Type, StateType<any>> {
  public constructor(props: Props.Type) {
    super(props);
  }

  private operationThatAccessesState(): void {
    let a = State.fromKeyValue(this.state).valueAtNode('a.b.c.d');
    ...
  }

  private operationThatSetsState(state: State.Self<any>): void {
    this.setState(this.convertStateForPlatform(state));
  }

  private convertStateForPlatform(state: State.Self<any>): StateType<any> {
    return this.platform === REACT_NATIVE ? state.flatten() : state;
  }
}

Since StateType is defined as:

type StateType<T> = State.Type<T> | {[key: string]: T};

Or as written in the source code:

type StateType<T> = State.Type<T> | JSObject<T>;

Using StateType as the state type for a component effectively takes care of both normal React.js and React Native. State.fromKeyValue checks whether the object is of class State.Self first before doing anything (and does nothing if it is), so we do not have to worry about unnecessary work.

WHAT IS IT?

Functional, type-safe nested state object that can be used for (but not limited to) Redux architectures. Since it is immutable by default (all update operations must be done via a limited selection of functions), we do not need to worry about shared state management.

To use this State:

import {State} from 'type-safe-state-js';

/// Note that we only expose the state interface for better encapsulation.
let state: State.Type<any> = State.empty<any>();

This State object contains the a key-value object of the current state values, as well as a key-value object of nested substates. To access the value at any node, use:

state.valueAtNode(string);

The parameter of this function should be a String whose components are joined with the specified substateSeparator (which is by default '.'). For example:

state.valueAtNode('a.b.c.d.e');

The above call will access the value at key 'e' of substate 'a.b.c.d'.

In order to update the value at some node, call:

state.updatingValue(string, Never<any>);

The State object will update the value at that node, and if necessary create new substates along the way.

This State is useful for Redux reducers because you will not need to check for existence of property keys before updating value at a node. A reducer can be as such:

function reduce(state: State.Type<any>, action: Action): State.Type<any> {
  return state
    .updatingValue('auth.login.username', action.username)
    .updatingValue('auth.login.password', action.password)
    .updatingValue('auth.login.email', action.email);
}

As a result, we have a robust, functional set of reducers.

Note that althought the source code defines a class called State.Self (which holds all implementations for State.Type), it is not exported in order to prevent unwanted state modifications. As a result, we would use State.Type for all state operations, and even cloneBuilder() (since it extends BuildableType). One limitation of this approach is that it becomes harder to provide a different implementation for State.Type due to the large number of required methods/properties, but I see little use in doing so.