1 | import { isAccessorNode, isArrayNode, isConstantNode, isFunctionNode, isIndexNode, isNode, isObjectNode, isParenthesisNode, isSymbolNode } from '../../utils/is.js';
|
2 | import { getSafeProperty } from '../../utils/customs.js';
|
3 | import { factory } from '../../utils/factory.js';
|
4 | import { accessFactory } from './utils/access.js';
|
5 | var name = 'AccessorNode';
|
6 | var dependencies = ['subset', 'Node'];
|
7 | export var createAccessorNode = factory(name, dependencies, (_ref) => {
|
8 | var {
|
9 | subset,
|
10 | Node
|
11 | } = _ref;
|
12 | var access = accessFactory({
|
13 | subset
|
14 | });
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function AccessorNode(object, index) {
|
26 | if (!(this instanceof AccessorNode)) {
|
27 | throw new SyntaxError('Constructor must be called with the new operator');
|
28 | }
|
29 |
|
30 | if (!isNode(object)) {
|
31 | throw new TypeError('Node expected for parameter "object"');
|
32 | }
|
33 |
|
34 | if (!isIndexNode(index)) {
|
35 | throw new TypeError('IndexNode expected for parameter "index"');
|
36 | }
|
37 |
|
38 | this.object = object || null;
|
39 | this.index = index;
|
40 |
|
41 | Object.defineProperty(this, 'name', {
|
42 | get: function () {
|
43 | if (this.index) {
|
44 | return this.index.isObjectProperty() ? this.index.getObjectProperty() : '';
|
45 | } else {
|
46 | return this.object.name || '';
|
47 | }
|
48 | }.bind(this),
|
49 | set: function set() {
|
50 | throw new Error('Cannot assign a new name, name is read-only');
|
51 | }
|
52 | });
|
53 | }
|
54 |
|
55 | AccessorNode.prototype = new Node();
|
56 | AccessorNode.prototype.type = 'AccessorNode';
|
57 | AccessorNode.prototype.isAccessorNode = true;
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | AccessorNode.prototype._compile = function (math, argNames) {
|
73 | var evalObject = this.object._compile(math, argNames);
|
74 |
|
75 | var evalIndex = this.index._compile(math, argNames);
|
76 |
|
77 | if (this.index.isObjectProperty()) {
|
78 | var prop = this.index.getObjectProperty();
|
79 | return function evalAccessorNode(scope, args, context) {
|
80 | return getSafeProperty(evalObject(scope, args, context), prop);
|
81 | };
|
82 | } else {
|
83 | return function evalAccessorNode(scope, args, context) {
|
84 | var object = evalObject(scope, args, context);
|
85 | var index = evalIndex(scope, args, object);
|
86 |
|
87 | return access(object, index);
|
88 | };
|
89 | }
|
90 | };
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | AccessorNode.prototype.forEach = function (callback) {
|
98 | callback(this.object, 'object', this);
|
99 | callback(this.index, 'index', this);
|
100 | };
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | AccessorNode.prototype.map = function (callback) {
|
110 | return new AccessorNode(this._ifNode(callback(this.object, 'object', this)), this._ifNode(callback(this.index, 'index', this)));
|
111 | };
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | AccessorNode.prototype.clone = function () {
|
119 | return new AccessorNode(this.object, this.index);
|
120 | };
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | AccessorNode.prototype._toString = function (options) {
|
129 | var object = this.object.toString(options);
|
130 |
|
131 | if (needParenthesis(this.object)) {
|
132 | object = '(' + object + ')';
|
133 | }
|
134 |
|
135 | return object + this.index.toString(options);
|
136 | };
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | AccessorNode.prototype.toHTML = function (options) {
|
145 | var object = this.object.toHTML(options);
|
146 |
|
147 | if (needParenthesis(this.object)) {
|
148 | object = '<span class="math-parenthesis math-round-parenthesis">(</span>' + object + '<span class="math-parenthesis math-round-parenthesis">)</span>';
|
149 | }
|
150 |
|
151 | return object + this.index.toHTML(options);
|
152 | };
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | AccessorNode.prototype._toTex = function (options) {
|
161 | var object = this.object.toTex(options);
|
162 |
|
163 | if (needParenthesis(this.object)) {
|
164 | object = '\\left(\' + object + \'\\right)';
|
165 | }
|
166 |
|
167 | return object + this.index.toTex(options);
|
168 | };
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | AccessorNode.prototype.toJSON = function () {
|
176 | return {
|
177 | mathjs: 'AccessorNode',
|
178 | object: this.object,
|
179 | index: this.index
|
180 | };
|
181 | };
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | AccessorNode.fromJSON = function (json) {
|
192 | return new AccessorNode(json.object, json.index);
|
193 | };
|
194 | |
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | function needParenthesis(node) {
|
201 |
|
202 | return !(isAccessorNode(node) || isArrayNode(node) || isConstantNode(node) || isFunctionNode(node) || isObjectNode(node) || isParenthesisNode(node) || isSymbolNode(node));
|
203 | }
|
204 |
|
205 | return AccessorNode;
|
206 | }, {
|
207 | isClass: true,
|
208 | isNode: true
|
209 | }); |
\ | No newline at end of file |