1 | ;
|
2 |
|
3 | var object = require('../../utils/object');
|
4 |
|
5 | function factory(type, config, load, typed, math) {
|
6 | var MATRIX = ['Matrix', 'Array']; // valid values for option matrix
|
7 |
|
8 | var NUMBER = ['number', 'BigNumber', 'Fraction']; // valid values for option number
|
9 |
|
10 | /**
|
11 | * Set configuration options for math.js, and get current options.
|
12 | * Will emit a 'config' event, with arguments (curr, prev, changes).
|
13 | *
|
14 | * Syntax:
|
15 | *
|
16 | * math.config(config: Object): Object
|
17 | *
|
18 | * Examples:
|
19 | *
|
20 | * math.config().number // outputs 'number'
|
21 | * math.eval('0.4') // outputs number 0.4
|
22 | * math.config({number: 'Fraction'})
|
23 | * math.eval('0.4') // outputs Fraction 2/5
|
24 | *
|
25 | * @param {Object} [options] Available options:
|
26 | * {number} epsilon
|
27 | * Minimum relative difference between two
|
28 | * compared values, used by all comparison functions.
|
29 | * {string} matrix
|
30 | * A string 'Matrix' (default) or 'Array'.
|
31 | * {string} number
|
32 | * A string 'number' (default), 'BigNumber', or 'Fraction'
|
33 | * {number} precision
|
34 | * The number of significant digits for BigNumbers.
|
35 | * Not applicable for Numbers.
|
36 | * {string} parenthesis
|
37 | * How to display parentheses in LaTeX and string
|
38 | * output.
|
39 | * {string} randomSeed
|
40 | * Random seed for seeded pseudo random number generator.
|
41 | * Set to null to randomly seed.
|
42 | * @return {Object} Returns the current configuration
|
43 | */
|
44 |
|
45 | function _config(options) {
|
46 | if (options) {
|
47 | var prev = object.map(config, object.clone); // validate some of the options
|
48 |
|
49 | validateOption(options, 'matrix', MATRIX);
|
50 | validateOption(options, 'number', NUMBER); // merge options
|
51 |
|
52 | object.deepExtend(config, options);
|
53 | var curr = object.map(config, object.clone);
|
54 | var changes = object.map(options, object.clone); // emit 'config' event
|
55 |
|
56 | math.emit('config', curr, prev, changes);
|
57 | return curr;
|
58 | } else {
|
59 | return object.map(config, object.clone);
|
60 | }
|
61 | } // attach the valid options to the function so they can be extended
|
62 |
|
63 |
|
64 | _config.MATRIX = MATRIX;
|
65 | _config.NUMBER = NUMBER;
|
66 | return _config;
|
67 | }
|
68 | /**
|
69 | * Test whether an Array contains a specific item.
|
70 | * @param {Array.<string>} array
|
71 | * @param {string} item
|
72 | * @return {boolean}
|
73 | */
|
74 |
|
75 |
|
76 | function contains(array, item) {
|
77 | return array.indexOf(item) !== -1;
|
78 | }
|
79 | /**
|
80 | * Find a string in an array. Case insensitive search
|
81 | * @param {Array.<string>} array
|
82 | * @param {string} item
|
83 | * @return {number} Returns the index when found. Returns -1 when not found
|
84 | */
|
85 |
|
86 |
|
87 | function findIndex(array, item) {
|
88 | return array.map(function (i) {
|
89 | return i.toLowerCase();
|
90 | }).indexOf(item.toLowerCase());
|
91 | }
|
92 | /**
|
93 | * Validate an option
|
94 | * @param {Object} options Object with options
|
95 | * @param {string} name Name of the option to validate
|
96 | * @param {Array.<string>} values Array with valid values for this option
|
97 | */
|
98 |
|
99 |
|
100 | function validateOption(options, name, values) {
|
101 | if (options[name] !== undefined && !contains(values, options[name])) {
|
102 | var index = findIndex(values, options[name]);
|
103 |
|
104 | if (index !== -1) {
|
105 | // right value, wrong casing
|
106 | // TODO: lower case values are deprecated since v3, remove this warning some day.
|
107 | console.warn('Warning: Wrong casing for configuration option "' + name + '", should be "' + values[index] + '" instead of "' + options[name] + '".');
|
108 | options[name] = values[index]; // change the option to the right casing
|
109 | } else {
|
110 | // unknown value
|
111 | console.warn('Warning: Unknown value "' + options[name] + '" for configuration option "' + name + '". Available options: ' + values.map(JSON.stringify).join(', ') + '.');
|
112 | }
|
113 | }
|
114 | }
|
115 |
|
116 | exports.name = 'config';
|
117 | exports.math = true; // request the math namespace as fifth argument
|
118 |
|
119 | exports.factory = factory; |
\ | No newline at end of file |