This commit refactors existing text utility functions into the application layer for broad reuse and integrates them across the codebase. Initially, these utilities were confined to test code, which limited their application. Changes: - Move text utilities to the application layer. - Centralize text utilities into dedicated files for better maintainability. - Improve robustness of utility functions with added type checks. - Replace duplicated logic with centralized utility functions throughout the codebase. - Expand unit tests to cover refactored code parts.
30 lines
948 B
TypeScript
30 lines
948 B
TypeScript
import { isString } from '@/TypeHelpers';
|
|
import { splitTextIntoLines } from './SplitTextIntoLines';
|
|
|
|
export function indentText(
|
|
text: string,
|
|
indentLevel = 1,
|
|
utilities: TextIndentationUtilities = DefaultUtilities,
|
|
): string {
|
|
if (!utilities.isStringType(text)) {
|
|
throw new Error(`Indentation error: The input must be a string. Received type: ${typeof text}.`);
|
|
}
|
|
if (indentLevel <= 0) {
|
|
throw new Error(`Indentation error: The indent level must be a positive integer. Received: ${indentLevel}.`);
|
|
}
|
|
const indentation = '\t'.repeat(indentLevel);
|
|
return utilities.splitIntoLines(text)
|
|
.map((line) => (line ? `${indentation}${line}` : line))
|
|
.join('\n');
|
|
}
|
|
|
|
interface TextIndentationUtilities {
|
|
readonly splitIntoLines: typeof splitTextIntoLines;
|
|
readonly isStringType: typeof isString;
|
|
}
|
|
|
|
const DefaultUtilities: TextIndentationUtilities = {
|
|
splitIntoLines: splitTextIntoLines,
|
|
isStringType: isString,
|
|
};
|