UNPKG

3.56 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17/**
18 * Converts a CommonMark DOM to a CiceroMark DOM
19 */
20class FormulaVisitor {
21 /**
22 * Visits a sub-tree and return CiceroMark DOM
23 * @param {*} visitor the visitor to use
24 * @param {*} thing the node to visit
25 * @param {*} [parameters] optional parameters
26 * @param {string} field where the children are
27 */
28 static visitChildren(visitor, thing, parameters) {
29 var field = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'nodes';
30 if (thing[field]) {
31 FormulaVisitor.visitNodes(visitor, thing[field], parameters);
32 }
33 }
34
35 /**
36 * Visits a list of nodes and return the CiceroMark DOM
37 * @param {*} visitor the visitor to use
38 * @param {*} things the list node to visit
39 * @param {*} [parameters] optional parameters
40 */
41 static visitNodes(visitor, things, parameters) {
42 things.forEach(node => {
43 node.accept(visitor, parameters);
44 });
45 }
46
47 /**
48 * Visit a node
49 * @param {*} thing the object being visited
50 * @param {*} parameters the parameters
51 */
52 visit(thing, parameters) {
53 switch (thing.getType()) {
54 case 'VariableDefinition':
55 case 'FormattedVariableDefinition':
56 case 'EnumVariableDefinition':
57 {
58 if (parameters.calculateDependencies) {
59 parameters.variables.push(thing.name);
60 }
61 }
62 break;
63 case 'FormulaDefinition':
64 {
65 if (parameters.calculateDependencies) {
66 thing.dependencies = parameters.variables;
67 } else {
68 parameters.result.push({
69 name: thing.name,
70 code: thing.code
71 });
72 }
73 }
74 break;
75 default:
76 FormulaVisitor.visitChildren(this, thing, parameters);
77 }
78 }
79
80 /**
81 * Calculate dependencies
82 * @param {*} serializer - the template mark serializer
83 * @param {object} ast - the template AST
84 * @param {object} options - options
85 * @param {number} [options.utcOffset] - UTC Offset for this execution
86 * @returns {*} the formulas
87 */
88 calculateDependencies(serializer, ast, options) {
89 var parameters = {
90 calculateDependencies: true,
91 variables: [],
92 result: []
93 };
94 var input = serializer.fromJSON(ast, options);
95 input.accept(this, parameters);
96 return serializer.toJSON(input, options);
97 }
98
99 /**
100 * Process formulas and returns the list of those formulas from a TemplateMark DOM
101 * @param {*} serializer - the template mark serializer
102 * @param {object} ast - the template AST
103 * @param {object} options - options
104 * @param {number} [options.utcOffset] - UTC Offset for this execution
105 * @returns {*} the formulas
106 */
107 processFormulas(serializer, ast, options) {
108 var parameters = {
109 calculateDependencies: false,
110 variables: [],
111 result: []
112 };
113 var input = serializer.fromJSON(ast, options);
114 input.accept(this, parameters);
115 return parameters.result;
116 }
117}
118module.exports = FormulaVisitor;
\No newline at end of file