ArrayNode.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.createArrayNode = 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 _array = require("../../utils/array.js");
  15. var _factory = require("../../utils/factory.js");
  16. 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); }; }
  17. 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; } }
  18. var name = 'ArrayNode';
  19. var dependencies = ['Node'];
  20. var createArrayNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  21. var Node = _ref.Node;
  22. var ArrayNode = /*#__PURE__*/function (_Node) {
  23. (0, _inherits2["default"])(ArrayNode, _Node);
  24. var _super = _createSuper(ArrayNode);
  25. /**
  26. * @constructor ArrayNode
  27. * @extends {Node}
  28. * Holds an 1-dimensional array with items
  29. * @param {Node[]} [items] 1 dimensional array with items
  30. */
  31. function ArrayNode(items) {
  32. var _this;
  33. (0, _classCallCheck2["default"])(this, ArrayNode);
  34. _this = _super.call(this);
  35. _this.items = items || [];
  36. // validate input
  37. if (!Array.isArray(_this.items) || !_this.items.every(_is.isNode)) {
  38. throw new TypeError('Array containing Nodes expected');
  39. }
  40. return _this;
  41. }
  42. (0, _createClass2["default"])(ArrayNode, [{
  43. key: "type",
  44. get: function get() {
  45. return name;
  46. }
  47. }, {
  48. key: "isArrayNode",
  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. var evalItems = (0, _array.map)(this.items, function (item) {
  69. return item._compile(math, argNames);
  70. });
  71. var asMatrix = math.config.matrix !== 'Array';
  72. if (asMatrix) {
  73. var matrix = math.matrix;
  74. return function evalArrayNode(scope, args, context) {
  75. return matrix((0, _array.map)(evalItems, function (evalItem) {
  76. return evalItem(scope, args, context);
  77. }));
  78. };
  79. } else {
  80. return function evalArrayNode(scope, args, context) {
  81. return (0, _array.map)(evalItems, function (evalItem) {
  82. return evalItem(scope, args, context);
  83. });
  84. };
  85. }
  86. }
  87. /**
  88. * Execute a callback for each of the child nodes of this node
  89. * @param {function(child: Node, path: string, parent: Node)} callback
  90. */
  91. }, {
  92. key: "forEach",
  93. value: function forEach(callback) {
  94. for (var i = 0; i < this.items.length; i++) {
  95. var node = this.items[i];
  96. callback(node, 'items[' + i + ']', this);
  97. }
  98. }
  99. /**
  100. * Create a new ArrayNode whose children are the results of calling
  101. * the provided callback function for each child of the original node.
  102. * @param {function(child: Node, path: string, parent: Node): Node} callback
  103. * @returns {ArrayNode} Returns a transformed copy of the node
  104. */
  105. }, {
  106. key: "map",
  107. value: function map(callback) {
  108. var items = [];
  109. for (var i = 0; i < this.items.length; i++) {
  110. items[i] = this._ifNode(callback(this.items[i], 'items[' + i + ']', this));
  111. }
  112. return new ArrayNode(items);
  113. }
  114. /**
  115. * Create a clone of this node, a shallow copy
  116. * @return {ArrayNode}
  117. */
  118. }, {
  119. key: "clone",
  120. value: function clone() {
  121. return new ArrayNode(this.items.slice(0));
  122. }
  123. /**
  124. * Get string representation
  125. * @param {Object} options
  126. * @return {string} str
  127. * @override
  128. */
  129. }, {
  130. key: "_toString",
  131. value: function _toString(options) {
  132. var items = this.items.map(function (node) {
  133. return node.toString(options);
  134. });
  135. return '[' + items.join(', ') + ']';
  136. }
  137. /**
  138. * Get a JSON representation of the node
  139. * @returns {Object}
  140. */
  141. }, {
  142. key: "toJSON",
  143. value: function toJSON() {
  144. return {
  145. mathjs: name,
  146. items: this.items
  147. };
  148. }
  149. /**
  150. * Instantiate an ArrayNode from its JSON representation
  151. * @param {Object} json An object structured like
  152. * `{"mathjs": "ArrayNode", items: [...]}`,
  153. * where mathjs is optional
  154. * @returns {ArrayNode}
  155. */
  156. }, {
  157. key: "toHTML",
  158. value:
  159. /**
  160. * Get HTML representation
  161. * @param {Object} options
  162. * @return {string} str
  163. * @override
  164. */
  165. function toHTML(options) {
  166. var items = this.items.map(function (node) {
  167. return node.toHTML(options);
  168. });
  169. return '<span class="math-parenthesis math-square-parenthesis">[</span>' + items.join('<span class="math-separator">,</span>') + '<span class="math-parenthesis math-square-parenthesis">]</span>';
  170. }
  171. /**
  172. * Get LaTeX representation
  173. * @param {Object} options
  174. * @return {string} str
  175. */
  176. }, {
  177. key: "_toTex",
  178. value: function _toTex(options) {
  179. function itemsToTex(items, nested) {
  180. var mixedItems = items.some(_is.isArrayNode) && !items.every(_is.isArrayNode);
  181. var itemsFormRow = nested || mixedItems;
  182. var itemSep = itemsFormRow ? '&' : '\\\\';
  183. var itemsTex = items.map(function (node) {
  184. if (node.items) {
  185. return itemsToTex(node.items, !nested);
  186. } else {
  187. return node.toTex(options);
  188. }
  189. }).join(itemSep);
  190. return mixedItems || !itemsFormRow || itemsFormRow && !nested ? '\\begin{bmatrix}' + itemsTex + '\\end{bmatrix}' : itemsTex;
  191. }
  192. return itemsToTex(this.items, false);
  193. }
  194. }], [{
  195. key: "fromJSON",
  196. value: function fromJSON(json) {
  197. return new ArrayNode(json.items);
  198. }
  199. }]);
  200. return ArrayNode;
  201. }(Node);
  202. (0, _defineProperty2["default"])(ArrayNode, "name", name);
  203. return ArrayNode;
  204. }, {
  205. isClass: true,
  206. isNode: true
  207. });
  208. exports.createArrayNode = createArrayNode;