Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

choose-it

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

choose-it - npm Package Compare versions

Comparing version 0.0.2 to 1.0.0

2

index.js

@@ -85,3 +85,3 @@ 'use strict'

node.resource &&
(opts.maxResults === 0 || resourceStack.length < opts.maxResults)) {
(!opts.maxResults || resourceStack.length < opts.maxResults)) {
resourceStack.push(node.resource)

@@ -88,0 +88,0 @@ }

{
"name": "choose-it",
"version": "0.0.2",
"description": "Choose a resource in a 🌴 tree",
"version": "1.0.0",
"description": "Choose a resource in a Jungle 🌴🌴🌴 tree",
"main": "index.js",
"scripts": {
"test": "standard && tap --coverage test/**/*.test.js",
"coveralls": "tap --coverage-report=text-lcov | node ./node_modules/coveralls/bin/coveralls.js",
"coveralls": "tap --coverage-report=text-lcov test/**/*.test.js | node ./node_modules/coveralls/bin/coveralls.js",
"coverage": "npm test && npm run coveralls"

@@ -15,2 +15,5 @@ },

},
"engines": {
"node": ">=6"
},
"keywords": [

@@ -22,3 +25,4 @@ "resource",

"choose",
"pool"
"pool",
"tree"
],

@@ -33,2 +37,3 @@ "author": "Manuel Spigolon <behemoth89@gmail.com> (https://github.com/Eomm)",

"coveralls": "^3.0.3",
"pre-commit": "^1.2.2",
"standard": "^12.0.1",

@@ -35,0 +40,0 @@ "tap": "^12.5.3"

# choose-it
[![Coverage Status](https://coveralls.io/repos/github/Eomm/choose-it/badge.svg?branch=master)](https://coveralls.io/github/Eomm/choose-it?branch=master)
[![Build Status](https://travis-ci.com/Eomm/choose-it.svg?branch=master)](https://travis-ci.com/Eomm/choose-it)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![install size](https://packagephobia.now.sh/badge?p=choose-it)](https://packagephobia.now.sh/result?p=choose-it)
This module let you focus on the business logic instead of going crazy in a `if/else` jungle 🐵🌴
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,
or simply some settings you need to use based on some logic of your company.
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 this case you have to write always the same `if` conditions to pick one of those resources.
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!
`choose-it` will solve this problem: build the tree, grab the resource 🎉
![Generic Tree](./docs/tree.png)
*This tree is built in our tests!
## Installation
```
npm install choose-it
```
## Usage
```js
const ChooseIt = require('choose-it')
const exampleConfig = {

@@ -31,6 +53,5 @@ admin: {

const ChooseIt = require('choose-it')
const resouceChooser = new ChooseIt()
// Optionally, assign a "resource" to a Criteria
resouceChooser.addCriteria((item) => item.admin === true, exampleConfig.admin)

@@ -40,4 +61,28 @@

resouceChooser.addCriteria((item) => item.guest === true, exampleConfig.guest)
.addCriteria((item) => item.external === true, exampleConfig.external)
.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 = {

@@ -50,14 +95,33 @@ guest: true,

console.log(res)
/** It will print out
[
{ viewAll: false, login: 'http://login.log' },
{ viewAll: false, login: 'http://external.login.log' }
]
/** 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
Default options:
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:
```js

@@ -68,14 +132,18 @@ {

algorithm: 'BFS', // [BFS, DFS]
order: 'NLR' // when DFS: [NLR, LRN]
order: 'NLR' // [NLR, LRN]
}
```
## Roadmap
[] Docs
⬜ Emit events `onAdd`, `onFind`, `onMax`, `onEnd`
[] Emit events `onAdd`, `onFind`, `onMax`, `onEnd`
⬜ Manage Promise in Criteria
[] Manage Primise Criteria
⬜ Performance
[] Performance
## License
Copyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc