-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
config.js
102 lines (90 loc) · 3.69 KB
/
config.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { clone, mapObject, deepExtend } from '../../utils/object.js'
import { DEFAULT_CONFIG } from '../config.js'
export const MATRIX_OPTIONS = ['Matrix', 'Array'] // valid values for option matrix
export const NUMBER_OPTIONS = ['number', 'BigNumber', 'Fraction'] // valid values for option number
export function configFactory (config, emit) {
/**
* Set configuration options for math.js, and get current options.
* Will emit a 'config' event, with arguments (curr, prev, changes).
*
* This function is only available on a mathjs instance created using `create`.
*
* Syntax:
*
* math.config(config: Object): Object
*
* Examples:
*
*
* import { create, all } from 'mathjs'
*
* // create a mathjs instance
* const math = create(all)
*
* math.config().number // outputs 'number'
* math.evaluate('0.4') // outputs number 0.4
* math.config({number: 'Fraction'})
* math.evaluate('0.4') // outputs Fraction 2/5
*
* @param {Object} [options] Available options:
* {number} epsilon
* Minimum relative difference between two
* compared values, used by all comparison functions.
* {string} matrix
* A string 'Matrix' (default) or 'Array'.
* {string} number
* A string 'number' (default), 'BigNumber', or 'Fraction'
* {number} precision
* The number of significant digits for BigNumbers.
* Not applicable for Numbers.
* {string} parenthesis
* How to display parentheses in LaTeX and string
* output.
* {string} randomSeed
* Random seed for seeded pseudo random number generator.
* Set to null to randomly seed.
* @return {Object} Returns the current configuration
*/
function _config (options) {
if (options) {
const prev = mapObject(config, clone)
// validate some of the options
validateOption(options, 'matrix', MATRIX_OPTIONS)
validateOption(options, 'number', NUMBER_OPTIONS)
// merge options
deepExtend(config, options)
const curr = mapObject(config, clone)
const changes = mapObject(options, clone)
// emit 'config' event
emit('config', curr, prev, changes)
return curr
} else {
return mapObject(config, clone)
}
}
// attach the valid options to the function so they can be extended
_config.MATRIX_OPTIONS = MATRIX_OPTIONS
_config.NUMBER_OPTIONS = NUMBER_OPTIONS
// attach the config properties as readonly properties to the config function
Object.keys(DEFAULT_CONFIG).forEach(key => {
Object.defineProperty(_config, key, {
get: () => config[key],
enumerable: true,
configurable: true
})
})
return _config
}
/**
* Validate an option
* @param {Object} options Object with options
* @param {string} name Name of the option to validate
* @param {Array.<string>} values Array with valid values for this option
*/
function validateOption (options, name, values) {
if (options[name] !== undefined && !values.includes(options[name])) {
// unknown value
console.warn('Warning: Unknown value "' + options[name] + '" for configuration option "' + name + '". ' +
'Available options: ' + values.map(value => JSON.stringify(value)).join(', ') + '.')
}
}