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

@brendonjohn/status-finder

v0.3.3

Published

Business logic tends to have many `if` statements and embedded `if` statements to decide what the current status is.

Downloads

5,297

Readme

@brendonjohn/status-finder

Business logic tends to have many if statements and embedded if statements to decide what the current status is.

This tool is an alternative for writing and reviewing business logic that needs to decide what status we are currently in.

I thought I was making a finite state machine… but that’s really something else. This is instead a little tool to simplify how a status of many paths can be derived from state.

Install

npm install @brendonjohn/status-finder

Goals

  • Represent possible states as data instead
  • Make domain knowledge easier to review, test and typecheck
  • Encourage all related state to be found before deriving what the current status is
  • Encourage a clear distinction between retrieving data and deriving information from the data

Concepts

Status Tree

This is a data structure that represents the a hierarchy of logic to make sense of the state.

The Status Tree is made up of Status Nodes and subStates. Each node contains a test function to decide if this is the current status.

Each node in the StatusTree can have an array of sub states that also need to be checked.

Status Node

One of the possible outcomes from evaluating a Status Tree

Status

The value of the status property on a Status Node. This value will be returned by the statusFinder

Test

The value of the test property on a Status Node.

This value is a function that will be proided the application state as an argument and returns a boolean.

This function has to decide whether the current Status Node is a match or not.

Sub Status Nodes

Each Status Node can specify Sub Status Nodes in the subStates property.

In the same way we like to write nested if statements, subStates exist to reduce code duplication.

Each status node has the option to specify subStates. Each subState implements the same shape as the Status Node.

State

A value that each Status Node will be tested against. The state should be designed by you and can be as simple or complex as your application requires.

Status Finder

  1. A recursive method that determines which status in the StatusTree matches the provided state. We start at the first position of the array (arrays start at zero) and evaluate one node at a time.

  2. When a status match is found, we store the value of the current match and check if there are sub states we need to test against.

  3. We repeat point 2 on the sub states until we have run out of sub states to check.

  4. When we have run out of sub states to check, we return the value of the latest match

  5. When we have found a node that matches, the value of the status property of that node will be returned.

Example

I've put together an example that shows sub states being used. See src/example.ts

Author

Brendon Muschamp

License

Copyright 2019 @brendonjohn

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.