simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

config.js (3960B)


      1 import { clone, mapObject, deepExtend } from '../../utils/object.js';
      2 import { DEFAULT_CONFIG } from '../config.js';
      3 export var MATRIX_OPTIONS = ['Matrix', 'Array']; // valid values for option matrix
      4 
      5 export var NUMBER_OPTIONS = ['number', 'BigNumber', 'Fraction']; // valid values for option number
      6 
      7 export 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(key => {
     73     Object.defineProperty(_config, key, {
     74       get: () => config[key],
     75       enumerable: true,
     76       configurable: true
     77     });
     78   });
     79   return _config;
     80 }
     81 /**
     82  * Test whether an Array contains a specific item.
     83  * @param {Array.<string>} array
     84  * @param {string} item
     85  * @return {boolean}
     86  */
     87 
     88 function contains(array, item) {
     89   return array.indexOf(item) !== -1;
     90 }
     91 /**
     92  * Validate an option
     93  * @param {Object} options         Object with options
     94  * @param {string} name            Name of the option to validate
     95  * @param {Array.<string>} values  Array with valid values for this option
     96  */
     97 
     98 
     99 function validateOption(options, name, values) {
    100   if (options[name] !== undefined && !contains(values, options[name])) {
    101     // unknown value
    102     console.warn('Warning: Unknown value "' + options[name] + '" for configuration option "' + name + '". ' + 'Available options: ' + values.map(value => JSON.stringify(value)).join(', ') + '.');
    103   }
    104 }