1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
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 | import {
|
40 | isAccessorNode,
|
41 | isArray,
|
42 | isArrayNode,
|
43 | isAssignmentNode,
|
44 | isBigNumber,
|
45 | isBlockNode,
|
46 | isBoolean,
|
47 | isChain,
|
48 | isComplex,
|
49 | isConditionalNode,
|
50 | isConstantNode,
|
51 | isDate,
|
52 | isDenseMatrix,
|
53 | isFraction,
|
54 | isFunction,
|
55 | isFunctionAssignmentNode,
|
56 | isFunctionNode,
|
57 | isHelp,
|
58 | isIndex,
|
59 | isIndexNode,
|
60 | isMatrix,
|
61 | isNode,
|
62 | isNull,
|
63 | isNumber,
|
64 | isObject,
|
65 | isObjectNode,
|
66 | isOperatorNode,
|
67 | isParenthesisNode,
|
68 | isRange,
|
69 | isRangeNode,
|
70 | isRegExp,
|
71 | isResultSet,
|
72 | isSparseMatrix,
|
73 | isString,
|
74 | isSymbolNode,
|
75 | isUndefined,
|
76 | isUnit
|
77 | } from '../../utils/is'
|
78 | import typedFunction from 'typed-function'
|
79 | import { digits } from '../../utils/number'
|
80 | import { factory } from '../../utils/factory'
|
81 |
|
82 |
|
83 | let _createTyped = function () {
|
84 |
|
85 |
|
86 | _createTyped = typedFunction.create
|
87 | return typedFunction
|
88 | }
|
89 |
|
90 | const dependencies = [
|
91 | '?BigNumber',
|
92 | '?Complex',
|
93 | '?DenseMatrix',
|
94 | '?Fraction'
|
95 | ]
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | export const createTyped = factory('typed', dependencies, function createTyped ({ BigNumber, Complex, DenseMatrix, Fraction }) {
|
103 |
|
104 |
|
105 |
|
106 | const typed = _createTyped()
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | typed.types = [
|
112 | { name: 'number', test: isNumber },
|
113 | { name: 'Complex', test: isComplex },
|
114 | { name: 'BigNumber', test: isBigNumber },
|
115 | { name: 'Fraction', test: isFraction },
|
116 | { name: 'Unit', test: isUnit },
|
117 | { name: 'string', test: isString },
|
118 | { name: 'Chain', test: isChain },
|
119 | { name: 'Array', test: isArray },
|
120 | { name: 'Matrix', test: isMatrix },
|
121 | { name: 'DenseMatrix', test: isDenseMatrix },
|
122 | { name: 'SparseMatrix', test: isSparseMatrix },
|
123 | { name: 'Range', test: isRange },
|
124 | { name: 'Index', test: isIndex },
|
125 | { name: 'boolean', test: isBoolean },
|
126 | { name: 'ResultSet', test: isResultSet },
|
127 | { name: 'Help', test: isHelp },
|
128 | { name: 'function', test: isFunction },
|
129 | { name: 'Date', test: isDate },
|
130 | { name: 'RegExp', test: isRegExp },
|
131 | { name: 'null', test: isNull },
|
132 | { name: 'undefined', test: isUndefined },
|
133 |
|
134 | { name: 'AccessorNode', test: isAccessorNode },
|
135 | { name: 'ArrayNode', test: isArrayNode },
|
136 | { name: 'AssignmentNode', test: isAssignmentNode },
|
137 | { name: 'BlockNode', test: isBlockNode },
|
138 | { name: 'ConditionalNode', test: isConditionalNode },
|
139 | { name: 'ConstantNode', test: isConstantNode },
|
140 | { name: 'FunctionNode', test: isFunctionNode },
|
141 | { name: 'FunctionAssignmentNode', test: isFunctionAssignmentNode },
|
142 | { name: 'IndexNode', test: isIndexNode },
|
143 | { name: 'Node', test: isNode },
|
144 | { name: 'ObjectNode', test: isObjectNode },
|
145 | { name: 'OperatorNode', test: isOperatorNode },
|
146 | { name: 'ParenthesisNode', test: isParenthesisNode },
|
147 | { name: 'RangeNode', test: isRangeNode },
|
148 | { name: 'SymbolNode', test: isSymbolNode },
|
149 |
|
150 | { name: 'Object', test: isObject }
|
151 | ]
|
152 |
|
153 | typed.conversions = [
|
154 | {
|
155 | from: 'number',
|
156 | to: 'BigNumber',
|
157 | convert: function (x) {
|
158 | if (!BigNumber) {
|
159 | throwNoBignumber(x)
|
160 | }
|
161 |
|
162 |
|
163 | if (digits(x) > 15) {
|
164 | throw new TypeError('Cannot implicitly convert a number with >15 significant digits to BigNumber ' +
|
165 | '(value: ' + x + '). ' +
|
166 | 'Use function bignumber(x) to convert to BigNumber.')
|
167 | }
|
168 | return new BigNumber(x)
|
169 | }
|
170 | }, {
|
171 | from: 'number',
|
172 | to: 'Complex',
|
173 | convert: function (x) {
|
174 | if (!Complex) {
|
175 | throwNoComplex(x)
|
176 | }
|
177 |
|
178 | return new Complex(x, 0)
|
179 | }
|
180 | }, {
|
181 | from: 'number',
|
182 | to: 'string',
|
183 | convert: function (x) {
|
184 | return x + ''
|
185 | }
|
186 | }, {
|
187 | from: 'BigNumber',
|
188 | to: 'Complex',
|
189 | convert: function (x) {
|
190 | if (!Complex) {
|
191 | throwNoComplex(x)
|
192 | }
|
193 |
|
194 | return new Complex(x.toNumber(), 0)
|
195 | }
|
196 | }, {
|
197 | from: 'Fraction',
|
198 | to: 'BigNumber',
|
199 | convert: function (x) {
|
200 | throw new TypeError('Cannot implicitly convert a Fraction to BigNumber or vice versa. ' +
|
201 | 'Use function bignumber(x) to convert to BigNumber or fraction(x) to convert to Fraction.')
|
202 | }
|
203 | }, {
|
204 | from: 'Fraction',
|
205 | to: 'Complex',
|
206 | convert: function (x) {
|
207 | if (!Complex) {
|
208 | throwNoComplex(x)
|
209 | }
|
210 |
|
211 | return new Complex(x.valueOf(), 0)
|
212 | }
|
213 | }, {
|
214 | from: 'number',
|
215 | to: 'Fraction',
|
216 | convert: function (x) {
|
217 | if (!Fraction) {
|
218 | throwNoFraction(x)
|
219 | }
|
220 |
|
221 | const f = new Fraction(x)
|
222 | if (f.valueOf() !== x) {
|
223 | throw new TypeError('Cannot implicitly convert a number to a Fraction when there will be a loss of precision ' +
|
224 | '(value: ' + x + '). ' +
|
225 | 'Use function fraction(x) to convert to Fraction.')
|
226 | }
|
227 | return f
|
228 | }
|
229 | }, {
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | from: 'string',
|
238 | to: 'number',
|
239 | convert: function (x) {
|
240 | const n = Number(x)
|
241 | if (isNaN(n)) {
|
242 | throw new Error('Cannot convert "' + x + '" to a number')
|
243 | }
|
244 | return n
|
245 | }
|
246 | }, {
|
247 | from: 'string',
|
248 | to: 'BigNumber',
|
249 | convert: function (x) {
|
250 | if (!BigNumber) {
|
251 | throwNoBignumber(x)
|
252 | }
|
253 |
|
254 | try {
|
255 | return new BigNumber(x)
|
256 | } catch (err) {
|
257 | throw new Error('Cannot convert "' + x + '" to BigNumber')
|
258 | }
|
259 | }
|
260 | }, {
|
261 | from: 'string',
|
262 | to: 'Fraction',
|
263 | convert: function (x) {
|
264 | if (!Fraction) {
|
265 | throwNoFraction(x)
|
266 | }
|
267 |
|
268 | try {
|
269 | return new Fraction(x)
|
270 | } catch (err) {
|
271 | throw new Error('Cannot convert "' + x + '" to Fraction')
|
272 | }
|
273 | }
|
274 | }, {
|
275 | from: 'string',
|
276 | to: 'Complex',
|
277 | convert: function (x) {
|
278 | if (!Complex) {
|
279 | throwNoComplex(x)
|
280 | }
|
281 |
|
282 | try {
|
283 | return new Complex(x)
|
284 | } catch (err) {
|
285 | throw new Error('Cannot convert "' + x + '" to Complex')
|
286 | }
|
287 | }
|
288 | }, {
|
289 | from: 'boolean',
|
290 | to: 'number',
|
291 | convert: function (x) {
|
292 | return +x
|
293 | }
|
294 | }, {
|
295 | from: 'boolean',
|
296 | to: 'BigNumber',
|
297 | convert: function (x) {
|
298 | if (!BigNumber) {
|
299 | throwNoBignumber(x)
|
300 | }
|
301 |
|
302 | return new BigNumber(+x)
|
303 | }
|
304 | }, {
|
305 | from: 'boolean',
|
306 | to: 'Fraction',
|
307 | convert: function (x) {
|
308 | if (!Fraction) {
|
309 | throwNoFraction(x)
|
310 | }
|
311 |
|
312 | return new Fraction(+x)
|
313 | }
|
314 | }, {
|
315 | from: 'boolean',
|
316 | to: 'string',
|
317 | convert: function (x) {
|
318 | return String(x)
|
319 | }
|
320 | }, {
|
321 | from: 'Array',
|
322 | to: 'Matrix',
|
323 | convert: function (array) {
|
324 | if (!DenseMatrix) {
|
325 | throwNoMatrix()
|
326 | }
|
327 |
|
328 | return new DenseMatrix(array)
|
329 | }
|
330 | }, {
|
331 | from: 'Matrix',
|
332 | to: 'Array',
|
333 | convert: function (matrix) {
|
334 | return matrix.valueOf()
|
335 | }
|
336 | }
|
337 | ]
|
338 |
|
339 | return typed
|
340 | })
|
341 |
|
342 | function throwNoBignumber (x) {
|
343 | throw new Error(`Cannot convert value ${x} into a BigNumber: no class 'BigNumber' provided`)
|
344 | }
|
345 |
|
346 | function throwNoComplex (x) {
|
347 | throw new Error(`Cannot convert value ${x} into a Complex number: no class 'Complex' provided`)
|
348 | }
|
349 |
|
350 | function throwNoMatrix () {
|
351 | throw new Error('Cannot convert array into a Matrix: no class \'DenseMatrix\' provided')
|
352 | }
|
353 |
|
354 | function throwNoFraction (x) {
|
355 | throw new Error(`Cannot convert value ${x} into a Fraction, no class 'Fraction' provided.`)
|
356 | }
|