mathjs/lib/core/core.js
Max Bruckner 5b8bb06804 New configuration option 'parenthesis'
This option can be one of 'keep', 'auto' or 'all'
2015-05-07 10:12:41 +02:00

117 lines
3.7 KiB
JavaScript

var isFactory = require('./../util/object').isFactory;
var deepExtend = require('./../util/object').deepExtend;
var typedFactory = require('./../util/typed');
var emitter = require('./../util/emitter');
var importFactory = require('./import');
var configFactory = require('./config');
/**
* Math.js core. Creates a new, empty math.js instance
* @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) or 'bignumber'
* {number} precision
* The number of significant digits for BigNumbers.
* Not applicable for Numbers.
* {string} parenthesis
* How to display parentheses in LaTeX and String
* output.
* @returns {Object} Returns a bare-bone math.js instance containing
* functions:
* - `import` to add new functions
* - `config` to change configuration
* - `on`, `off`, `once`, `emit` for events
*/
exports.create = function create (options) {
// simple test for ES5 support
if (typeof Object.create !== 'function') {
throw new Error('ES5 not supported by this JavaScript engine. ' +
'Please load the es5-shim and es5-sham library for compatibility.');
}
// cached factories and instances
var factories = [];
var instances = [];
// create a namespace for the mathjs instance, and attach emitter functions
var math = emitter.mixin({
type: {}
});
// create a new typed instance
var typed = typedFactory.create(math);
// create configuration options. These are private
var _config = {
// type of default matrix output. Choose 'matrix' (default) or 'array'
matrix: 'matrix',
// type of default number output. Choose 'number' (default) or 'bignumber'
number: 'number',
// number of significant digits in BigNumbers
precision: 64,
// minimum relative difference between two compared values,
// used by all comparison functions
epsilon: 1e-14,
// which type of parenthesis to use for toString and toTex
// one of 'keep', 'auto', and 'all'
parenthesis: 'keep'
};
if (options) {
// merge options
deepExtend(_config, options);
}
/**
* Load a function or data type from a factory.
* If the function or data type already exists, the existing instance is
* returned.
* @param {{type: string, name: string, factory: function}} factory
* @returns {*}
*/
function load (factory) {
if (!isFactory(factory)) {
throw new Error('Factory object with properties `type`, `name`, and `factory` expected');
}
var index = factories.indexOf(factory);
var instance;
if (index === -1) {
// doesn't yet exist
if (factory.math) {
// pass with math namespace
instance = factory.factory(math.type, _config, load, typed, math);
}
else {
instance = factory.factory(math.type, _config, load, typed);
}
// append to the cache
factories.push(factory);
instances.push(instance);
}
else {
// already existing function, return the cached instance
instance = instances[index];
}
return instance;
}
// load the import and config functions
math['import'] = load(importFactory);
math['config'] = load(configFactory);
return math;
};