/**
 * Insert a template token at the caret (docs/08 §2.9).
 *
 * @param {string} source Source string.
 * @param {string} token  Token to insert.
 * @param {number} start  Selection start.
 * @param {number} end    Selection end.
 * @return {string} Updated string.
 *
 * @package
 */
export function insertToken(
	source: string,
	token: string,
	start: number,
	end: number
): string {
	const from = Math.max( 0, Math.min( start, source.length ) );
	const to = Math.max( from, Math.min( end, source.length ) );
	return source.slice( 0, from ) + token + source.slice( to );
}
