ArrayNode.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  2. import { isArrayNode, isNode } from '../../utils/is.js';
  3. import { map } from '../../utils/array.js';
  4. import { factory } from '../../utils/factory.js';
  5. var name = 'ArrayNode';
  6. var dependencies = ['Node'];
  7. export var createArrayNode = /* #__PURE__ */factory(name, dependencies, _ref => {
  8. var {
  9. Node
  10. } = _ref;
  11. class ArrayNode extends Node {
  12. /**
  13. * @constructor ArrayNode
  14. * @extends {Node}
  15. * Holds an 1-dimensional array with items
  16. * @param {Node[]} [items] 1 dimensional array with items
  17. */
  18. constructor(items) {
  19. super();
  20. this.items = items || [];
  21. // validate input
  22. if (!Array.isArray(this.items) || !this.items.every(isNode)) {
  23. throw new TypeError('Array containing Nodes expected');
  24. }
  25. }
  26. get type() {
  27. return name;
  28. }
  29. get isArrayNode() {
  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. var evalItems = map(this.items, function (item) {
  47. return item._compile(math, argNames);
  48. });
  49. var asMatrix = math.config.matrix !== 'Array';
  50. if (asMatrix) {
  51. var matrix = math.matrix;
  52. return function evalArrayNode(scope, args, context) {
  53. return matrix(map(evalItems, function (evalItem) {
  54. return evalItem(scope, args, context);
  55. }));
  56. };
  57. } else {
  58. return function evalArrayNode(scope, args, context) {
  59. return map(evalItems, function (evalItem) {
  60. return evalItem(scope, args, context);
  61. });
  62. };
  63. }
  64. }
  65. /**
  66. * Execute a callback for each of the child nodes of this node
  67. * @param {function(child: Node, path: string, parent: Node)} callback
  68. */
  69. forEach(callback) {
  70. for (var i = 0; i < this.items.length; i++) {
  71. var node = this.items[i];
  72. callback(node, 'items[' + i + ']', this);
  73. }
  74. }
  75. /**
  76. * Create a new ArrayNode whose children are the results of calling
  77. * the provided callback function for each child of the original node.
  78. * @param {function(child: Node, path: string, parent: Node): Node} callback
  79. * @returns {ArrayNode} Returns a transformed copy of the node
  80. */
  81. map(callback) {
  82. var items = [];
  83. for (var i = 0; i < this.items.length; i++) {
  84. items[i] = this._ifNode(callback(this.items[i], 'items[' + i + ']', this));
  85. }
  86. return new ArrayNode(items);
  87. }
  88. /**
  89. * Create a clone of this node, a shallow copy
  90. * @return {ArrayNode}
  91. */
  92. clone() {
  93. return new ArrayNode(this.items.slice(0));
  94. }
  95. /**
  96. * Get string representation
  97. * @param {Object} options
  98. * @return {string} str
  99. * @override
  100. */
  101. _toString(options) {
  102. var items = this.items.map(function (node) {
  103. return node.toString(options);
  104. });
  105. return '[' + items.join(', ') + ']';
  106. }
  107. /**
  108. * Get a JSON representation of the node
  109. * @returns {Object}
  110. */
  111. toJSON() {
  112. return {
  113. mathjs: name,
  114. items: this.items
  115. };
  116. }
  117. /**
  118. * Instantiate an ArrayNode from its JSON representation
  119. * @param {Object} json An object structured like
  120. * `{"mathjs": "ArrayNode", items: [...]}`,
  121. * where mathjs is optional
  122. * @returns {ArrayNode}
  123. */
  124. static fromJSON(json) {
  125. return new ArrayNode(json.items);
  126. }
  127. /**
  128. * Get HTML representation
  129. * @param {Object} options
  130. * @return {string} str
  131. * @override
  132. */
  133. toHTML(options) {
  134. var items = this.items.map(function (node) {
  135. return node.toHTML(options);
  136. });
  137. return '<span class="math-parenthesis math-square-parenthesis">[</span>' + items.join('<span class="math-separator">,</span>') + '<span class="math-parenthesis math-square-parenthesis">]</span>';
  138. }
  139. /**
  140. * Get LaTeX representation
  141. * @param {Object} options
  142. * @return {string} str
  143. */
  144. _toTex(options) {
  145. function itemsToTex(items, nested) {
  146. var mixedItems = items.some(isArrayNode) && !items.every(isArrayNode);
  147. var itemsFormRow = nested || mixedItems;
  148. var itemSep = itemsFormRow ? '&' : '\\\\';
  149. var itemsTex = items.map(function (node) {
  150. if (node.items) {
  151. return itemsToTex(node.items, !nested);
  152. } else {
  153. return node.toTex(options);
  154. }
  155. }).join(itemSep);
  156. return mixedItems || !itemsFormRow || itemsFormRow && !nested ? '\\begin{bmatrix}' + itemsTex + '\\end{bmatrix}' : itemsTex;
  157. }
  158. return itemsToTex(this.items, false);
  159. }
  160. }
  161. _defineProperty(ArrayNode, "name", name);
  162. return ArrayNode;
  163. }, {
  164. isClass: true,
  165. isNode: true
  166. });