Skip to content

Commit 8a8f26b

Browse files
abeledovictorevykassirer
authored andcommitted
removed utils/clone, added .cloneDeep() where clone() was used (google#235)
1 parent e5a69b7 commit 8a8f26b

29 files changed

Lines changed: 73 additions & 126 deletions

lib/equation/Equation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const math = require('mathjs');
22

3-
const clone = require('../util/clone');
43
const printNode = require('../util/print');
54

65
// This represents an equation, made up of the leftNode (LHS), the
@@ -31,8 +30,8 @@ class Equation {
3130
}
3231

3332
clone() {
34-
const newLeft = clone(this.leftNode);
35-
const newRight = clone(this.rightNode);
33+
const newLeft = this.leftNode.cloneDeep();
34+
const newRight = this.rightNode.cloneDeep();
3635
return new Equation(newLeft, newRight, this.comparator);
3736
}
3837
}

lib/node/Status.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const clone = require('../util/clone');
2-
31
const ChangeTypes = require('../ChangeTypes');
42
const Type = require('./Type');
53

@@ -29,7 +27,7 @@ class Status {
2927
}
3028

3129
Status.resetChangeGroups = function(node) {
32-
node = clone(node);
30+
node = node.cloneDeep();
3331
node.filter(node => node.changeGroup).forEach(change => {
3432
delete change.changeGroup;
3533
});
@@ -60,8 +58,8 @@ Status.nodeChanged = function(
6058
// updated to have the newNode/oldNode metadata (changeGroups)
6159
// e.g. (2 + 2) + x --> 4 + x has to update the left argument
6260
Status.childChanged = function(node, childStatus, childArgIndex=null) {
63-
const oldNode = clone(node);
64-
const newNode = clone(node);
61+
const oldNode = node.cloneDeep();
62+
const newNode = node.cloneDeep();
6563
let substeps = childStatus.substeps;
6664

6765
if (!childStatus.oldNode) {
@@ -80,8 +78,8 @@ Status.childChanged = function(node, childStatus, childArgIndex=null) {
8078
oldNode.content = childStatus.oldNode;
8179
newNode.content = childStatus.newNode;
8280
substeps = updateSubsteps(substeps, (step) => {
83-
const oldNode = clone(node);
84-
const newNode = clone(node);
81+
const oldNode = node.cloneDeep();
82+
const newNode = node.cloneDeep();
8583
oldNode.content = step.oldNode;
8684
newNode.content = step.newNode;
8785
step.oldNode = oldNode;
@@ -94,8 +92,8 @@ Status.childChanged = function(node, childStatus, childArgIndex=null) {
9492
oldNode.args[childArgIndex] = childStatus.oldNode;
9593
newNode.args[childArgIndex] = childStatus.newNode;
9694
substeps = updateSubsteps(substeps, (step) => {
97-
const oldNode = clone(node);
98-
const newNode = clone(node);
95+
const oldNode = node.cloneDeep();
96+
const newNode = node.cloneDeep();
9997
oldNode.args[childArgIndex] = step.oldNode;
10098
newNode.args[childArgIndex] = step.newNode;
10199
step.oldNode = oldNode;
@@ -107,8 +105,8 @@ Status.childChanged = function(node, childStatus, childArgIndex=null) {
107105
oldNode.args[0] = childStatus.oldNode;
108106
newNode.args[0] = childStatus.newNode;
109107
substeps = updateSubsteps(substeps, (step) => {
110-
const oldNode = clone(node);
111-
const newNode = clone(node);
108+
const oldNode = node.cloneDeep();
109+
const newNode = node.cloneDeep();
112110
oldNode.args[0] = step.oldNode;
113111
newNode.args[0] = step.newNode;
114112
step.oldNode = oldNode;

lib/simplifyExpression/basicsSearch/convertMixedNumberToImproperFraction.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const clone = require('../../util/clone');
2-
31
const ChangeTypes = require('../../ChangeTypes');
42
const Node = require('../../node');
53

@@ -12,7 +10,7 @@ function convertMixedNumberToImproperFraction(node) {
1210
}
1311

1412
const substeps = [];
15-
let newNode = clone(node);
13+
let newNode = node.cloneDeep();
1614

1715
// e.g. 1 2/3
1816
const wholeNumber = Node.MixedNumber.getWholeNumberValue(node); // 1

lib/simplifyExpression/basicsSearch/rearrangeCoefficient.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const checks = require('../../checks');
2-
const clone = require('../../util/clone');
32

43
const ChangeTypes = require('../../ChangeTypes');
54
const Node = require('../../node');
@@ -12,7 +11,7 @@ function rearrangeCoefficient(node) {
1211
return Node.Status.noChange(node);
1312
}
1413

15-
let newNode = clone(node);
14+
let newNode = node.cloneDeep();
1615

1716
const polyNode = new Node.PolynomialTerm(newNode.args[0]);
1817
const constNode = newNode.args[1];

lib/simplifyExpression/basicsSearch/removeAdditionOfZero.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const clone = require('../../util/clone');
2-
31
const ChangeTypes = require('../../ChangeTypes');
42
const Node = require('../../node');
53

@@ -12,7 +10,7 @@ function removeAdditionOfZero(node) {
1210
const zeroIndex = node.args.findIndex(arg => {
1311
return Node.Type.isConstant(arg) && arg.value === '0';
1412
});
15-
let newNode = clone(node);
13+
let newNode = node.cloneDeep();
1614
if (zeroIndex >= 0) {
1715
// remove the 0 node
1816
newNode.args.splice(zeroIndex, 1);

lib/simplifyExpression/basicsSearch/removeDivisionByOne.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const clone = require('../../util/clone');
2-
31
const ChangeTypes = require('../../ChangeTypes');
42
const Negative = require('../../Negative');
53
const Node = require('../../node');
@@ -14,7 +12,9 @@ function removeDivisionByOne(node) {
1412
if (!Node.Type.isConstant(denominator)) {
1513
return Node.Status.noChange(node);
1614
}
17-
let numerator = clone(node.args[0]);
15+
// It's taken 40ms on average to pass distribution test,
16+
// TODO: see if we should keep using utils/clone here
17+
let numerator = node.args[0].cloneDeep();
1818

1919
// if denominator is -1, we make the numerator negative
2020
if (parseFloat(denominator.value) === -1) {

lib/simplifyExpression/basicsSearch/removeExponentBaseOne.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const checks = require('../../checks');
2-
const clone = require('../../util/clone');
32

43
const ChangeTypes = require('../../ChangeTypes');
54
const Node = require('../../node');
@@ -11,7 +10,7 @@ function removeExponentBaseOne(node) {
1110
checks.resolvesToConstant(node.args[1]) && // a power not a symbol and
1211
Node.Type.isConstant(node.args[0]) && // a constant base
1312
node.args[0].value === '1') { // of value 1
14-
const newNode = clone(node.args[0]);
13+
const newNode = node.args[0].cloneDeep();
1514
return Node.Status.nodeChanged(
1615
ChangeTypes.REMOVE_EXPONENT_BASE_ONE, node, newNode);
1716
}

lib/simplifyExpression/basicsSearch/removeExponentByOne.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const clone = require('../../util/clone');
2-
31
const ChangeTypes = require('../../ChangeTypes');
42
const Node = require('../../node');
53

@@ -9,7 +7,7 @@ function removeExponentByOne(node) {
97
if (node.op === '^' && // exponent of anything
108
Node.Type.isConstant(node.args[1]) && // to a constant
119
node.args[1].value === '1') { // of value 1
12-
const newNode = clone(node.args[0]);
10+
const newNode = node.args[0].cloneDeep();
1311
return Node.Status.nodeChanged(
1412
ChangeTypes.REMOVE_EXPONENT_BY_ONE, node, newNode);
1513
}

lib/simplifyExpression/basicsSearch/removeMultiplicationByNegativeOne.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const clone = require('../../util/clone');
2-
31
const ChangeTypes = require('../../ChangeTypes');
42
const Negative = require('../../Negative');
53
const Node = require('../../node');
@@ -36,10 +34,10 @@ function removeMultiplicationByNegativeOne(node) {
3634
return Node.Status.noChange(node);
3735
}
3836

39-
let newNode = clone(node);
37+
let newNode = node.cloneDeep();
4038

4139
// Get rid of the -1
42-
nodeToCombine = Negative.negate(clone(nodeToCombine));
40+
nodeToCombine = Negative.negate(nodeToCombine.cloneDeep());
4341

4442
// replace the node next to -1 and remove -1
4543
newNode.args[nodeToCombineIndex] = nodeToCombine;

lib/simplifyExpression/basicsSearch/removeMultiplicationByOne.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const clone = require('../../util/clone');
2-
31
const ChangeTypes = require('../../ChangeTypes');
42
const Node = require('../../node');
53

@@ -13,7 +11,7 @@ function removeMultiplicationByOne(node) {
1311
return Node.Type.isConstant(arg) && arg.value === '1';
1412
});
1513
if (oneIndex >= 0) {
16-
let newNode = clone(node);
14+
let newNode = node.cloneDeep();
1715
// remove the 1 node
1816
newNode.args.splice(oneIndex, 1);
1917
// if there's only one operand left, there's nothing left to multiply it

0 commit comments

Comments
 (0)