ParenthesisNode.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.createParenthesisNode = void 0;
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  10. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  11. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  12. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  13. var _is = require("../../utils/is.js");
  14. var _factory = require("../../utils/factory.js");
  15. function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
  16. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
  17. var name = 'ParenthesisNode';
  18. var dependencies = ['Node'];
  19. var createParenthesisNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  20. var Node = _ref.Node;
  21. var ParenthesisNode = /*#__PURE__*/function (_Node) {
  22. (0, _inherits2["default"])(ParenthesisNode, _Node);
  23. var _super = _createSuper(ParenthesisNode);
  24. /**
  25. * @constructor ParenthesisNode
  26. * @extends {Node}
  27. * A parenthesis node describes manual parenthesis from the user input
  28. * @param {Node} content
  29. * @extends {Node}
  30. */
  31. function ParenthesisNode(content) {
  32. var _this;
  33. (0, _classCallCheck2["default"])(this, ParenthesisNode);
  34. _this = _super.call(this);
  35. // validate input
  36. if (!(0, _is.isNode)(content)) {
  37. throw new TypeError('Node expected for parameter "content"');
  38. }
  39. _this.content = content;
  40. return _this;
  41. }
  42. (0, _createClass2["default"])(ParenthesisNode, [{
  43. key: "type",
  44. get: function get() {
  45. return name;
  46. }
  47. }, {
  48. key: "isParenthesisNode",
  49. get: function get() {
  50. return true;
  51. }
  52. /**
  53. * Compile a node into a JavaScript function.
  54. * This basically pre-calculates as much as possible and only leaves open
  55. * calculations which depend on a dynamic scope with variables.
  56. * @param {Object} math Math.js namespace with functions and constants.
  57. * @param {Object} argNames An object with argument names as key and `true`
  58. * as value. Used in the SymbolNode to optimize
  59. * for arguments from user assigned functions
  60. * (see FunctionAssignmentNode) or special symbols
  61. * like `end` (see IndexNode).
  62. * @return {function} Returns a function which can be called like:
  63. * evalNode(scope: Object, args: Object, context: *)
  64. */
  65. }, {
  66. key: "_compile",
  67. value: function _compile(math, argNames) {
  68. return this.content._compile(math, argNames);
  69. }
  70. /**
  71. * Get the content of the current Node.
  72. * @return {Node} content
  73. * @override
  74. **/
  75. }, {
  76. key: "getContent",
  77. value: function getContent() {
  78. return this.content.getContent();
  79. }
  80. /**
  81. * Execute a callback for each of the child nodes of this node
  82. * @param {function(child: Node, path: string, parent: Node)} callback
  83. */
  84. }, {
  85. key: "forEach",
  86. value: function forEach(callback) {
  87. callback(this.content, 'content', this);
  88. }
  89. /**
  90. * Create a new ParenthesisNode whose child is the result of calling
  91. * the provided callback function on the child of this node.
  92. * @param {function(child: Node, path: string, parent: Node) : Node} callback
  93. * @returns {ParenthesisNode} Returns a clone of the node
  94. */
  95. }, {
  96. key: "map",
  97. value: function map(callback) {
  98. var content = callback(this.content, 'content', this);
  99. return new ParenthesisNode(content);
  100. }
  101. /**
  102. * Create a clone of this node, a shallow copy
  103. * @return {ParenthesisNode}
  104. */
  105. }, {
  106. key: "clone",
  107. value: function clone() {
  108. return new ParenthesisNode(this.content);
  109. }
  110. /**
  111. * Get string representation
  112. * @param {Object} options
  113. * @return {string} str
  114. * @override
  115. */
  116. }, {
  117. key: "_toString",
  118. value: function _toString(options) {
  119. if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') {
  120. return '(' + this.content.toString(options) + ')';
  121. }
  122. return this.content.toString(options);
  123. }
  124. /**
  125. * Get a JSON representation of the node
  126. * @returns {Object}
  127. */
  128. }, {
  129. key: "toJSON",
  130. value: function toJSON() {
  131. return {
  132. mathjs: name,
  133. content: this.content
  134. };
  135. }
  136. /**
  137. * Instantiate an ParenthesisNode from its JSON representation
  138. * @param {Object} json An object structured like
  139. * `{"mathjs": "ParenthesisNode", "content": ...}`,
  140. * where mathjs is optional
  141. * @returns {ParenthesisNode}
  142. */
  143. }, {
  144. key: "toHTML",
  145. value:
  146. /**
  147. * Get HTML representation
  148. * @param {Object} options
  149. * @return {string} str
  150. * @override
  151. */
  152. function toHTML(options) {
  153. if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') {
  154. return '<span class="math-parenthesis math-round-parenthesis">(</span>' + this.content.toHTML(options) + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  155. }
  156. return this.content.toHTML(options);
  157. }
  158. /**
  159. * Get LaTeX representation
  160. * @param {Object} options
  161. * @return {string} str
  162. * @override
  163. */
  164. }, {
  165. key: "_toTex",
  166. value: function _toTex(options) {
  167. if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') {
  168. return "\\left(".concat(this.content.toTex(options), "\\right)");
  169. }
  170. return this.content.toTex(options);
  171. }
  172. }], [{
  173. key: "fromJSON",
  174. value: function fromJSON(json) {
  175. return new ParenthesisNode(json.content);
  176. }
  177. }]);
  178. return ParenthesisNode;
  179. }(Node);
  180. (0, _defineProperty2["default"])(ParenthesisNode, "name", name);
  181. return ParenthesisNode;
  182. }, {
  183. isClass: true,
  184. isNode: true
  185. });
  186. exports.createParenthesisNode = createParenthesisNode;