npm install @orioro/expression
Framework of expression interpreters.
Main modules: comparison
and object
const person = {
givenName: 'JoĂŁo',
middleName: 'Cruz',
familyName: 'Silva',
age: 32,
interests: ['sport', 'music', 'books'],
mother: {
givenName: 'Maria',
familyName: 'Cruz',
age: 57
},
father: {
givenName: 'Pedro',
familyName: 'Silva',
age: 56
}
}
const context = {
interpreters,
scope: { $$VALUE: person }
}
// Simple equality comparison
evaluate(context, ['$objectMatches', { givenName: 'JoĂŁo' }]) // true
evaluate(context, ['$objectMatches', { givenName: 'Maria' }]) // false
// Use dot (.) path notation to access nested properties
evaluate(context, ['$objectMatches', {
'mother.age': { $gte: 20, $lte: 50 },
'father.age': { $gte: 20, $lte: 50 }
}]) // false
TODO
TODO
// const context = {
// interpreters,
// scope: {
// $$VALUE: {
// name: 'JoĂŁo',
// interests: ['music', 'sports']
// age: 30
// }
// }
// }
// const cases = [
// [['$objectMatches', {
// interests: {
// $arrayIncludes: 'music'
// }
// }]]
// ]
Built-in expressions:
External expression modules:
$arrayIncludes(searchValue, array)
$arrayIncludesAll(searchValues, array)
$arrayIncludesAny(searchValue, array)
$arrayLength(array)
$arrayReduce(reduceExp, start, array)
$arrayMap(mapExp, array)
$arrayEvery(testExp, array)
$arraySome(someExp, array)
$arrayFilter(queryExp, array)
$arrayFindIndex(queryExp, array)
$arrayIndexOf(value, array)
$arrayFind(queryExp, array)
$arrayReverse(array)
$arraySort(sort, array)
$arrayPush(valueExp, array)
$arrayPop(array)
$arrayUnshift(valueExp, array)
$arrayShift(array)
$arraySlice(start, end, array)
$arrayReplace(indexOrRange, values, array)
$arrayAddAt(index, values, array)
$arrayRemoveAt(index, countExp, array)
$arrayJoin(separator, array)
$arrayAt(index, array)
Equivalent of Array.prototype.includes
.
searchValue
{*}array
{Array}- Returns:
includes
{Boolean}
Similar to $arrayIncludes
, but receives an array
of values to be searched for and returns whether the
context array contains all of the searched values.
searchValues
{Array}array
{Array}- Returns:
includesAll
{Boolean}
Similar to $arrayIncludes
, but returns true if
any of the searched values is in the array.
searchValue
{Array}array
{Array}- Returns:
includesAny
{Boolean}
array
{Array}- Returns:
length
{Number}
reduceExp
{Expression}start
{*}array
{Array}
mapExp
{Expression}array
{Array}
Array.prototype.every
Result is similar to logical operator $and
. Main difference
(and reason for existence as isolate expression) is that
$arrayEvery exposes array iteration variables:
$$PARENT_SCOPE
, $$VALUE
, $$INDEX
, $$ARRAY
testExp
{Expression}array
{Array}
Array.prototype.some
someExp
{Expression}array
{Array}
queryExp
{Boolean}array
{Array}
queryExp
{Boolean}array
{Array}
value
{*}array
{Array}
queryExp
{Boolean}array
{Array}
array
{Array}
sort
{String | Expression | [Expression, string]}array
{Array}
valueExp
{*}array
{Array}
array
{Array}
valueExp
{*}array
{Array}
array
{Array}
start
{Number}end
{Number}array
{Array}- Returns: {Array}
indexOrRange
{Number | [Number, Number]}values
{Array}array
{Array}- Returns: {Array}
Adds items at the given position.
index
{Number}values
{* | Array}array
{Array}- Returns:
resultingArray
{Array} The array with items added at position
index
{Number}countExp
{Number}array
{Array}- Returns:
resultingArray
{Array} The array without the removed item
separator
{String}array
{Array}- Returns: {String}
index
{Number}array
{Array}- Returns:
value
{*}
value
{*}- Returns: {Boolean}
$eq(referenceExp, valueExp)
$notEq(referenceExp, valueExp)
$in(arrayExp, valueExp)
$notIn(arrayExp, valueExp)
$gt(referenceExp, valueExp)
$gte(referenceExp, valueExp)
$lt(referenceExp, valueExp)
$lte(referenceExp, valueExp)
$matches(criteriaExp, valueExp)
Checks if the two values
referenceExp
{*}valueExp
{*}- Returns: {Boolean}
referenceExp
{*}valueExp
{*}- Returns: {Boolean}
Checks whether the value is in the given array.
arrayExp
{Array}valueExp
{*}- Returns: {Boolean}
Checks whether the value is not in the given array.
arrayExp
{Array}valueExp
{*}- Returns: {Boolean}
Greater than value > threshold
referenceExp
{Number}valueExp
{Number}- Returns: {Boolean}
Greater than or equal value >= threshold
referenceExp
{Number}valueExp
{Number}- Returns: {Boolean}
Lesser than value < threshold
referenceExp
{Number}valueExp
{Number}- Returns: {Boolean}
Lesser than or equal value <= threshold
referenceExp
{Number}valueExp
{Number}- Returns: {Boolean}
Checks if the value matches the set of criteria.
criteriaExp
{Object}valueExp
{Number}- Returns: {Boolean}
expressions
{Expression[]}- Returns:
pipeResult
{*}
expressionOrValue
{Expression | *}catchExpressionOrValue
{Expression | *}
$and(expressionsExp)
$or(expressionsExp)
$not(expressionsExp)
$nor(expressionsExp)
$xor(expressionA, expressionB)
$if(conditionExp, thenExp, elseExp)
$switch(cases, defaultExp)
$switchKey(cases, defaultExp, ValueExp)
expressionsExp
{Array}- Returns: {Boolean}
expressionsExp
{Array}- Returns: {Boolean}
expressionsExp
{Array}- Returns: {Boolean}
expressionsExp
{Array}- Returns: {Boolean}
expressionA
{Boolean}expressionB
{Boolean}- Returns: {Boolean}
conditionExp
{Boolean}thenExp
{Expression}elseExp
{Expression}- Returns:
result
{*}
cases
{Array}defaultExp
{Expression}- Returns:
result
{*}
cases
{Cases[]}0
{String}1
{*}
defaultExp
{*}ValueExp
{String}- Returns: {*}
$mathSum(sum, base)
$mathSub(subtract, base)
$mathMult(multiplier, base)
$mathDiv(divisor, dividend)
$mathMod(divisor, dividend)
$mathPow(exponent, base)
$mathAbs(value)
$mathMax(otherValue, value)
$mathMin(otherValue, value)
$mathRound(value)
$mathFloor(value)
$mathCeil(value)
sum
{Number}base
{Number}- Returns:
result
{Number}
subtract
{Number}base
{Number}- Returns:
result
{Number}
multiplier
{Number}base
{Number}- Returns:
result
{Number}
divisor
{Number}dividend
{Number}- Returns:
result
{Number}
divisor
{Number}dividend
{Number}- Returns:
result
{Number}
exponent
{Number}base
{Number}- Returns:
result
{Number}
value
{Number}- Returns:
result
{Number}
otherValue
{Number | Number[]}value
{Number}- Returns:
result
{Number}
otherValue
{Number | Number[]}value
{Number}- Returns:
result
{Number}
value
{Number}- Returns:
result
{Number}
value
{Number}- Returns:
result
{Number}
value
{Number}- Returns:
result
{Number}
radix
{Number}value
{*}- Returns: {Number}
value
{*}- Returns: {Number}
$objectMatches(criteriaByPath, value)
$objectFormat(format, source)
$objectDefaults(defaultValuesExp, base)
$objectAssign(values, base)
$objectKeys(object)
criteriaByPath
{Object}value
{Object}- Returns:
matches
{Boolean}
format
{Object | Array}source
{*}- Returns:
object
{Object | Array}
defaultValuesExp
{Object}base
{Object}- Returns: {Object}
values
{Object}base
{Object}- Returns: {Object}
object
{Object}- Returns: {String[]}
$string(value)
$stringStartsWith(query, str)
$stringEndsWith(query, str)
$stringLength(str)
$stringSubstr(start, end, str)
$stringConcat(concat, base)
$stringTrim(str)
$stringPadStart(targetLengthExp, padStringExp, str)
$stringPadEnd(targetLengthExp, padStringExp, str)
$stringToUpperCase(value)
$stringToLowerCase(value)
INTERPOLATION_REGEXP
$stringInterpolate(data, template)
value
{*}- Returns: {String}
query
{String}str
{String}- Returns: {Boolean}
query
{String}str
{string}- Returns: {Boolean}
str
{String}- Returns: {Number}
start
{Number}end
{Number}str
{String}
concat
{String | String[]}base
{String}- Returns: {String}
str
{String}- Returns: {String}
targetLengthExp
{Number}padStringExp
{String}str
{String}- Returns: {String}
targetLengthExp
{Number}padStringExp
{String}str
{String}- Returns: {String}
value
{String}- Returns: {String}
value
{String}- Returns: {String}
RegExp used for matching interpolation expressions. Allows a non-interrupted sequence of alphanumeric chars ([A-Za-z0-9_]), dollar signs ($) and dots (.) wrapped in curly braces ({}) with or without any number of whitespace chars (' ') between braces and the value identifier.
Some resources on RegExp safety concerning RegExp Denial of Service (ReDOS) through Catastrophic backtracking, for future study and reference:
- Catastrophic backtracking
- Regular expression visualizer
- Validator.js
- Stack Overflow interesting question
- Catastrophic backtracking - JavaScript Info
- Google re2 library
- Google re2 for Node.js - re2
data
{Object | Array}template
{String}
valueExp
{*}- Returns:
type
{String} Possible values:- string
- regexp
- number
- bigint
- nan
- null
- undefined
- boolean
- function
- object
- array
- date
- symbol
- map
- set
- weakmap
- weakset
type
{ExpectedType}value
{*}- Returns: {Boolean}
path
{String}defaultExp
{*}- Returns:
value
{*}
value
{*}- Returns: {*}
expression
{Expression}scope
{Object}- Returns: {*}