From ee008d992e8a8ed80725ba96bc4fd4d62dcdafd4 Mon Sep 17 00:00:00 2001 From: jos Date: Mon, 4 Jun 2018 21:19:10 +0200 Subject: [PATCH] Fixes failing unit tests on IE11/Edge --- lib/function/utils/isNaN.js | 9 ++++--- lib/utils/object.js | 32 ++++++++++++++++++++++++ test/function/trigonometry/sinh.test.js | 6 +++-- test/type/matrix/Range.test.js | 2 +- test/type/unit/physicalConstants.test.js | 2 +- 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/function/utils/isNaN.js b/lib/function/utils/isNaN.js index 26bdeeffc..275aae302 100644 --- a/lib/function/utils/isNaN.js +++ b/lib/function/utils/isNaN.js @@ -3,6 +3,9 @@ var deepMap = require('../../utils/collection/deepMap'); var number = require('../../utils/number'); +// Number.isNaN is more robust than isNaN, but Number.isNaN is not available for example on IE11 +var _isNaN = Number.isNaN || isNaN; + function factory (type, config, load, typed) { /** * Test whether a value is NaN (not a number). @@ -35,7 +38,7 @@ function factory (type, config, load, typed) { */ var isNaN = typed('isNaN', { 'number': function (x) { - return Number.isNaN(x); + return _isNaN(x); }, 'BigNumber': function (x) { @@ -51,11 +54,11 @@ function factory (type, config, load, typed) { }, 'Unit': function (x) { - return Number.isNaN(x.value); + return _isNaN(x.value); }, 'Array | Matrix': function (x) { - return deepMap(x, Number.isNaN); + return deepMap(x, _isNaN); } }); diff --git a/lib/utils/object.js b/lib/utils/object.js index d8a405201..43db53779 100644 --- a/lib/utils/object.js +++ b/lib/utils/object.js @@ -262,3 +262,35 @@ exports.hasOwnProperty = function (object, property) { exports.isFactory = function (object) { return object && typeof object.factory === 'function'; }; + +// Polyfill for Object.assign for IE 11 (Object.assign is used in `escape-latex`) +// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign +if (typeof Object.assign !== 'function') { + // Must be writable: true, enumerable: false, configurable: true + Object.defineProperty(Object, "assign", { + value: function assign(target, varArgs) { // .length of function is 2 + 'use strict'; + if (target == null) { // TypeError if undefined or null + throw new TypeError('Cannot convert undefined or null to object'); + } + + var to = Object(target); + + for (var index = 1; index < arguments.length; index++) { + var nextSource = arguments[index]; + + if (nextSource != null) { // Skip over if undefined or null + for (var nextKey in nextSource) { + // Avoid bugs when hasOwnProperty is shadowed + if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { + to[nextKey] = nextSource[nextKey]; + } + } + } + } + return to; + }, + writable: true, + configurable: true + }); +} diff --git a/test/function/trigonometry/sinh.test.js b/test/function/trigonometry/sinh.test.js index 1ee82c695..4f33e3100 100644 --- a/test/function/trigonometry/sinh.test.js +++ b/test/function/trigonometry/sinh.test.js @@ -33,8 +33,10 @@ describe('sinh', function() { it('should return the sinh of very small numbers (avoid returning zero)', function() { // If sinh returns 0, that is bad, so we are using assert.equal, not approx.equal - assert.equal(sinh(-1e-10), -1e-10); - assert.equal(sinh(1e-50), 1e-50); + assert(sinh(-1e-10) !== 0); + assert(sinh(-1e-50) !== 0); + assert(Math.abs(sinh(-1e-10) - -1e-10) < EPSILON); + assert(Math.abs(sinh(1e-50) - 1e-50) < EPSILON); }); } diff --git a/test/type/matrix/Range.test.js b/test/type/matrix/Range.test.js index 919318357..f0cb6c32c 100644 --- a/test/type/matrix/Range.test.js +++ b/test/type/matrix/Range.test.js @@ -152,7 +152,7 @@ describe('range', function() { it('should stringify a range to format start:step:end with given precision', function () { assert.equal(new math.type.Range(1/3, 4/3, 2/3).format(3), '0.333:0.667:1.33'); assert.equal(new math.type.Range(1/3, 4/3, 2/3).format(4), '0.3333:0.6667:1.333'); - assert.equal(new math.type.Range(1/3, 4/3, 2/3).format(), '0.3333333333333333:0.6666666666666666:1.3333333333333333'); + assert.equal(new math.type.Range(1/3, 4/3, 2/3).format(14), '0.33333333333333:0.66666666666667:1.3333333333333'); }); }); diff --git a/test/type/unit/physicalConstants.test.js b/test/type/unit/physicalConstants.test.js index 38b159769..12dc748e8 100644 --- a/test/type/unit/physicalConstants.test.js +++ b/test/type/unit/physicalConstants.test.js @@ -17,7 +17,7 @@ describe('physical constants', function() { assert.equal(math.magneticConstant.toString(), '1.2566370614e-6 N / A^2'); assert.equal(math.electricConstant.toString(), '8.854187817e-12 F / m'); assert.equal(math.vacuumImpedance.toString(), '376.730313461 ohm'); - assert.equal(math.coulomb.toString(), '8.987551787368176e+9 (N m^2) / C^2'); + assert.equal(math.coulomb.format({precision: 14}), '8.9875517873682e+9 (N m^2) / C^2'); // round off issues on IE11 if not using precisions assert.equal(math.elementaryCharge.toString(), '1.60217656535e-19 C'); assert.equal(math.bohrMagneton.toString(), '9.274009682e-24 J / T'); assert.equal(math.conductanceQuantum.toString(), '7.748091734625e-5 S');