1 | import _extends from "@babel/runtime/helpers/extends";
|
2 | import './../utils/polyfills.js';
|
3 | import { deepFlatten, isLegacyFactory, values } from '../utils/object.js';
|
4 | import * as emitter from './../utils/emitter.js';
|
5 | import { importFactory } from './function/import.js';
|
6 | import { configFactory } from './function/config.js';
|
7 | import { factory, isFactory } from '../utils/factory.js';
|
8 | import { isAccessorNode, isArray, isArrayNode, isAssignmentNode, isBigNumber, isBlockNode, isBoolean, isChain, isCollection, isComplex, isConditionalNode, isConstantNode, isDate, isDenseMatrix, isFraction, isFunction, isFunctionAssignmentNode, isFunctionNode, isHelp, isIndex, isIndexNode, isMatrix, isNode, isNull, isNumber, isObject, isObjectNode, isOperatorNode, isParenthesisNode, isRange, isRangeNode, isRegExp, isResultSet, isSparseMatrix, isString, isSymbolNode, isUndefined, isUnit } from '../utils/is.js';
|
9 | import { ArgumentsError } from '../error/ArgumentsError.js';
|
10 | import { DimensionError } from '../error/DimensionError.js';
|
11 | import { IndexError } from '../error/IndexError.js';
|
12 | import { DEFAULT_CONFIG } from './config.js';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
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 | export function create(factories, config) {
|
54 | var configInternal = _extends({}, DEFAULT_CONFIG, config);
|
55 |
|
56 |
|
57 | if (typeof Object.create !== 'function') {
|
58 | throw new Error('ES5 not supported by this JavaScript engine. ' + 'Please load the es5-shim and es5-sham library for compatibility.');
|
59 | }
|
60 |
|
61 |
|
62 | var math = emitter.mixin({
|
63 |
|
64 | isNumber,
|
65 | isComplex,
|
66 | isBigNumber,
|
67 | isFraction,
|
68 | isUnit,
|
69 | isString,
|
70 | isArray,
|
71 | isMatrix,
|
72 | isCollection,
|
73 | isDenseMatrix,
|
74 | isSparseMatrix,
|
75 | isRange,
|
76 | isIndex,
|
77 | isBoolean,
|
78 | isResultSet,
|
79 | isHelp,
|
80 | isFunction,
|
81 | isDate,
|
82 | isRegExp,
|
83 | isObject,
|
84 | isNull,
|
85 | isUndefined,
|
86 | isAccessorNode,
|
87 | isArrayNode,
|
88 | isAssignmentNode,
|
89 | isBlockNode,
|
90 | isConditionalNode,
|
91 | isConstantNode,
|
92 | isFunctionAssignmentNode,
|
93 | isFunctionNode,
|
94 | isIndexNode,
|
95 | isNode,
|
96 | isObjectNode,
|
97 | isOperatorNode,
|
98 | isParenthesisNode,
|
99 | isRangeNode,
|
100 | isSymbolNode,
|
101 | isChain
|
102 | });
|
103 |
|
104 | math.config = configFactory(configInternal, math.emit);
|
105 | math.expression = {
|
106 | transform: {},
|
107 | mathWithTransform: {
|
108 | config: math.config
|
109 | }
|
110 | };
|
111 |
|
112 | var legacyFactories = [];
|
113 | var legacyInstances = [];
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | function load(factory) {
|
123 | if (isFactory(factory)) {
|
124 | return factory(math);
|
125 | }
|
126 |
|
127 | var firstProperty = factory[Object.keys(factory)[0]];
|
128 |
|
129 | if (isFactory(firstProperty)) {
|
130 | return firstProperty(math);
|
131 | }
|
132 |
|
133 | if (!isLegacyFactory(factory)) {
|
134 | console.warn('Factory object with properties `type`, `name`, and `factory` expected', factory);
|
135 | throw new Error('Factory object with properties `type`, `name`, and `factory` expected');
|
136 | }
|
137 |
|
138 | var index = legacyFactories.indexOf(factory);
|
139 | var instance;
|
140 |
|
141 | if (index === -1) {
|
142 |
|
143 | if (factory.math === true) {
|
144 |
|
145 | instance = factory.factory(math.type, configInternal, load, math.typed, math);
|
146 | } else {
|
147 | instance = factory.factory(math.type, configInternal, load, math.typed);
|
148 | }
|
149 |
|
150 |
|
151 | legacyFactories.push(factory);
|
152 | legacyInstances.push(instance);
|
153 | } else {
|
154 |
|
155 | instance = legacyInstances[index];
|
156 | }
|
157 |
|
158 | return instance;
|
159 | }
|
160 |
|
161 | var importedFactories = {};
|
162 |
|
163 | function lazyTyped() {
|
164 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
165 | args[_key] = arguments[_key];
|
166 | }
|
167 |
|
168 | return math.typed.apply(math.typed, args);
|
169 | }
|
170 |
|
171 | var internalImport = importFactory(lazyTyped, load, math, importedFactories);
|
172 | math.import = internalImport;
|
173 |
|
174 |
|
175 | math.on('config', () => {
|
176 | values(importedFactories).forEach(factory => {
|
177 | if (factory && factory.meta && factory.meta.recreateOnConfigChange) {
|
178 |
|
179 |
|
180 | internalImport(factory, {
|
181 | override: true
|
182 | });
|
183 | }
|
184 | });
|
185 | });
|
186 |
|
187 |
|
188 | math.create = create.bind(null, factories);
|
189 |
|
190 | math.factory = factory;
|
191 |
|
192 |
|
193 | math.import(values(deepFlatten(factories)));
|
194 | math.ArgumentsError = ArgumentsError;
|
195 | math.DimensionError = DimensionError;
|
196 | math.IndexError = IndexError;
|
197 | return math;
|
198 | } |
\ | No newline at end of file |