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