Skip to content

Commit

Permalink
Fix empty terms in the binom function
Browse files Browse the repository at this point in the history
Closes: #31
  • Loading branch information
runarberg committed Dec 5, 2019
1 parent bdf4648 commit 320b02d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ function parser(options) {
} else if (ascii.startsWith("binom")) {
const [, , group, , after] = syntax.splitNextGroup(ascii);
const [a, b] = colsplit(group);
const over = parsegroup(a.trim());
const under = parsegroup(b.trim());
const over = parsegroup(a.trim()) || mrow("");
const under = parsegroup(b.trim()) || mrow("");

el = mfenced(mfrac(over + under, { linethickness: 0 }), {
open: "(",
Expand Down
33 changes: 33 additions & 0 deletions test/groupings.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ test("Complex groupings", t => {
);
});

test("Binom function", t => {
t.is(
a2ml("binom(n, k)"),
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>n</mi><mi>k</mi></mfrac></mfenced></math>'
);
});

test("Binom function accepts expressions", t => {
t.is(
a2ml("binom(a, b + c)"),
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>a</mi><mrow><mi>b</mi><mo>+</mo><mi>c</mi></mrow></mfrac></mfenced></math>'
);
});

test("Missing argument in the binom function", t => {
t.is(
a2ml("binom(a,)"),
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>a</mi><mrow></mrow></mfrac></mfenced></math>'
);

t.is(
a2ml("binom(,b)"),
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mrow></mrow><mi>b</mi></mfrac></mfenced></math>'
);
});

test("Simplify polynomials", t => {
t.is(
a2ml("(x+y)(x-y) = x^2-y^2"),
Expand Down Expand Up @@ -113,3 +139,10 @@ test("Average over time", t => {
'<math><mfenced open="⟨" close="⟩"><mrow><mi>V</mi><msup><mfenced open="(" close=")"><mi>t</mi></mfenced><mn>2</mn></msup></mrow></mfenced><mo>=</mo><munder><mi>lim</mi><mrow><mi>T</mi><mo>→</mo><mi mathvariant="normal">∞</mi></mrow></munder><mfrac><mn>1</mn><mi>T</mi></mfrac><msubsup><mo>∫</mo><mrow><mo>-</mo><mfrac bevelled="true"><mi>T</mi><mn>2</mn></mfrac></mrow><mfrac bevelled="true"><mi>T</mi><mn>2</mn></mfrac></msubsup><mi>V</mi><msup><mfenced open="(" close=")"><mi>t</mi></mfenced><mn>2</mn></msup><mi>d</mi><mi>t</mi></math>'
);
});

test("The binomial coefficient", t => {
t.is(
a2ml("binom(n, k) = n! / (n-k)!k!"),
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>n</mi><mi>k</mi></mfrac></mfenced><mo>=</mo><mfrac><mrow><mi>n</mi><mo>!</mo></mrow><mrow><mfenced open="(" close=")"><mrow><mi>n</mi><mo>-</mo><mi>k</mi></mrow></mfenced><mo>!</mo><mi>k</mi><mo>!</mo></mrow></mfrac></math>'
);
});
21 changes: 0 additions & 21 deletions test/roots.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@ test("Omits brackets in roots", t => {
t.is(a2ml("root(3)(2)"), "<math><mroot><mn>2</mn><mn>3</mn></mroot></math>");
});

test("Display the binomial coefficient", t => {
t.is(
a2ml("binom(n, k)"),
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>n</mi><mi>k</mi></mfrac></mfenced></math>'
);
});

test("Displays the whole terms of the binomial coefficient", t => {
t.is(
a2ml("binom(a, b + c)"),
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>a</mi><mrow><mi>b</mi><mo>+</mo><mi>c</mi></mrow></mfrac></mfenced></math>'
);
});

test("Allows empty roots", t => {
t.is(a2ml("sqrt"), "<math><msqrt><mrow></mrow></msqrt></math>");
t.is(
Expand Down Expand Up @@ -73,10 +59,3 @@ test("Continued square root", t => {
"<math><msqrt><mrow><mn>1</mn><mo>+</mo><msqrt><mrow><mn>1</mn><mo>+</mo><msqrt><mrow><mn>1</mn><mo>+</mo><msqrt><mrow><mn>1</mn><mo>+</mo><msqrt><mrow><mn>1</mn><mo>+</mo><msqrt><mrow><mn>1</mn><mo>+</mo><msqrt><mrow><mn>1</mn><mo>+</mo><mo>⋯</mo></mrow></msqrt></mrow></msqrt></mrow></msqrt></mrow></msqrt></mrow></msqrt></mrow></msqrt></mrow></msqrt></math>"
);
});

test("The binomial coefficient", t => {
t.is(
a2ml("binom(n, k) = n! / (n-k)!k!"),
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>n</mi><mi>k</mi></mfrac></mfenced><mo>=</mo><mfrac><mrow><mi>n</mi><mo>!</mo></mrow><mrow><mfenced open="(" close=")"><mrow><mi>n</mi><mo>-</mo><mi>k</mi></mrow></mfenced><mo>!</mo><mi>k</mi><mo>!</mo></mrow></mfrac></math>'
);
});

0 comments on commit 320b02d

Please sign in to comment.