diff --git a/packages/roosterjs-content-model-plugins/lib/autoFormat/numbers/transformOrdinals.ts b/packages/roosterjs-content-model-plugins/lib/autoFormat/numbers/transformOrdinals.ts index 49da2c899be..7726b5592b3 100644 --- a/packages/roosterjs-content-model-plugins/lib/autoFormat/numbers/transformOrdinals.ts +++ b/packages/roosterjs-content-model-plugins/lib/autoFormat/numbers/transformOrdinals.ts @@ -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, @@ -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; +} diff --git a/packages/roosterjs-content-model-plugins/test/autoFormat/numbers/transformOrdinalsTest.ts b/packages/roosterjs-content-model-plugins/test/autoFormat/numbers/transformOrdinalsTest.ts index 74ffb14f773..f5ffeda194d 100644 --- a/packages/roosterjs-content-model-plugins/test/autoFormat/numbers/transformOrdinalsTest.ts +++ b/packages/roosterjs-content-model-plugins/test/autoFormat/numbers/transformOrdinalsTest.ts @@ -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); + }); });