Skip to content

Commit

Permalink
Changed default value of the option wrap of function math.import
Browse files Browse the repository at this point in the history
…to false, and fixed a but in math.import
  • Loading branch information
josdejong committed Aug 16, 2014
1 parent f23c87c commit 8f9995c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
- Implemented support for `null` in all functions.
- Implemented support for "raw" functions in the expression parser. Raw
functions are invoked with unevaluated parameters (nodes).
- Changed default value of the option `wrap` of function `math.import` to false.
- Changed the default value for new entries in a resized matrix when to zero.
To leave new entries uninitialized, use the new constant `math.uninitialized`
as default value.
- Fixed a bug in `math.import` not applying options when passing a module name.
- A returned matrix subset is now only squeezed when the `index` consists of
scalar values, and no longer for ranges resolving into a single value.

Expand Down
4 changes: 2 additions & 2 deletions examples/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ print(math.eval('hello("user")')); // 'hello, user!'
*/
try {
// import the numbers.js library into math.js
math.import('numbers');
math.import('numbers', {wrap: true});
}
catch (err) {
console.log('Warning: to import numbers.js, the library must\n' +
Expand All @@ -61,7 +61,7 @@ if (math.fibonacci) {
*/
try {
// import the numeric.js library into math.js
math.import('numeric');
math.import('numeric', {wrap: true});
}
catch (err) {
console.log('Warning: to import numeric.js, the library must\n' +
Expand Down
16 changes: 7 additions & 9 deletions lib/function/utils/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ module.exports = function (math) {
* - `override: boolean`
* If true, existing functions will be overwritten. False by default.
* - `wrap: boolean`
* If true (default), the functions will be wrapped in a wrapper function
* If true, the functions will be wrapped in a wrapper function
* which converts data types like Matrix to primitive data types like Array.
* The wrapper is needed when extending math.js with libraries which do not
* support these data types. False by default.
*
* Examples:
*
Expand All @@ -47,7 +48,7 @@ module.exports = function (math) {
*
* // import the npm module numbers
* // (must be installed first with `npm install numbers`)
* math.import('numbers');
* math.import('numbers', {wrap: true});
*
* math.fibonacci(7); // returns 13
*
Expand All @@ -63,12 +64,9 @@ module.exports = function (math) {

var name;
var opts = {
override: false,
wrap: true
override: options && options.override || false,
wrap: options && options.wrap || false
};
if (options && options instanceof Object) {
util.object.extend(opts, options);
}

if (isString(object)) {
// a string with a filename
Expand All @@ -77,7 +75,7 @@ module.exports = function (math) {
if (typeof (require) !== 'undefined') {
// load the file using require
var _module = require(object);
math_import(_module);
math_import(_module, options);
}
else {
throw new Error('Cannot load module: require not available.');
Expand All @@ -92,7 +90,7 @@ module.exports = function (math) {
_import(name, value, opts);
}
else {
math_import(value);
math_import(value, options);
}
}
}
Expand Down
24 changes: 13 additions & 11 deletions test/function/utils/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,26 @@ describe('import', function() {
it('should parse the user defined members', function() {
if (math.parser) {
var parser = math.parser();
math.add(math.myvalue, 10)
math.add(math.myvalue, 10);
parser.eval('myvalue + 10'); // 52
parser.eval('hello("user")'); // 'hello, user!'
}
});

var getSize = function (array) { return array.length; };
var getSize = function (array) {
return array.length;
};

it('should wrap the custom functions automatically', function () {
math.import({ getSizeWrapped: getSize });
assert.equal(math.getSizeWrapped([1,2,3]), 3);
assert.equal(math.getSizeWrapped(math.matrix([1,2,3])), 3);
it('shouldn\'t wrap custom functions by default', function () {
math.import({ getSizeNotWrapped: getSize });
assert.strictEqual(math.getSizeNotWrapped([1,2,3]), 3);
assert.strictEqual(math.getSizeNotWrapped(math.matrix([1,2,3])), undefined);
});

it('shouldn\'t wrap the custom functions if wrap = false', function () {
math.import({ getSizeNotWrapped: getSize }, { wrap: false });
assert.equal(math.getSizeNotWrapped([1,2,3]), 3);
assert.equal(math.getSizeNotWrapped(math.matrix([1,2,3])), undefined);
it('should wrap custom functions if wrap = true', function () {
math.import({ getSizeWrapped: getSize }, { wrap: true});
assert.strictEqual(math.getSizeWrapped([1,2,3]), 3);
assert.strictEqual(math.getSizeWrapped(math.matrix([1,2,3])), 3);
});

it('wrapped imported functions should accept undefined and null', function () {
Expand All @@ -82,7 +84,7 @@ describe('import', function() {
it('should extend math with numbers', function() {
// extend math.js with numbers.js
// examples copied from https://github.com/sjkaliski/numbers.js/blob/master/examples/statistic.js
math.import('numbers');
math.import('numbers', {wrap: true});

assert.equal(math.fibonacci(7), 13);

Expand Down

0 comments on commit 8f9995c

Please sign in to comment.