ConstantNode.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.createConstantNode = 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 _string = require("../../utils/string.js");
  14. var _is = require("../../utils/is.js");
  15. var _latex = require("../../utils/latex.js");
  16. var _factory = require("../../utils/factory.js");
  17. 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); }; }
  18. 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; } }
  19. var name = 'ConstantNode';
  20. var dependencies = ['Node'];
  21. var createConstantNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  22. var Node = _ref.Node;
  23. var ConstantNode = /*#__PURE__*/function (_Node) {
  24. (0, _inherits2["default"])(ConstantNode, _Node);
  25. var _super = _createSuper(ConstantNode);
  26. /**
  27. * A ConstantNode holds a constant value like a number or string.
  28. *
  29. * Usage:
  30. *
  31. * new ConstantNode(2.3)
  32. * new ConstantNode('hello')
  33. *
  34. * @param {*} value Value can be any type (number, BigNumber, string, ...)
  35. * @constructor ConstantNode
  36. * @extends {Node}
  37. */
  38. function ConstantNode(value) {
  39. var _this;
  40. (0, _classCallCheck2["default"])(this, ConstantNode);
  41. _this = _super.call(this);
  42. _this.value = value;
  43. return _this;
  44. }
  45. (0, _createClass2["default"])(ConstantNode, [{
  46. key: "type",
  47. get: function get() {
  48. return name;
  49. }
  50. }, {
  51. key: "isConstantNode",
  52. get: function get() {
  53. return true;
  54. }
  55. /**
  56. * Compile a node into a JavaScript function.
  57. * This basically pre-calculates as much as possible and only leaves open
  58. * calculations which depend on a dynamic scope with variables.
  59. * @param {Object} math Math.js namespace with functions and constants.
  60. * @param {Object} argNames An object with argument names as key and `true`
  61. * as value. Used in the SymbolNode to optimize
  62. * for arguments from user assigned functions
  63. * (see FunctionAssignmentNode) or special symbols
  64. * like `end` (see IndexNode).
  65. * @return {function} Returns a function which can be called like:
  66. * evalNode(scope: Object, args: Object, context: *)
  67. */
  68. }, {
  69. key: "_compile",
  70. value: function _compile(math, argNames) {
  71. var value = this.value;
  72. return function evalConstantNode() {
  73. return value;
  74. };
  75. }
  76. /**
  77. * Execute a callback for each of the child nodes of this node
  78. * @param {function(child: Node, path: string, parent: Node)} callback
  79. */
  80. }, {
  81. key: "forEach",
  82. value: function forEach(callback) {
  83. // nothing to do, we don't have any children
  84. }
  85. /**
  86. * Create a new ConstantNode with children produced by the given callback.
  87. * Trivial because there are no children.
  88. * @param {function(child: Node, path: string, parent: Node) : Node} callback
  89. * @returns {ConstantNode} Returns a clone of the node
  90. */
  91. }, {
  92. key: "map",
  93. value: function map(callback) {
  94. return this.clone();
  95. }
  96. /**
  97. * Create a clone of this node, a shallow copy
  98. * @return {ConstantNode}
  99. */
  100. }, {
  101. key: "clone",
  102. value: function clone() {
  103. return new ConstantNode(this.value);
  104. }
  105. /**
  106. * Get string representation
  107. * @param {Object} options
  108. * @return {string} str
  109. */
  110. }, {
  111. key: "_toString",
  112. value: function _toString(options) {
  113. return (0, _string.format)(this.value, options);
  114. }
  115. /**
  116. * Get HTML representation
  117. * @param {Object} options
  118. * @return {string} str
  119. */
  120. }, {
  121. key: "toHTML",
  122. value: function toHTML(options) {
  123. var value = this._toString(options);
  124. switch ((0, _is.typeOf)(this.value)) {
  125. case 'number':
  126. case 'BigNumber':
  127. case 'Fraction':
  128. return '<span class="math-number">' + value + '</span>';
  129. case 'string':
  130. return '<span class="math-string">' + value + '</span>';
  131. case 'boolean':
  132. return '<span class="math-boolean">' + value + '</span>';
  133. case 'null':
  134. return '<span class="math-null-symbol">' + value + '</span>';
  135. case 'undefined':
  136. return '<span class="math-undefined">' + value + '</span>';
  137. default:
  138. return '<span class="math-symbol">' + value + '</span>';
  139. }
  140. }
  141. /**
  142. * Get a JSON representation of the node
  143. * @returns {Object}
  144. */
  145. }, {
  146. key: "toJSON",
  147. value: function toJSON() {
  148. return {
  149. mathjs: name,
  150. value: this.value
  151. };
  152. }
  153. /**
  154. * Instantiate a ConstantNode from its JSON representation
  155. * @param {Object} json An object structured like
  156. * `{"mathjs": "SymbolNode", value: 2.3}`,
  157. * where mathjs is optional
  158. * @returns {ConstantNode}
  159. */
  160. }, {
  161. key: "_toTex",
  162. value:
  163. /**
  164. * Get LaTeX representation
  165. * @param {Object} options
  166. * @return {string} str
  167. */
  168. function _toTex(options) {
  169. var value = this._toString(options);
  170. switch ((0, _is.typeOf)(this.value)) {
  171. case 'string':
  172. return '\\mathtt{' + (0, _latex.escapeLatex)(value) + '}';
  173. case 'number':
  174. case 'BigNumber':
  175. {
  176. if (!isFinite(this.value)) {
  177. return this.value.valueOf() < 0 ? '-\\infty' : '\\infty';
  178. }
  179. var index = value.toLowerCase().indexOf('e');
  180. if (index !== -1) {
  181. return value.substring(0, index) + '\\cdot10^{' + value.substring(index + 1) + '}';
  182. }
  183. }
  184. return value;
  185. case 'Fraction':
  186. return this.value.toLatex();
  187. default:
  188. return value;
  189. }
  190. }
  191. }], [{
  192. key: "fromJSON",
  193. value: function fromJSON(json) {
  194. return new ConstantNode(json.value);
  195. }
  196. }]);
  197. return ConstantNode;
  198. }(Node);
  199. (0, _defineProperty2["default"])(ConstantNode, "name", name);
  200. return ConstantNode;
  201. }, {
  202. isClass: true,
  203. isNode: true
  204. });
  205. exports.createConstantNode = createConstantNode;