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