0.2.2 • Published 6 years ago

switch-tree v0.2.2

Weekly downloads
11
License
MIT
Repository
github
Last release
6 years ago

switch-tree

This package is used to simply evaluate against a value in a switch-like manner with the use of tagged template literals.

Installation

This package uses ES6 tagged template literals, make sure you're using it in a project that has Babel or something similar for transpilation.

npm install --save switch-tree
yarn add switch-tree

Usage

Example

const toCheck = 1;

const _evaluator = input => input === 1;

const execute = () => "Hello";
const execute2 = () => "Bye";

const result = tree`
  all ${toCheck}
    type ${26} ${execute}
    value ${3} ${execute}
    type ${"number"} ${execute2}
    value ${1} ${execute2}
    expression ${_evaluator} ${execute}
`;
/*
 *  In this example:
 *  all = tree type
 *  toCheck = value
 *  type and value = evaluator types
 *  26, 3, "number" and _evaluator = evaluators
 *  execute and execute 2 = executors
*/

In this specific example, result will return an array containing execute and execute2 (each one time).

Terms

  • Tree type - The type of tree. Evaluates all or uses lazy evaluation (quits on the first match)
  • Value - The value against which all evaluators will be matched
  • Evaluator type - The type of evaluation. Either evaluates values, types or expressions
  • Evaluator - The variable or function against which the value will be evaluated
  • Executor - The value or function that will be returned afterwards

Tree types

  • lazy - Returns one value the second a match is found
  • all - Returns all executors that match an evaluator in an array

Evaluator types

  • value - Compares value and evaluator using their respective value
  • type - Compares typeof value and evaluator. Evaluators are typeof return values
  • expression - Returns a boolean after evaluating the value

Roadmap

  • Default case
  • Auto-executed evaluators
  • Nested trees
  • Evaluate indentation (needed for nested trees)
  • Flags
0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago