Skip to content

Commit

Permalink
Merge pull request #2759 from microsoft/u/juliaroldi/fix-superscript
Browse files Browse the repository at this point in the history
Fix ordinal trigger
  • Loading branch information
juliaroldi authored Aug 8, 2024
2 parents 84ac428 + 3a3c09b commit c439f32
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ const getOrdinal = (value: number) => {
};

/**
* @internal
* The two last characters of ordinal number (st, nd, rd, th)
*/
export function transformOrdinals(
const ORDINAL_LENGTH = 2;

/**
* @internal
*/ export function transformOrdinals(
previousSegment: ContentModelText,
paragraph: ShallowMutableContentModelParagraph,
context: FormatContentModelContext
): boolean {
const value = previousSegment.text.split(' ').pop()?.trim();
if (value) {
const ordinal = value.substring(value.length - 2);
const ordinalValue = parseInt(value);
if (ordinalValue && getOrdinal(ordinalValue) === ordinal) {
const ordinal = value.substring(value.length - ORDINAL_LENGTH); // This value is equal st, nd, rd, th
const numericValue = getNumericValue(value); //This is the numeric part. Ex: 10th, numeric value = 10
if (numericValue && getOrdinal(numericValue) === ordinal) {
const ordinalSegment = splitTextSegment(
previousSegment,
paragraph,
Expand All @@ -41,3 +45,12 @@ export function transformOrdinals(
}
return false;
}

function getNumericValue(text: string) {
const number = text.substring(0, text.length - ORDINAL_LENGTH);
const isNumber = /^-?\d+$/.test(number);
if (isNumber) {
return parseInt(text);
}
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,60 @@ describe('transformOrdinals', () => {
};
runTest(segment, paragraph, { canUndoByBackspace: true } as any, false);
});

it('word and th', () => {
const segment: ContentModelText = {
segmentType: 'Text',
text: '12-month',
format: {},
};
const paragraph: ContentModelParagraph = {
blockType: 'Paragraph',
segments: [segment],
format: {},
};
runTest(segment, paragraph, { canUndoByBackspace: true } as any, false);
});

it('word and rd', () => {
const segment: ContentModelText = {
segmentType: 'Text',
text: '13-rd',
format: {},
};
const paragraph: ContentModelParagraph = {
blockType: 'Paragraph',
segments: [segment],
format: {},
};
runTest(segment, paragraph, { canUndoByBackspace: true } as any, false);
});

it('word and nd', () => {
const segment: ContentModelText = {
segmentType: 'Text',
text: '14-second',
format: {},
};
const paragraph: ContentModelParagraph = {
blockType: 'Paragraph',
segments: [segment],
format: {},
};
runTest(segment, paragraph, { canUndoByBackspace: true } as any, false);
});

it('large number', () => {
const segment: ContentModelText = {
segmentType: 'Text',
text: '145th',
format: {},
};
const paragraph: ContentModelParagraph = {
blockType: 'Paragraph',
segments: [segment],
format: {},
};
runTest(segment, paragraph, { canUndoByBackspace: true } as any, true);
});
});

0 comments on commit c439f32

Please sign in to comment.