From 420446452b4fc412cfd00a2c2237c4d48650b7cd Mon Sep 17 00:00:00 2001 From: jos Date: Fri, 25 Jan 2019 22:27:59 +0100 Subject: [PATCH] Fixed a bug the methods `map`, `forEach`, `traverse`, and `transform` of `FunctionNode` --- HISTORY.md | 4 ++- src/expression/node/FunctionNode.js | 4 ++- test/expression/node/FunctionNode.test.js | 34 +++++++++++++++-------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 9f90fe31b..ae53e36ab 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,8 +3,10 @@ # not yet released, version 5.4.2 - Fix `math.format` not working for BigNumbers with a precision above - 1025 digits. Thanks @ericman314. + 1025 digits (see #1385). Thanks @ericman314. - Fix incorrect LaTeX output of `RelationalNode`. Thanks @rianmcguire. +- Fixed a bug the methods `map`, `forEach`, `traverse`, and `transform` + of `FunctionNode`. # 2019-01-10, version 5.4.1 diff --git a/src/expression/node/FunctionNode.js b/src/expression/node/FunctionNode.js index c9a75ceb0..4759fddd3 100644 --- a/src/expression/node/FunctionNode.js +++ b/src/expression/node/FunctionNode.js @@ -159,6 +159,8 @@ function factory (type, config, load, typed, math) { * @param {function(child: Node, path: string, parent: Node)} callback */ FunctionNode.prototype.forEach = function (callback) { + callback(this.fn, 'fn', this) + for (let i = 0; i < this.args.length; i++) { callback(this.args[i], 'args[' + i + ']', this) } @@ -171,7 +173,7 @@ function factory (type, config, load, typed, math) { * @returns {FunctionNode} Returns a transformed copy of the node */ FunctionNode.prototype.map = function (callback) { - const fn = this.fn.map(callback) + const fn = this._ifNode(callback(this.fn, 'fn', this)) const args = [] for (let i = 0; i < this.args.length; i++) { args[i] = this._ifNode(callback(this.args[i], 'args[' + i + ']', this)) diff --git a/test/expression/node/FunctionNode.test.js b/test/expression/node/FunctionNode.test.js index 5b57150f9..566f0112a 100644 --- a/test/expression/node/FunctionNode.test.js +++ b/test/expression/node/FunctionNode.test.js @@ -114,7 +114,9 @@ describe('FunctionNode', function () { const b = new mymath.expression.node.ConstantNode(5) const n = new mymath.expression.node.FunctionNode(s, [a, b]) - let scope = {} + let scope = { + foo: 'bar' + } assert.strictEqual(n.compile().eval(scope), 'myFunction(4, 5)') }) @@ -197,10 +199,11 @@ describe('FunctionNode', function () { assert.strictEqual(parent, f) }) - assert.strictEqual(nodes.length, 2) - assert.strictEqual(nodes[0], c) - assert.strictEqual(nodes[1], d) - assert.deepStrictEqual(paths, ['args[0]', 'args[1]']) + assert.strictEqual(nodes.length, 3) + assert.strictEqual(nodes[0], s) + assert.strictEqual(nodes[1], c) + assert.strictEqual(nodes[2], d) + assert.deepStrictEqual(paths, ['fn', 'args[0]', 'args[1]']) }) it('should map a FunctionNode', function () { @@ -223,16 +226,17 @@ describe('FunctionNode', function () { return node instanceof SymbolNode && node.name === 'x' ? g : node }) - assert.strictEqual(nodes.length, 2) - assert.strictEqual(nodes[0], c) - assert.strictEqual(nodes[1], d) - assert.deepStrictEqual(paths, ['args[0]', 'args[1]']) + assert.strictEqual(nodes.length, 3) + assert.strictEqual(nodes[0], s) + assert.strictEqual(nodes[1], c) + assert.strictEqual(nodes[2], d) + assert.deepStrictEqual(paths, ['fn', 'args[0]', 'args[1]']) assert.notStrictEqual(h, f) + assert.strictEqual(h.fn.name, 'multiply') assert.strictEqual(h.args[0], c) assert.strictEqual(h.args[0].args[0], a) assert.strictEqual(h.args[0].args[1], b) - assert.strictEqual(h.fn.name, 'multiply') assert.strictEqual(h.args[1], g) }) @@ -319,12 +323,18 @@ describe('FunctionNode', function () { break case 2: + assert.strictEqual(node, s) + assert.strictEqual(path, 'fn') + assert.strictEqual(parent, d) + break + + case 3: assert.strictEqual(node, b) assert.strictEqual(path, 'args[0]') assert.strictEqual(parent, d) break - case 3: + case 4: assert.strictEqual(node, c) assert.strictEqual(path, 'args[1]') assert.strictEqual(parent, d) @@ -332,7 +342,7 @@ describe('FunctionNode', function () { } }) - assert.strictEqual(count, 3) + assert.strictEqual(count, 4) }) it('should clone a FunctionNode', function () {