1 | import { isNode } from '../../utils/is.js';
|
2 | import { escape, stringify } from '../../utils/string.js';
|
3 | import { isSafeProperty } from '../../utils/customs.js';
|
4 | import { hasOwnProperty } from '../../utils/object.js';
|
5 | import { factory } from '../../utils/factory.js';
|
6 | var name = 'ObjectNode';
|
7 | var dependencies = ['Node'];
|
8 | export var createObjectNode = factory(name, dependencies, (_ref) => {
|
9 | var {
|
10 | Node
|
11 | } = _ref;
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function ObjectNode(properties) {
|
20 | if (!(this instanceof ObjectNode)) {
|
21 | throw new SyntaxError('Constructor must be called with the new operator');
|
22 | }
|
23 |
|
24 | this.properties = properties || {};
|
25 |
|
26 | if (properties) {
|
27 | if (!(typeof properties === 'object') || !Object.keys(properties).every(function (key) {
|
28 | return isNode(properties[key]);
|
29 | })) {
|
30 | throw new TypeError('Object containing Nodes expected');
|
31 | }
|
32 | }
|
33 | }
|
34 |
|
35 | ObjectNode.prototype = new Node();
|
36 | ObjectNode.prototype.type = 'ObjectNode';
|
37 | ObjectNode.prototype.isObjectNode = true;
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | ObjectNode.prototype._compile = function (math, argNames) {
|
53 | var evalEntries = {};
|
54 |
|
55 | for (var key in this.properties) {
|
56 | if (hasOwnProperty(this.properties, key)) {
|
57 |
|
58 |
|
59 | var stringifiedKey = stringify(key);
|
60 | var parsedKey = JSON.parse(stringifiedKey);
|
61 |
|
62 | if (!isSafeProperty(this.properties, parsedKey)) {
|
63 | throw new Error('No access to property "' + parsedKey + '"');
|
64 | }
|
65 |
|
66 | evalEntries[parsedKey] = this.properties[key]._compile(math, argNames);
|
67 | }
|
68 | }
|
69 |
|
70 | return function evalObjectNode(scope, args, context) {
|
71 | var obj = {};
|
72 |
|
73 | for (var _key in evalEntries) {
|
74 | if (hasOwnProperty(evalEntries, _key)) {
|
75 | obj[_key] = evalEntries[_key](scope, args, context);
|
76 | }
|
77 | }
|
78 |
|
79 | return obj;
|
80 | };
|
81 | };
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | ObjectNode.prototype.forEach = function (callback) {
|
89 | for (var key in this.properties) {
|
90 | if (hasOwnProperty(this.properties, key)) {
|
91 | callback(this.properties[key], 'properties[' + stringify(key) + ']', this);
|
92 | }
|
93 | }
|
94 | };
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | ObjectNode.prototype.map = function (callback) {
|
104 | var properties = {};
|
105 |
|
106 | for (var key in this.properties) {
|
107 | if (hasOwnProperty(this.properties, key)) {
|
108 | properties[key] = this._ifNode(callback(this.properties[key], 'properties[' + stringify(key) + ']', this));
|
109 | }
|
110 | }
|
111 |
|
112 | return new ObjectNode(properties);
|
113 | };
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | ObjectNode.prototype.clone = function () {
|
121 | var properties = {};
|
122 |
|
123 | for (var key in this.properties) {
|
124 | if (hasOwnProperty(this.properties, key)) {
|
125 | properties[key] = this.properties[key];
|
126 | }
|
127 | }
|
128 |
|
129 | return new ObjectNode(properties);
|
130 | };
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | ObjectNode.prototype._toString = function (options) {
|
140 | var entries = [];
|
141 |
|
142 | for (var key in this.properties) {
|
143 | if (hasOwnProperty(this.properties, key)) {
|
144 | entries.push(stringify(key) + ': ' + this.properties[key].toString(options));
|
145 | }
|
146 | }
|
147 |
|
148 | return '{' + entries.join(', ') + '}';
|
149 | };
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | ObjectNode.prototype.toJSON = function () {
|
157 | return {
|
158 | mathjs: 'ObjectNode',
|
159 | properties: this.properties
|
160 | };
|
161 | };
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | ObjectNode.fromJSON = function (json) {
|
172 | return new ObjectNode(json.properties);
|
173 | };
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | ObjectNode.prototype.toHTML = function (options) {
|
183 | var entries = [];
|
184 |
|
185 | for (var key in this.properties) {
|
186 | if (hasOwnProperty(this.properties, key)) {
|
187 | entries.push('<span class="math-symbol math-property">' + escape(key) + '</span>' + '<span class="math-operator math-assignment-operator math-property-assignment-operator math-binary-operator">:</span>' + this.properties[key].toHTML(options));
|
188 | }
|
189 | }
|
190 |
|
191 | return '<span class="math-parenthesis math-curly-parenthesis">{</span>' + entries.join('<span class="math-separator">,</span>') + '<span class="math-parenthesis math-curly-parenthesis">}</span>';
|
192 | };
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | ObjectNode.prototype._toTex = function (options) {
|
201 | var entries = [];
|
202 |
|
203 | for (var key in this.properties) {
|
204 | if (hasOwnProperty(this.properties, key)) {
|
205 | entries.push('\\mathbf{' + key + ':} & ' + this.properties[key].toTex(options) + '\\\\');
|
206 | }
|
207 | }
|
208 |
|
209 | return "\\left\\{\\begin{array}{ll}".concat(entries.join('\n'), "\\end{array}\\right\\}");
|
210 | };
|
211 |
|
212 | return ObjectNode;
|
213 | }, {
|
214 | isClass: true,
|
215 | isNode: true
|
216 | }); |
\ | No newline at end of file |