UNPKG

2.01 kBJavaScriptView Raw
1'use strict';
2
3function factory(type, config, load, typed) {
4 var getTypeOf = load(require('../function/utils/typeof'));
5 var validInputTypes = {
6 'string': true,
7 'number': true,
8 'BigNumber': true,
9 'Fraction': true // Load the conversion functions for each output type
10
11 };
12 var validOutputTypes = {
13 'number': load(require('./number')),
14 'BigNumber': load(require('./bignumber/function/bignumber')),
15 'Fraction': load(require('./fraction/function/fraction'))
16 /**
17 * Convert a numeric value to a specific type: number, BigNumber, or Fraction
18 *
19 * @param {string | number | BigNumber | Fraction } value
20 * @param {'number' | 'BigNumber' | 'Fraction'} outputType
21 * @return {number | BigNumber | Fraction} Returns an instance of the
22 * numeric in the requested type
23 */
24
25 };
26
27 var numeric = function numeric(value, outputType) {
28 var inputType = getTypeOf(value);
29
30 if (!(inputType in validInputTypes)) {
31 throw new TypeError('Cannot convert ' + value + ' of type "' + inputType + '"; valid input types are ' + Object.keys(validInputTypes).join(', '));
32 }
33
34 if (!(outputType in validOutputTypes)) {
35 throw new TypeError('Cannot convert ' + value + ' to type "' + outputType + '"; valid output types are ' + Object.keys(validOutputTypes).join(', '));
36 }
37
38 if (outputType === inputType) {
39 return value;
40 } else {
41 return validOutputTypes[outputType](value);
42 }
43 };
44
45 numeric.toTex = function (node, options) {
46 // Not sure if this is strictly right but should work correctly for the vast majority of use cases.
47 return node.args[0].toTex();
48 };
49
50 return numeric;
51} // FIXME: expose numeric in the math namespace after we've decided on a name and have written proper docs for this function. See https://github.com/josdejong/mathjs/pull/1270
52// exports.name = 'type._numeric'
53
54
55exports.path = 'type';
56exports.name = '_numeric';
57exports.factory = factory;
\No newline at end of file