ConstantNode.js 5.0 KB

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