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