refactor: move mover and munder tag to single class

This commit is contained in:
Alexandre Nunes 2020-09-19 00:44:14 -03:00
parent ddaf33fef5
commit 5d84976977
5 changed files with 44 additions and 50 deletions

View File

@ -17,8 +17,7 @@ import {
MSub,
MSubsup,
MText,
MOver,
MUnder,
UnderOverTag,
} from './mathml-tags';
export class Dispatcher {
@ -69,9 +68,8 @@ export class Dispatcher {
case 'mtext':
return new MText(this._value, this._attributes, this._children);
case 'mover':
return new MOver(this._value, this._attributes, this._children);
case 'munder':
return new MUnder(this._value, this._attributes, this._children);
return new UnderOverTag(this._name, this._value, this._attributes, this._children);
case 'mrow':
case 'mpadded':
return new GenericContentWrapperTag(this._name, this._value, this._attributes, this._children);

View File

@ -1,22 +0,0 @@
import { MathMLTag } from './MathMLTag';
import { InvalidNumberOfChild } from '../../../../errors';
import { latexAccents } from '../../../../syntax/latexAccents';
export class MOver extends MathMLTag {
constructor(value: string, attributes: Record<string, string>, children: MathMLTag[]) {
super('mover', value, attributes, children);
}
convert(): string {
if (this._children.length !== 2) throw new InvalidNumberOfChild(this._name, 2, this._children.length);
const content = this._children[0].convert();
const accent = this._children[1].convert();
return this._overset(content, accent);
}
private _overset(content: string, accent: string): string {
return latexAccents.includes(accent) ? `${accent}{${content}}` : `\\overset{${accent}}{${content}}`;
}
}

View File

@ -1,22 +0,0 @@
import { MathMLTag } from './MathMLTag';
import { InvalidNumberOfChild } from '../../../../errors';
import { latexAccents } from '../../../../syntax/latexAccents';
export class MUnder extends MathMLTag {
constructor(value: string, attributes: Record<string, string>, children: MathMLTag[]) {
super('munder', value, attributes, children);
}
convert(): string {
if (this._children.length !== 2) throw new InvalidNumberOfChild(this._name, 2, this._children.length);
const content = this._children[0].convert();
const accent = this._children[1].convert();
return this._underset(content, accent);
}
private _underset(content: string, accent: string): string {
return latexAccents.includes(accent) ? `${accent}{${content}}` : `\\overset{${accent}}{${content}}`;
}
}

View File

@ -0,0 +1,41 @@
import { MathMLTag } from './MathMLTag';
import { InvalidNumberOfChild } from '../../../../errors';
import { latexAccents } from '../../../../syntax/latexAccents';
export class UnderOverTag extends MathMLTag {
convert(): string {
if (this._children.length !== 2) throw new InvalidNumberOfChild(this._name, 2, this._children.length);
const content = this._children[0].convert();
const accent = this._children[1].convert();
return this._applyCommand(content, accent);
}
private _applyCommand(content: string, accent: string): string {
const type = this._name.match(/under/) ? TagTypes.Under : TagTypes.Over;
return new UnderOverSetter(type).apply(content, accent);
}
}
class UnderOverSetter {
private readonly _type;
constructor(type: TagTypes) {
this._type = type;
}
apply(content: string, accent: string) {
return latexAccents.includes(accent) ? `${accent}{${content}}` : `${this._defaultCommand}{${accent}}{${content}}`;
}
private get _defaultCommand(): string {
if (this._type === TagTypes.Under) return '\\underset';
return '\\overset';
}
}
enum TagTypes {
Under,
Over,
}

View File

@ -16,5 +16,4 @@ export { MSup } from './MSup';
export { MSub } from './MSub';
export { MSubsup } from './MSubsup';
export { MText } from './MText';
export { MOver } from './MOver';
export { MUnder } from './MUnder';
export { UnderOverTag } from './UnderOverTag';