mirror of
https://github.com/josdejong/mathjs.git
synced 2026-02-01 16:07:46 +00:00
Updated to typed-function v0.8, creating a new instance of typed-function for each instance of math.js. Better error messages and new utility functions typed.convert and typed.find.
This commit is contained in:
parent
ba88236dc4
commit
868b2702b5
@ -1,6 +1,6 @@
|
||||
var isFactory = require('./../util/object').isFactory;
|
||||
var deepExtend = require('./../util/object').deepExtend;
|
||||
var typedFactory = require('./../util/typed');
|
||||
var typedFactory = require('./typed');
|
||||
var emitter = require('./../util/emitter');
|
||||
|
||||
var importFactory = require('./import');
|
||||
@ -42,7 +42,7 @@ exports.create = function create (options) {
|
||||
});
|
||||
|
||||
// create a new typed instance
|
||||
var typed = typedFactory.create(math);
|
||||
var typed = typedFactory.create(math.type);
|
||||
|
||||
// create configuration options. These are private
|
||||
var _config = {
|
||||
|
||||
@ -1,15 +1,25 @@
|
||||
var typed = require('typed-function');
|
||||
var digits = require('./number').digits;
|
||||
var typedFunction = require('typed-function');
|
||||
var digits = require('./../util/number').digits;
|
||||
|
||||
// returns a new instance of typed-function
|
||||
var createTyped = function () {
|
||||
// initially, return the original instance of typed-function
|
||||
// consecutively, return a new instance from typed.create.
|
||||
createTyped = typedFunction.create;
|
||||
return typedFunction;
|
||||
};
|
||||
|
||||
/**
|
||||
* Factory function for creating a new typed instance
|
||||
* @param {Object} math
|
||||
* @param {Object} type Object with data types like Complex and BigNumber
|
||||
* @returns {function}
|
||||
*/
|
||||
exports.create = function create(math) {
|
||||
// TODO: must create a separate instance of typed, with custom config.
|
||||
exports.create = function create(type) {
|
||||
// TODO: typed-function must be able to silently ignore signatures with unknown data types
|
||||
|
||||
// get a new instance of typed-function
|
||||
var typed = createTyped();
|
||||
|
||||
// configure typed functions
|
||||
typed.types['Complex'] = function (x) { return x && x.isComplex; };
|
||||
typed.types['Range'] = function (x) { return x && x.isRange; };
|
||||
@ -21,7 +31,8 @@ exports.create = function create(math) {
|
||||
typed.types['Help'] = function (x) { return x && x.isHelp; };
|
||||
typed.types['ResultSet'] = function (x) { return x && x.isResultSet; };
|
||||
typed.types['BigNumber'] = function (x) { return x && x.isBigNumber; };
|
||||
|
||||
|
||||
// TODO: add conversion from BigNumber to number?
|
||||
typed.conversions = [
|
||||
{
|
||||
from: 'number',
|
||||
@ -33,13 +44,13 @@ exports.create = function create(math) {
|
||||
'(value: ' + x + '). ' +
|
||||
'Use function bignumber(x) to convert to BigNumber.');
|
||||
}
|
||||
return new math.type.BigNumber(x);
|
||||
return new type.BigNumber(x);
|
||||
}
|
||||
}, {
|
||||
from: 'number',
|
||||
to: 'Complex',
|
||||
convert: function (x) {
|
||||
return new math.type.Complex(x, 0);
|
||||
return new type.Complex(x, 0);
|
||||
}
|
||||
}, {
|
||||
from: 'number',
|
||||
@ -51,7 +62,7 @@ exports.create = function create(math) {
|
||||
from: 'BigNumber',
|
||||
to: 'Complex',
|
||||
convert: function (x) {
|
||||
return new math.type.Complex(x.toNumber(), 0);
|
||||
return new type.Complex(x.toNumber(), 0);
|
||||
}
|
||||
}, {
|
||||
from: 'boolean',
|
||||
@ -63,7 +74,7 @@ exports.create = function create(math) {
|
||||
from: 'boolean',
|
||||
to: 'BigNumber',
|
||||
convert: function (x) {
|
||||
return new math.type.BigNumber(+x);
|
||||
return new type.BigNumber(+x);
|
||||
}
|
||||
}, {
|
||||
from: 'boolean',
|
||||
@ -87,13 +98,13 @@ exports.create = function create(math) {
|
||||
from: 'null',
|
||||
to: 'BigNumber',
|
||||
convert: function () {
|
||||
return new math.type.BigNumber(0);
|
||||
return new type.BigNumber(0);
|
||||
}
|
||||
}, {
|
||||
from: 'Array',
|
||||
to: 'Matrix',
|
||||
convert: function (array) {
|
||||
return new math.type.Matrix(array);
|
||||
return new type.Matrix(array);
|
||||
}
|
||||
}
|
||||
];
|
||||
@ -15,7 +15,7 @@ function factory(type, config, load, typed) {
|
||||
* @return {Number | BigNumber | Complex | Unit} Sum of `x` and `y`
|
||||
* @private
|
||||
*/
|
||||
var addScalar = typed('addScalar', {
|
||||
var addScalar = typed('add', {
|
||||
|
||||
'number, number': function (x, y) {
|
||||
return x + y;
|
||||
|
||||
@ -14,7 +14,7 @@ function factory(type, config, load, typed) {
|
||||
* @return {Number | BigNumber | Complex | Unit} Quotient, `x / y`
|
||||
* @private
|
||||
*/
|
||||
var divideScalar = typed('divideScalar', {
|
||||
var divideScalar = typed('divide', {
|
||||
'number, number': function (x, y) {
|
||||
return x / y;
|
||||
},
|
||||
|
||||
@ -52,7 +52,7 @@
|
||||
"dependencies": {
|
||||
"decimal.js": "^4.0.2",
|
||||
"tiny-emitter": "^1.0.0",
|
||||
"typed-function": "^0.7.0"
|
||||
"typed-function": "^0.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"webpack": "^1.8.9",
|
||||
|
||||
@ -87,9 +87,9 @@ describe('add', function() {
|
||||
});
|
||||
|
||||
it('should throw an error in case of a unit and non-unit argument', function() {
|
||||
assert.throws(function () {add(math.unit('5cm'), 2);}, /TypeError/);
|
||||
assert.throws(function () {add(math.unit('5cm'), new Date());}, /TypeError/);
|
||||
assert.throws(function () {add(new Date(), math.unit('5cm'));}, /TypeError/);
|
||||
assert.throws(function () {add(math.unit('5cm'), 2);}, /TypeError: Unexpected type of argument in function add/);
|
||||
assert.throws(function () {add(math.unit('5cm'), new Date());}, /TypeError: Unexpected type of argument in function add/);
|
||||
assert.throws(function () {add(new Date(), math.unit('5cm'));}, /TypeError: Unexpected type of argument in function add/);
|
||||
});
|
||||
|
||||
it('should concatenate two strings', function() {
|
||||
|
||||
@ -115,7 +115,7 @@ describe('divide', function() {
|
||||
// TODO: divide units by a bignumber
|
||||
it('should divide units by a big number', function() {
|
||||
//assert.equal(divide(math.unit('5 m'), bignumber(10)).toString(), '500 mm'); // TODO
|
||||
assert.throws(function () {divide(math.unit('5 m'), bignumber(10))}, /TypeError: Unexpected type of argument \(expected: number or boolean or null, actual: BigNumber, index: 1\)/);
|
||||
assert.throws(function () {divide(math.unit('5 m'), bignumber(10))}, /TypeError: Unexpected type of argument in function divide \(expected: number or boolean or null, actual: BigNumber, index: 1\)/);
|
||||
});
|
||||
|
||||
it('should divide each elements in a matrix by a number', function() {
|
||||
|
||||
@ -46,8 +46,8 @@ describe('log', function() {
|
||||
});
|
||||
|
||||
it('should throw an error if invalid number of arguments', function() {
|
||||
assert.throws(function () {log()}, /TypeError: Too few arguments \(expected: any, index: 1\)/);
|
||||
assert.throws(function () {log(1, 2, 3)}, /TypeError: Too many arguments \(expected: 2, actual: 3\)/);
|
||||
assert.throws(function () {log()}, /TypeError: Too few arguments in function log \(expected: any, index: 1\)/);
|
||||
assert.throws(function () {log(1, 2, 3)}, /TypeError: Too many arguments in function log \(expected: 2, actual: 3\)/);
|
||||
});
|
||||
|
||||
it('should return the log of positive bignumbers', function() {
|
||||
|
||||
@ -77,8 +77,8 @@ describe('pow', function() {
|
||||
});
|
||||
|
||||
it('should throw an error if used with wrong number of arguments', function() {
|
||||
assert.throws(function () {pow(1)}, /TypeError: Too few arguments \(expected: number or BigNumber or Complex or boolean or null, index: 1\)/);
|
||||
assert.throws(function () {pow(1, 2, 3)}, /TypeError: Too many arguments \(expected: 2, actual: 3\)/);
|
||||
assert.throws(function () {pow(1)}, /TypeError: Too few arguments in function pow \(expected: number or BigNumber or Complex or boolean or null, index: 1\)/);
|
||||
assert.throws(function () {pow(1, 2, 3)}, /TypeError: Too many arguments in function pow \(expected: 2, actual: 3\)/);
|
||||
});
|
||||
|
||||
it('should exponentiate a complex number to the given power', function() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user