ObjectNode.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.createObjectNode = void 0;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  10. var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  11. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  12. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  13. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  14. var _is = require("../../utils/is.js");
  15. var _string = require("../../utils/string.js");
  16. var _customs = require("../../utils/customs.js");
  17. var _object = require("../../utils/object.js");
  18. var _factory = require("../../utils/factory.js");
  19. 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); }; }
  20. 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; } }
  21. var name = 'ObjectNode';
  22. var dependencies = ['Node'];
  23. var createObjectNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  24. var Node = _ref.Node;
  25. var ObjectNode = /*#__PURE__*/function (_Node) {
  26. (0, _inherits2["default"])(ObjectNode, _Node);
  27. var _super = _createSuper(ObjectNode);
  28. /**
  29. * @constructor ObjectNode
  30. * @extends {Node}
  31. * Holds an object with keys/values
  32. * @param {Object.<string, Node>} [properties] object with key/value pairs
  33. */
  34. function ObjectNode(properties) {
  35. var _this;
  36. (0, _classCallCheck2["default"])(this, ObjectNode);
  37. _this = _super.call(this);
  38. _this.properties = properties || {};
  39. // validate input
  40. if (properties) {
  41. if (!((0, _typeof2["default"])(properties) === 'object') || !Object.keys(properties).every(function (key) {
  42. return (0, _is.isNode)(properties[key]);
  43. })) {
  44. throw new TypeError('Object containing Nodes expected');
  45. }
  46. }
  47. return _this;
  48. }
  49. (0, _createClass2["default"])(ObjectNode, [{
  50. key: "type",
  51. get: function get() {
  52. return name;
  53. }
  54. }, {
  55. key: "isObjectNode",
  56. get: function get() {
  57. return true;
  58. }
  59. /**
  60. * Compile a node into a JavaScript function.
  61. * This basically pre-calculates as much as possible and only leaves open
  62. * calculations which depend on a dynamic scope with variables.
  63. * @param {Object} math Math.js namespace with functions and constants.
  64. * @param {Object} argNames An object with argument names as key and `true`
  65. * as value. Used in the SymbolNode to optimize
  66. * for arguments from user assigned functions
  67. * (see FunctionAssignmentNode) or special symbols
  68. * like `end` (see IndexNode).
  69. * @return {function} Returns a function which can be called like:
  70. * evalNode(scope: Object, args: Object, context: *)
  71. */
  72. }, {
  73. key: "_compile",
  74. value: function _compile(math, argNames) {
  75. var evalEntries = {};
  76. for (var key in this.properties) {
  77. if ((0, _object.hasOwnProperty)(this.properties, key)) {
  78. // we stringify/parse the key here to resolve unicode characters,
  79. // so you cannot create a key like {"co\\u006Estructor": null}
  80. var stringifiedKey = (0, _string.stringify)(key);
  81. var parsedKey = JSON.parse(stringifiedKey);
  82. if (!(0, _customs.isSafeProperty)(this.properties, parsedKey)) {
  83. throw new Error('No access to property "' + parsedKey + '"');
  84. }
  85. evalEntries[parsedKey] = this.properties[key]._compile(math, argNames);
  86. }
  87. }
  88. return function evalObjectNode(scope, args, context) {
  89. var obj = {};
  90. for (var _key in evalEntries) {
  91. if ((0, _object.hasOwnProperty)(evalEntries, _key)) {
  92. obj[_key] = evalEntries[_key](scope, args, context);
  93. }
  94. }
  95. return obj;
  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. }, {
  103. key: "forEach",
  104. value: function forEach(callback) {
  105. for (var key in this.properties) {
  106. if ((0, _object.hasOwnProperty)(this.properties, key)) {
  107. callback(this.properties[key], 'properties[' + (0, _string.stringify)(key) + ']', this);
  108. }
  109. }
  110. }
  111. /**
  112. * Create a new ObjectNode whose children are the results of calling
  113. * the provided callback function for each child of the original node.
  114. * @param {function(child: Node, path: string, parent: Node): Node} callback
  115. * @returns {ObjectNode} Returns a transformed copy of the node
  116. */
  117. }, {
  118. key: "map",
  119. value: function map(callback) {
  120. var properties = {};
  121. for (var key in this.properties) {
  122. if ((0, _object.hasOwnProperty)(this.properties, key)) {
  123. properties[key] = this._ifNode(callback(this.properties[key], 'properties[' + (0, _string.stringify)(key) + ']', this));
  124. }
  125. }
  126. return new ObjectNode(properties);
  127. }
  128. /**
  129. * Create a clone of this node, a shallow copy
  130. * @return {ObjectNode}
  131. */
  132. }, {
  133. key: "clone",
  134. value: function clone() {
  135. var properties = {};
  136. for (var key in this.properties) {
  137. if ((0, _object.hasOwnProperty)(this.properties, key)) {
  138. properties[key] = this.properties[key];
  139. }
  140. }
  141. return new ObjectNode(properties);
  142. }
  143. /**
  144. * Get string representation
  145. * @param {Object} options
  146. * @return {string} str
  147. * @override
  148. */
  149. }, {
  150. key: "_toString",
  151. value: function _toString(options) {
  152. var entries = [];
  153. for (var key in this.properties) {
  154. if ((0, _object.hasOwnProperty)(this.properties, key)) {
  155. entries.push((0, _string.stringify)(key) + ': ' + this.properties[key].toString(options));
  156. }
  157. }
  158. return '{' + entries.join(', ') + '}';
  159. }
  160. /**
  161. * Get a JSON representation of the node
  162. * @returns {Object}
  163. */
  164. }, {
  165. key: "toJSON",
  166. value: function toJSON() {
  167. return {
  168. mathjs: name,
  169. properties: this.properties
  170. };
  171. }
  172. /**
  173. * Instantiate an OperatorNode from its JSON representation
  174. * @param {Object} json An object structured like
  175. * `{"mathjs": "ObjectNode", "properties": {...}}`,
  176. * where mathjs is optional
  177. * @returns {ObjectNode}
  178. */
  179. }, {
  180. key: "toHTML",
  181. value:
  182. /**
  183. * Get HTML representation
  184. * @param {Object} options
  185. * @return {string} str
  186. * @override
  187. */
  188. function toHTML(options) {
  189. var entries = [];
  190. for (var key in this.properties) {
  191. if ((0, _object.hasOwnProperty)(this.properties, key)) {
  192. entries.push('<span class="math-symbol math-property">' + (0, _string.escape)(key) + '</span>' + '<span class="math-operator math-assignment-operator ' + 'math-property-assignment-operator math-binary-operator">' + ':</span>' + this.properties[key].toHTML(options));
  193. }
  194. }
  195. return '<span class="math-parenthesis math-curly-parenthesis">{</span>' + entries.join('<span class="math-separator">,</span>') + '<span class="math-parenthesis math-curly-parenthesis">}</span>';
  196. }
  197. /**
  198. * Get LaTeX representation
  199. * @param {Object} options
  200. * @return {string} str
  201. */
  202. }, {
  203. key: "_toTex",
  204. value: function _toTex(options) {
  205. var entries = [];
  206. for (var key in this.properties) {
  207. if ((0, _object.hasOwnProperty)(this.properties, key)) {
  208. entries.push('\\mathbf{' + key + ':} & ' + this.properties[key].toTex(options) + '\\\\');
  209. }
  210. }
  211. var tex = '\\left\\{\\begin{array}{ll}' + entries.join('\n') + '\\end{array}\\right\\}';
  212. return tex;
  213. }
  214. }], [{
  215. key: "fromJSON",
  216. value: function fromJSON(json) {
  217. return new ObjectNode(json.properties);
  218. }
  219. }]);
  220. return ObjectNode;
  221. }(Node);
  222. (0, _defineProperty2["default"])(ObjectNode, "name", name);
  223. return ObjectNode;
  224. }, {
  225. isClass: true,
  226. isNode: true
  227. });
  228. exports.createObjectNode = createObjectNode;