All files / roosterjs-content-model-dom/lib/domUtils stringUtil.ts

100% Statements 9/9
84.62% Branches 11/13
100% Functions 3/3
100% Lines 9/9

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 311x 1x           1x 187x               1x 187x 187x                 1x 81x    
const SPACES_REGEX = /[\u2000\u2009\u200a​\u200b​\u202f\u205f​\u3000\s\t\r\n]/gm;
const PUNCTUATIONS = '.,?!:"()[]\\/';
 
/**
 * Check if the given character is punctuation
 * @param char The character to check
 */
export function isPunctuation(char: string) {
    return PUNCTUATIONS.indexOf(char) >= 0;
}
 
/**
 * Check if the give character is a space. A space can be normal ASCII pace (32) or non-break space (160) or other kinds of spaces
 * such as ZeroWidthSpace, ...
 * @param char The character to check
 */
export function isSpace(char: string) {
    const code = char?.charCodeAt(0) ?? 0;
    return code == 160 || code == 32 || SPACES_REGEX.test(char);
}
 
/**
 * Normalize spaces of the given string. After normalization, all leading (for forward) or trailing (for backward) spaces
 * will be replaces with non-break space (160)
 * @param txt The string to normalize
 * @param isForward Whether normalize forward or backward
 */
export function normalizeText(txt: string, isForward: boolean): string {
    return txt.replace(isForward ? /^\u0020+/ : /\u0020+$/, '\u00A0');
}