This module implements the evaluation process of the projection
language JM2MP: given a resolved and normalized projection
document, it evaluates its root template over a source document.
The evaluation process is asynchronous because the
QueryAdapter's
contract is async.
Two API levels:
-
The evaluate function is part of the low level API. If it is invoked without first passing through validateModule, errors that validation would have detected (like non-existent references or out-of-scope aliases) will manifest as EvaluationError at runtime.
-
Use project as the high level API for the full workflow with: module resolution, projection validation and projection evaluation.
Adapter error handling:
- The
gettemplate command wraps any QueryAdapter exception that is not a ProjectionError in an EvaluationError. This ensures that the language’s error hierarchy is closed: any error catchable in atry/catchblock (and of type ProjectionError) will be one of JM2MP's.
Isolation between evaluations:
- Each call to
evaluate
creates its own
cacheof compiled expressions (independent by$syntax). There is no shared state between concurrent evaluations; two evaluate calls running in parallel do not interfere with each other. This allows for safe use in concurrent applications (such as web servers) without the need for an additional synchronization mechanism.
Source
Namespaces
Members
DEFAULT_MAX_DEPTHinteger
It sets the default value to 1000 for the maximum logical nesting
depth of expressions evaluated by JM2MP.JS.
This serves as a safeguard against infinite recursion and extremely nested logical expressions, whether created inadvertently or maliciously.
It is not affected by the JavaScript stack, since evaluation is
performed asynchronously (async/await).
Type
-
integer
Source
Methods
evaluate(module, document, options) → {Promise.<*>}
Evalúa un módulo resuelto sobre un documento de origen.
Parameters
-
module
objectA projection module previously resolved and normalized.
-
document
*The source document.
-
options
objectMandatory options for evaluation.
Properties
-
registry
AdapterRegistryThe mandatory AdapterRegistry used to evaluate each query language expression.
-
maxDepth
number<optional>
1000See DEFAULT_MAX_DEPTH.
-
registry
Returns
-
Promise.<*>The (promised) resultant document of apply the root template from
module.
Source
compareOrdered(a, b, opName) → {integer}
It compares two ordinal values (sortables): number/number or string/string.
Parameters
-
a
*The left operand.
-
b
*The right operand.
-
opName
*The name of the operation/operator.
Returns
-
integer(-1)|(+1)|(0) when (a<b)|(a>b)|(a===b)
Throws
-
Whenever types of a/b are neither number/number nor string/string.
- Type
-
EvaluationError
Source
deepen(env) → {object}
It returns an execution environment with its depth value increased by one (1).
Parameters
-
env
*The JM2MP's execution environment.
Returns
-
objectSame as
envbut withenv.depthincreased by1.
Source
deepEqual(a, b) → {boolean}
It test the recursive structural equality over JSON values.
The rules are:
- For scalar (primitive) types it applies
equalaccording to===. - Arrays are equal if they have the same length and corresponding elements are equal.
- Objects are equal if they have the same set of keys and corresponding values are equal.
- Different types means that they never are equal (type casting neither implicit nor explicit is implemented in JM2MP 1.0).
Parameters
-
a
*The left operand.
-
b
*The right operand.
Returns
-
boolean(a==b)
Source
evalLiteralObject(obj, env) → {*}
It projects (evaluates) a literal object (JSON value) obj over
the execution environment env and returns its resultant value.
For literal objects: each property's value is projected and each property's name (key) can be escaped (using '$' to start literally by '$').
Parameters
-
obj
*The literal object (JSON value) to project.
-
env
*The execution environment
Returns
-
*It returns the resultant value of project every object's property.
Source
evalOperation(op, env) → {*}
It invokes the specified operation handler from the specified operation's name.
Parameters
-
op
functionThe name of the operation (template command, predicate or operator) to evaluate.
-
env
*The execution environment where the operation will be evaluated.
Returns
-
*The resultant value obtained.
Source
evalProjection(proj, env) → {*}
It evaluates the projection proj over the execution environment
env and returns its resultant value.
It is async to support asynchronous
QueryAdapters.
Parameters
-
proj
*The projection to evaluatue.
-
env
*The execution environment where
projis evaluated.
Returns
-
*The result of the
projoverenv.
Source
expectType(value, expectedType, opName, argName)
Asegura que value sea del tipo esperado o lanza EvaluationError.
Parameters
-
value
*The JSON value to test its type.
-
expectedType
*The expected type's name.
-
opName
*The name of the operation in which the value is evaluated.
-
argName
*The argument's name.
Source
typeName(v) → {string}
It returns the name of the JSON data type associated to v, one of:
- null
- boolean
- number
- string
- array
- object
Parameters
-
v
*The value to be tested.
Returns
-
stringA text string with the corresponding JSON data type (not exactly like JavaScript).