From 868b2702b5a936ad407ade185f1c13057f48aebe Mon Sep 17 00:00:00 2001 From: jos Date: Sat, 9 May 2015 14:53:36 +0200 Subject: [PATCH] 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`. --- lib/core/core.js | 4 +-- lib/{util => core}/typed.js | 35 ++++++++++++++++--------- lib/function/arithmetic/addScalar.js | 2 +- lib/function/arithmetic/divideScalar.js | 2 +- package.json | 2 +- test/function/arithmetic/add.test.js | 6 ++--- test/function/arithmetic/divide.test.js | 2 +- test/function/arithmetic/log.test.js | 4 +-- test/function/arithmetic/pow.test.js | 4 +-- 9 files changed, 36 insertions(+), 25 deletions(-) rename lib/{util => core}/typed.js (74%) diff --git a/lib/core/core.js b/lib/core/core.js index a4e928823..dfaa8798b 100644 --- a/lib/core/core.js +++ b/lib/core/core.js @@ -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 = { diff --git a/lib/util/typed.js b/lib/core/typed.js similarity index 74% rename from lib/util/typed.js rename to lib/core/typed.js index ade49e33b..b7539ddad 100644 --- a/lib/util/typed.js +++ b/lib/core/typed.js @@ -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); } } ]; diff --git a/lib/function/arithmetic/addScalar.js b/lib/function/arithmetic/addScalar.js index 57233bfb7..79cdcfbc6 100644 --- a/lib/function/arithmetic/addScalar.js +++ b/lib/function/arithmetic/addScalar.js @@ -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; diff --git a/lib/function/arithmetic/divideScalar.js b/lib/function/arithmetic/divideScalar.js index bf0128931..e05fd4abd 100644 --- a/lib/function/arithmetic/divideScalar.js +++ b/lib/function/arithmetic/divideScalar.js @@ -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; }, diff --git a/package.json b/package.json index 4795a6b9a..271711e89 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/function/arithmetic/add.test.js b/test/function/arithmetic/add.test.js index 7329d0412..1cacf8634 100644 --- a/test/function/arithmetic/add.test.js +++ b/test/function/arithmetic/add.test.js @@ -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() { diff --git a/test/function/arithmetic/divide.test.js b/test/function/arithmetic/divide.test.js index a343ad81a..13b8c971c 100644 --- a/test/function/arithmetic/divide.test.js +++ b/test/function/arithmetic/divide.test.js @@ -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() { diff --git a/test/function/arithmetic/log.test.js b/test/function/arithmetic/log.test.js index 94e765882..2f39e389c 100644 --- a/test/function/arithmetic/log.test.js +++ b/test/function/arithmetic/log.test.js @@ -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() { diff --git a/test/function/arithmetic/pow.test.js b/test/function/arithmetic/pow.test.js index 49cc90b90..27051fd5a 100644 --- a/test/function/arithmetic/pow.test.js +++ b/test/function/arithmetic/pow.test.js @@ -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() {