All files / roosterjs-content-model-plugins/lib/autoFormat/numbers transformFraction.ts

100% Statements 13/13
80% Branches 8/10
100% Functions 1/1
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x             1x                 1x         7x 7x   7x 4x 4x 4x 4x   4x 4x     3x    
import { splitTextSegment } from 'roosterjs-content-model-api';
import type {
    ContentModelText,
    FormatContentModelContext,
    ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';
 
const FRACTIONS: Map<string, string> = new Map<string, string>([
    ['1/2', '½'],
    ['1/4', '¼'],
    ['3/4', '¾'],
]);
 
/**
 * @internal
 */
export function transformFraction(
    previousSegment: ContentModelText,
    paragraph: ShallowMutableContentModelParagraph,
    context: FormatContentModelContext
): boolean {
    const fraction = previousSegment.text.split(' ').pop()?.trim();
    const text = fraction ? FRACTIONS.get(fraction) : undefined;
 
    if (fraction && text) {
        const textLength = previousSegment.text.length - 1;
        const textIndex = textLength - fraction.length;
        const textSegment = splitTextSegment(previousSegment, paragraph, textIndex, textLength);
        textSegment.text = text;
 
        context.canUndoByBackspace = true;
        return true;
    }
 
    return false;
}