Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit 308a90c

Browse files
committed
Add changes
1 parent 41e9223 commit 308a90c

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

lib/checks/canFindDenominatorInNumerator.js

+22-27
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,20 @@ const Node = require('../node');
55
// e.g. (2x+1)/(2x+3) -> True because of the following simplification
66
// (2x+1)/(2x+3) -> (2x + 3)/(2x + 3) - 2/(2x + 3) -> 1 - 2/(2x + 3)
77
// e.g. (2x+1)/(2x^2 + 3) -> False
8-
// =========================================================================
8+
// ==============================================================================
99
// CHECKS
1010
// - Check for division in parent node
11-
// - Check that the number of arguments in parent node is 2
12-
// - Check that the number of numerator args is equal to 2 or 1. In the case
13-
// - of 1, we need that node to have a symbol (so it can't be a constant)
14-
// - Check that denominator has two args
15-
// - Check that the denominator op is '+' or '-'
16-
// - If the numerator has 2 args, check that the second arg is a constant node
17-
// - Check if the denominator's second arg is a constant node
18-
// - Check to see that the denominator and numerator both don't have exponents
11+
// - Numerator has to be addition/subtraction of a polynomial term to the power
12+
// of 1 and a constant term, in that order OR a polynomial term to the
13+
// power of 1
14+
// - Denominator has to be addition/subtraction of a polynomial term to the power
15+
// of 1 and a constant term, in that order.
1916
// - Check to see that the denominator and numerator have the same symbol
2017

2118
function canFindDenominatorInNumerator(node) {
2219
if (node.op !== '/' ) {
2320
return false;
2421
}
25-
if (node.args.length !== 2) {
26-
return false;
27-
}
2822
let numerator = node.args[0];
2923
let denominator = node.args[1];
3024
if (Node.Type.isParenthesis(numerator)) {
@@ -35,35 +29,36 @@ function canFindDenominatorInNumerator(node) {
3529
}
3630

3731
let numeratorArgsLength;
38-
// If numerator is '*' op, it signifies a single 'ax', should be assigned a
39-
// length of 2
40-
if ('args' in numerator &&
41-
(numerator.op === '+' || numerator.op === '-')) {
32+
// If numerator has args, but it's just a polynomial term, length is 1
33+
// Ex. 3x/2x+3 => numeratorArgsLength=1
34+
if (Node.PolynomialTerm.isPolynomialTerm(numerator)) {
35+
numeratorArgsLength = 1;
36+
}
37+
// If numerator has args and args are two seperate values length is 2
38+
// Ex. 3x+4/2x+3 => numeratorArgsLength=2
39+
else if (numerator.op === '+' || numerator.op === '-') {
4240
numeratorArgsLength = numerator.args.length;
4341
}
42+
// If numerator doesn't have args and isn't a polynomial term, there's
43+
// nothing the added functionality can do
44+
// Ex. 3/(2x + 3) => False
4445
else {
45-
numeratorArgsLength = 1;
46+
return false;
4647
}
4748
let denominatorArgsLength;
48-
if ('args' in denominator) {
49+
if (denominator.op === '+' || denominator.op === '-') {
4950
denominatorArgsLength = denominator.args.length;
5051
}
52+
// If denominator doesn't have args, it's length is 1. This case is already
53+
// resolved by splitting the denominator into all the numerators
54+
// Ex. (x + 3)/2x => x/2x + 3/2x
5155
else {
52-
denominatorArgsLength = 1;
53-
}
54-
// If numerator args isn't length 2 or length 1 with a polynomial return false
55-
if (numeratorArgsLength !== 2 &&
56-
(!(numeratorArgsLength === 1 && !Node.Type.isConstant(numerator)))) {
5756
return false;
5857
}
5958
// Function doesn't support denominators with args > 2
60-
// If denominatorArgsLength = 1 the normal functionality already covers it
6159
if (denominatorArgsLength !== 2) {
6260
return false;
6361
}
64-
if (!(denominator.op === '+' || denominator.op === '-')) {
65-
return false;
66-
}
6762
// Check if numerator's second argument is a constant if numerator has two arguments
6863
if (numeratorArgsLength === 2) {
6964
if (!Node.Type.isConstant(numerator.args[1])) {

0 commit comments

Comments
 (0)