Back

93.75% Statements 15/16
77.77% Branches 7/9
100% Functions 2/2
93.75% Lines 15/16

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            3x           3x 10x   10x 10x 10x       10x 10x 10x 10x 10x   10x   10x             3x 10x              
type PreventWidowsFunction = ( text: string, wordsToKeep?: number ) => string;
type PreventWidowsInPartFunction = (
	text: string,
	spacesToSubstitute: number
) => { part: string; substituted: number };
 
const reverseSpaceRegex = /\s+(\S*)$/;
 
// This implementation was copied and slighly modified from the Calypso codebase
// Src: https://github.com/Automattic/wp-calypso/blob/trunk/client/lib/formatting/prevent-widows.js
// The instances where the part is an array or a component have been removed as we don't use the
// same translate function that Calypso does.
const preventWidowsInPart: PreventWidowsInPartFunction = ( part, spacesToSubstitute ) => {
	let substituted = 0;
 
	Eif ( part && part.length > 0 ) {
		let text = part;
		let retVal = '';
 
		// If the part is a string, work from the right looking for spaces
		// TODO Work out if we can tell that this is a RTL language, and if it's appropriate to join words in this way
		while ( substituted < spacesToSubstitute && reverseSpaceRegex.test( text ) ) {
			const match = reverseSpaceRegex.exec( text );
			retVal = '\xA0' + match[ 1 ] + retVal;
			text = text.replace( reverseSpaceRegex, '' );
			substituted++;
		}
		retVal = text + retVal;
		// Return the modified string and the number of spaces substituted
		return { part: retVal, substituted };
	}
 
	// For anything else e.g. an element without children, there's nothing to do.
	return { part, substituted };
};
 
const preventWidows: PreventWidowsFunction = ( text, wordsToKeep = 2 ) => {
	return preventWidowsInPart(
		'string' === typeof text ? text.trim() : text,
		Math.max( 1, wordsToKeep - 1 )
	).part;
};
 
export default preventWidows;