UNPKG

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