forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
122 lines (106 loc) · 4.21 KB
/
Copy pathconfig.js
File metadata and controls
122 lines (106 loc) · 4.21 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict'
const object = require('../../utils/object')
function factory (type, config, load, typed, math) {
const MATRIX = ['Matrix', 'Array'] // valid values for option matrix
const NUMBER = ['number', 'BigNumber', 'Fraction'] // valid values for option number
/**
* Set configuration options for math.js, and get current options.
* Will emit a 'config' event, with arguments (curr, prev, changes).
*
* Syntax:
*
* math.config(config: Object): Object
*
* Examples:
*
* math.config().number // outputs 'number'
* math.eval('0.4') // outputs number 0.4
* math.config({number: 'Fraction'})
* math.eval('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 = object.map(config, object.clone)
// validate some of the options
validateOption(options, 'matrix', MATRIX)
validateOption(options, 'number', NUMBER)
// merge options
object.deepExtend(config, options)
const curr = object.map(config, object.clone)
const changes = object.map(options, object.clone)
// emit 'config' event
math.emit('config', curr, prev, changes)
return curr
} else {
return object.map(config, object.clone)
}
}
// attach the valid options to the function so they can be extended
_config.MATRIX = MATRIX
_config.NUMBER = NUMBER
return _config
}
/**
* Test whether an Array contains a specific item.
* @param {Array.<string>} array
* @param {string} item
* @return {boolean}
*/
function contains (array, item) {
return array.indexOf(item) !== -1
}
/**
* Find a string in an array. Case insensitive search
* @param {Array.<string>} array
* @param {string} item
* @return {number} Returns the index when found. Returns -1 when not found
*/
function findIndex (array, item) {
return array
.map(function (i) {
return i.toLowerCase()
})
.indexOf(item.toLowerCase())
}
/**
* 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 && !contains(values, options[name])) {
const index = findIndex(values, options[name])
if (index !== -1) {
// right value, wrong casing
// TODO: lower case values are deprecated since v3, remove this warning some day.
console.warn('Warning: Wrong casing for configuration option "' + name + '", should be "' + values[index] + '" instead of "' + options[name] + '".')
options[name] = values[index] // change the option to the right casing
} else {
// unknown value
console.warn('Warning: Unknown value "' + options[name] + '" for configuration option "' + name + '". Available options: ' + values.map(JSON.stringify).join(', ') + '.')
}
}
}
exports.name = 'config'
exports.math = true // request the math namespace as fifth argument
exports.factory = factory