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

choose-it

v1.1.0

Published

Choose a resource in a Jungle 🌴🌴🌴 tree

Downloads

50

Readme

choose-it

Build Status JavaScript Style Guide install size

This module let you focus on the business logic instead of going crazy in a if/else jungle 🌴🐡🌴🌴

It implements a generic tree where each node is a Criteria and you can attach optionally a Resouce. When you need the resource, you will evaluate the tree criterias and if the criteria return true the resource is returned. You can build a series of Criteria with many branches as you want!

An example use case is when you have a set of databases connections that are not configured as cluster, and you have to choose one base on some filters, or simply there are settings you need to use based on some boolean of your company crazy configuration!

In these cases you have to write always the same if/else conditions to pick one of those resources.

choose-it will solve this problem: build the tree, grab the resource πŸŽ‰

graph TD;
  ROOT((Root))-->A((i<0 R:-1));
  ROOT-->B((i==0 R: 0));
  ROOT-->C((i>0 R: 1));
  A-->A1((i<-5 R: -5));
  A-->A2((i>-5 R: -4));
  B-->B1((i<=10 R: -10));
  C-->C1((i>10 R: 10));
  C-->C2((i<10 R: 9));
  C-->C3((i>99 R: 100));
  C1-->C11((i>6 R: 7));

*This tree is built in our tests!

Installation

npm install choose-it

Usage

const ChooseIt = require('choose-it')

const exampleConfig = {
  admin: {
    viewAll: true
  },
  external: {
    viewAll: false,
    login: 'http://external.login.log'
  },
  guest: {
    viewAll: false,
    login: 'http://login.log'
  }
}

const resouceChooser = new ChooseIt()

// Optionally, assign a "resource" to a Criteria
resouceChooser.addCriteria((item) => item.admin === true, exampleConfig.admin)

// You can chain the Criteria to build sub-conditions
resouceChooser.addCriteria((item) => item.guest === true, exampleConfig.guest)
  .addCriteria((item) => item.external === true, exampleConfig.external)

// Assign a node to a variable to use it later
const myNode = resouceChooser.addCriteria((item) => item.power === false, { noPower: true })

// Add a sibling node. You can't call this method on the root node!
myNode.addSiblingCriteria((item) => item.power === true, { gotThePower: true })

// View your tree
resouceChooser.prettyPrint()
// function noop () { return true }
// β”œβ”€β”€ (item) => item.admin === true [object Object]
// β”œβ”€β”¬ (item) => item.guest === true [object Object]
// β”‚ └── (item) => item.external === true [object Object]
// β”œβ”€β”€ (item) => item.power === false [object Object]
// └── (item) => item.power === true [object Object]

// View your tree with a custom output
resouceChooser.prettyPrint((criteria, resource = '') => `${criteria.toString()} = Resource [${resource.viewAll}]`)
// function noop () { return true } = Resource [undefined]
// β”œβ”€β”€ (item) => item.admin === true = Resource [true]
// β”œβ”€β”¬ (item) => item.guest === true = Resource [false]
// β”‚ └── (item) => item.external === true = Resource [false]
// β”œβ”€β”€ (item) => item.power === false = Resource [undefined]
// └── (item) => item.power === true = Resource [undefined]

const user = {
  guest: true,
  external: true
}

const res = resouceChooser.evaluate(user)
console.log(res)
/** It will print out:
 [
    { viewAll: false, login: 'http://login.log' },
    { viewAll: false, login: 'http://external.login.log' }
  ]
*/

const needOnlyOne = resouceChooser.evaluate(user, { maxResults: 1 })
console.log(needOnlyOne)
/** It will print out:
 * [ { viewAll: false, login: 'http://login.log' } ]
 */

API

You have seen all the API in action in the "Usage" paragraph.

We need only to go deeper on evaluate options πŸ‘

| Option | Default | Description | |--------|---------|-------------| | traverseAll | false | If you have a branch like C1-->C2-->C3 if traverseAll is true, even if C2 fail, C3 will be evaluated and could return its resource (if it will be valid criteria of course). traverseAll = false will stop the execution of the branch whenever a falsy criteria is found | | maxResults | 0 | Limit the output length. 0 means "no limit" | | algorithm | BFS | You can choose between two main traverse tree algorithm: Breadth First-Search and Depth First-Search | | order | NLR | This param set the Depth First-Search to be Pre-Order (NLR) or Post-Order (LRN). The traverseAll parameter is ignored by the Post-Order traversal |

Copy-Paste default options:

{
  traverseAll: false,
  maxResults: 0, // 0 = disabled
  algorithm: 'BFS', // [BFS, DFS]
  order: 'NLR' // [NLR, LRN]
}

Roadmap

  • [ ] Emit events onAdd, onFind, onMax, onEnd
  • [ ] Manage Promise in Criteria
  • [ ] Performance assessment

License

Copyright Manuel Spigolon, Licensed under MIT.