Skip to content

Commit 320b02d

Browse files
committed
Fix empty terms in the binom function
Closes: #31
1 parent bdf4648 commit 320b02d

3 files changed

Lines changed: 35 additions & 23 deletions

File tree

src/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ function parser(options) {
165165
} else if (ascii.startsWith("binom")) {
166166
const [, , group, , after] = syntax.splitNextGroup(ascii);
167167
const [a, b] = colsplit(group);
168-
const over = parsegroup(a.trim());
169-
const under = parsegroup(b.trim());
168+
const over = parsegroup(a.trim()) || mrow("");
169+
const under = parsegroup(b.trim()) || mrow("");
170170

171171
el = mfenced(mfrac(over + under, { linethickness: 0 }), {
172172
open: "(",

test/groupings.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,32 @@ test("Complex groupings", t => {
7979
);
8080
});
8181

82+
test("Binom function", t => {
83+
t.is(
84+
a2ml("binom(n, k)"),
85+
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>n</mi><mi>k</mi></mfrac></mfenced></math>'
86+
);
87+
});
88+
89+
test("Binom function accepts expressions", t => {
90+
t.is(
91+
a2ml("binom(a, b + c)"),
92+
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>a</mi><mrow><mi>b</mi><mo>+</mo><mi>c</mi></mrow></mfrac></mfenced></math>'
93+
);
94+
});
95+
96+
test("Missing argument in the binom function", t => {
97+
t.is(
98+
a2ml("binom(a,)"),
99+
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>a</mi><mrow></mrow></mfrac></mfenced></math>'
100+
);
101+
102+
t.is(
103+
a2ml("binom(,b)"),
104+
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mrow></mrow><mi>b</mi></mfrac></mfenced></math>'
105+
);
106+
});
107+
82108
test("Simplify polynomials", t => {
83109
t.is(
84110
a2ml("(x+y)(x-y) = x^2-y^2"),
@@ -113,3 +139,10 @@ test("Average over time", t => {
113139
'<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>'
114140
);
115141
});
142+
143+
test("The binomial coefficient", t => {
144+
t.is(
145+
a2ml("binom(n, k) = n! / (n-k)!k!"),
146+
'<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>'
147+
);
148+
});

test/roots.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@ test("Omits brackets in roots", t => {
1414
t.is(a2ml("root(3)(2)"), "<math><mroot><mn>2</mn><mn>3</mn></mroot></math>");
1515
});
1616

17-
test("Display the binomial coefficient", t => {
18-
t.is(
19-
a2ml("binom(n, k)"),
20-
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>n</mi><mi>k</mi></mfrac></mfenced></math>'
21-
);
22-
});
23-
24-
test("Displays the whole terms of the binomial coefficient", t => {
25-
t.is(
26-
a2ml("binom(a, b + c)"),
27-
'<math><mfenced open="(" close=")"><mfrac linethickness="0"><mi>a</mi><mrow><mi>b</mi><mo>+</mo><mi>c</mi></mrow></mfrac></mfenced></math>'
28-
);
29-
});
30-
3117
test("Allows empty roots", t => {
3218
t.is(a2ml("sqrt"), "<math><msqrt><mrow></mrow></msqrt></math>");
3319
t.is(
@@ -73,10 +59,3 @@ test("Continued square root", t => {
7359
"<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>"
7460
);
7561
});
76-
77-
test("The binomial coefficient", t => {
78-
t.is(
79-
a2ml("binom(n, k) = n! / (n-k)!k!"),
80-
'<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>'
81-
);
82-
});

0 commit comments

Comments
 (0)