All files / roosterjs-content-model-api/lib/publicApi/segment changeCapitalization.ts

100% Statements 21/21
90% Branches 9/10
100% Functions 3/3
100% Lines 20/20

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 591x                     1x         13x   13x 13x 12x   3x 3x     4x 4x     4x   4x 8x         4x 4x     1x           1x   1x   6x 1x          
import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';
import type { IEditor } from 'roosterjs-content-model-types';
 
/**
 * Change the capitalization of text in the selection
 * @param editor The editor instance
 * @param capitalization The case option
 * @param language Optional parameter for language string that should comply to "IETF BCP 47 Tags for
 * Identifying Languages". For example: 'en' or 'en-US' for English, 'tr' for Turkish.
 * Default is the host environment’s current locale.
 */
export function changeCapitalization(
    editor: IEditor,
    capitalization: 'sentence' | 'lowerCase' | 'upperCase' | 'capitalize',
    language?: string
) {
    editor.focus();
 
    formatSegmentWithContentModel(editor, 'changeCapitalization', (_, __, segment) => {
        if (segment?.segmentType == 'Text') {
            switch (capitalization) {
                case 'lowerCase':
                    segment.text = segment.text.toLocaleLowerCase(language);
                    break;
 
                case 'upperCase':
                    segment.text = segment.text.toLocaleUpperCase(language);
                    break;
 
                case 'capitalize':
                    const wordArray = segment.text.toLocaleLowerCase(language).split(' ');
 
                    for (let i = 0; i < wordArray.length; i++) {
                        wordArray[i] =
                            wordArray[i].charAt(0).toLocaleUpperCase(language) +
                            wordArray[i].slice(1);
                    }
 
                    segment.text = wordArray.join(' ');
                    break;
 
                case 'sentence':
                    const punctuationMarks = '[\\.\\!\\?]';
                    // Find a match of a word character either:
                    // - At the beginning of a string with or without preceding whitespace, for
                    // example: '  hello world' and 'hello world' strings would both match 'h'.
                    // - Or preceded by a punctuation mark and at least one whitespace, for
                    // example 'yes. hello world' would match 'y' and 'h'.
                    const regex = new RegExp('^\\s*\\w|' + punctuationMarks + '\\s+\\w', 'g');
 
                    segment.text = segment.text
                        .toLocaleLowerCase(language)
                        .replace(regex, match => match.toLocaleUpperCase(language));
                    break;
            }
        }
    });
}