BlockNode.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.createBlockNode = 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 = 'BlockNode';
  19. var dependencies = ['ResultSet', 'Node'];
  20. var createBlockNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  21. var ResultSet = _ref.ResultSet,
  22. Node = _ref.Node;
  23. var BlockNode = /*#__PURE__*/function (_Node) {
  24. (0, _inherits2["default"])(BlockNode, _Node);
  25. var _super = _createSuper(BlockNode);
  26. /**
  27. * @constructor BlockNode
  28. * @extends {Node}
  29. * Holds a set with blocks
  30. * @param {Array.<{node: Node} | {node: Node, visible: boolean}>} blocks
  31. * An array with blocks, where a block is constructed as an
  32. * Object with properties block, which is a Node, and visible,
  33. * which is a boolean. The property visible is optional and
  34. * is true by default
  35. */
  36. function BlockNode(blocks) {
  37. var _this;
  38. (0, _classCallCheck2["default"])(this, BlockNode);
  39. _this = _super.call(this);
  40. // validate input, copy blocks
  41. if (!Array.isArray(blocks)) throw new Error('Array expected');
  42. _this.blocks = blocks.map(function (block) {
  43. var node = block && block.node;
  44. var visible = block && block.visible !== undefined ? block.visible : true;
  45. if (!(0, _is.isNode)(node)) throw new TypeError('Property "node" must be a Node');
  46. if (typeof visible !== 'boolean') {
  47. throw new TypeError('Property "visible" must be a boolean');
  48. }
  49. return {
  50. node: node,
  51. visible: visible
  52. };
  53. });
  54. return _this;
  55. }
  56. (0, _createClass2["default"])(BlockNode, [{
  57. key: "type",
  58. get: function get() {
  59. return name;
  60. }
  61. }, {
  62. key: "isBlockNode",
  63. get: function get() {
  64. return true;
  65. }
  66. /**
  67. * Compile a node into a JavaScript function.
  68. * This basically pre-calculates as much as possible and only leaves open
  69. * calculations which depend on a dynamic scope with variables.
  70. * @param {Object} math Math.js namespace with functions and constants.
  71. * @param {Object} argNames An object with argument names as key and `true`
  72. * as value. Used in the SymbolNode to optimize
  73. * for arguments from user assigned functions
  74. * (see FunctionAssignmentNode) or special symbols
  75. * like `end` (see IndexNode).
  76. * @return {function} Returns a function which can be called like:
  77. * evalNode(scope: Object, args: Object, context: *)
  78. */
  79. }, {
  80. key: "_compile",
  81. value: function _compile(math, argNames) {
  82. var evalBlocks = (0, _array.map)(this.blocks, function (block) {
  83. return {
  84. evaluate: block.node._compile(math, argNames),
  85. visible: block.visible
  86. };
  87. });
  88. return function evalBlockNodes(scope, args, context) {
  89. var results = [];
  90. (0, _array.forEach)(evalBlocks, function evalBlockNode(block) {
  91. var result = block.evaluate(scope, args, context);
  92. if (block.visible) {
  93. results.push(result);
  94. }
  95. });
  96. return new ResultSet(results);
  97. };
  98. }
  99. /**
  100. * Execute a callback for each of the child blocks of this node
  101. * @param {function(child: Node, path: string, parent: Node)} callback
  102. */
  103. }, {
  104. key: "forEach",
  105. value: function forEach(callback) {
  106. for (var i = 0; i < this.blocks.length; i++) {
  107. callback(this.blocks[i].node, 'blocks[' + i + '].node', this);
  108. }
  109. }
  110. /**
  111. * Create a new BlockNode whose children are the results of calling
  112. * the provided callback function for each child of the original node.
  113. * @param {function(child: Node, path: string, parent: Node): Node} callback
  114. * @returns {BlockNode} Returns a transformed copy of the node
  115. */
  116. }, {
  117. key: "map",
  118. value: function map(callback) {
  119. var blocks = [];
  120. for (var i = 0; i < this.blocks.length; i++) {
  121. var block = this.blocks[i];
  122. var node = this._ifNode(callback(block.node, 'blocks[' + i + '].node', this));
  123. blocks[i] = {
  124. node: node,
  125. visible: block.visible
  126. };
  127. }
  128. return new BlockNode(blocks);
  129. }
  130. /**
  131. * Create a clone of this node, a shallow copy
  132. * @return {BlockNode}
  133. */
  134. }, {
  135. key: "clone",
  136. value: function clone() {
  137. var blocks = this.blocks.map(function (block) {
  138. return {
  139. node: block.node,
  140. visible: block.visible
  141. };
  142. });
  143. return new BlockNode(blocks);
  144. }
  145. /**
  146. * Get string representation
  147. * @param {Object} options
  148. * @return {string} str
  149. * @override
  150. */
  151. }, {
  152. key: "_toString",
  153. value: function _toString(options) {
  154. return this.blocks.map(function (param) {
  155. return param.node.toString(options) + (param.visible ? '' : ';');
  156. }).join('\n');
  157. }
  158. /**
  159. * Get a JSON representation of the node
  160. * @returns {Object}
  161. */
  162. }, {
  163. key: "toJSON",
  164. value: function toJSON() {
  165. return {
  166. mathjs: name,
  167. blocks: this.blocks
  168. };
  169. }
  170. /**
  171. * Instantiate an BlockNode from its JSON representation
  172. * @param {Object} json
  173. * An object structured like
  174. * `{"mathjs": "BlockNode", blocks: [{node: ..., visible: false}, ...]}`,
  175. * where mathjs is optional
  176. * @returns {BlockNode}
  177. */
  178. }, {
  179. key: "toHTML",
  180. value:
  181. /**
  182. * Get HTML representation
  183. * @param {Object} options
  184. * @return {string} str
  185. * @override
  186. */
  187. function toHTML(options) {
  188. return this.blocks.map(function (param) {
  189. return param.node.toHTML(options) + (param.visible ? '' : '<span class="math-separator">;</span>');
  190. }).join('<span class="math-separator"><br /></span>');
  191. }
  192. /**
  193. * Get LaTeX representation
  194. * @param {Object} options
  195. * @return {string} str
  196. */
  197. }, {
  198. key: "_toTex",
  199. value: function _toTex(options) {
  200. return this.blocks.map(function (param) {
  201. return param.node.toTex(options) + (param.visible ? '' : ';');
  202. }).join('\\;\\;\n');
  203. }
  204. }], [{
  205. key: "fromJSON",
  206. value: function fromJSON(json) {
  207. return new BlockNode(json.blocks);
  208. }
  209. }]);
  210. return BlockNode;
  211. }(Node);
  212. (0, _defineProperty2["default"])(BlockNode, "name", name);
  213. return BlockNode;
  214. }, {
  215. isClass: true,
  216. isNode: true
  217. });
  218. exports.createBlockNode = createBlockNode;