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

ts-truth-table

v0.1.1

Published

Many times, we need to make a decision, retrieve a value, call a function based on a combination of values/options/arguments.

Downloads

9

Readme

Truth Table

Many times, we need to make a decision, retrieve a value, call a function based on a combination of values/options/arguments.

Imagine your boss gives you an excel table with 5 columns and 40 rows of combinations of ui states(checkboxes, dropdowns...). In human language, if this is checked and this is selected than do this :).

What are our options in this case:

  1. writing a bunch of if else's
  2. using a switch statement
  3. rewriting the excel into a tree and parsing it

The most common solution seen is the if table, sometimes you find a few switch statements and the last time I had to deal with this topic, I rewrote the table into a tree.

All of these solutions work but they have 2 big problems. They are impossible to read and maintain.

After showing my tree solution to my colleague..he actually came up with a great approach and I decided to write a small utility to help with this task.

We can call it a Truth table a decision tree, it doesn't really match the mathematical definition but matches google search queries :)

yarn add ts-truth-table

Example

Imagine we have 2 checkboxes, 1 dropdown and an input field on our website. The value of the input field will change based on the checkboxes and dropdown values.

| Checkbox 1 | Checkbox 2 | Dropdown value | Input value | | ----------------- | -------------------- | --------------- | ------------ | | true | false | 1 | value 1 | | true | true | 1 | value 2 | | false | true | 1 | value 3 | | false | false | 1 | value 4 | | true | false | 2 | value 5 | | true | true | 2 | value 6 | | false | true | 2 | value 7 | | false | false | 2 | value 8 |

We can immediately see how much work this will be and how hrd it will be to work with and the dropwdown can hold a list of countries and we have immediately more combinations.

Looking at the table, and thinking about the best structure we could put the data in, the evident solutions is to use a multi dimensional array. Why? Because it copies the best the table and if the business decides to change any value, it will be easy to find and update.

const table = [
  [true,  false, 1, 'value 1']
  [true,  true,  1, 'value 2']
  [false, true,  1, 'value 3']
  [false, false, 1, 'value 4']
  [true,  false, 2, 'value 5']
  [true,  true,  2, 'value 6']
  [false, true,  2, 'value 7']
  [false, false, 2, 'value 8']
];

Now we can utilize our little helper class :)

import TruthTable from '../TruthTable';

const truthTable = new TruthTable(table);

We can do 4 operations on the truth table:

  1. checking if we have an exact match - useful if we need to do a certain task if a match is found. [getExactMatch]
  2. finding a match - in this case we want to find a match based on all or some of the arguments. [getMatch]
  3. finding all matches - in this case we want an array of all items with match 1, 2... or all arguments. [getAllMatches]
  4. finding the last leaf of a match - this is usefull for the example above. We want to get the input value based(last leaf) based on the first 3 arguments. [getLastLeafOfMatch]
truthTable.getLastLeafOfMatch([false, true, 2]); // value 7

So what happens when we only have the first 2 arguments:

truthTable.getLastLeafOfMatch([false, true]); // value 3

We received the first match's last leaf. This may be what we want if the order matters or we could:

truthTable.getAllMatches([false, true]); // [value 3, value 7]

Advanced tip

The last item of an option can be a function as well. So for instance if we need to call different functions for each match, the getLastLeafOfMatch will return the last item in a match and if it is a function we can just call it :)

Interface

type ConfigType = any[][];

interface TruthTableInterface {
  config: ConfigType;

  getExactMatch(match: any[]): any[] | undefined;

  getMatch(match: any[]): any[] | undefined;

  getAllMatches(match: any[]): any[];

  getLastLeafOfMatch(match: any[]): any;
}

Typescript

The library is written in typescript. Unfortunately flow is really slow on windows and webstorm has also some issues with it :(. The code is compiled down to es5 and typings are exported.