RangeNode.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  2. import { isNode, isSymbolNode } from '../../utils/is.js';
  3. import { factory } from '../../utils/factory.js';
  4. import { getPrecedence } from '../operators.js';
  5. var name = 'RangeNode';
  6. var dependencies = ['Node'];
  7. export var createRangeNode = /* #__PURE__ */factory(name, dependencies, _ref => {
  8. var {
  9. Node
  10. } = _ref;
  11. /**
  12. * Calculate the necessary parentheses
  13. * @param {Node} node
  14. * @param {string} parenthesis
  15. * @param {string} implicit
  16. * @return {Object} parentheses
  17. * @private
  18. */
  19. function calculateNecessaryParentheses(node, parenthesis, implicit) {
  20. var precedence = getPrecedence(node, parenthesis, implicit);
  21. var parens = {};
  22. var startPrecedence = getPrecedence(node.start, parenthesis, implicit);
  23. parens.start = startPrecedence !== null && startPrecedence <= precedence || parenthesis === 'all';
  24. if (node.step) {
  25. var stepPrecedence = getPrecedence(node.step, parenthesis, implicit);
  26. parens.step = stepPrecedence !== null && stepPrecedence <= precedence || parenthesis === 'all';
  27. }
  28. var endPrecedence = getPrecedence(node.end, parenthesis, implicit);
  29. parens.end = endPrecedence !== null && endPrecedence <= precedence || parenthesis === 'all';
  30. return parens;
  31. }
  32. class RangeNode extends Node {
  33. /**
  34. * @constructor RangeNode
  35. * @extends {Node}
  36. * create a range
  37. * @param {Node} start included lower-bound
  38. * @param {Node} end included upper-bound
  39. * @param {Node} [step] optional step
  40. */
  41. constructor(start, end, step) {
  42. super();
  43. // validate inputs
  44. if (!isNode(start)) throw new TypeError('Node expected');
  45. if (!isNode(end)) throw new TypeError('Node expected');
  46. if (step && !isNode(step)) throw new TypeError('Node expected');
  47. if (arguments.length > 3) throw new Error('Too many arguments');
  48. this.start = start; // included lower-bound
  49. this.end = end; // included upper-bound
  50. this.step = step || null; // optional step
  51. }
  52. get type() {
  53. return name;
  54. }
  55. get isRangeNode() {
  56. return true;
  57. }
  58. /**
  59. * Check whether the RangeNode needs the `end` symbol to be defined.
  60. * This end is the size of the Matrix in current dimension.
  61. * @return {boolean}
  62. */
  63. needsEnd() {
  64. // find all `end` symbols in this RangeNode
  65. var endSymbols = this.filter(function (node) {
  66. return isSymbolNode(node) && node.name === 'end';
  67. });
  68. return endSymbols.length > 0;
  69. }
  70. /**
  71. * Compile a node into a JavaScript function.
  72. * This basically pre-calculates as much as possible and only leaves open
  73. * calculations which depend on a dynamic scope with variables.
  74. * @param {Object} math Math.js namespace with functions and constants.
  75. * @param {Object} argNames An object with argument names as key and `true`
  76. * as value. Used in the SymbolNode to optimize
  77. * for arguments from user assigned functions
  78. * (see FunctionAssignmentNode) or special symbols
  79. * like `end` (see IndexNode).
  80. * @return {function} Returns a function which can be called like:
  81. * evalNode(scope: Object, args: Object, context: *)
  82. */
  83. _compile(math, argNames) {
  84. var range = math.range;
  85. var evalStart = this.start._compile(math, argNames);
  86. var evalEnd = this.end._compile(math, argNames);
  87. if (this.step) {
  88. var evalStep = this.step._compile(math, argNames);
  89. return function evalRangeNode(scope, args, context) {
  90. return range(evalStart(scope, args, context), evalEnd(scope, args, context), evalStep(scope, args, context));
  91. };
  92. } else {
  93. return function evalRangeNode(scope, args, context) {
  94. return range(evalStart(scope, args, context), evalEnd(scope, args, context));
  95. };
  96. }
  97. }
  98. /**
  99. * Execute a callback for each of the child nodes of this node
  100. * @param {function(child: Node, path: string, parent: Node)} callback
  101. */
  102. forEach(callback) {
  103. callback(this.start, 'start', this);
  104. callback(this.end, 'end', this);
  105. if (this.step) {
  106. callback(this.step, 'step', this);
  107. }
  108. }
  109. /**
  110. * Create a new RangeNode whose children are the results of calling
  111. * the provided callback function for each child of the original node.
  112. * @param {function(child: Node, path: string, parent: Node): Node} callback
  113. * @returns {RangeNode} Returns a transformed copy of the node
  114. */
  115. map(callback) {
  116. return new RangeNode(this._ifNode(callback(this.start, 'start', this)), this._ifNode(callback(this.end, 'end', this)), this.step && this._ifNode(callback(this.step, 'step', this)));
  117. }
  118. /**
  119. * Create a clone of this node, a shallow copy
  120. * @return {RangeNode}
  121. */
  122. clone() {
  123. return new RangeNode(this.start, this.end, this.step && this.step);
  124. }
  125. /**
  126. * Get string representation
  127. * @param {Object} options
  128. * @return {string} str
  129. */
  130. _toString(options) {
  131. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  132. var parens = calculateNecessaryParentheses(this, parenthesis, options && options.implicit);
  133. // format string as start:step:stop
  134. var str;
  135. var start = this.start.toString(options);
  136. if (parens.start) {
  137. start = '(' + start + ')';
  138. }
  139. str = start;
  140. if (this.step) {
  141. var step = this.step.toString(options);
  142. if (parens.step) {
  143. step = '(' + step + ')';
  144. }
  145. str += ':' + step;
  146. }
  147. var end = this.end.toString(options);
  148. if (parens.end) {
  149. end = '(' + end + ')';
  150. }
  151. str += ':' + end;
  152. return str;
  153. }
  154. /**
  155. * Get a JSON representation of the node
  156. * @returns {Object}
  157. */
  158. toJSON() {
  159. return {
  160. mathjs: name,
  161. start: this.start,
  162. end: this.end,
  163. step: this.step
  164. };
  165. }
  166. /**
  167. * Instantiate an RangeNode from its JSON representation
  168. * @param {Object} json
  169. * An object structured like
  170. * `{"mathjs": "RangeNode", "start": ..., "end": ..., "step": ...}`,
  171. * where mathjs is optional
  172. * @returns {RangeNode}
  173. */
  174. static fromJSON(json) {
  175. return new RangeNode(json.start, json.end, json.step);
  176. }
  177. /**
  178. * Get HTML representation
  179. * @param {Object} options
  180. * @return {string} str
  181. */
  182. toHTML(options) {
  183. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  184. var parens = calculateNecessaryParentheses(this, parenthesis, options && options.implicit);
  185. // format string as start:step:stop
  186. var str;
  187. var start = this.start.toHTML(options);
  188. if (parens.start) {
  189. start = '<span class="math-parenthesis math-round-parenthesis">(</span>' + start + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  190. }
  191. str = start;
  192. if (this.step) {
  193. var step = this.step.toHTML(options);
  194. if (parens.step) {
  195. step = '<span class="math-parenthesis math-round-parenthesis">(</span>' + step + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  196. }
  197. str += '<span class="math-operator math-range-operator">:</span>' + step;
  198. }
  199. var end = this.end.toHTML(options);
  200. if (parens.end) {
  201. end = '<span class="math-parenthesis math-round-parenthesis">(</span>' + end + '<span class="math-parenthesis math-round-parenthesis">)</span>';
  202. }
  203. str += '<span class="math-operator math-range-operator">:</span>' + end;
  204. return str;
  205. }
  206. /**
  207. * Get LaTeX representation
  208. * @params {Object} options
  209. * @return {string} str
  210. */
  211. _toTex(options) {
  212. var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
  213. var parens = calculateNecessaryParentheses(this, parenthesis, options && options.implicit);
  214. var str = this.start.toTex(options);
  215. if (parens.start) {
  216. str = "\\left(".concat(str, "\\right)");
  217. }
  218. if (this.step) {
  219. var step = this.step.toTex(options);
  220. if (parens.step) {
  221. step = "\\left(".concat(step, "\\right)");
  222. }
  223. str += ':' + step;
  224. }
  225. var end = this.end.toTex(options);
  226. if (parens.end) {
  227. end = "\\left(".concat(end, "\\right)");
  228. }
  229. str += ':' + end;
  230. return str;
  231. }
  232. }
  233. _defineProperty(RangeNode, "name", name);
  234. return RangeNode;
  235. }, {
  236. isClass: true,
  237. isNode: true
  238. });