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