1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.importFactory = importFactory;
|
7 |
|
8 | var _is = require("../../utils/is");
|
9 |
|
10 | var _factory = require("../../utils/factory");
|
11 |
|
12 | var _object = require("../../utils/object");
|
13 |
|
14 | var _array = require("../../utils/array");
|
15 |
|
16 | var _ArgumentsError = require("../../error/ArgumentsError");
|
17 |
|
18 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
19 |
|
20 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
21 |
|
22 | function importFactory(typed, load, math, importedFactories) {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function mathImport(functions, options) {
|
79 | var num = arguments.length;
|
80 |
|
81 | if (num !== 1 && num !== 2) {
|
82 | throw new _ArgumentsError.ArgumentsError('import', num, 1, 2);
|
83 | }
|
84 |
|
85 | if (!options) {
|
86 | options = {};
|
87 | }
|
88 |
|
89 | function flattenImports(flatValues, value, name) {
|
90 | if (Array.isArray(value)) {
|
91 | value.forEach(function (item) {
|
92 | return flattenImports(flatValues, item);
|
93 | });
|
94 | } else if (_typeof(value) === 'object') {
|
95 | for (var _name in value) {
|
96 | if ((0, _object.hasOwnProperty)(value, _name)) {
|
97 | flattenImports(flatValues, value[_name], _name);
|
98 | }
|
99 | }
|
100 | } else if ((0, _factory.isFactory)(value) || name !== undefined) {
|
101 | var flatName = (0, _factory.isFactory)(value) ? isTransformFunctionFactory(value) ? value.fn + '.transform' :
|
102 | value.fn : name;
|
103 |
|
104 | if ((0, _object.hasOwnProperty)(flatValues, flatName) && flatValues[flatName] !== value && !options.silent) {
|
105 | throw new Error('Cannot import "' + flatName + '" twice');
|
106 | }
|
107 |
|
108 | flatValues[flatName] = value;
|
109 | } else {
|
110 | if (!options.silent) {
|
111 | throw new TypeError('Factory, Object, or Array expected');
|
112 | }
|
113 | }
|
114 | }
|
115 |
|
116 | var flatValues = {};
|
117 | flattenImports(flatValues, functions);
|
118 |
|
119 | for (var name in flatValues) {
|
120 | if ((0, _object.hasOwnProperty)(flatValues, name)) {
|
121 |
|
122 | var value = flatValues[name];
|
123 |
|
124 | if ((0, _factory.isFactory)(value)) {
|
125 |
|
126 |
|
127 |
|
128 | _importFactory(value, options);
|
129 | } else if (isSupportedType(value)) {
|
130 | _import(name, value, options);
|
131 | } else {
|
132 | if (!options.silent) {
|
133 | throw new TypeError('Factory, Object, or Array expected');
|
134 | }
|
135 | }
|
136 | }
|
137 | }
|
138 | }
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | function _import(name, value, options) {
|
149 |
|
150 | if (options.wrap && typeof value === 'function') {
|
151 |
|
152 | value = _wrap(value);
|
153 | }
|
154 |
|
155 |
|
156 | if (hasTypedFunctionSignature(value)) {
|
157 | value = typed(name, _defineProperty({}, value.signature, value));
|
158 | }
|
159 |
|
160 | if (isTypedFunction(math[name]) && isTypedFunction(value)) {
|
161 | if (options.override) {
|
162 |
|
163 | value = typed(name, value.signatures);
|
164 | } else {
|
165 |
|
166 | value = typed(math[name], value);
|
167 | }
|
168 |
|
169 | math[name] = value;
|
170 | delete importedFactories[name];
|
171 |
|
172 | _importTransform(name, value);
|
173 |
|
174 | math.emit('import', name, function resolver() {
|
175 | return value;
|
176 | });
|
177 | return;
|
178 | }
|
179 |
|
180 | if (math[name] === undefined || options.override) {
|
181 | math[name] = value;
|
182 | delete importedFactories[name];
|
183 |
|
184 | _importTransform(name, value);
|
185 |
|
186 | math.emit('import', name, function resolver() {
|
187 | return value;
|
188 | });
|
189 | return;
|
190 | }
|
191 |
|
192 | if (!options.silent) {
|
193 | throw new Error('Cannot import "' + name + '": already exists');
|
194 | }
|
195 | }
|
196 |
|
197 | function _importTransform(name, value) {
|
198 | if (value && typeof value.transform === 'function') {
|
199 | math.expression.transform[name] = value.transform;
|
200 |
|
201 | if (allowedInExpressions(name)) {
|
202 | math.expression.mathWithTransform[name] = value.transform;
|
203 | }
|
204 | } else {
|
205 |
|
206 | delete math.expression.transform[name];
|
207 |
|
208 | if (allowedInExpressions(name)) {
|
209 | math.expression.mathWithTransform[name] = value;
|
210 | }
|
211 | }
|
212 | }
|
213 |
|
214 | function _deleteTransform(name) {
|
215 | delete math.expression.transform[name];
|
216 |
|
217 | if (allowedInExpressions(name)) {
|
218 | math.expression.mathWithTransform[name] = math[name];
|
219 | } else {
|
220 | delete math.expression.mathWithTransform[name];
|
221 | }
|
222 | }
|
223 | |
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | function _wrap(fn) {
|
233 | var wrapper = function wrapper() {
|
234 | var args = [];
|
235 |
|
236 | for (var i = 0, len = arguments.length; i < len; i++) {
|
237 | var arg = arguments[i];
|
238 | args[i] = arg && arg.valueOf();
|
239 | }
|
240 |
|
241 | return fn.apply(math, args);
|
242 | };
|
243 |
|
244 | if (fn.transform) {
|
245 | wrapper.transform = fn.transform;
|
246 | }
|
247 |
|
248 | return wrapper;
|
249 | }
|
250 | |
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | function _importFactory(factory, options) {
|
260 | var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : factory.fn;
|
261 |
|
262 | if ((0, _array.contains)(name, '.')) {
|
263 | throw new Error('Factory name should not contain a nested path. ' + 'Name: ' + JSON.stringify(name));
|
264 | }
|
265 |
|
266 | var namespace = isTransformFunctionFactory(factory) ? math.expression.transform : math;
|
267 | var existingTransform = (name in math.expression.transform);
|
268 | var existing = (0, _object.hasOwnProperty)(namespace, name) ? namespace[name] : undefined;
|
269 |
|
270 | var resolver = function resolver() {
|
271 |
|
272 | var dependencies = {};
|
273 | factory.dependencies.map(_factory.stripOptionalNotation).forEach(function (dependency) {
|
274 | if ((0, _array.contains)(dependency, '.')) {
|
275 | throw new Error('Factory dependency should not contain a nested path. ' + 'Name: ' + JSON.stringify(dependency));
|
276 | }
|
277 |
|
278 | if (dependency === 'math') {
|
279 | dependencies.math = math;
|
280 | } else if (dependency === 'mathWithTransform') {
|
281 | dependencies.mathWithTransform = math.expression.mathWithTransform;
|
282 | } else if (dependency === 'classes') {
|
283 |
|
284 | dependencies.classes = math;
|
285 | } else {
|
286 | dependencies[dependency] = math[dependency];
|
287 | }
|
288 | });
|
289 | var instance = factory(dependencies);
|
290 |
|
291 | if (instance && typeof instance.transform === 'function') {
|
292 | throw new Error('Transforms cannot be attached to factory functions. ' + 'Please create a separate function for it with exports.path="expression.transform"');
|
293 | }
|
294 |
|
295 | if (existing === undefined || options.override) {
|
296 | return instance;
|
297 | }
|
298 |
|
299 | if (isTypedFunction(existing) && isTypedFunction(instance)) {
|
300 |
|
301 | return typed(existing, instance);
|
302 | }
|
303 |
|
304 | if (options.silent) {
|
305 |
|
306 | return existing;
|
307 | } else {
|
308 | throw new Error('Cannot import "' + name + '": already exists');
|
309 | }
|
310 | };
|
311 |
|
312 |
|
313 | if (!factory.meta || factory.meta.lazy !== false) {
|
314 | (0, _object.lazy)(namespace, name, resolver);
|
315 |
|
316 | if (existing && existingTransform) {
|
317 | _deleteTransform(name);
|
318 | } else {
|
319 | if (isTransformFunctionFactory(factory) || factoryAllowedInExpressions(factory)) {
|
320 | (0, _object.lazy)(math.expression.mathWithTransform, name, function () {
|
321 | return namespace[name];
|
322 | });
|
323 | }
|
324 | }
|
325 | } else {
|
326 | namespace[name] = resolver();
|
327 |
|
328 | if (existing && existingTransform) {
|
329 | _deleteTransform(name);
|
330 | } else {
|
331 | if (isTransformFunctionFactory(factory) || factoryAllowedInExpressions(factory)) {
|
332 | (0, _object.lazy)(math.expression.mathWithTransform, name, function () {
|
333 | return namespace[name];
|
334 | });
|
335 | }
|
336 | }
|
337 | }
|
338 |
|
339 |
|
340 | importedFactories[name] = factory;
|
341 | math.emit('import', name, resolver);
|
342 | }
|
343 | |
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 | function isSupportedType(object) {
|
352 | return typeof object === 'function' || typeof object === 'number' || typeof object === 'string' || typeof object === 'boolean' || object === null || (0, _is.isUnit)(object) || (0, _is.isComplex)(object) || (0, _is.isBigNumber)(object) || (0, _is.isFraction)(object) || (0, _is.isMatrix)(object) || Array.isArray(object);
|
353 | }
|
354 | |
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | function isTypedFunction(fn) {
|
362 | return typeof fn === 'function' && _typeof(fn.signatures) === 'object';
|
363 | }
|
364 |
|
365 | function hasTypedFunctionSignature(fn) {
|
366 | return typeof fn === 'function' && typeof fn.signature === 'string';
|
367 | }
|
368 |
|
369 | function allowedInExpressions(name) {
|
370 | return !(0, _object.hasOwnProperty)(unsafe, name);
|
371 | }
|
372 |
|
373 | function factoryAllowedInExpressions(factory) {
|
374 | return factory.fn.indexOf('.') === -1 &&
|
375 | !(0, _object.hasOwnProperty)(unsafe, factory.fn) && (!factory.meta || !factory.meta.isClass);
|
376 | }
|
377 |
|
378 | function isTransformFunctionFactory(factory) {
|
379 | return factory !== undefined && factory.meta !== undefined && factory.meta.isTransformFunction === true || false;
|
380 | }
|
381 |
|
382 |
|
383 | var unsafe = {
|
384 | expression: true,
|
385 | type: true,
|
386 | docs: true,
|
387 | error: true,
|
388 | json: true,
|
389 | chain: true
|
390 |
|
391 | };
|
392 | return mathImport;
|
393 | } |
\ | No newline at end of file |