Permalink
Browse files

Moved related functions to `./lib/core`

  • Loading branch information...
1 parent 7b4f83b commit 052c6109afc1d0406324b4eede906c875b8ed00f @josdejong committed May 5, 2015
View
@@ -1,106 +1 @@
-var isFactory = require('./lib/util/object').isFactory;
-var deepExtend = require('./lib/util/object').deepExtend;
-var typedFactory = require('./lib/util/typed');
-var emitter = require('./lib/util/emitter');
-
-/**
- * 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.
- * @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
- };
-
- 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(require('./lib/function/utils/import'));
- math['config'] = load(require('./lib/function/utils/config'));
-
- return math;
-};
+module.exports = require('./lib/core/core');
@@ -1,6 +1,6 @@
'use strict';
-var object = require('../../util/object');
+var object = require('../util/object');
function factory (type, config, load, typed, math) {
/**
View
@@ -0,0 +1,109 @@
+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.
+ * @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
+ };
+
+ 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;
+};
@@ -1,7 +1,7 @@
'use strict';
-var isFactory = require('../../util/object').isFactory;
-var traverse = require('../../util/object').traverse;
+var isFactory = require('../util/object').isFactory;
+var traverse = require('../util/object').traverse;
function factory (type, config, load, typed, math) {
/**
@@ -1,9 +1,7 @@
module.exports = [
require('./clone'),
- // note: config is already loaded by core.js
require('./filter'),
require('./format'),
- // note: import is already loaded by core.js
require('./map'),
require('./print'),
require('./sort'),
@@ -1,5 +1,5 @@
var assert = require('assert');
-var math = require('../../../index');
+var math = require('../../index');
describe('config', function() {
// TODO: test function config
@@ -1,8 +1,8 @@
// test import
var assert = require('assert'),
- error = require('../../../lib/error/index'),
- mathjs = require('../../../index'),
- approx = require('../../../tools/approx');
+ error = require('../../lib/error/index'),
+ mathjs = require('../../index'),
+ approx = require('../../tools/approx');
describe('import', function() {
var math = null;
File renamed without changes.

0 comments on commit 052c610

Please sign in to comment.