simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

ArrayNode.js (5857B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createArrayNode = void 0;
      7 
      8 var _is = require("../../utils/is.js");
      9 
     10 var _array = require("../../utils/array.js");
     11 
     12 var _factory = require("../../utils/factory.js");
     13 
     14 var name = 'ArrayNode';
     15 var dependencies = ['Node'];
     16 var createArrayNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var Node = _ref.Node;
     18 
     19   /**
     20    * @constructor ArrayNode
     21    * @extends {Node}
     22    * Holds an 1-dimensional array with items
     23    * @param {Node[]} [items]   1 dimensional array with items
     24    */
     25   function ArrayNode(items) {
     26     if (!(this instanceof ArrayNode)) {
     27       throw new SyntaxError('Constructor must be called with the new operator');
     28     }
     29 
     30     this.items = items || []; // validate input
     31 
     32     if (!Array.isArray(this.items) || !this.items.every(_is.isNode)) {
     33       throw new TypeError('Array containing Nodes expected');
     34     }
     35   }
     36 
     37   ArrayNode.prototype = new Node();
     38   ArrayNode.prototype.type = 'ArrayNode';
     39   ArrayNode.prototype.isArrayNode = true;
     40   /**
     41    * Compile a node into a JavaScript function.
     42    * This basically pre-calculates as much as possible and only leaves open
     43    * calculations which depend on a dynamic scope with variables.
     44    * @param {Object} math     Math.js namespace with functions and constants.
     45    * @param {Object} argNames An object with argument names as key and `true`
     46    *                          as value. Used in the SymbolNode to optimize
     47    *                          for arguments from user assigned functions
     48    *                          (see FunctionAssignmentNode) or special symbols
     49    *                          like `end` (see IndexNode).
     50    * @return {function} Returns a function which can be called like:
     51    *                        evalNode(scope: Object, args: Object, context: *)
     52    */
     53 
     54   ArrayNode.prototype._compile = function (math, argNames) {
     55     var evalItems = (0, _array.map)(this.items, function (item) {
     56       return item._compile(math, argNames);
     57     });
     58     var asMatrix = math.config.matrix !== 'Array';
     59 
     60     if (asMatrix) {
     61       var matrix = math.matrix;
     62       return function evalArrayNode(scope, args, context) {
     63         return matrix((0, _array.map)(evalItems, function (evalItem) {
     64           return evalItem(scope, args, context);
     65         }));
     66       };
     67     } else {
     68       return function evalArrayNode(scope, args, context) {
     69         return (0, _array.map)(evalItems, function (evalItem) {
     70           return evalItem(scope, args, context);
     71         });
     72       };
     73     }
     74   };
     75   /**
     76    * Execute a callback for each of the child nodes of this node
     77    * @param {function(child: Node, path: string, parent: Node)} callback
     78    */
     79 
     80 
     81   ArrayNode.prototype.forEach = function (callback) {
     82     for (var i = 0; i < this.items.length; i++) {
     83       var node = this.items[i];
     84       callback(node, 'items[' + i + ']', this);
     85     }
     86   };
     87   /**
     88    * Create a new ArrayNode having it's childs be the results of calling
     89    * the provided callback function for each of the childs of the original node.
     90    * @param {function(child: Node, path: string, parent: Node): Node} callback
     91    * @returns {ArrayNode} Returns a transformed copy of the node
     92    */
     93 
     94 
     95   ArrayNode.prototype.map = function (callback) {
     96     var items = [];
     97 
     98     for (var i = 0; i < this.items.length; i++) {
     99       items[i] = this._ifNode(callback(this.items[i], 'items[' + i + ']', this));
    100     }
    101 
    102     return new ArrayNode(items);
    103   };
    104   /**
    105    * Create a clone of this node, a shallow copy
    106    * @return {ArrayNode}
    107    */
    108 
    109 
    110   ArrayNode.prototype.clone = function () {
    111     return new ArrayNode(this.items.slice(0));
    112   };
    113   /**
    114    * Get string representation
    115    * @param {Object} options
    116    * @return {string} str
    117    * @override
    118    */
    119 
    120 
    121   ArrayNode.prototype._toString = function (options) {
    122     var items = this.items.map(function (node) {
    123       return node.toString(options);
    124     });
    125     return '[' + items.join(', ') + ']';
    126   };
    127   /**
    128    * Get a JSON representation of the node
    129    * @returns {Object}
    130    */
    131 
    132 
    133   ArrayNode.prototype.toJSON = function () {
    134     return {
    135       mathjs: 'ArrayNode',
    136       items: this.items
    137     };
    138   };
    139   /**
    140    * Instantiate an ArrayNode from its JSON representation
    141    * @param {Object} json  An object structured like
    142    *                       `{"mathjs": "ArrayNode", items: [...]}`,
    143    *                       where mathjs is optional
    144    * @returns {ArrayNode}
    145    */
    146 
    147 
    148   ArrayNode.fromJSON = function (json) {
    149     return new ArrayNode(json.items);
    150   };
    151   /**
    152    * Get HTML representation
    153    * @param {Object} options
    154    * @return {string} str
    155    * @override
    156    */
    157 
    158 
    159   ArrayNode.prototype.toHTML = function (options) {
    160     var items = this.items.map(function (node) {
    161       return node.toHTML(options);
    162     });
    163     return '<span class="math-parenthesis math-square-parenthesis">[</span>' + items.join('<span class="math-separator">,</span>') + '<span class="math-parenthesis math-square-parenthesis">]</span>';
    164   };
    165   /**
    166    * Get LaTeX representation
    167    * @param {Object} options
    168    * @return {string} str
    169    */
    170 
    171 
    172   ArrayNode.prototype._toTex = function (options) {
    173     function itemsToTex(items, nested) {
    174       var mixedItems = items.some(_is.isArrayNode) && !items.every(_is.isArrayNode);
    175       var itemsFormRow = nested || mixedItems;
    176       var itemSep = itemsFormRow ? '&' : '\\\\';
    177       var itemsTex = items.map(function (node) {
    178         if (node.items) {
    179           return itemsToTex(node.items, !nested);
    180         } else {
    181           return node.toTex(options);
    182         }
    183       }).join(itemSep);
    184       return mixedItems || !itemsFormRow || itemsFormRow && !nested ? '\\begin{bmatrix}' + itemsTex + '\\end{bmatrix}' : itemsTex;
    185     }
    186 
    187     return itemsToTex(this.items, false);
    188   };
    189 
    190   return ArrayNode;
    191 }, {
    192   isClass: true,
    193   isNode: true
    194 });
    195 exports.createArrayNode = createArrayNode;