RelationalNode.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  2. import { getPrecedence } from '../operators.js';
  3. import { escape } from '../../utils/string.js';
  4. import { getSafeProperty } from '../../utils/customs.js';
  5. import { latexOperators } from '../../utils/latex.js';
  6. import { factory } from '../../utils/factory.js';
  7. var name = 'RelationalNode';
  8. var dependencies = ['Node'];
  9. export var createRelationalNode = /* #__PURE__ */factory(name, dependencies, _ref => {
  10. var {
  11. Node
  12. } = _ref;
  13. var operatorMap = {
  14. equal: '==',
  15. unequal: '!=',
  16. smaller: '<',
  17. larger: '>',
  18. smallerEq: '<=',
  19. largerEq: '>='
  20. };
  21. class RelationalNode extends Node {
  22. /**
  23. * A node representing a chained conditional expression, such as 'x > y > z'
  24. *
  25. * @param {String[]} conditionals
  26. * An array of conditional operators used to compare the parameters
  27. * @param {Node[]} params
  28. * The parameters that will be compared
  29. *
  30. * @constructor RelationalNode
  31. * @extends {Node}
  32. */
  33. constructor(conditionals, params) {
  34. super();
  35. if (!Array.isArray(conditionals)) {
  36. throw new TypeError('Parameter conditionals must be an array');
  37. }
  38. if (!Array.isArray(params)) {
  39. throw new TypeError('Parameter params must be an array');
  40. }
  41. if (conditionals.length !== params.length - 1) {
  42. throw new TypeError('Parameter params must contain exactly one more element ' + 'than parameter conditionals');
  43. }
  44. this.conditionals = conditionals;
  45. this.params = params;
  46. }
  47. get type() {
  48. return name;
  49. }
  50. get isRelationalNode() {
  51. return true;
  52. }
  53. /**
  54. * Compile a node into a JavaScript function.
  55. * This basically pre-calculates as much as possible and only leaves open
  56. * calculations which depend on a dynamic scope with variables.
  57. * @param {Object} math Math.js namespace with functions and constants.
  58. * @param {Object} argNames An object with argument names as key and `true`
  59. * as value. Used in the SymbolNode to optimize
  60. * for arguments from user assigned functions
  61. * (see FunctionAssignmentNode) or special symbols
  62. * like `end` (see IndexNode).
  63. * @return {function} Returns a function which can be called like:
  64. * evalNode(scope: Object, args: Object, context: *)
  65. */
  66. _compile(math, argNames) {
  67. var self = this;
  68. var compiled = this.params.map(p => p._compile(math, argNames));
  69. return function evalRelationalNode(scope, args, context) {
  70. var evalLhs;
  71. var evalRhs = compiled[0](scope, args, context);
  72. for (var i = 0; i < self.conditionals.length; i++) {
  73. evalLhs = evalRhs;
  74. evalRhs = compiled[i + 1](scope, args, context);
  75. var condFn = getSafeProperty(math, self.conditionals[i]);
  76. if (!condFn(evalLhs, evalRhs)) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. };
  82. }
  83. /**
  84. * Execute a callback for each of the child nodes of this node
  85. * @param {function(child: Node, path: string, parent: Node)} callback
  86. */
  87. forEach(callback) {
  88. this.params.forEach((n, i) => callback(n, 'params[' + i + ']', this), this);
  89. }
  90. /**
  91. * Create a new RelationalNode whose children are the results of calling
  92. * the provided callback function for each child of the original node.
  93. * @param {function(child: Node, path: string, parent: Node): Node} callback
  94. * @returns {RelationalNode} Returns a transformed copy of the node
  95. */
  96. map(callback) {
  97. return new RelationalNode(this.conditionals.slice(), this.params.map((n, i) => this._ifNode(callback(n, 'params[' + i + ']', this)), this));
  98. }
  99. /**
  100. * Create a clone of this node, a shallow copy
  101. * @return {RelationalNode}
  102. */
  103. clone() {
  104. return new RelationalNode(this.conditionals, this.params);
  105. }
  106. /**
  107. * Get string representation.
  108. * @param {Object} options
  109. * @return {string} str
  110. */
  111. _toString(options) {
  112. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  113. var precedence = getPrecedence(this, parenthesis, options && options.implicit);
  114. var paramStrings = this.params.map(function (p, index) {
  115. var paramPrecedence = getPrecedence(p, parenthesis, options && options.implicit);
  116. return parenthesis === 'all' || paramPrecedence !== null && paramPrecedence <= precedence ? '(' + p.toString(options) + ')' : p.toString(options);
  117. });
  118. var ret = paramStrings[0];
  119. for (var i = 0; i < this.conditionals.length; i++) {
  120. ret += ' ' + operatorMap[this.conditionals[i]];
  121. ret += ' ' + paramStrings[i + 1];
  122. }
  123. return ret;
  124. }
  125. /**
  126. * Get a JSON representation of the node
  127. * @returns {Object}
  128. */
  129. toJSON() {
  130. return {
  131. mathjs: name,
  132. conditionals: this.conditionals,
  133. params: this.params
  134. };
  135. }
  136. /**
  137. * Instantiate a RelationalNode from its JSON representation
  138. * @param {Object} json
  139. * An object structured like
  140. * `{"mathjs": "RelationalNode", "conditionals": ..., "params": ...}`,
  141. * where mathjs is optional
  142. * @returns {RelationalNode}
  143. */
  144. static fromJSON(json) {
  145. return new RelationalNode(json.conditionals, json.params);
  146. }
  147. /**
  148. * Get HTML representation
  149. * @param {Object} options
  150. * @return {string} str
  151. */
  152. toHTML(options) {
  153. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  154. var precedence = getPrecedence(this, parenthesis, options && options.implicit);
  155. var paramStrings = this.params.map(function (p, index) {
  156. var paramPrecedence = getPrecedence(p, parenthesis, options && options.implicit);
  157. return parenthesis === 'all' || paramPrecedence !== null && paramPrecedence <= precedence ? '<span class="math-parenthesis math-round-parenthesis">(</span>' + p.toHTML(options) + '<span class="math-parenthesis math-round-parenthesis">)</span>' : p.toHTML(options);
  158. });
  159. var ret = paramStrings[0];
  160. for (var i = 0; i < this.conditionals.length; i++) {
  161. ret += '<span class="math-operator math-binary-operator ' + 'math-explicit-binary-operator">' + escape(operatorMap[this.conditionals[i]]) + '</span>' + paramStrings[i + 1];
  162. }
  163. return ret;
  164. }
  165. /**
  166. * Get LaTeX representation
  167. * @param {Object} options
  168. * @return {string} str
  169. */
  170. _toTex(options) {
  171. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  172. var precedence = getPrecedence(this, parenthesis, options && options.implicit);
  173. var paramStrings = this.params.map(function (p, index) {
  174. var paramPrecedence = getPrecedence(p, parenthesis, options && options.implicit);
  175. return parenthesis === 'all' || paramPrecedence !== null && paramPrecedence <= precedence ? '\\left(' + p.toTex(options) + '\right)' : p.toTex(options);
  176. });
  177. var ret = paramStrings[0];
  178. for (var i = 0; i < this.conditionals.length; i++) {
  179. ret += latexOperators[this.conditionals[i]] + paramStrings[i + 1];
  180. }
  181. return ret;
  182. }
  183. }
  184. _defineProperty(RelationalNode, "name", name);
  185. return RelationalNode;
  186. }, {
  187. isClass: true,
  188. isNode: true
  189. });