ParenthesisNode.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  2. import { isNode } from '../../utils/is.js';
  3. import { factory } from '../../utils/factory.js';
  4. var name = 'ParenthesisNode';
  5. var dependencies = ['Node'];
  6. export var createParenthesisNode = /* #__PURE__ */factory(name, dependencies, _ref => {
  7. var {
  8. Node
  9. } = _ref;
  10. class ParenthesisNode extends Node {
  11. /**
  12. * @constructor ParenthesisNode
  13. * @extends {Node}
  14. * A parenthesis node describes manual parenthesis from the user input
  15. * @param {Node} content
  16. * @extends {Node}
  17. */
  18. constructor(content) {
  19. super();
  20. // validate input
  21. if (!isNode(content)) {
  22. throw new TypeError('Node expected for parameter "content"');
  23. }
  24. this.content = content;
  25. }
  26. get type() {
  27. return name;
  28. }
  29. get isParenthesisNode() {
  30. return true;
  31. }
  32. /**
  33. * Compile a node into a JavaScript function.
  34. * This basically pre-calculates as much as possible and only leaves open
  35. * calculations which depend on a dynamic scope with variables.
  36. * @param {Object} math Math.js namespace with functions and constants.
  37. * @param {Object} argNames An object with argument names as key and `true`
  38. * as value. Used in the SymbolNode to optimize
  39. * for arguments from user assigned functions
  40. * (see FunctionAssignmentNode) or special symbols
  41. * like `end` (see IndexNode).
  42. * @return {function} Returns a function which can be called like:
  43. * evalNode(scope: Object, args: Object, context: *)
  44. */
  45. _compile(math, argNames) {
  46. return this.content._compile(math, argNames);
  47. }
  48. /**
  49. * Get the content of the current Node.
  50. * @return {Node} content
  51. * @override
  52. **/
  53. getContent() {
  54. return this.content.getContent();
  55. }
  56. /**
  57. * Execute a callback for each of the child nodes of this node
  58. * @param {function(child: Node, path: string, parent: Node)} callback
  59. */
  60. forEach(callback) {
  61. callback(this.content, 'content', this);
  62. }
  63. /**
  64. * Create a new ParenthesisNode whose child is the result of calling
  65. * the provided callback function on the child of this node.
  66. * @param {function(child: Node, path: string, parent: Node) : Node} callback
  67. * @returns {ParenthesisNode} Returns a clone of the node
  68. */
  69. map(callback) {
  70. var content = callback(this.content, 'content', this);
  71. return new ParenthesisNode(content);
  72. }
  73. /**
  74. * Create a clone of this node, a shallow copy
  75. * @return {ParenthesisNode}
  76. */
  77. clone() {
  78. return new ParenthesisNode(this.content);
  79. }
  80. /**
  81. * Get string representation
  82. * @param {Object} options
  83. * @return {string} str
  84. * @override
  85. */
  86. _toString(options) {
  87. if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') {
  88. return '(' + this.content.toString(options) + ')';
  89. }
  90. return this.content.toString(options);
  91. }
  92. /**
  93. * Get a JSON representation of the node
  94. * @returns {Object}
  95. */
  96. toJSON() {
  97. return {
  98. mathjs: name,
  99. content: this.content
  100. };
  101. }
  102. /**
  103. * Instantiate an ParenthesisNode from its JSON representation
  104. * @param {Object} json An object structured like
  105. * `{"mathjs": "ParenthesisNode", "content": ...}`,
  106. * where mathjs is optional
  107. * @returns {ParenthesisNode}
  108. */
  109. static fromJSON(json) {
  110. return new ParenthesisNode(json.content);
  111. }
  112. /**
  113. * Get HTML representation
  114. * @param {Object} options
  115. * @return {string} str
  116. * @override
  117. */
  118. toHTML(options) {
  119. if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') {
  120. return '<span class="math-parenthesis math-round-parenthesis">(</span>' + this.content.toHTML(options) + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  121. }
  122. return this.content.toHTML(options);
  123. }
  124. /**
  125. * Get LaTeX representation
  126. * @param {Object} options
  127. * @return {string} str
  128. * @override
  129. */
  130. _toTex(options) {
  131. if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') {
  132. return "\\left(".concat(this.content.toTex(options), "\\right)");
  133. }
  134. return this.content.toTex(options);
  135. }
  136. }
  137. _defineProperty(ParenthesisNode, "name", name);
  138. return ParenthesisNode;
  139. }, {
  140. isClass: true,
  141. isNode: true
  142. });