From 5c12c07e2c5a97a234a51a9f6e4b0dbc11afe9c4 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 24 Apr 2024 08:14:48 +0200 Subject: [PATCH 1/3] chore: replace utility function `values` with `Object.values` (fix #3194) --- src/core/create.js | 6 +++--- src/utils/object.js | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/core/create.js b/src/core/create.js index 2999898d36..f9e7ed572f 100644 --- a/src/core/create.js +++ b/src/core/create.js @@ -1,5 +1,5 @@ import typedFunction from 'typed-function' -import { deepFlatten, isLegacyFactory, values } from '../utils/object.js' +import { deepFlatten, isLegacyFactory } from '../utils/object.js' import * as emitter from './../utils/emitter.js' import { importFactory } from './function/import.js' import { configFactory } from './function/config.js' @@ -216,7 +216,7 @@ export function create (factories, config) { // listen for changes in config, import all functions again when changed // TODO: move this listener into the import function? math.on('config', () => { - values(importedFactories).forEach(factory => { + Object.values(importedFactories).forEach(factory => { if (factory && factory.meta && factory.meta.recreateOnConfigChange) { // FIXME: only re-create when the current instance is the same as was initially created // FIXME: delete the functions/constants before importing them again? @@ -234,7 +234,7 @@ export function create (factories, config) { // import the factory functions like createAdd as an array instead of object, // else they will get a different naming (`createAdd` instead of `add`). - math.import(values(deepFlatten(factories))) + math.import(Object.values(deepFlatten(factories))) math.ArgumentsError = ArgumentsError math.DimensionError = DimensionError diff --git a/src/utils/object.js b/src/utils/object.js index c5dbc692e2..7fbbdbc4a5 100644 --- a/src/utils/object.js +++ b/src/utils/object.js @@ -390,10 +390,6 @@ export function pickShallow (object, properties) { return copy } -export function values (object) { - return Object.keys(object).map(key => object[key]) -} - // helper function to test whether a string contains a path like 'user.name' function isPath (str) { return str.indexOf('.') !== -1 From 81d2e71445ac8f4dc4c93ece5562492bbbe16240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20G=C3=A9rin?= <41303636+lgerin@users.noreply.github.com> Date: Wed, 24 Apr 2024 03:31:06 -0400 Subject: [PATCH 2/3] fix #3192: function `isNaN` returns `false` for `NaN` units in a matrix or array * Use referToSelf() to recursively check if various types are NaN in an array or matrix * fix array test description from isNegative to isNaN * Add test for units in a matrix --------- Co-authored-by: Jos de Jong --- src/function/utils/isNaN.js | 4 +--- test/unit-tests/function/utils/isNaN.test.js | 8 ++++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/function/utils/isNaN.js b/src/function/utils/isNaN.js index b12f224276..36e985d695 100644 --- a/src/function/utils/isNaN.js +++ b/src/function/utils/isNaN.js @@ -54,8 +54,6 @@ export const createIsNaN = /* #__PURE__ */ factory(name, dependencies, ({ typed return Number.isNaN(x.value) }, - 'Array | Matrix': function (x) { - return deepMap(x, Number.isNaN) - } + 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) }) }) diff --git a/test/unit-tests/function/utils/isNaN.test.js b/test/unit-tests/function/utils/isNaN.test.js index 97d5c7db01..e7e586da05 100644 --- a/test/unit-tests/function/utils/isNaN.test.js +++ b/test/unit-tests/function/utils/isNaN.test.js @@ -59,14 +59,18 @@ describe('isNegative', function () { assert.strictEqual(isNaN(''), false) }) - it('should test isNegative element wise on an Array', function () { + it('should test isNaN element wise on an Array', function () { assert.deepStrictEqual(isNaN([0, 5, -2, NaN]), [false, false, false, true]) }) - it('should test isNegative element wise on a Matrix', function () { + it('should test isNaN element wise on a Matrix', function () { assert.deepStrictEqual(isNaN(math.matrix([0, 5, -2, NaN])), math.matrix([false, false, false, true])) }) + it('should test isNaN element wise on a Matrix of units', function () { + assert.deepStrictEqual(isNaN(math.matrix([new Unit(3, 'ft'), new Unit(NaN, 'ft')])), math.matrix([false, true])) + }) + it('should throw an error in case of unsupported data types', function () { assert.throws(function () { isNaN(new Date()) }, /TypeError: Unexpected type of argument/) assert.throws(function () { isNaN({}) }, /TypeError: Unexpected type of argument/) From 80a0ac179a7754caa04d0487eca59e76088aeb91 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 24 Apr 2024 09:31:29 +0200 Subject: [PATCH 3/3] chore: update history --- HISTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 5d2575c4b2..8976aba342 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,8 @@ # unpublished changes since 12.4.1 +- Fix #3192: function `isNaN` returns `false` for `NaN` units in a matrix or + array (#3193). Thanks @lgerin. - Fix: #3180 fix type definitions of functions `add` and `multiply` to allow more than two arguments. - Docs: correct the docs about `traverse` returning void (#3177).