uservars v1.6.3
UserVars
UserVars is a Typescript library made to let end users create variables and use them wherever they're supported by the application. It includes dependency chains, decision trees, and mathematical expressions. All input and storage is valid JSON, removing the need for custom serialization logic.
Installation
$ npm install uservarsUsage
Examples can be found in this file.
- Examples for literal variables start from the top.
- Examples for references start with
basicLiteral. - Examples for scopes start with
basicScoped.
To begin using the library, you must first import it, and then create a new UserVars object to interact with it.
import { UserVars } from "uservars";
const userVars = new UserVars();After that, you can start adding variables. This is done with the UserVars.setVar function. It requires input conforming to one of the four variable types shown here. In many cases (anywhere Reference is listed as a possible type), string literals can be replaced by References to other variables. This library also has scopes. These let you separate variables, so you can reuse names and do other scope magic. To go back up to global from a scoped variable, add ../ to the start of the path.
Variable Types
All variable types included in this library have four common fields:
nameThe last part of the path used to reference the variable. Must match the Regex pattern/^[A‑Z\d_]+$/iscopeThe top level name of the variable. If "global", it can be omitted from the path anywhere the variable is referred to. Must match the Regex pattern/^[A‑Z\d_]+$/ivalueThe value stored in the variable. Depending on which type of variable it is, this will have a different required structure.varTypeThis is used internally to tell what kind of variable is being evaluated. If it doesn't match the actual structure aTypeErrorwill be thrown.
Basic
valueEither the literal value of the variable, or a Reference object pointing to another variable relative toscope.
{
"name": string,
"scope": string,
"value": string | Reference,
"varType": "basic"
}List
valueAn array of either strings or References. The variable evaluates to the whole list, with references being resolved to their end values. This is flattened at the end of resolution to allow combining lists.
{
"name": string,
"scope": string,
"value": Array<string | Reference>,
"varType": "list"
}Table
Table variables were once called parameterized variables, which might help you understand what they do. When they are evaluated, the items in value are each evaluated in order from first to last (if priority is "first") or last to first (if priority is "last"). Whichever TableRow outputs its value first is output for the full table. If none are output, default is output instead.
valueAn array of TableRows. The output value of either the first or last row where all conditions pass is output for the whole variable, ordefaultif none pass.defaultThe default value to output if no rows are output, or a Reference to it.priority"first"to output the first passing row, or"last"to output the last one.
{
"name": string,
"scope": string,
"value": Array<TableRow>,
"varType": "table"
"default": string | Reference,
"priority": "first" | "last",
}Expression
Expressions execute value using functions from functions and variables from vars. https://github.com/silentmatt/expr-eval is used to safely perform the math.
functionsOPTIONAL Each item of this array is prepended tovaluein order, so the functions can be used in the expression. Referenced lists of functions are flattened. These should be formatted likename(x) = x + 2.valueEither a string, which will be used literally, or a Reference which will be resolved first. This is what is actually executed.varsA mapping of variable names used invalueto either strings or References. The resolved values of these are used to replace variables with the same names invalue.
{
"name": string,
"scope": string,
"value": string | Reference,
"vars": {[name: string]: string | Reference},
"functions"?: Array<string | Reference>
}Auxiliary Types
Reference
References are used wherever you want to refer to another variable from within a variable. They can be used pretty much anywhere a string is accepted in value and related fields.
valueThe path to the referenced variable, relative to the scope of the variable it's a part of.
{
"value": string
}TableRow
TableRows are used exclusively in the value field of Tables, which is an array of them. They are evaluated with the table, and are only output if all of their conditions pass.
conditionsAn array of conditions that must pass in order for the row to output its value.outputThe value or reference to the variable containing the value that should be output if all conditions pass.
{
"conditions": Array<Condition>,
"output": string | Reference
}Condition
Conditions are used to decide which TableRow's value gets output from a Table.
val1The first operand to be compared withval2.comparisonThe comparison type to be made. Depending on what this is set to,val1andval2may be altered to fit the comparison.eqNo conversions, uses strict equality (===), with shallow comparisons for Lists.lt/gtConverts strings to floats and Lists to numbers representing their length.inChecks ifvar1is contained withinvar2.var2must be a List, and ifvar1is a list it checks for full intersection ofvar1intovar2.
val2The second operand to be compared withval1.
{
"val1": string | Reference,
"comparison": "eq" | "lt" | "gt" | "in",
"val2": string | Reference
}TableData
Full data of a Table, including all Conditions and outputs, and their paths.
outputThe final output value.outputPathThe pathoutputcame from, or an empty string if none.outIndexThe row ofvaluethat output the final value, or -1 for default.valueAn array of TableRowData.defaultThe default value that would be output if nothing else was.defaultPathThe pathdefaultcame from, or an empty string if none.priorityEither"first"or"last", just for convenience.
{
"output": Literal,
"outputPath": string,
"outIndex": number,
"value": Array<TableRowData>,
"default": Literal,
"defaultPath": string,
"priority": string
}TableRowData
Full data of a TableRow, including all conditions and outputs, and their paths.
conditionsArray of ConditionData that was evaluated to see if this row should be output.outputThe resolved value of the output field of the matching TableRow.outputPathThe pathoutputcame from, or an empty string if none.
{
"conditions": Array<ConditionData>,
"output": Literal,
"outputPath": string
}ConditionData
Full data of a Condition, including paths.
val1The resolved value ofval1from the matching Condition.val1PathThe pathval1came from, or an empty string if none.comparisonEither"eq","lt","gt", or"in".val2The resolved value ofval2from the matching Condition.val2PathThe pathval2came from, or an empty string if none.
{
"val1": Literal,
"val1Path": string,
"comparison": Comparison,
"val2": Literal,
"val2Path": string
}Docs
UserVars.setVar(value: Var, forceOverwrite: boolean = false)
Sets a variable according to value.
Arguments
valueA Var containing all the data needed to set the variable.forceOverwriteOPTIONALtrueif variable can overwrite a scope with the same name and vice versa, orfalseif not
UserVars.setVarBulk(...values: Array)
Wrapper for calling UserVars.setVar multiple times with each item of values.
Arguments
valuesAn array of Vars to be passed toUserVars.setVar.
UserVars.getVar(path: string, full?: boolean)
Evaluates a variable (or hits the cache), and returns the output.
Arguments
pathThe absolute path to the variable you want to get.fullOPTIONAL If the variable atpathis a Table, this controls whether just the output is returned (false) or the full TableData (true).
UserVars.getRawVar(path: string)
Returns the raw JSON data behind the variable at path.
Arguments
pathThe path to get the variable data from.