Implemented mfenced separators case

This commit is contained in:
Alexandre Nunes 2020-09-04 00:49:32 -03:00
parent c2610a3860
commit 397f0f1a49
10 changed files with 174 additions and 60 deletions

@ -240,75 +240,159 @@ describe('#convert', () => {
});
});
describe('given math string with mfenced with single content and no attr', () => {
test('convert mfenced wrapping it content in dots', () => {
const mathml = `
<root>
<math>
<mfenced>
<mn>3</mn>
</mfenced>
</math>
</root>
`;
describe('given mfenced tag', () => {
describe('with single content and no attr', () => {
test('convert mfenced wrapping it content in dots', () => {
const mathml = `
<root>
<math>
<mfenced>
<mn>3</mn>
</mfenced>
</math>
</root>
`;
const result = MathMLToLaTeX.convert(mathml);
const result = MathMLToLaTeX.convert(mathml);
expect(result).toMatch('\\left.3\\right.');
expect(result).toMatch('\\left(3\\right)');
});
});
});
describe('given math string with mfenced with single content and open attribute in parenthesis char', () => {
test('convert mfenced wrapping it content between parenthesis and dot', () => {
const mathml = `
<root>
<math>
<mfenced open="(">
<mn>3</mn>
</mfenced>
</math>
</root>
`;
describe('with single content and open attribute in bracket char', () => {
test('convert mfenced wrapping it content between bracket and parenthesis', () => {
const mathml = `
<root>
<math>
<mfenced open="{">
<mn>3</mn>
</mfenced>
</math>
</root>
`;
const result = MathMLToLaTeX.convert(mathml);
const result = MathMLToLaTeX.convert(mathml);
expect(result).toMatch('\\left(3\\right.');
expect(result).toMatch('\\left{3\\right)');
});
});
});
describe('given math string with mfenced with single content and open and closes attributes in parenthesis char', () => {
test('convert mfenced wrapping it content between parenthesis', () => {
const mathml = `
<root>
<math>
<mfenced open="(" close=")">
<mn>3</mn>
</mfenced>
</math>
</root>
`;
describe('with single content and open and closes attributes in parenthesis char', () => {
test('convert mfenced wrapping it content between parenthesis', () => {
const mathml = `
<root>
<math>
<mfenced open="(" close=")">
<mn>3</mn>
</mfenced>
</math>
</root>
`;
const result = MathMLToLaTeX.convert(mathml);
const result = MathMLToLaTeX.convert(mathml);
expect(result).toMatch('\\left(3\\right)');
expect(result).toMatch('\\left(3\\right)');
});
});
});
describe('given math string with mfenced with single content and open attribute in parenthesis char and close attribute without value', () => {
test('convert mfenced wrapping it content between parenthesis', () => {
const mathml = `
<root>
<math>
<mfenced open="(" close>
<mn>3</mn>
</mfenced>
</math>
</root>
`;
describe('with single content and open attribute in parenthesis char and close attribute without value', () => {
test('convert mfenced wrapping it content between bracket and parenthesis', () => {
const mathml = `
<root>
<math>
<mfenced open="{" close>
<mn>3</mn>
</mfenced>
</math>
</root>
`;
const result = MathMLToLaTeX.convert(mathml);
const result = MathMLToLaTeX.convert(mathml);
expect(result).toMatch('\\left(3\\right.');
expect(result).toMatch('\\left{3\\right)');
});
});
describe('with more than one content and no attr', () => {
test('convert mfenced wrapping it content inside parenthesis and joining using commas', () => {
const mathml = `
<root>
<math>
<mfenced>
<mn>3</mn>
<mn>2</mn>
<mn>1</mn>
</mfenced>
</math>
</root>
`;
const result = MathMLToLaTeX.convert(mathml);
expect(result).toMatch('\\left(3,2,1\\right)');
});
});
describe('with four contents with separator attribute as empty string', () => {
test('convert mfenced wrapping it content inside parentheses and joining using commas', () => {
const mathml = `
<root>
<math>
<mfenced separators=''>
<mn>3</mn>
<mn>2</mn>
<mn>1</mn>
<mn>7</mn>
</mfenced>
</math>
</root>
`;
const result = MathMLToLaTeX.convert(mathml);
expect(result).toMatch('\\left(3,2,1,7\\right)');
});
});
describe("with mfenced with three contents with separator attribute ';;;'", () => {
test("parse mfenced wrapping it content inside parentheses and joining using ';'", () => {
const mathml = `
<root>
<math>
<mfenced separators=';;;'>
<mn>3</mn>
<mn>2</mn>
<mn>1</mn>
</mfenced>
</math>
</root>
`;
const result = MathMLToLaTeX.convert(mathml);
expect(result).toMatch('\\left(3;2;1\\right)');
});
});
describe("with four contents with separator attribute ';.'", () => {
test("convert mfenced wrapping it content inside parentheses and joining using ';' for the first, '.' for the second and on", () => {
const mathml = `
<root>
<math>
<mfenced separators=';.'>
<mn>3</mn>
<mn>2</mn>
<mn>1</mn>
<mn>7</mn>
</mfenced>
</math>
</root>
`;
const result = MathMLToLaTeX.convert(mathml);
expect(result).toMatch('\\left(3;2.1.7\\right)');
});
});
});
});

@ -1,18 +1,23 @@
import { MathMLTag } from './MathMLTag';
import { GenericWrapper } from '../../../../wrappers';
import { GenericWrapper } from '../../../../../utils/wrappers';
import { JoinWithManySeparators } from '../../../../../utils';
export class MFenced extends MathMLTag {
private _wrapper: GenericWrapper;
private _separators: string[];
constructor(value: string, attributes: Record<string, string>, children: MathMLTag[]) {
super('mfenced', value, attributes, children);
const open: string = this._attributes.open || '.';
const close: string = this._attributes.close || '.';
const open: string = this._attributes.open || '(';
const close: string = this._attributes.close || ')';
this._wrapper = new GenericWrapper(open, close);
this._separators = Array.from(this._attributes.separators || '');
}
convert(): string {
return this._wrapper.wrap(this._mapChildrenToLaTeX().join(' '));
const withoutWrapper = JoinWithManySeparators.join(this._mapChildrenToLaTeX(), this._separators);
return this._wrapper.wrap(withoutWrapper);
}
}

@ -1,5 +1,5 @@
import { MathMLTag } from './MathMLTag';
import { BracketWrapper, ParenthesisWrapper } from '../../../../wrappers';
import { BracketWrapper, ParenthesisWrapper } from '../../../../../utils/wrappers';
export class MSup extends MathMLTag {
constructor(value: string, attributes: Record<string, string>, children: MathMLTag[]) {

23
utils/Separators.ts Normal file

@ -0,0 +1,23 @@
export class JoinWithManySeparators {
private _separators: string[];
constructor(separators: string[]) {
this._separators = separators;
}
static join(arr: string[], separators: string[]): string {
return new JoinWithManySeparators(separators)._join(arr);
}
private _join(arr: string[]): string {
return arr.reduce((joinedStr, currentStr, currentIndex, strArr) => {
const separator = currentIndex === strArr.length - 1 ? '' : this._get(currentIndex);
return joinedStr + currentStr + separator;
}, '');
}
private _get(index: number) {
if (this._separators[index]) return this._separators[index];
return this._separators.length > 0 ? this._separators[this._separators.length - 1] : ',';
}
}

2
utils/index.ts Normal file

@ -0,0 +1,2 @@
export { JoinWithManySeparators } from './Separators';
export * from './wrappers';